Skip to content

Commit 5c53112

Browse files
authored
chore: add unit tests (#11)
Co-authored-by: rick <[email protected]>
1 parent fd3741f commit 5c53112

File tree

5 files changed

+117
-4
lines changed

5 files changed

+117
-4
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22
ORM database Store Extension of API Testing
33

44
All issues of this project could be found at [linuxsuren/api-testing](https://github.com/LinuxSuRen/api-testing/issues).
5+
6+
## Q&A
7+
8+
Run the command `apt-get install build-essential libsqlite3-dev` if you meet the sqlite errors.

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ require (
1010
github.com/stretchr/testify v1.9.0
1111
gorm.io/driver/mysql v1.5.2
1212
gorm.io/driver/postgres v1.5.4
13-
gorm.io/gorm v1.25.5
13+
gorm.io/driver/sqlite v1.5.6
14+
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde
1415
)
1516

1617
require (
@@ -54,6 +55,7 @@ require (
5455
github.com/linuxsuren/go-fake-runtime v0.0.4 // indirect
5556
github.com/linuxsuren/unstructured v0.0.1 // indirect
5657
github.com/mailru/easyjson v0.7.7 // indirect
58+
github.com/mattn/go-sqlite3 v1.14.22 // indirect
5759
github.com/mitchellh/copystructure v1.2.0 // indirect
5860
github.com/mitchellh/reflectwalk v1.0.2 // indirect
5961
github.com/phpdave11/gofpdi v1.0.14-0.20211212211723-1f10f9844311 // indirect

go.sum

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ github.com/linuxsuren/unstructured v0.0.1 h1:ilUA8MUYbR6l9ebo/YPV2bKqlf62bzQursD
104104
github.com/linuxsuren/unstructured v0.0.1/go.mod h1:KH6aTj+FegzGBzc1vS6mzZx3/duhTUTEVyW5sO7p4as=
105105
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
106106
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
107+
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
108+
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
107109
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
108110
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
109111
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
@@ -261,6 +263,8 @@ gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
261263
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
262264
gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo=
263265
gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0=
266+
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
267+
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
264268
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
265-
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
266-
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
269+
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde h1:9DShaph9qhkIYw7QF91I/ynrr4cOO2PZra2PFD7Mfeg=
270+
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=

pkg/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"gorm.io/driver/postgres"
3232
"gorm.io/gorm"
3333
"gorm.io/gorm/logger"
34+
"gorm.io/driver/sqlite"
3435
)
3536

3637
type dbserver struct {
@@ -50,6 +51,9 @@ func createDB(user, password, address, database, driver string) (db *gorm.DB, er
5051
case "mysql", "":
5152
dsn = fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4", user, password, address, database)
5253
dialector = mysql.Open(dsn)
54+
case "sqlite":
55+
dsn = fmt.Sprintf("%s.db", database)
56+
dialector = sqlite.Open(dsn)
5357
case "postgres":
5458
obj := strings.Split(address, ":")
5559
host, port := obj[0], "5432"

pkg/server_test.go

Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2023 API Testing Authors.
2+
Copyright 2023-2024 API Testing Authors.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -17,9 +17,11 @@ package pkg
1717

1818
import (
1919
"context"
20+
"os"
2021
"testing"
2122

2223
"github.com/linuxsuren/api-testing/pkg/server"
24+
atest "github.com/linuxsuren/api-testing/pkg/testing"
2325
"github.com/linuxsuren/api-testing/pkg/testing/remote"
2426
"github.com/stretchr/testify/assert"
2527
)
@@ -85,3 +87,100 @@ func TestNewRemoteServer(t *testing.T) {
8587
assert.False(t, reply.Ready)
8688
})
8789
}
90+
91+
func TestSQLite(t *testing.T) {
92+
remoteServer := NewRemoteServer()
93+
assert.NotNil(t, remoteServer)
94+
defaultCtx := remote.WithIncomingStoreContext(context.TODO(), &atest.Store{
95+
Properties: map[string]string{
96+
"driver": "sqlite",
97+
"database": "atest",
98+
},
99+
})
100+
defer func() {
101+
_ = os.Remove("atest.db")
102+
}()
103+
104+
t.Run("CreateTestSuite", func(t *testing.T) {
105+
_, err := remoteServer.CreateTestSuite(defaultCtx, &remote.TestSuite{
106+
Name: "test",
107+
})
108+
assert.NoError(t, err)
109+
})
110+
111+
t.Run("ListTestSuite", func(t *testing.T) {
112+
result, err := remoteServer.ListTestSuite(defaultCtx, nil)
113+
assert.NoError(t, err)
114+
assert.Equal(t, 1, len(result.Data))
115+
assert.Equal(t, "test", result.Data[0].Name)
116+
})
117+
118+
t.Run("UpdateTestSuite", func(t *testing.T) {
119+
_, err := remoteServer.UpdateTestSuite(defaultCtx, &remote.TestSuite{
120+
Name: "test",
121+
Api: "fake",
122+
})
123+
assert.NoError(t, err)
124+
125+
var suite *remote.TestSuite
126+
suite, err = remoteServer.GetTestSuite(defaultCtx, &remote.TestSuite{
127+
Name: "test",
128+
})
129+
assert.NoError(t, err)
130+
assert.Equal(t, "fake", suite.Api)
131+
})
132+
133+
t.Run("CreateTestCase", func(t *testing.T) {
134+
_, err := remoteServer.CreateTestCase(defaultCtx, &server.TestCase{
135+
SuiteName: "test",
136+
Name: "test",
137+
})
138+
assert.NoError(t, err)
139+
140+
var testcases *server.TestCases
141+
testcases, err = remoteServer.ListTestCases(defaultCtx, &remote.TestSuite{
142+
Name: "test",
143+
})
144+
assert.NoError(t, err)
145+
assert.Equal(t, 1, len(testcases.Data))
146+
})
147+
148+
t.Run("UpdateTestCase", func(t *testing.T) {
149+
_, err := remoteServer.UpdateTestCase(defaultCtx, &server.TestCase{
150+
SuiteName: "test",
151+
Name: "test",
152+
Request: &server.Request{
153+
Api: "api",
154+
},
155+
})
156+
assert.NoError(t, err)
157+
158+
var testcase *server.TestCase
159+
testcase, err = remoteServer.GetTestCase(defaultCtx, &server.TestCase{
160+
SuiteName: "test",
161+
Name: "test",
162+
})
163+
assert.NoError(t, err)
164+
assert.Equal(t, "api", testcase.Request.Api)
165+
})
166+
167+
t.Run("DeleteTestCase", func(t *testing.T) {
168+
_, err := remoteServer.DeleteTestCase(defaultCtx, &server.TestCase{
169+
SuiteName: "test",
170+
Name: "test",
171+
})
172+
assert.NoError(t, err)
173+
})
174+
175+
t.Run("DeleteTestSuite", func(t *testing.T) {
176+
_, err := remoteServer.DeleteTestSuite(defaultCtx, &remote.TestSuite{
177+
Name: "test",
178+
})
179+
assert.NoError(t, err)
180+
})
181+
182+
t.Run("PProf", func(t *testing.T) {
183+
_, err := remoteServer.PProf(defaultCtx, &server.PProfRequest{})
184+
assert.NoError(t, err)
185+
})
186+
}

0 commit comments

Comments
 (0)