-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhas_test.go
101 lines (84 loc) · 2.82 KB
/
has_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
package bob_test
import (
"reflect"
"testing"
"github.com/aldy505/bob"
)
func TestHasTable(t *testing.T) {
sql, args, err := bob.HasTable("users").ToSql()
if err != nil {
t.Fatal(err.Error())
}
result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = current_schema();"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}
argsResult := []interface{}{"users"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
}
func TestHasColumn(t *testing.T) {
t.Run("should be able to create a hasColumn query", func(t *testing.T) {
sql, args, err := bob.HasTable("users").HasColumn("name").ToSql()
if err != nil {
t.Fatal(err.Error())
}
result := "SELECT * FROM information_schema.columns WHERE table_name = ? AND column_name = ? AND table_schema = current_schema();"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}
argsResult := []interface{}{"users", "name"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
})
t.Run("should be able to create a hasColumn query (but reversed)", func(t *testing.T) {
sql, args, err := bob.HasColumn("name").HasTable("users").ToSql()
if err != nil {
t.Fatal(err.Error())
}
result := "SELECT * FROM information_schema.columns WHERE table_name = ? AND column_name = ? AND table_schema = current_schema();"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}
argsResult := []interface{}{"users", "name"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
})
}
func TestHas_Schema(t *testing.T) {
sql, args, err := bob.HasTable("users").WithSchema("private").ToSql()
if err != nil {
t.Fatal(err.Error())
}
result := "SELECT * FROM information_schema.tables WHERE table_name = ? AND table_schema = ?;"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}
argsResult := []interface{}{"users", "private"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
}
func TestHas_PlaceholderFormats(t *testing.T) {
sql, args, err := bob.HasTable("users").HasColumn("name").PlaceholderFormat(bob.Dollar).ToSql()
if err != nil {
t.Fatal(err.Error())
}
result := "SELECT * FROM information_schema.columns WHERE table_name = $1 AND column_name = $2 AND table_schema = current_schema();"
if sql != result {
t.Fatal("sql is not equal with result:", sql)
}
argsResult := []interface{}{"users", "name"}
if !reflect.DeepEqual(args, argsResult) {
t.Fatal("args is not equal with argsResult:", args)
}
}
func TestHas_EmitError(t *testing.T) {
_, _, err := bob.HasTable("").ToSql()
if err.Error() != "has statement should have a table name" {
t.Fatal("error is different:", err.Error())
}
}