Skip to content

Commit 1d2db92

Browse files
author
xbucks
committed
add tests
1 parent cb63c62 commit 1d2db92

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

.vscode/launch.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Launch file",
9+
"type": "go",
10+
"request": "launch",
11+
"mode": "debug",
12+
"program": "${file}"
13+
}
14+
]
15+
}

internal/server/router_test.go

+33-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package server
22

33
import (
44
"context"
5-
"github.com/rameshsunkara/go-rest-api-example/internal/models"
65
"net/http"
76
"testing"
87
"time"
98

9+
"github.com/rameshsunkara/go-rest-api-example/internal/models"
10+
"github.com/stretchr/testify/assert"
11+
1012
"github.com/gin-gonic/gin"
1113
"go.mongodb.org/mongo-driver/mongo"
1214
"go.mongodb.org/mongo-driver/mongo/options"
@@ -37,12 +39,20 @@ func (m *MockMongoDataBase) Collection(name string, opts ...*options.CollectionO
3739
func TestListOfRoutes(t *testing.T) {
3840
router := WebRouter(svcInfo, &MockMongoDBClient{}, &MockMongoDataBase{})
3941
list := router.Routes()
42+
mode := gin.Mode()
43+
44+
assert.Equal(t, gin.ReleaseMode, mode)
4045

4146
assertRoutePresent(t, list, gin.RouteInfo{
4247
Method: http.MethodGet,
4348
Path: "/status",
4449
})
4550

51+
assertRouteNotPresent(t, list, gin.RouteInfo{
52+
Method: http.MethodPost,
53+
Path: "/seedDB",
54+
})
55+
4656
assertRoutePresent(t, list, gin.RouteInfo{
4757
Method: http.MethodGet,
4858
Path: "/api/v1/orders",
@@ -70,6 +80,20 @@ func TestListOfRoutes(t *testing.T) {
7080

7181
}
7282

83+
func TestModeSpecificRoutes(t *testing.T) {
84+
svcInfo.Environment = "dev"
85+
router := WebRouter(svcInfo, &MockMongoDBClient{}, &MockMongoDataBase{})
86+
list := router.Routes()
87+
mode := gin.Mode()
88+
89+
assert.Equal(t, gin.DebugMode, mode)
90+
91+
assertRoutePresent(t, list, gin.RouteInfo{
92+
Method: http.MethodPost,
93+
Path: "/seedDB",
94+
})
95+
}
96+
7397
func assertRoutePresent(t *testing.T, gotRoutes gin.RoutesInfo, wantRoute gin.RouteInfo) {
7498
for _, gotRoute := range gotRoutes {
7599
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
@@ -78,3 +102,11 @@ func assertRoutePresent(t *testing.T, gotRoutes gin.RoutesInfo, wantRoute gin.Ro
78102
}
79103
t.Errorf("route not found: %v", wantRoute)
80104
}
105+
106+
func assertRouteNotPresent(t *testing.T, gotRoutes gin.RoutesInfo, wantRoute gin.RouteInfo) {
107+
for _, gotRoute := range gotRoutes {
108+
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
109+
t.Errorf("route found: %v", wantRoute)
110+
}
111+
}
112+
}

0 commit comments

Comments
 (0)