-
Notifications
You must be signed in to change notification settings - Fork 101
/
postgres_test.go
230 lines (193 loc) · 5.3 KB
/
postgres_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package qbs
import (
_ "github.com/lib/pq"
"testing"
//"time"
)
var pgSyntax = dialectSyntax{
NewPostgres(),
`CREATE TABLE IF NOT EXISTS "without_pk" ( "first" text, "last" text, "amount" bigint )`,
`CREATE TABLE "with_pk" ( "primary" bigserial PRIMARY KEY, "first" text, "last" text, "amount" bigint )`,
`INSERT INTO "sql_gen_model" ("prim", "first", "last", "amount") VALUES ($1, $2, $3, $4) RETURNING "prim"`,
`UPDATE "sql_gen_model" SET "first" = $1, "last" = $2, "amount" = $3 WHERE "prim" = $4`,
`DELETE FROM "sql_gen_model" WHERE "prim" = $1`,
`SELECT "post"."id", "post"."author_id", "post"."content", "author"."id" AS author___id, "author"."name" AS author___name FROM "post" LEFT JOIN "user" AS "author" ON "post"."author_id" = "author"."id"`,
`SELECT "name", "grade", "score" FROM "student" WHERE (grade IN ($1, $2, $3)) AND ((score <= $4) OR (score >= $5)) ORDER BY "name", "grade" DESC LIMIT $6 OFFSET $7`,
`DROP TABLE IF EXISTS "drop_table"`,
`ALTER TABLE "a" ADD COLUMN "newc" varchar(100)`,
`CREATE UNIQUE INDEX "iname" ON "itable" ("a", "b", "c")`,
`CREATE INDEX "iname2" ON "itable2" ("d", "e")`,
}
func registerPgTest() {
RegisterWithDataSourceName(DefaultPostgresDataSourceName(testDbName))
}
func setupPgDb() (*Migration, *Qbs) {
registerPgTest()
mg, _ := GetMigration()
q, _ := GetQbs()
return mg, q
}
var postgresSqlTypeResults []string = []string{
"boolean",
"integer",
"integer",
"integer",
"integer",
"integer",
"integer",
"bigint",
"bigint",
"bigint",
"bigint",
"double precision",
"double precision",
"varchar(128)",
"text",
"timestamp with time zone",
"bytea",
"bigint",
"integer",
"boolean",
"double precision",
"timestamp with time zone",
"varchar(128)",
"text",
}
func TestSqlTypeForPgDialect(t *testing.T) {
assert := NewAssert(t)
d := NewPostgres()
testModel := structPtrToModel(new(typeTestTable), false, nil)
for index, column := range testModel.fields {
if storedResult := postgresSqlTypeResults[index]; storedResult != "-" {
result := d.sqlType(*column)
assert.Equal(storedResult, result)
}
}
}
func TestPgTransaction(t *testing.T) {
registerPgTest()
doTestTransaction(NewAssert(t))
}
func TestPgSaveAndDelete(t *testing.T) {
mg, q := setupPgDb()
doTestSaveAndDelete(NewAssert(t), mg, q)
}
func TestPgSaveAgain(t *testing.T) {
mg, q := setupPgDb()
doTestSaveAgain(NewAssert(t), mg, q)
}
func TestPgForeignKey(t *testing.T) {
registerPgTest()
doTestForeignKey(NewAssert(t))
}
func TestPgFind(t *testing.T) {
registerPgTest()
doTestFind(NewAssert(t))
}
func TestPgCreateTable(t *testing.T) {
mg, _ := setupPgDb()
doTestCreateTable(NewAssert(t), mg)
}
func TestPgUpdate(t *testing.T) {
mg, q := setupPgDb()
doTestUpdate(NewAssert(t), mg, q)
}
func TestPgValidation(t *testing.T) {
mg, q := setupPgDb()
doTestValidation(NewAssert(t), mg, q)
}
func TestPgBoolType(t *testing.T) {
mg, q := setupPgDb()
doTestBoolType(NewAssert(t), mg, q)
}
func TestPgStringPk(t *testing.T) {
mg, q := setupPgDb()
doTestStringPk(NewAssert(t), mg, q)
}
func TestPgCount(t *testing.T) {
registerPgTest()
doTestCount(NewAssert(t))
}
func TestPgQueryMap(t *testing.T) {
mg, q := setupPgDb()
doTestQueryMap(NewAssert(t), mg, q)
}
func TestPgBulkInsert(t *testing.T) {
registerPgTest()
doTestBulkInsert(NewAssert(t))
}
func TestPgQueryStruct(t *testing.T) {
registerPgTest()
doTestQueryStruct(NewAssert(t))
}
func TestPgConnectionLimit(t *testing.T) {
registerPgTest()
doTestConnectionLimit(NewAssert(t))
}
func TestPgIterate(t *testing.T) {
registerPgTest()
doTestIterate(NewAssert(t))
}
func TestPgAddColumnSQL(t *testing.T) {
doTestAddColumSQL(NewAssert(t), pgSyntax)
}
func TestPgCreateTableSQL(t *testing.T) {
doTestCreateTableSQL(NewAssert(t), pgSyntax)
}
func TestPgCreateIndexSQL(t *testing.T) {
doTestCreateIndexSQL(NewAssert(t), pgSyntax)
}
func TestPgInsertSQL(t *testing.T) {
doTestInsertSQL(NewAssert(t), pgSyntax)
}
func TestPgUpdateSQL(t *testing.T) {
doTestUpdateSQL(NewAssert(t), pgSyntax)
}
func TestPgDeleteSQL(t *testing.T) {
doTestDeleteSQL(NewAssert(t), pgSyntax)
}
func TestPgSelectionSQL(t *testing.T) {
doTestSelectionSQL(NewAssert(t), pgSyntax)
}
func TestPgQuerySQL(t *testing.T) {
doTestQuerySQL(NewAssert(t), pgSyntax)
}
func TestPgDropTableSQL(t *testing.T) {
doTestDropTableSQL(NewAssert(t), pgSyntax)
}
func TestPgSaveNullable(t *testing.T) {
mg, q := setupPgDb()
doTestSaveNullable(NewAssert(t), mg, q)
}
func TestPgDataSourceName(t *testing.T) {
dsn := new(DataSourceName)
dsn.DbName = "abc"
dsn.Username = "john"
dsn.Dialect = NewPostgres()
assert := NewAssert(t)
assert.Equal("user=john dbname=abc", dsn)
dsn.Password = "123"
assert.Equal("user=john password=123 dbname=abc", dsn)
dsn.Host = "192.168.1.3"
assert.Equal("user=john password=123 dbname=abc host=192.168.1.3", dsn)
dsn.UnixSocket = true
assert.Equal("user=john password=123 dbname=abc host=/192.168.1.3", dsn)
dsn.Port = "9876"
assert.Equal("user=john password=123 dbname=abc host=/192.168.1.3 port=9876", dsn)
}
func BenchmarkPgFind(b *testing.B) {
registerPgTest()
doBenchmarkFind(b, b.N)
}
func BenchmarkPgDbQuery(b *testing.B) {
registerPgTest()
doBenchmarkDbQuery(b, b.N)
}
func BenchmarkPgStmtQuery(b *testing.B) {
registerPgTest()
doBenchmarkStmtQuery(b, b.N)
}
func BenchmarkPgTransaction(b *testing.B) {
registerPgTest()
doBenchmarkTransaction(b, b.N)
}