Skip to content

Commit de32170

Browse files
committed
separating tests
1 parent 6b112b0 commit de32170

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

sql/parse_insert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (p *Parser) parseInsert() (*InsertStatement, error) {
7676
} else if tok == VALUES {
7777
p.unscan()
7878
} else {
79-
return nil, fmt.Errorf("found %q, expect ( or VALUES", lit)
79+
return nil, fmt.Errorf("found %q, expected ( or VALUES", lit)
8080
}
8181

8282
if tok, lit := p.scanIgnoreWhitespace(); tok != VALUES {

sql/parse_insert_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package sql_test
2+
3+
import (
4+
"reflect"
5+
"strings"
6+
"testing"
7+
8+
"github.com/AdarshNaidu/simple-sql-parser/sql"
9+
)
10+
11+
// Ensure the parser can parse strings into Statement ASTs.
12+
func TestParser_ParseInsertStatement(t *testing.T) {
13+
var tests = []struct {
14+
s string
15+
stmt *sql.InsertStatement
16+
err string
17+
}{
18+
{
19+
s: `INSERT INTO employee (name, role) VALUES (kattappa, senapati)`,
20+
stmt: &sql.InsertStatement{
21+
Fields: []string{"name", "role"},
22+
Values: []string{"kattappa", "senapati"},
23+
TableName: "employee",
24+
},
25+
},
26+
27+
{
28+
s: `insert into my_table values (hello, world)`,
29+
stmt: &sql.InsertStatement{
30+
Fields: nil,
31+
Values: []string{"hello", "world"},
32+
TableName: "my_table",
33+
},
34+
},
35+
36+
{
37+
s: `insert into my_table () values (hello, world)`,
38+
stmt: &sql.InsertStatement{
39+
Fields: nil,
40+
Values: []string{"hello", "world"},
41+
TableName: "my_table",
42+
},
43+
},
44+
45+
// Errors
46+
{s: `INSERT INTO my_table (one values (hello, world)`, err: `found "values", expected )`},
47+
{s: `INSERT INTO my_table (hello, world)`, err: `found "", expected VALUES`},
48+
{s: `INSERT INTO my_table hello`, err: `found "hello", expected ( or VALUES`},
49+
{s: `INSERT my_table values (hello)`, err: `found "my_table", expected INTO`},
50+
}
51+
52+
for i, tt := range tests {
53+
stmt, err := sql.NewParser(strings.NewReader(tt.s)).Parse()
54+
if !reflect.DeepEqual(tt.err, errstring(err)) {
55+
t.Errorf("%d. %q: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.s, tt.err, err)
56+
} else if tt.err == "" && !reflect.DeepEqual(tt.stmt, stmt) {
57+
t.Errorf("%d. %q\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.s, tt.stmt, stmt)
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)