-
Notifications
You must be signed in to change notification settings - Fork 14
/
table_test.go
125 lines (101 loc) · 3.06 KB
/
table_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
119
120
121
122
123
124
125
package goql
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type nilProvider struct {
}
func (nilProvider) Provide(in interface{}) []interface{} {
return nil
}
func TestTables(t *testing.T) {
quit := make(chan struct{}, 1)
RegisterTable("test1", provider{})
RegisterField("test1", "c1", c1{})
RegisterField("test1", "c2", c2{})
RegisterField("test1", "c3", c3{})
RegisterField("test1", "c4", c4{})
tt, err := getTable("not-exists")
assert.Error(t, err)
assert.Nil(t, tt)
tbl, err := getTable("test1")
assert.NoError(t, err)
assert.Equal(t, 4, len(tbl))
assert.Equal(t, 0, tbl["c1"].Order())
assert.Equal(t, 1, tbl["c2"].Order())
assert.Equal(t, 2, tbl["c3"].Order())
assert.Equal(t, 3, tbl["c4"].Order())
assert.Equal(t, ValueTypeNumber, tbl["c1"].Type())
assert.Equal(t, ValueTypeString, tbl["c2"].Type())
assert.Equal(t, ValueTypeBool, tbl["c3"].Type())
assert.Equal(t, ValueTypeDefinition, tbl["c4"].Type())
res := make(chan []Getter, 3)
err = getTableFields(tablet(1), "test1", res, quit, "c1", "c2", "c3", "c4")
assert.NoError(t, err)
var cnt int64
for i := range res {
assert.Equal(t, 4, len(i))
assert.Equal(t, float64(cnt*2), i[0].(Number).Number)
assert.Equal(t, fmt.Sprintf("%dth row", cnt), i[1].(String).String)
assert.Equal(t, cnt%2 == 0, i[2].(Bool).Bool)
if cnt%2 == 0 {
assert.Equal(t, "int", i[3].(Definition).Definition.String())
} else {
assert.Equal(t, "string", i[3].(Definition).Definition.String())
}
cnt++
}
res = make(chan []Getter, 3)
err = getTableFields(tablet(1), "test1", res, quit, "c2", "c3")
assert.NoError(t, err)
cnt = 0
for i := range res {
assert.Equal(t, 2, len(i))
assert.Equal(t, fmt.Sprintf("%dth row", cnt), i[0].(String).String)
assert.Equal(t, cnt%2 == 0, i[1].(Bool).Bool)
cnt++
}
res = make(chan []Getter, 3)
err = getTableFields(tablet(1), "test1", res, quit, "c2", "", "c3")
assert.NoError(t, err)
cnt = 0
for i := range res {
assert.Equal(t, 3, len(i))
assert.Equal(t, fmt.Sprintf("%dth row", cnt), i[0].(String).String)
assert.Equal(t, cnt%2 == 0, i[2].(Bool).Bool)
assert.Equal(t, nil, i[1])
cnt++
}
assert.Panics(t, func() { RegisterTable("test1", nilProvider{}) })
assert.Panics(t, func() { RegisterField("not-exist", "test", c1{}) })
assert.Panics(t, func() { RegisterField("test1", "c1", c1{}) })
assert.Panics(t, func() { RegisterField("test1", "c11", 10) })
assert.Error(t, getTableFields(1, "not-exist", res, quit, "col"))
assert.Error(t, getTableFields(1, "test1", res, quit, "col"))
assert.Error(t, getTableFields(1, "test1", res, quit))
}
func TestTypes(t *testing.T) {
b := Bool{}
assert.Equal(t, false, b.Get())
b.Bool = true
assert.Equal(t, true, b.Get())
b.Null = true
assert.Nil(t, b.Get())
n := Number{}
assert.Equal(t, 0.0, n.Get())
n.Number = 10.0
assert.Equal(t, 10.0, n.Get())
n.Null = true
assert.Nil(t, n.Get())
s := String{}
assert.Equal(t, "", s.Get())
s.String = "test"
assert.Equal(t, "test", s.Get())
s.Null = true
assert.Nil(t, s.Get())
cd := columnDef{
typ: 10,
}
assert.Panics(t, func() { cd.Type() })
}