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