Test Setup Failed
Push — master ( c03f6f...d09424 )
by DEATH
01:32
created

test.Test_Increment   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
package test
2
3
import (
4
    "github.com/deathkel/sqlbuilder/builder"
5
    "testing"
6
    "reflect"
7
)
8
9
func Test_Select(t *testing.T) {
10
    b := new(builder.Builder)
11
    sql, bindings := b.Select([]string{"*", "sex", "a.name", "count(1) as count"}).
12
        From("user").
13
        Where("a", "1").
14
        GroupBy("a").
15
        Having("a", ">", "2").
16
        Limit("3").
17
        Offset("4").
18
        ToSql()
19
    if sql != "select *, `sex`, a.name, count(1) as count from `user` where (`a` = ?) group by `a` having `a` > ? offset ? limit ?" {
20
        t.Error(sql)
21
    }
22
    if !reflect.DeepEqual(bindings, []string{"1", "2", "4", "3"}) {
23
        t.Error(bindings)
24
    }
25
}
26
27
func Test_Join(t *testing.T) {
28
    b := new(builder.Builder)
29
    
30
    sql, bindings := b.Select([]string{"*"}).From("ta").
31
        Join("tb", "`tb`.`aid` = `ta`.`id`").
32
        Where("ta.id", ">", "1").
33
        Where("tb.name", "=", "jack").
34
        ToSql()
35
    
36
    if sql != "select * from `ta` join `tb` on `tb`.`aid` = `ta`.`id` where (ta.id > ? and tb.name = ?)" {
37
        t.Error(sql)
38
    }
39
    if !reflect.DeepEqual(bindings, []string{"1", "jack"}) {
40
        t.Error(bindings)
41
    }
42
}
43
44
func Test_Insert(t *testing.T) {
45
    b := new(builder.Builder)
46
    
47
    info := map[string]string{"name": "john"}
48
    sql, bindings := b.Insert("ta", info).ToSql()
49
    if sql != "insert into `ta` (`name`) values (?)" {
50
        t.Error(sql)
51
    }
52
    
53
    if !reflect.DeepEqual(bindings, []string{"john"}) {
54
        t.Error(bindings)
55
    }
56
}
57
58
func Test_Update(t *testing.T) {
59
    b := new(builder.Builder)
60
    
61
    info := map[string]interface{}{"name": "john"}
62
    sql, bindings := b.Update("ta", info).
63
        Where("name", "kel").
64
        Where("sex", "2").
65
        Offset("1").
66
        Limit("2").
67
        ToSql()
68
    if sql != "update `ta` set `name` = ? where (`name` = ? and `sex` = ?) offset ? limit ?" {
69
        t.Error(sql)
70
    }
71
    
72
    if !reflect.DeepEqual(bindings, []string{"john", "kel", "2", "1", "2"}) {
73
        t.Error(bindings)
74
    }
75
}
76
77
func Test_Delete(t *testing.T) {
78
    b := new(builder.Builder)
79
    sql, bindings := b.Delete("ta").
80
        Where("name", "kel").
81
        Where("sex", "2").
82
        Offset("1").
83
        Limit("2").
84
        ToSql()
85
    if sql != "delete `ta` where (`name` = ? and `sex` = ?) offset ? limit ?" {
86
        t.Error(sql)
87
    }
88
    
89
    if !reflect.DeepEqual(bindings, []string{"kel", "2", "1", "2"}) {
90
        t.Error(bindings)
91
    }
92
}
93
94
func Test_Increment(t *testing.T) {
95
    b := new(builder.Builder)
96
    sql, bindings := b.Update("ta", map[string]interface{}{"increase a": &builder.Expression{Value: "a = a + 1"}}).
97
        ToSql()
98
    if sql != "update `ta` set a = a + 1" {
99
        t.Error(sql)
100
    }
101
    
102
    if len(bindings) > 0 {
103
        t.Error(bindings)
104
    }
105
}
106