forked from coocood/qbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres_test.go
156 lines (128 loc) · 3.84 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
package qbs
import (
"fmt"
"github.com/coocood/assrt"
_ "github.com/lib/pq"
"testing"
"time"
)
const (
pgDriver = "postgres"
pgDrvFormat = "user=%v password=%v dbname=%v sslmode=disable"
)
var pgSyntax = dialectSyntax{
NewPostgres(),
`CREATE TABLE IF NOT EXISTS "without_pk" ( "first" text, "last" text, "amount" integer )`,
`CREATE TABLE "with_pk" ( "primary" bigserial PRIMARY KEY, "first" text, "last" text, "amount" integer )`,
`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 "c" varchar(100)`,
`CREATE UNIQUE INDEX "iname" ON "itable" ("a", "b", "c")`,
`CREATE INDEX "iname2" ON "itable2" ("d", "e")`,
}
func registerPgTest() {
Register(pgDriver, fmt.Sprintf(pgDrvFormat, testDbUser, testDbUser, testDbName), testDbName, NewPostgres())
}
func setupPgDb() (*Migration, *Qbs) {
registerPgTest()
mg, _ := GetMigration()
q, _ := GetQbs()
return mg, q
}
func TestSqlTypeForPgDialect(t *testing.T) {
assert := assrt.NewAssert(t)
d := NewPostgres()
assert.Equal("boolean", d.sqlType(true, 0))
var indirect interface{} = true
assert.Equal("boolean", d.sqlType(indirect, 0))
assert.Equal("integer", d.sqlType(uint32(2), 0))
assert.Equal("bigint", d.sqlType(int64(1), 0))
assert.Equal("double precision", d.sqlType(1.8, 0))
assert.Equal("bytea", d.sqlType([]byte("asdf"), 0))
assert.Equal("text", d.sqlType("astring", 0))
assert.Equal("varchar(255)", d.sqlType("a", 255))
assert.Equal("varchar(128)", d.sqlType("b", 128))
assert.Equal("timestamp with time zone", d.sqlType(time.Now(), 0))
}
func TestPgTransaction(t *testing.T) {
mg, q := setupPgDb()
doTestTransaction(t, mg, q)
}
func TestPgSaveAndDelete(t *testing.T) {
mg, q := setupPgDb()
doTestSaveAndDelete(t, mg, q)
}
func TestPgSaveAgain(t *testing.T) {
mg, q := setupPgDb()
doTestSaveAgain(t, mg, q)
}
func TestPgForeignKey(t *testing.T) {
mg, q := setupPgDb()
doTestForeignKey(t, mg, q)
}
func TestPgFind(t *testing.T) {
registerPgTest()
doTestFind(t)
}
func TestPgCreateTable(t *testing.T) {
mg, _ := setupPgDb()
doTestCreateTable(t, mg)
}
func TestPgUpdate(t *testing.T) {
mg, q := setupPgDb()
doTestUpdate(t, mg, q)
}
func TestPgValidation(t *testing.T) {
mg, q := setupPgDb()
doTestValidation(t, mg, q)
}
func TestPgBoolType(t *testing.T) {
mg, q := setupPgDb()
doTestBoolType(t, mg, q)
}
func TestPgStringPk(t *testing.T) {
mg, q := setupPgDb()
doTestStringPk(t, mg, q)
}
func TestPgCount(t *testing.T) {
mg, q := setupPgDb()
doTestCount(t, mg, q)
}
func TestPgQueryMap(t *testing.T) {
mg, q := setupPgDb()
doTestQueryMap(t, mg, q)
}
func TestPgAddColumnSQL(t *testing.T) {
doTestAddColumSQL(t, pgSyntax)
}
func TestPgCreateTableSQL(t *testing.T) {
doTestCreateTableSQL(t, pgSyntax)
}
func TestPgCreateIndexSQL(t *testing.T) {
doTestCreateIndexSQL(t, pgSyntax)
}
func TestPgInsertSQL(t *testing.T) {
doTestInsertSQL(t, pgSyntax)
}
func TestPgUpdateSQL(t *testing.T) {
doTestUpdateSQL(t, pgSyntax)
}
func TestPgDeleteSQL(t *testing.T) {
doTestDeleteSQL(t, pgSyntax)
}
func TestPgSelectionSQL(t *testing.T) {
doTestSelectionSQL(t, pgSyntax)
}
func TestPgQuerySQL(t *testing.T) {
doTestQuerySQL(t, pgSyntax)
}
func TestPgDropTableSQL(t *testing.T) {
doTestDropTableSQL(t, pgSyntax)
}
func BenchmarkPgFind(b *testing.B) {
doBenchmarkFind(b)
}