Skip to content

Commit 31e24c5

Browse files
committed
feat: add test with httptest
add test with httptest add test with httptest
1 parent 1f9f823 commit 31e24c5

File tree

5 files changed

+114
-5
lines changed

5 files changed

+114
-5
lines changed

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ services:
3636
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
3737
ports:
3838
- ${MYSQL_PORT}:${MYSQL_PORT}
39-
volumes:
40-
- ./data:/var/lib/mysql
4139
logging:
4240
driver: json-file
4341
options:

go.mod

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
module github.com/leetcode-golang-classroom/golang-gin-with-dynamic-json
22

3-
go 1.21.4
3+
go 1.22.4
44

55
require (
6-
github.com/go-sql-driver/mysql v1.7.0
6+
github.com/go-sql-driver/mysql v1.8.1
77
github.com/spf13/viper v1.19.0
8+
github.com/stretchr/testify v1.9.0
89
gorm.io/driver/mysql v1.5.7
910
gorm.io/gorm v1.25.11
1011
)
1112

1213
require (
14+
filippo.io/edwards25519 v1.1.0 // indirect
1315
github.com/bytedance/sonic v1.11.6 // indirect
1416
github.com/bytedance/sonic/loader v0.1.1 // indirect
1517
github.com/cloudwego/base64x v0.1.4 // indirect
1618
github.com/cloudwego/iasm v0.2.0 // indirect
19+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
1720
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
1821
github.com/gin-contrib/sse v0.1.0 // indirect
1922
github.com/go-playground/locales v0.14.1 // indirect
@@ -28,6 +31,7 @@ require (
2831
github.com/mattn/go-isatty v0.0.20 // indirect
2932
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3033
github.com/modern-go/reflect2 v1.0.2 // indirect
34+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
3135
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
3236
github.com/ugorji/go/codec v1.2.12 // indirect
3337
golang.org/x/arch v0.8.0 // indirect

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
2+
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
13
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
24
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
35
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
@@ -28,8 +30,9 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
2830
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
2931
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
3032
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
31-
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
3233
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
34+
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
35+
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
3336
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
3437
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
3538
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package controller_test
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"os"
8+
"testing"
9+
10+
"github.com/gin-gonic/gin"
11+
"github.com/leetcode-golang-classroom/golang-gin-with-dynamic-json/internal/config"
12+
"github.com/leetcode-golang-classroom/golang-gin-with-dynamic-json/internal/model"
13+
"github.com/leetcode-golang-classroom/golang-gin-with-dynamic-json/internal/service/blog"
14+
models_test "github.com/leetcode-golang-classroom/golang-gin-with-dynamic-json/tests/models"
15+
"github.com/stretchr/testify/assert"
16+
)
17+
18+
func TestMain(m *testing.M) {
19+
// Setup the MySQL test database
20+
models_test.SetupTestDB(config.AppConfig)
21+
code := m.Run()
22+
// cleanup
23+
models_test.TeardownTestDB()
24+
os.Exit(code)
25+
}
26+
27+
type Response struct {
28+
Blogs []model.Blog `json:"blogs"`
29+
Page int `json:"page"`
30+
PageSize int `json:"pageSize"`
31+
TotalPages int `json:"totalPages"`
32+
}
33+
34+
func TestBlogIndex(t *testing.T) {
35+
gin.SetMode(gin.TestMode)
36+
req := httptest.NewRequest(http.MethodGet, "/blogs?format=json", nil)
37+
w := httptest.NewRecorder()
38+
ctx, _ := gin.CreateTestContext(w)
39+
ctx.Request = req
40+
41+
// Seed the database with some test data
42+
blog1 := model.Blog{Title: "First Blog", Content: "Content of the first blog"}
43+
blog2 := model.Blog{Title: "Second Blog", Content: "Content of the second blog"}
44+
models_test.TRdb.Db.Create(&blog1)
45+
models_test.TRdb.Db.Create(&blog2)
46+
store := blog.NewBlogStore(models_test.TRdb)
47+
hdr := blog.NewHandler(store)
48+
hdr.BlogIndex(ctx)
49+
50+
res := w.Result()
51+
defer res.Body.Close()
52+
// Check the status code
53+
assert.Equal(t, res.StatusCode, http.StatusOK, "API should return 200 stauts code")
54+
55+
// Read data from the body and parse the JSON
56+
var result Response
57+
err := json.NewDecoder(res.Body).Decode(&result)
58+
assert.NoError(t, err)
59+
// Check the length of the blogs array
60+
assert.Len(t, result.Blogs, 2)
61+
// Check content
62+
assert.Equal(t, result.Blogs[0].Title, blog2.Title)
63+
assert.Equal(t, result.Blogs[0].Content, blog2.Content)
64+
}

tests/models/setup.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package models_test
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/go-sql-driver/mysql"
8+
"github.com/leetcode-golang-classroom/golang-gin-with-dynamic-json/internal/config"
9+
"github.com/leetcode-golang-classroom/golang-gin-with-dynamic-json/internal/db"
10+
)
11+
12+
var TRdb *db.Rdb
13+
14+
func SetupTestDB(appConfig *config.Config) error {
15+
rdb, err := db.New(mysql.Config{
16+
User: appConfig.MysqlUser,
17+
Passwd: appConfig.MysqlPassword,
18+
Addr: appConfig.MysqlAddr,
19+
DBName: appConfig.MysqlDatabase,
20+
Net: "tcp",
21+
AllowNativePasswords: true,
22+
ParseTime: true,
23+
})
24+
TRdb = rdb
25+
if err != nil {
26+
log.Fatal(err)
27+
}
28+
TRdb.Migration()
29+
return nil
30+
}
31+
32+
func TeardownTestDB() {
33+
// cleanup
34+
var tableNames []string
35+
TRdb.Db.Raw("SHOW TABLES").Scan(&tableNames)
36+
// Iterate over each table name and drop it
37+
for _, tableName := range tableNames {
38+
TRdb.Db.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", tableName))
39+
}
40+
}

0 commit comments

Comments
 (0)