package gocqlx import "testing" func TestCompileQuery(t *testing.T) { table := []struct { Q, R string V []string }{ // basic test for named parameters, invalid char ',' terminating { Q: `INSERT INTO foo (a,b,c,d) VALUES (:name, :age, :first, :last)`, R: `INSERT INTO foo (a,b,c,d) VALUES (?, ?, ?, ?)`, V: []string{"name", "age", "first", "last"}, }, // This query tests a named parameter ending the string as well as numbers { Q: `SELECT * FROM a WHERE first_name=:name1 AND last_name=:name2`, R: `SELECT * FROM a WHERE first_name=? AND last_name=?`, V: []string{"name1", "name2"}, }, { Q: `SELECT "::foo" FROM a WHERE first_name=:name1 AND last_name=:name2`, R: `SELECT ":foo" FROM a WHERE first_name=? AND last_name=?`, V: []string{"name1", "name2"}, }, { Q: `SELECT 'a::b::c' || first_name, '::::ABC::_::' FROM person WHERE first_name=:first_name AND last_name=:last_name`, R: `SELECT 'a:b:c' || first_name, '::ABC:_:' FROM person WHERE first_name=? AND last_name=?`, V: []string{"first_name", "last_name"}, }, /* This unicode awareness test sadly fails, because of our byte-wise worldview. * We could certainly iterate by Rune instead, though it's a great deal slower, * it's probably the RightWay(tm) { Q: `INSERT INTO foo (a,b,c,d) VALUES (:あ, :b, :キコ, :名前)`, R: `INSERT INTO foo (a,b,c,d) VALUES (?, ?, ?, ?)`, }, */ } for _, test := range table { qr, names, err := CompileNamedQuery([]byte(test.Q)) if err != nil { t.Error(err) } if qr != test.R { t.Errorf("expected %s, got %s", test.R, qr) } if len(names) != len(test.V) { t.Errorf("expected %#v, got %#v", test.V, names) } else { for i, name := range names { if name != test.V[i] { t.Errorf("expected %dth name to be %s, got %s", i+1, test.V[i], name) } } } } }