This repository has been archived by the owner on Dec 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase_test.go
188 lines (148 loc) · 3.98 KB
/
database_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package database
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
var (
testDB *Database
testings *Collection
testingsAuto *Collection
testingsHooker *Collection
testingsRelParent *Collection
testingsRelChild *Collection
)
type testingModel struct {
ModelTracking
Code string `db:"code,pk"`
Name string `db:"name"`
Ignored string `db:"-"`
}
func (model *testingModel) TableName() string {
return "testing"
}
type testingAutoModel struct {
ModelTracking
ID int64 `db:"id,pk"`
Name string `db:"name"`
Ignored string `db:"-"`
}
func (model *testingAutoModel) TableName() string {
return "testing_auto"
}
type testingHooker struct {
ModelTracking
Code string `db:"code,pk"`
Executed bool `db:"executed"`
Changed string `db:"changed"`
}
func (model *testingHooker) TableName() string {
return "testing_hooker"
}
func (model *testingHooker) OnBeforePutHook() error {
model.Changed = "changed"
return nil
}
func (model *testingHooker) OnAfterPutHook() error {
model.Executed = true
return nil
}
type testingRelParent struct {
ModelTracking
ID int64 `db:"id,pk"`
}
func (model *testingRelParent) TableName() string {
return "testing_relparent"
}
type testingRelChild struct {
ModelTracking
ID int64 `db:"id,pk"`
Parent int64 `db:"parent"`
Foo string `db:"foo"`
}
func (model *testingRelChild) TableName() string {
return "testing_relchild"
}
func initDatabase(t *testing.T) {
var err error
testDB, err = Open(Credentials{
User: "dev-user",
Password: "dev-password",
Address: "localhost:3307",
Database: "test",
Charset: "utf8mb4",
Collation: "utf8mb4_bin",
}, WithDebug(os.Getenv("DEBUG") == "true"))
require.Nil(t, err)
require.Nil(t, testDB.Exec(`DROP TABLE IF EXISTS testing`))
err = testDB.Exec(`
CREATE TABLE testing (
code VARCHAR(191),
name VARCHAR(191),
revision INT(11) NOT NULL,
PRIMARY KEY(code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
`)
require.Nil(t, err)
require.Nil(t, testDB.Exec(`DROP TABLE IF EXISTS testing_auto`))
err = testDB.Exec(`
CREATE TABLE testing_auto (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(191),
revision INT(11) NOT NULL,
PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
`)
require.Nil(t, err)
require.Nil(t, testDB.Exec(`DROP TABLE IF EXISTS testing_hooker`))
err = testDB.Exec(`
CREATE TABLE testing_hooker (
code VARCHAR(191),
executed BOOLEAN,
changed VARCHAR(191),
revision INT(11) NOT NULL,
PRIMARY KEY(code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
`)
require.Nil(t, err)
require.Nil(t, testDB.Exec(`DROP TABLE IF EXISTS testing_relparent`))
err = testDB.Exec(`
CREATE TABLE testing_relparent (
id INT(11) NOT NULL AUTO_INCREMENT,
revision INT(11) NOT NULL,
PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
`)
require.Nil(t, err)
require.Nil(t, testDB.Exec(`DROP TABLE IF EXISTS testing_relchild`))
err = testDB.Exec(`
CREATE TABLE testing_relchild (
id INT(11) NOT NULL AUTO_INCREMENT,
parent INT(11),
foo VARCHAR(191) NOT NULL,
revision INT(11) NOT NULL,
PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
`)
require.Nil(t, err)
testings = testDB.Collection(new(testingModel))
testingsAuto = testDB.Collection(new(testingAutoModel))
testingsHooker = testDB.Collection(new(testingHooker))
testingsRelParent = testDB.Collection(new(testingRelParent))
testingsRelChild = testDB.Collection(new(testingRelChild))
}
func closeDatabase() {
testDB.Close()
}
func TestQueryRow(t *testing.T) {
initDatabase(t)
model := &testingModel{
Code: "Test",
Name: "test",
}
require.Nil(t, testings.Put(model))
row := testDB.QueryRow(`SELECT name FROM testing`)
var name string
require.NoError(t, row.Scan(&name))
require.Equal(t, "test", name)
}