mirror of
https://github.com/chaisql/chai.git
synced 2026-04-22 23:07:05 +08:00
db: only strict schemas
This commit is contained in:
@@ -24,58 +24,15 @@ func TestParserInsert(t *testing.T) {
|
||||
expected *stream.Stream
|
||||
fails bool
|
||||
}{
|
||||
{"Objects", `INSERT INTO test VALUES {a: 1, "b": "foo", c: 'bar', d: 1 = 1, e: {f: "baz"}}`,
|
||||
stream.New(rows.Emit(
|
||||
&expr.KVPairs{SelfReferenced: true, Pairs: []expr.KVPair{
|
||||
{K: "a", V: testutil.IntegerValue(1)},
|
||||
{K: "b", V: testutil.TextValue("foo")},
|
||||
{K: "c", V: testutil.TextValue("bar")},
|
||||
{K: "d", V: testutil.BoolValue(true)},
|
||||
{K: "e", V: &expr.KVPairs{SelfReferenced: true, Pairs: []expr.KVPair{
|
||||
{K: "f", V: testutil.TextValue("baz")},
|
||||
}}},
|
||||
}},
|
||||
)).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(table.Insert("test")).
|
||||
Pipe(stream.Discard()),
|
||||
false},
|
||||
{"Objects / Multiple", `INSERT INTO test VALUES {"a": 'a', b: -2.3}, {a: 1, d: true}`,
|
||||
stream.New(rows.Emit(
|
||||
&expr.KVPairs{SelfReferenced: true, Pairs: []expr.KVPair{
|
||||
{K: "a", V: testutil.TextValue("a")},
|
||||
{K: "b", V: testutil.DoubleValue(-2.3)},
|
||||
}},
|
||||
&expr.KVPairs{SelfReferenced: true, Pairs: []expr.KVPair{{K: "a", V: testutil.IntegerValue(1)}, {K: "d", V: testutil.BoolValue(true)}}},
|
||||
)).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(table.Insert("test")).
|
||||
Pipe(stream.Discard()),
|
||||
false},
|
||||
{"Objects / Positional Param", "INSERT INTO test VALUES ?, ?",
|
||||
stream.New(rows.Emit(
|
||||
expr.PositionalParam(1),
|
||||
expr.PositionalParam(2),
|
||||
)).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(table.Insert("test")).
|
||||
Pipe(stream.Discard()),
|
||||
false},
|
||||
{"Objects / Named Param", "INSERT INTO test VALUES $foo, $bar",
|
||||
stream.New(rows.Emit(
|
||||
expr.NamedParam("foo"),
|
||||
expr.NamedParam("bar"),
|
||||
)).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(table.Insert("test")).
|
||||
Pipe(stream.Discard()),
|
||||
false},
|
||||
{"Values / With fields", "INSERT INTO test (a, b) VALUES ('c', 'd')",
|
||||
stream.New(rows.Emit(
|
||||
&expr.KVPairs{Pairs: []expr.KVPair{
|
||||
{K: "a", V: testutil.TextValue("c")},
|
||||
{K: "b", V: testutil.TextValue("d")},
|
||||
}},
|
||||
expr.Row{
|
||||
Columns: []string{"a", "b"},
|
||||
Exprs: []expr.Expr{
|
||||
testutil.TextValue("c"),
|
||||
testutil.TextValue("d"),
|
||||
},
|
||||
},
|
||||
)).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(table.Insert("test")).
|
||||
@@ -85,14 +42,20 @@ func TestParserInsert(t *testing.T) {
|
||||
nil, true},
|
||||
{"Values / Multiple", "INSERT INTO test (a, b) VALUES ('c', 'd'), ('e', 'f')",
|
||||
stream.New(rows.Emit(
|
||||
&expr.KVPairs{Pairs: []expr.KVPair{
|
||||
{K: "a", V: testutil.TextValue("c")},
|
||||
{K: "b", V: testutil.TextValue("d")},
|
||||
}},
|
||||
&expr.KVPairs{Pairs: []expr.KVPair{
|
||||
{K: "a", V: testutil.TextValue("e")},
|
||||
{K: "b", V: testutil.TextValue("f")},
|
||||
}},
|
||||
expr.Row{
|
||||
Columns: []string{"a", "b"},
|
||||
Exprs: []expr.Expr{
|
||||
testutil.TextValue("c"),
|
||||
testutil.TextValue("d"),
|
||||
},
|
||||
},
|
||||
expr.Row{
|
||||
Columns: []string{"a", "b"},
|
||||
Exprs: []expr.Expr{
|
||||
testutil.TextValue("e"),
|
||||
testutil.TextValue("f"),
|
||||
},
|
||||
},
|
||||
)).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(table.Insert("test")).
|
||||
@@ -100,10 +63,13 @@ func TestParserInsert(t *testing.T) {
|
||||
false},
|
||||
{"Values / Returning", "INSERT INTO test (a, b) VALUES ('c', 'd') RETURNING *, a, b as B, c",
|
||||
stream.New(rows.Emit(
|
||||
&expr.KVPairs{Pairs: []expr.KVPair{
|
||||
{K: "a", V: testutil.TextValue("c")},
|
||||
{K: "b", V: testutil.TextValue("d")},
|
||||
}},
|
||||
expr.Row{
|
||||
Columns: []string{"a", "b"},
|
||||
Exprs: []expr.Expr{
|
||||
testutil.TextValue("c"),
|
||||
testutil.TextValue("d"),
|
||||
},
|
||||
},
|
||||
)).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(table.Insert("test")).
|
||||
@@ -115,46 +81,62 @@ func TestParserInsert(t *testing.T) {
|
||||
nil, true},
|
||||
{"Values / ON CONFLICT DO NOTHING", "INSERT INTO test (a, b) VALUES ('c', 'd') ON CONFLICT DO NOTHING RETURNING *",
|
||||
stream.New(rows.Emit(
|
||||
&expr.KVPairs{Pairs: []expr.KVPair{
|
||||
{K: "a", V: testutil.TextValue("c")},
|
||||
{K: "b", V: testutil.TextValue("d")},
|
||||
}},
|
||||
expr.Row{
|
||||
Columns: []string{"a", "b"},
|
||||
Exprs: []expr.Expr{
|
||||
testutil.TextValue("c"),
|
||||
testutil.TextValue("d"),
|
||||
},
|
||||
},
|
||||
)).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(stream.OnConflict(nil)).
|
||||
Pipe(table.Insert("test")),
|
||||
Pipe(table.Insert("test")).
|
||||
Pipe(rows.Project(expr.Wildcard{})),
|
||||
false},
|
||||
{"Values / ON CONFLICT IGNORE", "INSERT INTO test (a, b) VALUES ('c', 'd') ON CONFLICT IGNORE RETURNING *",
|
||||
stream.New(rows.Emit(
|
||||
&expr.KVPairs{Pairs: []expr.KVPair{
|
||||
{K: "a", V: testutil.TextValue("c")},
|
||||
{K: "b", V: testutil.TextValue("d")},
|
||||
}},
|
||||
expr.Row{
|
||||
Columns: []string{"a", "b"},
|
||||
Exprs: []expr.Expr{
|
||||
testutil.TextValue("c"),
|
||||
testutil.TextValue("d"),
|
||||
},
|
||||
},
|
||||
)).Pipe(table.Validate("test")).
|
||||
Pipe(stream.OnConflict(nil)).
|
||||
Pipe(table.Insert("test")),
|
||||
Pipe(table.Insert("test")).
|
||||
Pipe(rows.Project(expr.Wildcard{})),
|
||||
false},
|
||||
{"Values / ON CONFLICT DO REPLACE", "INSERT INTO test (a, b) VALUES ('c', 'd') ON CONFLICT DO REPLACE RETURNING *",
|
||||
stream.New(rows.Emit(
|
||||
&expr.KVPairs{Pairs: []expr.KVPair{
|
||||
{K: "a", V: testutil.TextValue("c")},
|
||||
{K: "b", V: testutil.TextValue("d")},
|
||||
}},
|
||||
expr.Row{
|
||||
Columns: []string{"a", "b"},
|
||||
Exprs: []expr.Expr{
|
||||
testutil.TextValue("c"),
|
||||
testutil.TextValue("d"),
|
||||
},
|
||||
},
|
||||
)).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(stream.OnConflict(stream.New(table.Replace("test")))).
|
||||
Pipe(table.Insert("test")),
|
||||
Pipe(table.Insert("test")).
|
||||
Pipe(rows.Project(expr.Wildcard{})),
|
||||
false},
|
||||
{"Values / ON CONFLICT REPLACE", "INSERT INTO test (a, b) VALUES ('c', 'd') ON CONFLICT REPLACE RETURNING *",
|
||||
stream.New(rows.Emit(
|
||||
&expr.KVPairs{Pairs: []expr.KVPair{
|
||||
{K: "a", V: testutil.TextValue("c")},
|
||||
{K: "b", V: testutil.TextValue("d")},
|
||||
}},
|
||||
expr.Row{
|
||||
Columns: []string{"a", "b"},
|
||||
Exprs: []expr.Expr{
|
||||
testutil.TextValue("c"),
|
||||
testutil.TextValue("d"),
|
||||
},
|
||||
},
|
||||
)).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(stream.OnConflict(stream.New(table.Replace("test")))).
|
||||
Pipe(table.Insert("test")),
|
||||
Pipe(table.Insert("test")).
|
||||
Pipe(rows.Project(expr.Wildcard{})),
|
||||
false},
|
||||
{"Values / ON CONFLICT BLA", "INSERT INTO test (a, b) VALUES ('c', 'd') ON CONFLICT BLA RETURNING *",
|
||||
nil, true},
|
||||
@@ -162,6 +144,7 @@ func TestParserInsert(t *testing.T) {
|
||||
nil, true},
|
||||
{"Select / Without fields", "INSERT INTO test SELECT * FROM foo",
|
||||
stream.New(table.Scan("foo")).
|
||||
Pipe(rows.Project(expr.Wildcard{})).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(table.Insert("test")).
|
||||
Pipe(stream.Discard()),
|
||||
@@ -175,6 +158,7 @@ func TestParserInsert(t *testing.T) {
|
||||
false},
|
||||
{"Select / With fields", "INSERT INTO test (a, b) SELECT * FROM foo",
|
||||
stream.New(table.Scan("foo")).
|
||||
Pipe(rows.Project(expr.Wildcard{})).
|
||||
Pipe(path.PathsRename("a", "b")).
|
||||
Pipe(table.Validate("test")).
|
||||
Pipe(table.Insert("test")).
|
||||
@@ -219,7 +203,7 @@ func TestParserInsert(t *testing.T) {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
db := testutil.NewTestDB(t)
|
||||
|
||||
testutil.MustExec(t, db, nil, "CREATE TABLE test; CREATE TABLE foo;")
|
||||
testutil.MustExec(t, db, nil, "CREATE TABLE test(a TEXT, b TEXT); CREATE TABLE foo(c TEXT, d TEXT);")
|
||||
|
||||
q, err := parser.ParseQuery(test.s)
|
||||
if test.fails {
|
||||
|
||||
Reference in New Issue
Block a user