Skip to content

Commit 53c23eb

Browse files
committed
Add a basic unit test example
1 parent 30c9715 commit 53c23eb

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

cmd/golang-docker-build-tutorial/main.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ import (
66
"net/http"
77

88
"github.com/gorilla/mux"
9+
10+
internal "github.com/miguno/golang-docker-build-tutorial/internal/pkg"
911
)
1012

11-
// Response is just a very basic example.
13+
// Response is just a basic example.
1214
type Response struct {
1315
Status string `json:"status,omitempty"`
1416
}
1517

16-
// GetStatus returns always the same response.
1718
func GetStatus(w http.ResponseWriter, _ *http.Request) {
18-
b := Response{Status: "idle"}
19-
json.NewEncoder(w).Encode(b)
19+
var response Response
20+
if internal.IsIdleToyFunction() {
21+
response = Response{Status: "idle"}
22+
} else {
23+
response = Response{Status: "busy"}
24+
}
25+
json.NewEncoder(w).Encode(response)
2026
}
2127

2228
func main() {

go.mod

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,14 @@ module github.com/miguno/golang-docker-build-tutorial
22

33
go 1.19
44

5-
require github.com/gorilla/mux v1.8.0
5+
require (
6+
github.com/gorilla/mux v1.8.0
7+
github.com/stretchr/testify v1.8.1
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/pmezard/go-difflib v1.0.0 // indirect
13+
github.com/stretchr/objx v0.5.0 // indirect
14+
gopkg.in/yaml.v3 v3.0.1 // indirect
15+
)

go.sum

+17
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
25
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
6+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
7+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
9+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
10+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
11+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
12+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
13+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
14+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
15+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
19+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/pkg/responses.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package internal
2+
3+
// To demonstrate having some code that can be unit-tested.
4+
func IsIdleToyFunction() bool {
5+
return true
6+
}

internal/pkg/responses_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package internal
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// To demonstrate how to integrate unit testing in a project.
10+
func TestIsIdleToyFunction(t *testing.T) {
11+
assert.True(t, IsIdleToyFunction())
12+
}

0 commit comments

Comments
 (0)