gitee.com/eden-framework/sqlx@v0.0.3/builder/condition_test.go (about) 1 package builder_test 2 3 import ( 4 "testing" 5 6 . "gitee.com/eden-framework/sqlx/builder" 7 . "gitee.com/eden-framework/sqlx/builder/buidertestingutils" 8 "github.com/onsi/gomega" 9 ) 10 11 func TestConditions(t *testing.T) { 12 t.Run("Chain Condition", func(t *testing.T) { 13 gomega.NewWithT(t).Expect( 14 Col("a").Eq(1). 15 And(nil). 16 And(Col("b").LeftLike("text")). 17 Or(Col("a").Eq(2)). 18 Xor(Col("b").RightLike("g")), 19 ).To(BeExpr( 20 "(((a = ?) AND (b LIKE ?)) OR (a = ?)) XOR (b LIKE ?)", 21 1, "%text", 2, "g%", 22 )) 23 }) 24 t.Run("Compose Condition", func(t *testing.T) { 25 gomega.NewWithT(t).Expect( 26 Xor( 27 Or( 28 And( 29 (*Condition)(nil), 30 (*Condition)(nil), 31 (*Condition)(nil), 32 (*Condition)(nil), 33 Col("c").In(1, 2), 34 Col("c").In([]int{3, 4}), 35 Col("a").Eq(1), 36 Col("b").Like("text"), 37 ), 38 Col("a").Eq(2), 39 ), 40 Col("b").Like("g"), 41 ), 42 ).To(BeExpr( 43 "(((c IN (?,?)) AND (c IN (?,?)) AND (a = ?) AND (b LIKE ?)) OR (a = ?)) XOR (b LIKE ?)", 44 1, 2, 3, 4, 1, "%text%", 2, "%g%", 45 )) 46 }) 47 t.Run("skip nil", func(t *testing.T) { 48 gomega.NewWithT(t).Expect( 49 Xor( 50 Col("a").In(), 51 Or( 52 Col("a").NotIn(), 53 And( 54 nil, 55 Col("a").Eq(1), 56 Col("b").Like("text"), 57 ), 58 Col("a").Eq(2), 59 ), 60 Col("b").Like("g"), 61 ), 62 ).To(BeExpr( 63 "(((a = ?) AND (b LIKE ?)) OR (a = ?)) XOR (b LIKE ?)", 64 1, "%text%", 2, "%g%", 65 )) 66 }) 67 t.Run("XOR and OR", func(t *testing.T) { 68 gomega.NewWithT(t).Expect( 69 Xor( 70 Col("a").In(), 71 Or( 72 Col("a").NotIn(), 73 And( 74 nil, 75 Col("a").Eq(1), 76 Col("b").Like("text"), 77 ), 78 Col("a").Eq(2), 79 ), 80 Col("b").Like("g"), 81 ), 82 ).To(BeExpr( 83 "(((a = ?) AND (b LIKE ?)) OR (a = ?)) XOR (b LIKE ?)", 84 1, "%text%", 2, "%g%", 85 )) 86 }) 87 t.Run("XOR", func(t *testing.T) { 88 gomega.NewWithT(t).Expect( 89 Xor( 90 Col("a").Eq(1), 91 Col("b").Like("g"), 92 ), 93 ).To(BeExpr( 94 "(a = ?) XOR (b LIKE ?)", 95 1, "%g%", 96 )) 97 }) 98 t.Run("Like", func(t *testing.T) { 99 gomega.NewWithT(t).Expect( 100 Col("d").Like("e"), 101 ).To(BeExpr( 102 "d LIKE ?", 103 "%e%", 104 )) 105 }) 106 t.Run("Not like", func(t *testing.T) { 107 gomega.NewWithT(t).Expect( 108 Col("d").NotLike("e"), 109 ).To(BeExpr( 110 "d NOT LIKE ?", 111 "%e%", 112 )) 113 }) 114 t.Run("Equal", func(t *testing.T) { 115 gomega.NewWithT(t).Expect( 116 Col("d").Eq("e"), 117 ).To(BeExpr( 118 "d = ?", 119 "e", 120 )) 121 }) 122 t.Run("Not Equal", func(t *testing.T) { 123 gomega.NewWithT(t).Expect( 124 Col("d").Neq("e"), 125 ).To(BeExpr( 126 "d <> ?", 127 "e", 128 )) 129 }) 130 t.Run("In", func(t *testing.T) { 131 gomega.NewWithT(t).Expect( 132 Col("d").In("e", "f"), 133 ).To(BeExpr( 134 "d IN (?,?)", 135 "e", "f", 136 )) 137 }) 138 t.Run("NotIn", func(t *testing.T) { 139 gomega.NewWithT(t).Expect( 140 Col("d").NotIn("e", "f"), 141 ).To(BeExpr( 142 "d NOT IN (?,?)", 143 "e", "f", 144 )) 145 }) 146 t.Run("Less than", func(t *testing.T) { 147 gomega.NewWithT(t).Expect( 148 Col("d").Lt(3), 149 ).To(BeExpr( 150 "d < ?", 151 3, 152 )) 153 }) 154 t.Run("Less or equal than", func(t *testing.T) { 155 gomega.NewWithT(t).Expect( 156 Col("d").Lte(3), 157 ).To(BeExpr( 158 "d <= ?", 159 3, 160 )) 161 }) 162 t.Run("Greater than", func(t *testing.T) { 163 gomega.NewWithT(t).Expect( 164 Col("d").Gt(3), 165 ).To(BeExpr( 166 "d > ?", 167 3, 168 )) 169 }) 170 t.Run("Greater or equal than", func(t *testing.T) { 171 gomega.NewWithT(t).Expect( 172 Col("d").Gte(3), 173 ).To(BeExpr( 174 "d >= ?", 175 3, 176 )) 177 }) 178 t.Run("Between", func(t *testing.T) { 179 gomega.NewWithT(t).Expect( 180 Col("d").Between(0, 2), 181 ).To(BeExpr( 182 "d BETWEEN ? AND ?", 183 0, 2, 184 )) 185 }) 186 t.Run("Not between", func(t *testing.T) { 187 gomega.NewWithT(t).Expect( 188 Col("d").NotBetween(0, 2), 189 ).To(BeExpr( 190 "d NOT BETWEEN ? AND ?", 191 0, 2, 192 )) 193 }) 194 t.Run("Is null", func(t *testing.T) { 195 gomega.NewWithT(t).Expect( 196 Col("d").IsNull(), 197 ).To(BeExpr( 198 "d IS NULL", 199 )) 200 }) 201 t.Run("Is not null", func(t *testing.T) { 202 gomega.NewWithT(t).Expect( 203 Col("d").IsNotNull(), 204 ).To(BeExpr( 205 "d IS NOT NULL", 206 )) 207 }) 208 }