Passed
Push — master ( b769f5...17bc27 )
by DEATH
01:50
created

grammar_test.go   A

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 92
dl 0
loc 132
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A sqlbuilder.Test_Insert 0 11 3
A sqlbuilder.Test_Increment 0 10 3
A sqlbuilder.Test_Join 0 14 3
A sqlbuilder.Test_Update 0 16 3
A sqlbuilder.Test_Select 0 15 3
A sqlbuilder.Test_Delete 0 14 3
A sqlbuilder.Test_WhereIn 0 12 3
A sqlbuilder.Test_Where 0 12 3
1
package sqlbuilder
2
3
import (
4
	"reflect"
5
	"testing"
6
)
7
8
func Test_Select(t *testing.T) {
9
	b := new(Builder)
10
	sql, bindings := b.Select([]string{"*", "sex", "a.name", "count(1) as count"}).
11
		From("user").
12
		Where("a", "1").
13
		GroupBy("a").
14
		Having("a", ">", "2").
15
		Limit("3").
16
		Offset("4").
17
		ToSql()
18
	if sql != "select *, `sex`, a.name, count(1) as count from `user` where (`a` = ?) group by `a` having `a` > ? limit ? offset ?" {
19
		t.Error(sql)
20
	}
21
	if !reflect.DeepEqual(bindings, []string{"1", "2", "3", "4"}) {
22
		t.Error(bindings)
23
	}
24
}
25
26
func Test_Join(t *testing.T) {
27
	b := new(Builder)
28
29
	sql, bindings := b.Select([]string{"*"}).From("ta").
30
		Join("tb", "`tb`.`aid` = `ta`.`id`").
31
		Where("ta.id", ">", "1").
32
		Where("tb.name", "jack").
33
		ToSql()
34
35
	if sql != "select * from `ta` join `tb` on `tb`.`aid` = `ta`.`id` where (ta.id > ? and tb.name = ?)" {
36
		t.Error(sql)
37
	}
38
	if !reflect.DeepEqual(bindings, []string{"1", "jack"}) {
39
		t.Error(bindings)
40
	}
41
}
42
43
func Test_Insert(t *testing.T) {
44
	b := new(Builder)
45
46
	info := map[string]string{"name": "john"}
47
	sql, bindings := b.Insert("ta", info).ToSql()
48
	if sql != "insert into `ta` (`name`) values (?)" {
49
		t.Error(sql)
50
	}
51
52
	if !reflect.DeepEqual(bindings, []string{"john"}) {
53
		t.Error(bindings)
54
	}
55
}
56
57
func Test_Update(t *testing.T) {
58
	b := new(Builder)
59
60
	info := map[string]interface{}{"name": "john"}
61
	sql, bindings := b.Update("ta", info).
62
		Where("name", "kel").
63
		Where("sex", "2").
64
		Offset("1").
65
		Limit("2").
66
		ToSql()
67
	if sql != "update `ta` set `name` = ? where (`name` = ? and `sex` = ?) limit ? offset ?" {
68
		t.Error(sql)
69
	}
70
71
	if !reflect.DeepEqual(bindings, []string{"john", "kel", "2", "2", "1"}) {
72
		t.Error(bindings)
73
	}
74
}
75
76
func Test_Delete(t *testing.T) {
77
	b := new(Builder)
78
	sql, bindings := b.Delete("ta").
79
		Where("name", "kel").
80
		Where("sex", "2").
81
		Offset("1").
82
		Limit("2").
83
		ToSql()
84
	if sql != "delete `ta` where (`name` = ? and `sex` = ?) limit ? offset ?" {
85
		t.Error(sql)
86
	}
87
88
	if !reflect.DeepEqual(bindings, []string{"kel", "2", "2", "1"}) {
89
		t.Error(bindings)
90
	}
91
}
92
93
func Test_Increment(t *testing.T) {
94
	b := new(Builder)
95
	sql, bindings := b.Update("ta", map[string]interface{}{"increase a": &Expression{Value: "a = a + 1"}}).
96
		ToSql()
97
	if sql != "update `ta` set a = a + 1" {
98
		t.Error(sql)
99
	}
100
101
	if len(bindings) > 0 {
102
		t.Error(bindings)
103
	}
104
}
105
106
func Test_WhereIn(t *testing.T) {
107
	b := new(Builder)
108
	sql, bindings := b.Select([]string{"*"}).
109
		From("user").
110
		Where("a", "in", []string{"1", "2", "3", "4"}).
111
		Where("b", "not in", []string{"1", "2", "3", "4"}).
112
		ToSql()
113
	if sql != "select * from `user` where (`a` in (?, ?, ?, ? ) and `b` not in (?, ?, ?, ? ))" {
114
		t.Error(sql)
115
	}
116
	if !reflect.DeepEqual(bindings, []string{"1", "2", "3", "4", "1", "2", "3", "4"}) {
117
		t.Error(bindings)
118
	}
119
}
120
121
func Test_Where(t *testing.T) {
122
	b := new(Builder)
123
	sql, bindings := b.Select([]string{"*"}).
124
		From("user").
125
		Where("a", "in", []string{"1", "2", "3", "4"}, "or").
126
		Where("b", "not in", []string{"1", "2", "3", "4"}).
127
		ToSql()
128
	if sql != "select * from `user` where (`a` in (?, ?, ?, ? ) or `b` not in (?, ?, ?, ? ))" {
129
		t.Error(sql)
130
	}
131
	if !reflect.DeepEqual(bindings, []string{"1", "2", "3", "4", "1", "2", "3", "4"}) {
132
		t.Error(bindings)
133
	}
134
}
135