Fix sql tests

This commit is contained in:
Tobie Morgan Hitchcock 2017-02-23 15:09:02 +00:00
parent 7b78ff5b6e
commit d27e00e737
2 changed files with 73 additions and 23 deletions

View file

@ -37,13 +37,13 @@ type parser struct {
} }
} }
// newParser returns a new instance of Parser.
func newParser(c *fibre.Context, v map[string]interface{}) *parser {
return &parser{c: c, v: v, o: newOptions(c)}
}
// Parse parses sql from a []byte, string, or io.Reader. // Parse parses sql from a []byte, string, or io.Reader.
func Parse(c *fibre.Context, i interface{}, v map[string]interface{}) (*Query, error) { func Parse(c *fibre.Context, i interface{}, v map[string]interface{}) (*Query, error) {
if v == nil {
v = make(map[string]interface{})
}
switch x := i.(type) { switch x := i.(type) {
default: default:
return nil, &EmptyError{} return nil, &EmptyError{}
@ -54,6 +54,12 @@ func Parse(c *fibre.Context, i interface{}, v map[string]interface{}) (*Query, e
case io.Reader: case io.Reader:
return parseBuffer(c, x, v) return parseBuffer(c, x, v)
} }
}
// newParser returns a new instance of Parser.
func newParser(c *fibre.Context, v map[string]interface{}) *parser {
return &parser{c: c, v: v, o: newOptions(c)}
} }
// parseBytes parses a byte array. // parseBytes parses a byte array.

View file

@ -70,15 +70,20 @@ func TestMain(t *testing.T) {
cnf.Settings = &cnf.Options{} cnf.Settings = &cnf.Options{}
cnf.Settings.DB.Base = "*" cnf.Settings.DB.Base = "*"
auth := &cnf.Auth{}
auth.Kind = AuthKV
auth.Possible.NS = "*"
auth.Selected.NS = "*"
auth.Possible.DB = "*"
auth.Selected.DB = "*"
c = fibre.NewContext(nil, nil, nil) c = fibre.NewContext(nil, nil, nil)
c.Set("kind", AuthKV) c.Set("auth", auth)
c.Set("auth", map[string]string{"NS": "*", "DB": "*"})
c.Set("conf", map[string]string{"NS": "*", "DB": "*"})
var tests = []tester{ var tests = []tester{
{ {
sql: `USE`, sql: `USE`,
err: "Found `` but expected `NAMESPACE, DATABASE`", err: "Found `` but expected `NAMESPACE, NS, DATABASE, DB`",
}, },
{ {
sql: `USE NAMESPACE`, sql: `USE NAMESPACE`,
@ -1083,15 +1088,30 @@ func Test_Parse_Queries_Select(t *testing.T) {
}, },
{ {
sql: "SELECT * FROM person WHERE {\"name\":\"\x0A\"} = test", sql: "SELECT * FROM person WHERE {\"name\":\"\x0A\"} = test",
err: "Invalid JSON: {\"name\":\"\n\"}", res: &Query{Statements: []Statement{&SelectStatement{
KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Alias: "*"}},
What: []Expr{&Table{"person"}},
Cond: &BinaryExpression{LHS: map[string]interface{}{"name": "\n"}, Op: EQ, RHS: &Ident{"test"}},
}}},
}, },
{ {
sql: "SELECT * FROM person WHERE test = {\"name\":\"\x0A\"}", sql: "SELECT * FROM person WHERE test = {\"name\":\"\x0A\"}",
err: "Invalid JSON: {\"name\":\"\n\"}", res: &Query{Statements: []Statement{&SelectStatement{
KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Alias: "*"}},
What: []Expr{&Table{"person"}},
Cond: &BinaryExpression{LHS: &Ident{"test"}, Op: EQ, RHS: map[string]interface{}{"name": "\n"}},
}}},
}, },
{ {
sql: "SELECT * FROM person WHERE test = {\"name\":\"\x0D\"}", sql: "SELECT * FROM person WHERE test = {\"name\":\"\x0D\"}",
err: "Invalid JSON: {\"name\":\"\r\"}", res: &Query{Statements: []Statement{&SelectStatement{
KV: "*", NS: "*", DB: "*",
Expr: []*Field{{Expr: &All{}, Alias: "*"}},
What: []Expr{&Table{"person"}},
Cond: &BinaryExpression{LHS: &Ident{"test"}, Op: EQ, RHS: map[string]interface{}{"name": "\r"}},
}}},
}, },
{ {
sql: `SELECT * FROM person WHERE test = {"name":"London"}`, sql: `SELECT * FROM person WHERE test = {"name":"London"}`,
@ -1185,6 +1205,7 @@ func Test_Parse_Queries_Create(t *testing.T) {
What: []Expr{ What: []Expr{
&Table{"person"}, &Table{"person"},
}, },
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1201,6 +1222,7 @@ func Test_Parse_Queries_Create(t *testing.T) {
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Void{}}}, Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Void{}}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1209,6 +1231,7 @@ func Test_Parse_Queries_Create(t *testing.T) {
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Empty{}}}, Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Empty{}}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1221,6 +1244,7 @@ func Test_Parse_Queries_Create(t *testing.T) {
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: "Tobie"}}, Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: "Tobie"}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1241,6 +1265,7 @@ func Test_Parse_Queries_Create(t *testing.T) {
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&MergeExpression{JSON: map[string]interface{}{"firstname": "Tobie"}}}, Data: []Expr{&MergeExpression{JSON: map[string]interface{}{"firstname": "Tobie"}}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1261,6 +1286,7 @@ func Test_Parse_Queries_Create(t *testing.T) {
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&ContentExpression{JSON: map[string]interface{}{"firstname": "Tobie"}}}, Data: []Expr{&ContentExpression{JSON: map[string]interface{}{"firstname": "Tobie"}}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1337,6 +1363,7 @@ func Test_Parse_Queries_Update(t *testing.T) {
What: []Expr{ What: []Expr{
&Table{"person"}, &Table{"person"},
}, },
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1353,6 +1380,7 @@ func Test_Parse_Queries_Update(t *testing.T) {
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Void{}}}, Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Void{}}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1361,6 +1389,7 @@ func Test_Parse_Queries_Update(t *testing.T) {
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Empty{}}}, Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: &Empty{}}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1373,6 +1402,7 @@ func Test_Parse_Queries_Update(t *testing.T) {
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: "Tobie"}}, Data: []Expr{&DataExpression{LHS: &Ident{"firstname"}, Op: EQ, RHS: "Tobie"}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1380,19 +1410,20 @@ func Test_Parse_Queries_Update(t *testing.T) {
err: "Found `something` but expected `json`", err: "Found `something` but expected `json`",
}, },
{ {
sql: `UPDATE person DIFF {"firstname"::"Tobie"}`, sql: `UPDATE person DIFF {} something`,
err: "Found `{\"firstname\"::\"Tobie\"}` but expected `json`", err: "Found `{}` but expected `json`",
}, },
{ {
sql: `UPDATE person DIFF {"firstname":"Tobie"} something`, sql: `UPDATE person DIFF [] something`,
err: "Found `something` but expected `EOF, ), ;`", err: "Found `something` but expected `EOF, ), ;`",
}, },
{ {
sql: `UPDATE person DIFF {"firstname":"Tobie"}`, sql: `UPDATE person DIFF []`,
res: &Query{Statements: []Statement{&UpdateStatement{ res: &Query{Statements: []Statement{&UpdateStatement{
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&DiffExpression{JSON: map[string]interface{}{"firstname": "Tobie"}}}, Data: []Expr{&DiffExpression{JSON: []interface{}{}}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1413,6 +1444,7 @@ func Test_Parse_Queries_Update(t *testing.T) {
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&MergeExpression{JSON: map[string]interface{}{"firstname": "Tobie"}}}, Data: []Expr{&MergeExpression{JSON: map[string]interface{}{"firstname": "Tobie"}}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1433,6 +1465,7 @@ func Test_Parse_Queries_Update(t *testing.T) {
KV: "*", NS: "*", DB: "*", KV: "*", NS: "*", DB: "*",
What: []Expr{&Table{"person"}}, What: []Expr{&Table{"person"}},
Data: []Expr{&ContentExpression{JSON: map[string]interface{}{"firstname": "Tobie"}}}, Data: []Expr{&ContentExpression{JSON: map[string]interface{}{"firstname": "Tobie"}}},
Echo: AFTER,
}}}, }}},
}, },
{ {
@ -1513,6 +1546,7 @@ func Test_Parse_Queries_Delete(t *testing.T) {
What: []Expr{ What: []Expr{
&Table{"person"}, &Table{"person"},
}, },
Echo: NONE,
}}}, }}},
}, },
{ {
@ -1600,7 +1634,7 @@ func Test_Parse_Queries_Define(t *testing.T) {
var tests = []tester{ var tests = []tester{
{ {
sql: `DEFINE`, sql: `DEFINE`,
err: "Found `` but expected `NAMESPACE, DATABASE, SCOPE, TABLE, FIELD, INDEX, VIEW`", err: "Found `` but expected `NAMESPACE, DATABASE, LOGIN, TOKEN, SCOPE, TABLE, FIELD, INDEX, VIEW`",
}, },
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
{ {
@ -1671,15 +1705,19 @@ func Test_Parse_Queries_Define(t *testing.T) {
}, },
{ {
sql: `DEFINE FIELD temp ON person`, sql: `DEFINE FIELD temp ON person`,
err: "Found `` but expected `TYPE`", res: &Query{Statements: []Statement{&DefineFieldStatement{
KV: "*", NS: "*", DB: "*",
Name: "temp",
What: []string{"person"},
}}},
}, },
{ {
sql: `DEFINE FIELD temp ON person TYPE`, sql: `DEFINE FIELD temp ON person TYPE`,
err: "Found `` but expected `any, url, uuid, color, email, phone, array, object, domain, record, string, number, double, custom, boolean, datetime, latitude, longitude`", err: "Found `` but expected `any, url, uuid, color, email, phone, array, object, domain, record, string, number, double, custom, boolean, password, datetime, latitude, longitude`",
}, },
{ {
sql: `DEFINE FIELD temp ON person TYPE something`, sql: `DEFINE FIELD temp ON person TYPE something`,
err: "Found `something` but expected `any, url, uuid, color, email, phone, array, object, domain, record, string, number, double, custom, boolean, datetime, latitude, longitude`", err: "Found `something` but expected `any, url, uuid, color, email, phone, array, object, domain, record, string, number, double, custom, boolean, password, datetime, latitude, longitude`",
}, },
{ {
sql: `DEFINE FIELD temp ON person TYPE any`, sql: `DEFINE FIELD temp ON person TYPE any`,
@ -1783,7 +1821,13 @@ func Test_Parse_Queries_Define(t *testing.T) {
}, },
{ {
sql: `DEFINE FIELD temp ON person TYPE custom ENUM ["default" "notdefault"]`, sql: `DEFINE FIELD temp ON person TYPE custom ENUM ["default" "notdefault"]`,
err: `Invalid JSON: ["default" "notdefault"]`, res: &Query{Statements: []Statement{&DefineFieldStatement{
KV: "*", NS: "*", DB: "*",
Name: "temp",
What: []string{"person"},
Type: "custom",
Enum: []interface{}{"default", "notdefault"},
}}},
}, },
{ {
sql: `DEFINE FIELD temp ON person TYPE any DEFAULT true`, sql: `DEFINE FIELD temp ON person TYPE any DEFAULT true`,
@ -2191,7 +2235,7 @@ func Test_Parse_Queries_Remove(t *testing.T) {
var tests = []tester{ var tests = []tester{
{ {
sql: `REMOVE`, sql: `REMOVE`,
err: "Found `` but expected `NAMESPACE, DATABASE, SCOPE, TABLE, FIELD, INDEX, VIEW`", err: "Found `` but expected `NAMESPACE, DATABASE, LOGIN, TOKEN, SCOPE, TABLE, FIELD, INDEX, VIEW`",
}, },
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
{ {