(t *testing.T)
| 1209 | } |
| 1210 | |
| 1211 | func TestInContext(t *testing.T) { |
| 1212 | // some quite normal situations |
| 1213 | type tr struct { |
| 1214 | q string |
| 1215 | args []interface{} |
| 1216 | c int |
| 1217 | } |
| 1218 | tests := []tr{ |
| 1219 | {"SELECT * FROM foo WHERE x = ? AND v in (?) AND y = ?", |
| 1220 | []interface{}{"foo", []int{0, 5, 7, 2, 9}, "bar"}, |
| 1221 | 7}, |
| 1222 | {"SELECT * FROM foo WHERE x in (?)", |
| 1223 | []interface{}{[]int{1, 2, 3, 4, 5, 6, 7, 8}}, |
| 1224 | 8}, |
| 1225 | } |
| 1226 | for _, test := range tests { |
| 1227 | q, a, err := In(test.q, test.args...) |
| 1228 | if err != nil { |
| 1229 | t.Error(err) |
| 1230 | } |
| 1231 | if len(a) != test.c { |
| 1232 | t.Errorf("Expected %d args, but got %d (%+v)", test.c, len(a), a) |
| 1233 | } |
| 1234 | if strings.Count(q, "?") != test.c { |
| 1235 | t.Errorf("Expected %d bindVars, got %d", test.c, strings.Count(q, "?")) |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | // too many bindVars, but no slices, so short circuits parsing |
| 1240 | // i'm not sure if this is the right behavior; this query/arg combo |
| 1241 | // might not work, but we shouldn't parse if we don't need to |
| 1242 | { |
| 1243 | orig := "SELECT * FROM foo WHERE x = ? AND y = ?" |
| 1244 | q, a, err := In(orig, "foo", "bar", "baz") |
| 1245 | if err != nil { |
| 1246 | t.Error(err) |
| 1247 | } |
| 1248 | if len(a) != 3 { |
| 1249 | t.Errorf("Expected 3 args, but got %d (%+v)", len(a), a) |
| 1250 | } |
| 1251 | if q != orig { |
| 1252 | t.Error("Expected unchanged query.") |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | tests = []tr{ |
| 1257 | // too many bindvars; slice present so should return error during parse |
| 1258 | {"SELECT * FROM foo WHERE x = ? and y = ?", |
| 1259 | []interface{}{"foo", []int{1, 2, 3}, "bar"}, |
| 1260 | 0}, |
| 1261 | // empty slice, should return error before parse |
| 1262 | {"SELECT * FROM foo WHERE x = ?", |
| 1263 | []interface{}{[]int{}}, |
| 1264 | 0}, |
| 1265 | // too *few* bindvars, should return an error |
| 1266 | {"SELECT * FROM foo WHERE x = ? AND y in (?)", |
| 1267 | []interface{}{[]int{1, 2, 3}}, |
| 1268 | 0}, |
nothing calls this directly
no test coverage detected