From 9492ed7244bbd2d80c894552634262e95bc8f41b Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Wed, 7 Sep 2016 16:58:50 +0100 Subject: [PATCH] Update SQL tests --- sql/scanner.go | 10 +++--- sql/sql_test.go | 86 ++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 73 insertions(+), 23 deletions(-) diff --git a/sql/scanner.go b/sql/scanner.go index fe5de7b5..4daed2e2 100644 --- a/sql/scanner.go +++ b/sql/scanner.go @@ -43,11 +43,11 @@ func (s *Scanner) Scan() (tok Token, lit string, val interface{}) { ch := s.next() // If we see whitespace then consume all contiguous whitespace. - if isWhitespace(ch) { + if isBlank(ch) { return s.scanBlank(ch) } - // If we see a letter then consume as an string. + // If we see a letter then consume as a string. if isLetter(ch) { return s.scanIdent(ch) } @@ -252,7 +252,7 @@ func (s *Scanner) scanBlank(chp ...rune) (tok Token, lit string, val interface{} for { if ch := s.next(); ch == eof { break - } else if !isWhitespace(ch) { + } else if !isBlank(ch) { s.undo() break } else { @@ -732,8 +732,8 @@ func (s *Scanner) undo() { } -// isWhitespace returns true if the rune is a space, tab, or newline. -func isWhitespace(ch rune) bool { +// isBlank returns true if the rune is a space, tab, or newline. +func isBlank(ch rune) bool { return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' } diff --git a/sql/sql_test.go b/sql/sql_test.go index 60e1e630..1062d38f 100644 --- a/sql/sql_test.go +++ b/sql/sql_test.go @@ -285,7 +285,7 @@ func Test_Parse_Queries_Select(t *testing.T) { }, { sql: `SELECT * FROM`, - err: "Found `` but expected `table name`", + err: "Found `` but expected `table name or record id`", }, { sql: `SELECT * FROM per!son`, @@ -307,20 +307,24 @@ func Test_Parse_Queries_Select(t *testing.T) { }, { sql: `SELECT * FROM @`, - err: "Found `` but expected `table name`", + err: "Found `@` but expected `table name or record id`", + }, + { + sql: `SELECT * FROM @person`, + err: "Found `@person` but expected `table name or record id`", + }, + { + sql: `SELECT * FROM @person:`, + err: "Found `@person:` but expected `table name or record id`", + }, + { + sql: `SELECT * FROM @person WHERE`, + err: "Found `@person` but expected `table name or record id`", }, { sql: `SELECT * FROM person:uuid`, err: "Found `:` but expected `EOF, ;`", }, - { - sql: `SELECT * FROM @person`, - err: "Found `` but expected `:`", - }, - { - sql: `SELECT * FROM @person:`, - err: "Found `` but expected `table id`", - }, { sql: `SELECT * FROM person`, res: &Query{Statements: []Statement{&SelectStatement{ @@ -370,6 +374,13 @@ func Test_Parse_Queries_Select(t *testing.T) { What: []Expr{&Thing{TB: "person", ID: "123.456.789.012"}}, }}}, }, + { + sql: `SELECT * FROM @person:{123.456.789.012}`, + res: &Query{Statements: []Statement{&SelectStatement{ + Expr: []*Field{{Expr: &All{}, Alias: "*"}}, + What: []Expr{&Thing{TB: "person", ID: "123.456.789.012"}}, + }}}, + }, { sql: `SELECT * FROM @person:⟨A250C5A3-948F-4657-88AD-FF5F27B5B24E⟩`, res: &Query{Statements: []Statement{&SelectStatement{ @@ -377,6 +388,13 @@ func Test_Parse_Queries_Select(t *testing.T) { What: []Expr{&Thing{TB: "person", ID: "A250C5A3-948F-4657-88AD-FF5F27B5B24E"}}, }}}, }, + { + sql: `SELECT * FROM @person:{A250C5A3-948F-4657-88AD-FF5F27B5B24E}`, + res: &Query{Statements: []Statement{&SelectStatement{ + Expr: []*Field{{Expr: &All{}, Alias: "*"}}, + What: []Expr{&Thing{TB: "person", ID: "A250C5A3-948F-4657-88AD-FF5F27B5B24E"}}, + }}}, + }, { sql: `SELECT * FROM @person:⟨8250C5A3-948F-4657-88AD-FF5F27B5B24E⟩`, res: &Query{Statements: []Statement{&SelectStatement{ @@ -384,6 +402,13 @@ func Test_Parse_Queries_Select(t *testing.T) { What: []Expr{&Thing{TB: "person", ID: "8250C5A3-948F-4657-88AD-FF5F27B5B24E"}}, }}}, }, + { + sql: `SELECT * FROM @person:{8250C5A3-948F-4657-88AD-FF5F27B5B24E}`, + res: &Query{Statements: []Statement{&SelectStatement{ + Expr: []*Field{{Expr: &All{}, Alias: "*"}}, + What: []Expr{&Thing{TB: "person", ID: "8250C5A3-948F-4657-88AD-FF5F27B5B24E"}}, + }}}, + }, { sql: `SELECT * FROM @person:⟨Tobie Morgan Hitchcock⟩`, res: &Query{Statements: []Statement{&SelectStatement{ @@ -391,6 +416,13 @@ func Test_Parse_Queries_Select(t *testing.T) { What: []Expr{&Thing{TB: "person", ID: "Tobie Morgan Hitchcock"}}, }}}, }, + { + sql: `SELECT * FROM @person:{Tobie Morgan Hitchcock}`, + res: &Query{Statements: []Statement{&SelectStatement{ + Expr: []*Field{{Expr: &All{}, Alias: "*"}}, + What: []Expr{&Thing{TB: "person", ID: "Tobie Morgan Hitchcock"}}, + }}}, + }, { sql: `SELECT * FROM @⟨email addresses⟩:⟨tobie@abcum.com⟩`, res: &Query{Statements: []Statement{&SelectStatement{ @@ -398,6 +430,13 @@ func Test_Parse_Queries_Select(t *testing.T) { What: []Expr{&Thing{TB: "email addresses", ID: "tobie@abcum.com"}}, }}}, }, + { + sql: `SELECT * FROM @{email addresses}:{tobie@abcum.com}`, + res: &Query{Statements: []Statement{&SelectStatement{ + Expr: []*Field{{Expr: &All{}, Alias: "*"}}, + What: []Expr{&Thing{TB: "email addresses", ID: "tobie@abcum.com"}}, + }}}, + }, { sql: `SELECT * FROM @⟨email addresses⟩:⟨tobie+spam@abcum.com⟩`, res: &Query{Statements: []Statement{&SelectStatement{ @@ -405,9 +444,20 @@ func Test_Parse_Queries_Select(t *testing.T) { What: []Expr{&Thing{TB: "email addresses", ID: "tobie+spam@abcum.com"}}, }}}, }, + { + sql: `SELECT * FROM @{email addresses}:{tobie+spam@abcum.com}`, + res: &Query{Statements: []Statement{&SelectStatement{ + Expr: []*Field{{Expr: &All{}, Alias: "*"}}, + What: []Expr{&Thing{TB: "email addresses", ID: "tobie+spam@abcum.com"}}, + }}}, + }, { sql: `SELECT * FROM @⟨email addresses⟩:⟨this\qis\nodd⟩`, - err: "Found `thisqis\nodd` but expected `table id`", + err: "Found `@email addresses:thisqis\nodd` but expected `table name or record id`", + }, + { + sql: `SELECT * FROM @{email addresses}:{this\qis\nodd}`, + err: "Found `@email addresses:thisqis\nodd` but expected `table name or record id`", }, { sql: `SELECT *, temp AS test FROM person`, @@ -679,11 +729,11 @@ func Test_Parse_Queries_Create(t *testing.T) { var tests = []tester{ { sql: `CREATE`, - err: "Found `` but expected `table name`", + err: "Found `` but expected `table name or record id`", }, { sql: `INSERT INTO`, - err: "Found `` but expected `table name`", + err: "Found `` but expected `table name or record id`", }, { sql: `CREATE person`, @@ -827,11 +877,11 @@ func Test_Parse_Queries_Update(t *testing.T) { var tests = []tester{ { sql: `UPDATE`, - err: "Found `` but expected `table name`", + err: "Found `` but expected `table name or record id`", }, { sql: `UPSERT INTO`, - err: "Found `` but expected `table name`", + err: "Found `` but expected `table name or record id`", }, { sql: `UPDATE person`, @@ -975,7 +1025,7 @@ func Test_Parse_Queries_Modify(t *testing.T) { var tests = []tester{ { sql: `MODIFY`, - err: "Found `` but expected `@`", + err: "Found `` but expected `table name or record id`", }, { sql: `MODIFY @person:test`, @@ -1073,11 +1123,11 @@ func Test_Parse_Queries_Delete(t *testing.T) { var tests = []tester{ { sql: `DELETE`, - err: "Found `` but expected `table name`", + err: "Found `` but expected `table name or record id`", }, { sql: `DELETE FROM`, - err: "Found `` but expected `table name`", + err: "Found `` but expected `table name or record id`", }, { sql: `DELETE person`,