/
ast_test.go
104 lines (100 loc) · 2.42 KB
/
ast_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package gosql
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStatement_GenerateCode(t *testing.T) {
tests := []struct {
result string
stmt Statement
}{
{
`DROP TABLE "foo";`,
Statement{
DropTableStatement: &DropTableStatement{
Name: Token{Value: "foo"},
},
Kind: DropTableKind,
},
},
{
`CREATE TABLE "users" (
"id" INT PRIMARY KEY,
"name" TEXT
);`,
Statement{
CreateTableStatement: &CreateTableStatement{
Name: Token{Value: "users"},
Cols: &[]*ColumnDefinition{
{
Name: Token{Value: "id"},
Datatype: Token{Value: "int"},
PrimaryKey: true,
},
{
Name: Token{Value: "name"},
Datatype: Token{Value: "text"},
},
},
},
Kind: CreateTableKind,
},
},
{
`CREATE UNIQUE INDEX "age_idx" ON "users" ("age");`,
Statement{
CreateIndexStatement: &CreateIndexStatement{
Name: Token{Value: "age_idx"},
Unique: true,
Table: Token{Value: "users"},
Exp: Expression{Literal: &Token{Value: "age", Kind: IdentifierKind}, Kind: LiteralKind},
},
Kind: CreateIndexKind,
},
},
{
`INSERT INTO "foo" VALUES (1, 'flubberty', true);`,
Statement{
InsertStatement: &InsertStatement{
Table: Token{Value: "foo"},
Values: &[]*Expression{
{Literal: &Token{Value: "1", Kind: NumericKind}, Kind: LiteralKind},
{Literal: &Token{Value: "flubberty", Kind: StringKind}, Kind: LiteralKind},
{Literal: &Token{Value: "true", Kind: BoolKind}, Kind: LiteralKind},
},
},
Kind: InsertKind,
},
},
{
`SELECT
"id",
"name"
FROM
"users"
WHERE
("id" = 2);`,
Statement{
SelectStatement: &SelectStatement{
Item: &[]*SelectItem{
{Exp: &Expression{Literal: &Token{Value: "id", Kind: IdentifierKind}, Kind: LiteralKind}},
{Exp: &Expression{Literal: &Token{Value: "name", Kind: IdentifierKind}, Kind: LiteralKind}},
},
From: &Token{Value: "users"},
Where: &Expression{
Binary: &BinaryExpression{
A: Expression{Literal: &Token{Value: "id", Kind: IdentifierKind}, Kind: LiteralKind},
B: Expression{Literal: &Token{Value: "2", Kind: NumericKind}, Kind: LiteralKind},
Op: Token{Value: "=", Kind: SymbolKind},
},
Kind: BinaryKind,
},
},
Kind: SelectKind,
},
},
}
for _, test := range tests {
assert.Equal(t, test.result, test.stmt.GenerateCode())
}
}