Skip to content

Commit 11c6394

Browse files
author
Vic Shóstak
committed
Add test to Public routes
1 parent cb9e6f1 commit 11c6394

File tree

8 files changed

+79
-302
lines changed

8 files changed

+79
-302
lines changed

app/controllers/book_controller.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import (
66
"github.com/dgrijalva/jwt-go"
77
"github.com/gofiber/fiber/v2"
88
"github.com/google/uuid"
9-
"github.com/koddr/tutorial-go-rest-api-fiber/app/models"
10-
"github.com/koddr/tutorial-go-rest-api-fiber/app/validators"
11-
"github.com/koddr/tutorial-go-rest-api-fiber/pkg/utils"
12-
"github.com/koddr/tutorial-go-rest-api-fiber/platform/database"
9+
"github.com/koddr/tutorial-go-fiber-rest-api/app/models"
10+
"github.com/koddr/tutorial-go-fiber-rest-api/app/validators"
11+
"github.com/koddr/tutorial-go-fiber-rest-api/pkg/utils"
12+
"github.com/koddr/tutorial-go-fiber-rest-api/platform/database"
1313
)
1414

1515
// GetBooks func gets all exists books.

app/queries/book_query.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package queries
33
import (
44
"github.com/google/uuid"
55
"github.com/jmoiron/sqlx"
6-
"github.com/koddr/tutorial-go-rest-api-fiber/app/models"
6+
"github.com/koddr/tutorial-go-fiber-rest-api/app/models"
77
)
88

99
// BookQueries struct for queries from Book model.

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/koddr/tutorial-go-rest-api-fiber
1+
module github.com/koddr/tutorial-go-fiber-rest-api
22

33
go 1.16
44

@@ -13,5 +13,6 @@ require (
1313
github.com/jackc/pgx/v4 v4.10.1
1414
github.com/jmoiron/sqlx v1.3.1
1515
github.com/joho/godotenv v1.3.0
16+
github.com/stretchr/testify v1.7.0
1617
github.com/swaggo/swag v1.7.0
1718
)

go.sum

-292
This file was deleted.

pkg/routes/private_routes.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package routes
22

33
import (
44
"github.com/gofiber/fiber/v2"
5-
"github.com/koddr/tutorial-go-rest-api-fiber/app/controllers"
6-
"github.com/koddr/tutorial-go-rest-api-fiber/pkg/middleware"
5+
"github.com/koddr/tutorial-go-fiber-rest-api/app/controllers"
6+
"github.com/koddr/tutorial-go-fiber-rest-api/pkg/middleware"
77
)
88

99
// PrivateRoutes func for describe group of private routes.

pkg/routes/public_routes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package routes
22

33
import (
44
"github.com/gofiber/fiber/v2"
5-
"github.com/koddr/tutorial-go-rest-api-fiber/app/controllers"
5+
"github.com/koddr/tutorial-go-fiber-rest-api/app/controllers"
66
)
77

88
// PublicRoutes func for describe group of public routes.

pkg/routes/public_routes_test.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package routes
2+
3+
import (
4+
"net/http/httptest"
5+
"testing"
6+
7+
"github.com/gofiber/fiber/v2"
8+
"github.com/koddr/tutorial-go-fiber-rest-api/pkg/configs"
9+
"github.com/koddr/tutorial-go-fiber-rest-api/pkg/routes"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestPublicRoutes(t *testing.T) {
14+
// Define a structure for specifying input and output
15+
// data of a single test case. This structure is then used
16+
// to create a so called test map, which contains all test
17+
// cases, that should be run for testing this function
18+
tests := []struct {
19+
description string
20+
route string // input route
21+
expectedError bool
22+
expectedCode int
23+
}{
24+
{
25+
description: "get all books",
26+
route: "/api/v1/books",
27+
expectedError: false,
28+
expectedCode: 200,
29+
},
30+
{
31+
description: "get book by ID",
32+
route: "/api/v1/book/1",
33+
expectedError: false,
34+
expectedCode: 200,
35+
},
36+
}
37+
38+
// Define Fiber config.
39+
config := configs.FiberConfig()
40+
41+
// Define a new Fiber app with config.
42+
app := fiber.New(config)
43+
44+
// Routes.
45+
routes.PublicRoutes(app) // Register a public routes for app.
46+
47+
// Iterate through test single test cases
48+
for _, test := range tests {
49+
// Create a new http request with the route from the test case.
50+
req := httptest.NewRequest("GET", test.route, nil)
51+
52+
// Perform the request plain with the app.
53+
// The -1 disables request latency.
54+
resp, err := app.Test(req, -1)
55+
56+
// Verify that no error occurred, that is not expected
57+
assert.Equalf(t, test.expectedError, err != nil, test.description)
58+
59+
// As expected errors lead to broken responses, the next
60+
// test case needs to be processed
61+
if test.expectedError {
62+
continue
63+
}
64+
65+
// Verify if the status code is as expected
66+
assert.Equalf(t, test.expectedCode, resp.StatusCode, test.description)
67+
}
68+
}

platform/database/open_db_connection.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package database
22

3-
import "github.com/koddr/tutorial-go-rest-api-fiber/app/queries"
3+
import "github.com/koddr/tutorial-go-fiber-rest-api/app/queries"
44

55
// Queries struct for collect all app queries.
66
type Queries struct {

0 commit comments

Comments
 (0)