MCPcopy
hub / github.com/jmoiron/sqlx / TestIn

Function TestIn

sqlx_test.go:1507–1605  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1505}
1506
1507func TestIn(t *testing.T) {
1508 // some quite normal situations
1509 type tr struct {
1510 q string
1511 args []interface{}
1512 c int
1513 }
1514 tests := []tr{
1515 {"SELECT * FROM foo WHERE x = ? AND v in (?) AND y = ?",
1516 []interface{}{"foo", []int{0, 5, 7, 2, 9}, "bar"},
1517 7},
1518 {"SELECT * FROM foo WHERE x in (?)",
1519 []interface{}{[]int{1, 2, 3, 4, 5, 6, 7, 8}},
1520 8},
1521 {"SELECT * FROM foo WHERE x = ? AND y in (?)",
1522 []interface{}{[]byte("foo"), []int{0, 5, 3}},
1523 4},
1524 {"SELECT * FROM foo WHERE x = ? AND y IN (?)",
1525 []interface{}{sql.NullString{Valid: false}, []string{"a", "b"}},
1526 3},
1527 }
1528 for _, test := range tests {
1529 q, a, err := In(test.q, test.args...)
1530 if err != nil {
1531 t.Error(err)
1532 }
1533 if len(a) != test.c {
1534 t.Errorf("Expected %d args, but got %d (%+v)", test.c, len(a), a)
1535 }
1536 if strings.Count(q, "?") != test.c {
1537 t.Errorf("Expected %d bindVars, got %d", test.c, strings.Count(q, "?"))
1538 }
1539 }
1540
1541 // too many bindVars, but no slices, so short circuits parsing
1542 // i'm not sure if this is the right behavior; this query/arg combo
1543 // might not work, but we shouldn't parse if we don't need to
1544 {
1545 orig := "SELECT * FROM foo WHERE x = ? AND y = ?"
1546 q, a, err := In(orig, "foo", "bar", "baz")
1547 if err != nil {
1548 t.Error(err)
1549 }
1550 if len(a) != 3 {
1551 t.Errorf("Expected 3 args, but got %d (%+v)", len(a), a)
1552 }
1553 if q != orig {
1554 t.Error("Expected unchanged query.")
1555 }
1556 }
1557
1558 tests = []tr{
1559 // too many bindvars; slice present so should return error during parse
1560 {"SELECT * FROM foo WHERE x = ? and y = ?",
1561 []interface{}{"foo", []int{1, 2, 3}, "bar"},
1562 0},
1563 // empty slice, should return error before parse
1564 {"SELECT * FROM foo WHERE x = ?",

Callers

nothing calls this directly

Calls 7

InFunction · 0.85
RunWithSchemaFunction · 0.85
loadDefaultFixtureFunction · 0.85
ErrorMethod · 0.80
ErrorfMethod · 0.80
RebindMethod · 0.65
SelectMethod · 0.45

Tested by

no test coverage detected