forked from open-trade/opentick
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema_test.go
More file actions
175 lines (155 loc) · 5.02 KB
/
Copy pathschema_test.go
File metadata and controls
175 lines (155 loc) · 5.02 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package opentick
import (
"fmt"
"github.com/apple/foundationdb/bindings/go/src/fdb"
"github.com/apple/foundationdb/bindings/go/src/fdb/directory"
"github.com/apple/foundationdb/bindings/go/src/fdb/subspace"
"github.com/apple/foundationdb/bindings/go/src/fdb/tuple"
"github.com/stretchr/testify/assert"
"testing"
)
var d = NewTableColDef("Test", Double)
func Test_EncodeTableColDef(t *testing.T) {
bytes := d.encode()
d2 := TableColDef{}
decodeTableColDef(bytes, &d2, schemaVersion)
assert.Equal(t, d2.Name, d.Name)
assert.Equal(t, d2.Type, d.Type)
}
func Benchmark_DecodeTableColDef(b *testing.B) {
bytes := d.encode()
b.ResetTimer()
for i := 0; i < b.N; i++ { //use b.N for looping
d2 := TableColDef{}
decodeTableColDef(bytes, &d2, schemaVersion)
}
}
var cols = []*TableColDef{NewTableColDef("Test", Double), NewTableColDef("Test", Double), NewTableColDef("Test", Double)}
var tbl = NewTableSchema(cols, []int{2, 1})
func Test_EncodeTableSchema(t *testing.T) {
bytes := tbl.encode()
t2 := decodeTableSchema(bytes)
assert.Equal(t, t2.Cols[2], tbl.Cols[2])
assert.Equal(t, *t2.Keys[1], *tbl.Keys[1])
}
func Benchmark_DecodeTableSchema(b *testing.B) {
bytes := tbl.encode()
b.ResetTimer()
for i := 0; i < b.N; i++ { //use b.N for looping
decodeTableSchema(bytes)
}
}
func Benchmark_SubspacePack(b *testing.B) {
sub := subspace.FromBytes([]byte("test"))
t := tuple.Tuple{"test", "test", "test"}
b.ResetTimer()
for i := 0; i < b.N; i++ { //use b.N for looping
sub.Pack(t)
}
}
func Benchmark_SubspaceUnpack(b *testing.B) {
sub := subspace.FromBytes([]byte("test"))
t := tuple.Tuple{"test", "test", "test"}
p := sub.Pack(t)
b.ResetTimer()
for i := 0; i < b.N; i++ { //use b.N for looping
sub.Unpack(p)
}
}
func Benchmark_TuplePack(b *testing.B) {
t := tuple.Tuple{"test", "test", "test"}
b.ResetTimer()
for i := 0; i < b.N; i++ { //use b.N for looping
t.Pack()
}
}
func Benchmark_TupleUnpack(b *testing.B) {
t := tuple.Tuple{"test", "test", "test"}
p := t.Pack()
b.ResetTimer()
for i := 0; i < b.N; i++ { //use b.N for looping
tuple.Unpack(p)
}
}
func concat(a []byte, b ...byte) []byte {
r := make([]byte, len(a)+len(b))
copy(r, a)
copy(r[len(a):], b)
return r
}
func Benchmark_concat(b *testing.B) {
x := []byte("test")
y := []byte("test")
b.ResetTimer()
for i := 0; i < b.N; i++ { //use b.N for looping
concat(x, y...)
}
}
func Benchmark_append(b *testing.B) {
x := []byte("test")
y := []byte("test")
var z []byte
b.ResetTimer()
for i := 0; i < b.N; i++ { //use b.N for looping
z = append(x, y...)
}
fmt.Print(len(z))
}
func Test_CreateTable(t *testing.T) {
fdb.MustAPIVersion(FdbVersion)
var db = fdb.MustOpenDefault()
sqlCreateTable1 := `
create table test.test(
symbol_id bigint,
tm timestamp,
interval int,
open double,
high double,
low double,
close double,
volume double,
primary key (symbol_id, interval, tm)
)
`
DropDatabase(db, "test")
ast, _ := Parse(sqlCreateTable1)
err := CreateTable(db, "", ast.Create.Table)
assert.NotEqual(t, nil, err)
assert.Equal(t, "Database test does not exist", err.Error())
CreateDatabase(db, "test")
ast1, _ := Parse("create table test.test(x int, y int, x int)")
err = CreateTable(db, "", ast1.Create.Table)
assert.Equal(t, "Multiple definition of identifier x", err.Error())
ast2, _ := Parse("create table test.test(x int, y int, primary key (x, x))")
err = CreateTable(db, "", ast2.Create.Table)
assert.Equal(t, "Duplicate definition x referenced in PRIMARY KEY", err.Error())
ast3, _ := Parse("create table test.test(x int, y int)")
err = CreateTable(db, "", ast3.Create.Table)
assert.Equal(t, "PRIMARY KEY not declared", err.Error())
ast4, _ := Parse("create table test.test(x int, y int, primary key(x, z))")
err = CreateTable(db, "", ast4.Create.Table)
assert.Equal(t, "Unknown definition z referenced in PRIMARY KEY", err.Error())
err = CreateTable(db, "", ast.Create.Table)
assert.Equal(t, nil, err)
tbl, err1 := GetTableSchema(db, "test", "test")
assert.Equal(t, nil, err1)
assert.Equal(t, "volume", tbl.Cols[7].Name)
assert.Equal(t, "interval", tbl.Keys[1].Name)
assert.Equal(t, uint32(0), tbl.NameMap["symbol_id"].PosCol)
assert.Equal(t, uint32(1), tbl.NameMap["tm"].PosCol)
assert.Equal(t, uint32(2), tbl.NameMap["tm"].Pos)
assert.Equal(t, uint32(6), tbl.NameMap["close"].PosCol)
assert.Equal(t, uint32(3), tbl.NameMap["close"].Pos)
dir, _ := directory.Open(db, []string{"db", "test", "test"}, nil)
dir2, _ := directory.Open(db, []string{"db", "test", "test", "scheme"}, nil)
assert.Equal(t, len(dir.Bytes()), len(dir2.Bytes()))
assert.Equal(t, string(tbl.Dir.Bytes()), string(dir.Bytes()))
_, err = Execute(db, "", "alter table test.test rename column tm to time", nil)
assert.Equal(t, nil, err)
_, err = Execute(db, "", "alter table test.test rename column tm to time", nil)
assert.Equal(t, "Column tm does not exist", err.Error())
_, err = Execute(db, "", "alter table test.test rename column time to tm", nil)
assert.Equal(t, nil, err)
_, err = Execute(db, "", "drop table test.test", nil)
assert.Equal(t, nil, err)
}