Debian Codegraph @

builder_standard_test.go

raw symbols SCIP
1 // Copyright 2016 Qiang Xue. All rights reserved.
2 // Use of this source code is governed by a MIT-style
3 // license that can be found in the LICENSE file.
4
5 package dbx
6
7 import (
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 )
12
13 func TestStandardBuilder_Quote(t *testing.T) {
14 b := getStandardBuilder()
15 assert.Equal(t, b.Quote(`abc`), `'abc'`, "t1")
16 assert.Equal(t, b.Quote(`I'm`), `'I''m'`, "t2")
17 assert.Equal(t, b.Quote(``), `''`, "t3")
18 }
19
20 func TestStandardBuilder_QuoteSimpleTableName(t *testing.T) {
21 b := getStandardBuilder()
22 assert.Equal(t, b.QuoteSimpleTableName(`abc`), `"abc"`, "t1")
23 assert.Equal(t, b.QuoteSimpleTableName(`"abc"`), `"abc"`, "t2")
24 assert.Equal(t, b.QuoteSimpleTableName(`{{abc}}`), `"{{abc}}"`, "t3")
25 assert.Equal(t, b.QuoteSimpleTableName(`a.bc`), `"a.bc"`, "t4")
26 }
27
28 func TestStandardBuilder_QuoteSimpleColumnName(t *testing.T) {
29 b := getStandardBuilder()
30 assert.Equal(t, b.QuoteSimpleColumnName(`abc`), `"abc"`, "t1")
31 assert.Equal(t, b.QuoteSimpleColumnName(`"abc"`), `"abc"`, "t2")
32 assert.Equal(t, b.QuoteSimpleColumnName(`{{abc}}`), `"{{abc}}"`, "t3")
33 assert.Equal(t, b.QuoteSimpleColumnName(`a.bc`), `"a.bc"`, "t4")
34 assert.Equal(t, b.QuoteSimpleColumnName(`*`), `*`, "t5")
35 }
36
37 func TestStandardBuilder_Insert(t *testing.T) {
38 b := getStandardBuilder()
39 q := b.Insert("users", Params{
40 "name": "James",
41 "age": 30,
42 })
43 assert.Equal(t, q.SQL(), `INSERT INTO "users" ("age", "name") VALUES ({:p0}, {:p1})`, "t1")
44 assert.Equal(t, q.Params()["p0"], 30, "t2")
45 assert.Equal(t, q.Params()["p1"], "James", "t3")
46
47 q = b.Insert("users", Params{})
48 assert.Equal(t, q.SQL(), `INSERT INTO "users" DEFAULT VALUES`, "t2")
49 }
50
51 func TestStandardBuilder_Upsert(t *testing.T) {
52 b := getStandardBuilder()
53 q := b.Upsert("users", Params{
54 "name": "James",
55 "age": 30,
56 })
57 assert.NotEqual(t, q.LastError, nil, "t1")
58 }
59
60 func TestStandardBuilder_Update(t *testing.T) {
61 b := getStandardBuilder()
62 q := b.Update("users", Params{
63 "name": "James",
64 "age": 30,
65 }, NewExp("id=10"))
66 assert.Equal(t, q.SQL(), `UPDATE "users" SET "age"={:p0}, "name"={:p1} WHERE id=10`, "t1")
67 assert.Equal(t, q.Params()["p0"], 30, "t2")
68 assert.Equal(t, q.Params()["p1"], "James", "t3")
69
70 q = b.Update("users", Params{
71 "name": "James",
72 "age": 30,
73 }, nil)
74 assert.Equal(t, q.SQL(), `UPDATE "users" SET "age"={:p0}, "name"={:p1}`, "t2")
75 }
76
77 func TestStandardBuilder_Delete(t *testing.T) {
78 b := getStandardBuilder()
79 q := b.Delete("users", NewExp("id=10"))
80 assert.Equal(t, q.SQL(), `DELETE FROM "users" WHERE id=10`, "t1")
81 q = b.Delete("users", nil)
82 assert.Equal(t, q.SQL(), `DELETE FROM "users"`, "t2")
83 }
84
85 func TestStandardBuilder_CreateTable(t *testing.T) {
86 b := getStandardBuilder()
87 q := b.CreateTable("users", map[string]string{
88 "id": "int primary key",
89 "name": "varchar(255)",
90 }, "ON DELETE CASCADE")
91 assert.Equal(t, q.SQL(), "CREATE TABLE \"users\" (\"id\" int primary key, \"name\" varchar(255)) ON DELETE CASCADE", "t1")
92 }
93
94 func TestStandardBuilder_RenameTable(t *testing.T) {
95 b := getStandardBuilder()
96 q := b.RenameTable("users", "user")
97 assert.Equal(t, q.SQL(), `RENAME TABLE "users" TO "user"`, "t1")
98 }
99
100 func TestStandardBuilder_DropTable(t *testing.T) {
101 b := getStandardBuilder()
102 q := b.DropTable("users")
103 assert.Equal(t, q.SQL(), `DROP TABLE "users"`, "t1")
104 }
105
106 func TestStandardBuilder_TruncateTable(t *testing.T) {
107 b := getStandardBuilder()
108 q := b.TruncateTable("users")
109 assert.Equal(t, q.SQL(), `TRUNCATE TABLE "users"`, "t1")
110 }
111
112 func TestStandardBuilder_AddColumn(t *testing.T) {
113 b := getStandardBuilder()
114 q := b.AddColumn("users", "age", "int")
115 assert.Equal(t, q.SQL(), `ALTER TABLE "users" ADD "age" int`, "t1")
116 }
117
118 func TestStandardBuilder_DropColumn(t *testing.T) {
119 b := getStandardBuilder()
120 q := b.DropColumn("users", "age")
121 assert.Equal(t, q.SQL(), `ALTER TABLE "users" DROP COLUMN "age"`, "t1")
122 }
123
124 func TestStandardBuilder_RenameColumn(t *testing.T) {
125 b := getStandardBuilder()
126 q := b.RenameColumn("users", "name", "username")
127 assert.Equal(t, q.SQL(), `ALTER TABLE "users" RENAME COLUMN "name" TO "username"`, "t1")
128 }
129
130 func TestStandardBuilder_AlterColumn(t *testing.T) {
131 b := getStandardBuilder()
132 q := b.AlterColumn("users", "name", "int")
133 assert.Equal(t, q.SQL(), `ALTER TABLE "users" CHANGE "name" "name" int`, "t1")
134 }
135
136 func TestStandardBuilder_AddPrimaryKey(t *testing.T) {
137 b := getStandardBuilder()
138 q := b.AddPrimaryKey("users", "pk", "id1", "id2")
139 assert.Equal(t, q.SQL(), `ALTER TABLE "users" ADD CONSTRAINT "pk" PRIMARY KEY ("id1", "id2")`, "t1")
140 }
141
142 func TestStandardBuilder_DropPrimaryKey(t *testing.T) {
143 b := getStandardBuilder()
144 q := b.DropPrimaryKey("users", "pk")
145 assert.Equal(t, q.SQL(), `ALTER TABLE "users" DROP CONSTRAINT "pk"`, "t1")
146 }
147
148 func TestStandardBuilder_AddForeignKey(t *testing.T) {
149 b := getStandardBuilder()
150 q := b.AddForeignKey("users", "fk", []string{"p1", "p2"}, []string{"f1", "f2"}, "profile", "opt")
151 assert.Equal(t, q.SQL(), `ALTER TABLE "users" ADD CONSTRAINT "fk" FOREIGN KEY ("p1", "p2") REFERENCES "profile" ("f1", "f2") opt`, "t1")
152 }
153
154 func TestStandardBuilder_DropForeignKey(t *testing.T) {
155 b := getStandardBuilder()
156 q := b.DropForeignKey("users", "fk")
157 assert.Equal(t, q.SQL(), `ALTER TABLE "users" DROP CONSTRAINT "fk"`, "t1")
158 }
159
160 func TestStandardBuilder_CreateIndex(t *testing.T) {
161 b := getStandardBuilder()
162 q := b.CreateIndex("users", "idx", "id1", "id2")
163 assert.Equal(t, q.SQL(), `CREATE INDEX "idx" ON "users" ("id1", "id2")`, "t1")
164 }
165
166 func TestStandardBuilder_CreateUniqueIndex(t *testing.T) {
167 b := getStandardBuilder()
168 q := b.CreateUniqueIndex("users", "idx", "id1", "id2")
169 assert.Equal(t, q.SQL(), `CREATE UNIQUE INDEX "idx" ON "users" ("id1", "id2")`, "t1")
170 }
171
172 func TestStandardBuilder_DropIndex(t *testing.T) {
173 b := getStandardBuilder()
174 q := b.DropIndex("users", "idx")
175 assert.Equal(t, q.SQL(), `DROP INDEX "idx" ON "users"`, "t1")
176 }
177
178 func getStandardBuilder() Builder {
179 db := getDB()
180 b := NewStandardBuilder(db, db.sqlDB)
181 db.Builder = b
182 return b
183 }