Test Failed
Push — master ( 1b1099...77c07c )
by DEATH
02:18
created

sqlbuilder.Test_Insert   A

Complexity

Conditions 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
package sqlbuilder
2
3
import (
4
    "testing"
5
    "reflect"
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` > ? offset ? limit ?" {
19
        t.Error(sql)
20
    }
21
    if !reflect.DeepEqual(bindings, []string{"1", "2", "4", "3"}) {
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` = ?) offset ? limit ?" {
68
        t.Error(sql)
69
    }
70
    
71
    if !reflect.DeepEqual(bindings, []string{"john", "kel", "2", "1", "2"}) {
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` = ?) offset ? limit ?" {
85
        t.Error(sql)
86
    }
87
    
88
    if !reflect.DeepEqual(bindings, []string{"kel", "2", "1", "2"}) {
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