Debian Codegraph @

builder_sqlite_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 TestSqliteBuilder_QuoteSimpleTableName(t *testing.T) {
14 b := getSqliteBuilder()
15 assert.Equal(t, b.QuoteSimpleTableName(`abc`), "`abc`", "t1")
16 assert.Equal(t, b.QuoteSimpleTableName("`abc`"), "`abc`", "t2")
17 assert.Equal(t, b.QuoteSimpleTableName(`{{abc}}`), "`{{abc}}`", "t3")
18 assert.Equal(t, b.QuoteSimpleTableName(`a.bc`), "`a.bc`", "t4")
19 }
20
21 func TestSqliteBuilder_QuoteSimpleColumnName(t *testing.T) {
22 b := getSqliteBuilder()
23 assert.Equal(t, b.QuoteSimpleColumnName(`abc`), "`abc`", "t1")
24 assert.Equal(t, b.QuoteSimpleColumnName("`abc`"), "`abc`", "t2")
25 assert.Equal(t, b.QuoteSimpleColumnName(`{{abc}}`), "`{{abc}}`", "t3")
26 assert.Equal(t, b.QuoteSimpleColumnName(`a.bc`), "`a.bc`", "t4")
27 assert.Equal(t, b.QuoteSimpleColumnName(`*`), `*`, "t5")
28 }
29
30 func TestSqliteBuilder_DropIndex(t *testing.T) {
31 b := getSqliteBuilder()
32 q := b.DropIndex("users", "idx")
33 assert.Equal(t, q.SQL(), "DROP INDEX `idx`", "t1")
34 }
35
36 func TestSqliteBuilder_TruncateTable(t *testing.T) {
37 b := getSqliteBuilder()
38 q := b.TruncateTable("users")
39 assert.Equal(t, q.SQL(), "DELETE FROM `users`", "t1")
40 }
41
42 func TestSqliteBuilder_RenameTable(t *testing.T) {
43 b := getSqliteBuilder()
44 q := b.RenameTable("usersOld", "usersNew")
45 assert.Equal(t, q.SQL(), "ALTER TABLE `usersOld` RENAME TO `usersNew`", "t1")
46 }
47
48 func TestSqliteBuilder_AlterColumn(t *testing.T) {
49 b := getSqliteBuilder()
50 q := b.AlterColumn("users", "name", "int")
51 assert.NotEqual(t, q.LastError, nil, "t1")
52 }
53
54 func TestSqliteBuilder_AddPrimaryKey(t *testing.T) {
55 b := getSqliteBuilder()
56 q := b.AddPrimaryKey("users", "pk", "id1", "id2")
57 assert.NotEqual(t, q.LastError, nil, "t1")
58 }
59
60 func TestSqliteBuilder_DropPrimaryKey(t *testing.T) {
61 b := getSqliteBuilder()
62 q := b.DropPrimaryKey("users", "pk")
63 assert.NotEqual(t, q.LastError, nil, "t1")
64 }
65
66 func TestSqliteBuilder_AddForeignKey(t *testing.T) {
67 b := getSqliteBuilder()
68 q := b.AddForeignKey("users", "fk", []string{"p1", "p2"}, []string{"f1", "f2"}, "profile", "opt")
69 assert.NotEqual(t, q.LastError, nil, "t1")
70 }
71
72 func TestSqliteBuilder_DropForeignKey(t *testing.T) {
73 b := getSqliteBuilder()
74 q := b.DropForeignKey("users", "fk")
75 assert.NotEqual(t, q.LastError, nil, "t1")
76 }
77
78 func TestSqliteBuilder_BuildUnion(t *testing.T) {
79 b := getSqliteBuilder()
80 qb := b.QueryBuilder()
81
82 params := Params{}
83 ui := UnionInfo{false, b.NewQuery("SELECT names").Bind(Params{"id": 1})}
84 sql := qb.BuildUnion([]UnionInfo{ui}, params)
85 expected := "UNION SELECT names"
86 assert.Equal(t, expected, sql, "BuildUnion@1")
87 assert.Equal(t, 1, len(params), "len(params)@1")
88
89 params = Params{}
90 ui = UnionInfo{true, b.NewQuery("SELECT names")}
91 sql = qb.BuildUnion([]UnionInfo{ui}, params)
92 expected = "UNION ALL SELECT names"
93 assert.Equal(t, expected, sql, "BuildUnion@2")
94 assert.Equal(t, 0, len(params), "len(params)@2")
95
96 sql = qb.BuildUnion([]UnionInfo{}, nil)
97 expected = ""
98 assert.Equal(t, expected, sql, "BuildUnion@3")
99
100 ui = UnionInfo{true, b.NewQuery("SELECT names")}
101 ui2 := UnionInfo{false, b.NewQuery("SELECT ages")}
102 sql = qb.BuildUnion([]UnionInfo{ui, ui2}, nil)
103 expected = "UNION ALL SELECT names UNION SELECT ages"
104 assert.Equal(t, expected, sql, "BuildUnion@4")
105 }
106
107 func TestSqliteBuilder_CombineUnion(t *testing.T) {
108 b := getSqliteBuilder()
109 qb := b.QueryBuilder()
110
111 sql := qb.CombineUnion("p1", "p2")
112 assert.Equal(t, "p1 p2", sql)
113 }
114
115 func getSqliteBuilder() Builder {
116 db := getDB()
117 b := NewSqliteBuilder(db, db.sqlDB)
118 db.Builder = b
119 return b
120 }