github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/go-xorm/builder/builder_test.go (about) 1 package builder 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 ) 8 9 func TestBuilderCond(t *testing.T) { 10 var cases = []struct { 11 cond Cond 12 sql string 13 args []interface{} 14 }{ 15 { 16 Eq{"a": 1}.And(Like{"b", "c"}).Or(Eq{"a": 2}.And(Like{"b", "g"})), 17 "(a=? AND b LIKE ?) OR (a=? AND b LIKE ?)", 18 []interface{}{1, "%c%", 2, "%g%"}, 19 }, 20 { 21 Eq{"a": 1}.Or(Like{"b", "c"}).And(Eq{"a": 2}.Or(Like{"b", "g"})), 22 "(a=? OR b LIKE ?) AND (a=? OR b LIKE ?)", 23 []interface{}{1, "%c%", 2, "%g%"}, 24 }, 25 { 26 Eq{"d": []string{"e", "f"}}, 27 "d IN (?,?)", 28 []interface{}{"e", "f"}, 29 }, 30 { 31 Neq{"d": []string{"e", "f"}}, 32 "d NOT IN (?,?)", 33 []interface{}{"e", "f"}, 34 }, 35 { 36 Lt{"d": 3}, 37 "d<?", 38 []interface{}{3}, 39 }, 40 { 41 Lte{"d": 3}, 42 "d<=?", 43 []interface{}{3}, 44 }, 45 { 46 Gt{"d": 3}, 47 "d>?", 48 []interface{}{3}, 49 }, 50 { 51 Gte{"d": 3}, 52 "d>=?", 53 []interface{}{3}, 54 }, 55 { 56 Between{"d", 0, 2}, 57 "d BETWEEN ? AND ?", 58 []interface{}{0, 2}, 59 }, 60 { 61 IsNull{"d"}, 62 "d IS NULL", 63 []interface{}{}, 64 }, 65 { 66 NotIn("a", 1, 2).And(NotIn("b", "c", "d")), 67 "a NOT IN (?,?) AND b NOT IN (?,?)", 68 []interface{}{1, 2, "c", "d"}, 69 }, 70 { 71 In("a", 1, 2).Or(In("b", "c", "d")), 72 "a IN (?,?) OR b IN (?,?)", 73 []interface{}{1, 2, "c", "d"}, 74 }, 75 { 76 In("a", []int{1, 2}).Or(In("b", []string{"c", "d"})), 77 "a IN (?,?) OR b IN (?,?)", 78 []interface{}{1, 2, "c", "d"}, 79 }, 80 { 81 In("a", Expr("select id from x where name > ?", "b")), 82 "a IN (select id from x where name > ?)", 83 []interface{}{"b"}, 84 }, 85 { 86 NotIn("a", Expr("select id from x where name > ?", "b")), 87 "a NOT IN (select id from x where name > ?)", 88 []interface{}{"b"}, 89 }, 90 { 91 Or(Eq{"a": 1, "b": 2}, Eq{"c": 3, "d": 4}), 92 "(a=? AND b=?) OR (c=? AND d=?)", 93 []interface{}{1, 2, 3, 4}, 94 }, 95 } 96 97 for _, k := range cases { 98 sql, args, err := ToSQL(k.cond) 99 if err != nil { 100 t.Error(err) 101 return 102 } 103 if sql != k.sql { 104 t.Error("want", k.sql, "get", sql) 105 return 106 } 107 fmt.Println(sql) 108 109 if !(len(args) == 0 && len(k.args) == 0) { 110 if !reflect.DeepEqual(args, k.args) { 111 t.Error("want", k.args, "get", args) 112 return 113 } 114 } 115 fmt.Println(args) 116 } 117 } 118 119 func TestBuilderSelect(t *testing.T) { 120 sql, args, err := Select("c, d").From("table1").Where(Eq{"a": 1}).ToSQL() 121 if err != nil { 122 t.Error(err) 123 return 124 } 125 fmt.Println(sql, args) 126 127 sql, args, err = Select("c, d").From("table1").LeftJoin("table2", Eq{"table1.id": 1}.And(Lt{"table2.id": 3})). 128 RightJoin("table3", "table2.id = table3.tid").Where(Eq{"a": 1}).ToSQL() 129 if err != nil { 130 t.Error(err) 131 return 132 } 133 fmt.Println(sql, args) 134 } 135 136 func TestBuilderInsert(t *testing.T) { 137 sql, args, err := Insert(Eq{"c": 1, "d": 2}).Into("table1").ToSQL() 138 if err != nil { 139 t.Error(err) 140 return 141 } 142 fmt.Println(sql, args) 143 } 144 145 func TestBuilderUpdate(t *testing.T) { 146 sql, args, err := Update(Eq{"a": 2}).From("table1").Where(Eq{"a": 1}).ToSQL() 147 if err != nil { 148 t.Error(err) 149 return 150 } 151 fmt.Println(sql, args) 152 153 sql, args, err = Update(Eq{"a": 2, "b": Incr(1)}).From("table2").Where(Eq{"a": 1}).ToSQL() 154 if err != nil { 155 t.Error(err) 156 return 157 } 158 fmt.Println(sql, args) 159 160 sql, args, err = Update(Eq{"a": 2, "b": Incr(1), "c": Decr(1), "d": Expr("select count(*) from table2")}).From("table2").Where(Eq{"a": 1}).ToSQL() 161 if err != nil { 162 t.Error(err) 163 return 164 } 165 fmt.Println(sql, args) 166 } 167 168 func TestBuilderDelete(t *testing.T) { 169 sql, args, err := Delete(Eq{"a": 1}).From("table1").ToSQL() 170 if err != nil { 171 t.Error(err) 172 return 173 } 174 fmt.Println(sql, args) 175 }