forked from open-trade/opentick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
118 lines (112 loc) · 2.5 KB
/
parser_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package opentick
import (
"github.com/alecthomas/repr"
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
var sqlSelectStmt = "select a, adj(b) from test where a > 1.2 and b < 2 limit -2"
var sqlInsertStmt = "INSERT into x(x, y) values(1., ?)"
var sqlInsertAst = `
&opentick.Ast{
Insert: &opentick.AstInsert{
Table: &opentick.AstTableName{
A: &"x",
},
Cols: []string{
"x",
"y",
},
Values: []opentick.AstValue{
opentick.AstValue{
Number: &opentick.AstNumber{
Float: &1,
},
},
opentick.AstValue{
Placeholder: &"?",
},
},
},
}
`
var sqlSelectAst = `
&opentick.Ast{
Select: &opentick.AstSelect{
Selected: &opentick.AstSelectExpression{
Cols: []opentick.AstSelectCol{
opentick.AstSelectCol{
Name: &"a",
},
opentick.AstSelectCol{
Func: &opentick.AstSelectFunc{
Name: &"ADJ",
Col: &"b",
},
},
},
},
Table: &opentick.AstTableName{
A: &"test",
},
Where: &opentick.AstExpression{
And: []opentick.AstCondition{
opentick.AstCondition{
LHS: &"a",
Operator: &">",
RHS: &opentick.AstValue{
Number: &opentick.AstNumber{
Float: &1.2,
},
},
},
opentick.AstCondition{
LHS: &"b",
Operator: &"<",
RHS: &opentick.AstValue{
Number: &opentick.AstNumber{
Int: &2,
},
},
},
},
},
Limit: &-2,
},
}
`
func Test_Parse(t *testing.T) {
stmt, err := Parse(sqlSelectStmt)
assert.Equal(t, nil, err)
assert.Equal(t, strings.TrimSpace(sqlSelectAst), repr.String(stmt, repr.Indent(" "), repr.OmitEmpty(true)))
stmt, err = Parse(sqlInsertStmt)
assert.Equal(t, nil, err)
assert.Equal(t, strings.TrimSpace(sqlInsertAst), repr.String(stmt, repr.Indent(" "), repr.OmitEmpty(true)))
}
func Benchmark_Parse(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := Parse(sqlSelectStmt)
if err != nil {
b.Fatal(err)
}
}
}
func Test_CreateTableSql(t *testing.T) {
sqlCreateTable1 := `
create table test.test(
symbol_id bigint,
interval int,
tm timestamp,
open double,
high double,
low double,
close double,
volume double,
primary key (symbol_id, interval, tm)
)
`
_, err := Parse(sqlCreateTable1)
assert.Equal(t, nil, err)
_, err = Parse("create table test.test(x x)")
assert.NotEqual(t, nil, err)
}