-
Notifications
You must be signed in to change notification settings - Fork 0
/
no_orm_test.go
163 lines (146 loc) · 3.38 KB
/
no_orm_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package orm
import (
"context"
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
"time"
)
type sqlTestSuite struct {
suite.Suite
// 配置字段
driver string
dsn string
// 初始化字段
db *sql.DB
}
func (s *sqlTestSuite) TearDownTest() {
_, err := s.db.Exec("DELETE FROM test_model;")
if err != nil {
s.T().Fatal(err)
}
}
func (s *sqlTestSuite) SetupSuite() {
db, err := sql.Open(s.driver, s.dsn)
if err != nil {
s.T().Fatal(err)
}
s.db = db
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
_, err = s.db.ExecContext(ctx, `
CREATE TABLE IF NOT EXISTS test_model(
id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
age INTEGER,
last_name TEXT NOT NULL
)
`)
if err != nil {
s.T().Fatal(err)
}
}
func (s *sqlTestSuite) TestCRUD() {
t := s.T()
db, err := sql.Open("sqlite3", "file:test.db?cache=shared&mode=memory")
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// 或者 Exec(xxx)
res, err := db.ExecContext(ctx, "INSERT INTO `test_model`(`id`, `first_name`, `age`, `last_name`) VALUES (1, 'Tom', 18, 'Jerry')")
if err != nil {
t.Fatal(err)
}
affected, err := res.RowsAffected()
if err != nil {
t.Fatal(err)
}
if affected != 1 {
t.Fatal(err)
}
rows, err := db.QueryContext(context.Background(),
"SELECT `id`, `first_name`,`age`, `last_name` FROM `test_model` LIMIT 1")
if err != nil {
t.Fatal()
}
for rows.Next() {
tm := &TestModel{}
err = rows.Scan(&tm.Id, &tm.FirstName, &tm.Age, &tm.LastName)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "Tom", tm.FirstName)
}
// 或者 Exec(xxx)
res, err = db.ExecContext(ctx, "UPDATE `test_model` SET `first_name` = 'changed' WHERE `id` = ?", 1)
if err != nil {
t.Fatal(err)
}
affected, err = res.RowsAffected()
if err != nil {
t.Fatal(err)
}
if affected != 1 {
t.Fatal(err)
}
row := db.QueryRowContext(context.Background(), "SELECT `id`, `first_name`,`age`, `last_name` FROM `test_model` LIMIT 1")
if row.Err() != nil {
t.Fatal(row.Err())
}
tm := &TestModel{}
err = row.Scan(&tm.Id, &tm.FirstName, &tm.Age, &tm.LastName)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "changed", tm.FirstName)
}
func TestSQLite(t *testing.T) {
suite.Run(t, &sqlTestSuite{
driver: "sqlite3",
dsn: "file:test.db?cache=shared&mode=memory",
})
}
type TestModel struct {
Id int64
FirstName string
Age int8
LastName *sql.NullString
// CreatedAt *time.Time
}
func (TestModel) CreateSQL() string {
return `
CREATE TABLE IF NOT EXISTS test_model(
id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
age INTEGER,
last_name TEXT NOT NULL
)
`
}
// memoryDB 返回一个基于内存的 ORM,它使用的是 sqlite3 内存模式。
func memoryDB(t *testing.T, opts ...DBOption) *DB {
orm, err := Open("sqlite3", "file:test.db?cache=shared&mode=memory", opts...)
if err != nil {
t.Fatal(err)
}
return orm
}
//func memoryDB(t *testing.T) *DB {
// orm, err := Open("mysql", "root:123456@tcp(192.168.2.108:3306)/test")
// if err != nil {
// t.Fatal(err)
// }
// return orm
//}
func memoryDBWithDB(db string, t *testing.T) *DB {
orm, err := Open("sqlite3", fmt.Sprintf("file:%s.db?cache=shared&mode=memory", db))
if err != nil {
t.Fatal(err)
}
return orm
}