Skip to content

Commit 929b876

Browse files
committed
feat: initial testing and mockery setup
1 parent e64050a commit 929b876

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

.goreleaser.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ project_name: movie-service
55
before:
66
hooks:
77
- go run github.com/99designs/gqlgen generate
8+
- go run github.com/vektra/mockery/v2@v2.33.2
89
- go mod tidy
910
- go generate ./...
1011
-

.mockery.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# .mockery.yaml
2+
3+
with-expecter: true
4+
packages: {}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/gin-gonic/gin v1.9.1
1010
github.com/prometheus/client_golang v1.16.0
1111
github.com/rs/zerolog v1.30.0
12+
github.com/stretchr/testify v1.8.4
1213
github.com/vektah/gqlparser/v2 v2.5.8
1314
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.42.0
1415
go.opentelemetry.io/otel v1.17.0
@@ -23,6 +24,7 @@ require (
2324
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2425
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
2526
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
27+
github.com/davecgh/go-spew v1.1.1 // indirect
2628
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
2729
github.com/gin-contrib/sse v0.1.0 // indirect
2830
github.com/go-logr/logr v1.2.4 // indirect
@@ -45,6 +47,7 @@ require (
4547
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4648
github.com/modern-go/reflect2 v1.0.2 // indirect
4749
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
50+
github.com/pmezard/go-difflib v1.0.0 // indirect
4851
github.com/prometheus/client_model v0.3.0 // indirect
4952
github.com/prometheus/common v0.42.0 // indirect
5053
github.com/prometheus/procfs v0.10.1 // indirect

server.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/seanb4t/example-movie-service/graph"
1313
"github.com/seanb4t/example-movie-service/internal"
1414
"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
15+
"go.opentelemetry.io/otel/sdk/trace"
1516
)
1617

1718
// Logger is the global logger
@@ -72,6 +73,12 @@ func main() {
7273
log.Info().Msg("Starting server")
7374

7475
// Setup Gin
76+
r := setupRouter(tracerProvider)
77+
r.Run()
78+
}
79+
80+
// setupRouter Creates and configures the main router
81+
func setupRouter(tracerProvider *trace.TracerProvider) *gin.Engine {
7582
r := gin.New()
7683
r.Use(gin.Recovery())
7784
r.Use(requestid.New())
@@ -84,7 +91,8 @@ func main() {
8491
return log.With().Ctx(ctx).Logger()
8592
})))
8693
r.POST("/query", graphqlHandler())
94+
r.GET("/query", graphqlHandler())
8795
r.GET("/", playgroundHandler())
8896
r.GET("/metrics", gin.WrapH(promhttp.Handler()))
89-
r.Run()
97+
return r
9098
}

server_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"github.com/seanb4t/example-movie-service/internal"
5+
"github.com/stretchr/testify/assert"
6+
"github.com/stretchr/testify/require"
7+
"net/http/httptest"
8+
"testing"
9+
)
10+
11+
func TestDefaultRouteGoesToGraphiql(t *testing.T) {
12+
tp, err := internal.InitTracer()
13+
require.Nil(t, err, "err must be nil for tracer init")
14+
15+
router := setupRouter(tp)
16+
require.NotNil(t, router, "router must not be nil")
17+
18+
w := httptest.NewRecorder()
19+
req := httptest.NewRequest("GET", "/", nil)
20+
router.ServeHTTP(w, req)
21+
22+
assert.Equal(t, 200, w.Code)
23+
assert.Contains(t, w.Body.String(), "<title>GraphQL</title>")
24+
}
25+
26+
func TestEmptyGetQueryFails(t *testing.T) {
27+
tp, err := internal.InitTracer()
28+
require.Nil(t, err, "err must be nil for tracer init")
29+
30+
router := setupRouter(tp)
31+
require.NotNil(t, router, "router must not be nil")
32+
33+
w := httptest.NewRecorder()
34+
req := httptest.NewRequest("GET", "/query", nil)
35+
router.ServeHTTP(w, req)
36+
37+
assert.Equal(t, 422, w.Code)
38+
assert.JSONEq(t, "{\"errors\":[{\"message\":\"no operation provided\",\"extensions\":{\"code\":\"GRAPHQL_VALIDATION_FAILED\"}}],\"data\":null}", w.Body.String())
39+
}
40+
41+
func TestMetricsRoute(t *testing.T) {
42+
tp, err := internal.InitTracer()
43+
require.NoError(t, err, "err must be nil for tracer init")
44+
45+
router := setupRouter(tp)
46+
require.NotNil(t, router, "router must not be nil")
47+
48+
w := httptest.NewRecorder()
49+
req := httptest.NewRequest("GET", "/metrics", nil)
50+
router.ServeHTTP(w, req)
51+
52+
assert.Equal(t, 200, w.Code)
53+
assert.Regexp(t, "^# HELP go_gc_duration_seconds", w.Body.String())
54+
}

0 commit comments

Comments
 (0)