-
Notifications
You must be signed in to change notification settings - Fork 12
/
table_test.go
188 lines (159 loc) · 5.75 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
package datatable_test
import (
"fmt"
"testing"
"github.com/datasweet/datatable"
"github.com/stretchr/testify/assert"
)
func TestNewTable(t *testing.T) {
tb := datatable.New("test")
assert.Equal(t, 0, tb.NumCols())
assert.NoError(t, tb.AddColumn("sessions", datatable.Int, datatable.Values(120)))
assert.NoError(t, tb.AddColumn("bounces", datatable.Int))
assert.NoError(t, tb.AddColumn("bounceRate", datatable.Float64))
assert.Error(t, tb.AddColumn("bounces", datatable.Int, datatable.Values(11)))
assert.Error(t, tb.AddColumn(" ", datatable.Int, datatable.Values(11)))
assert.Error(t, tb.AddColumn("nil", datatable.ColumnType("unknown")))
assert.NoError(t, tb.AddColumn("hidden", datatable.Int, datatable.Values(34), datatable.ColumnHidden(true)))
assert.Equal(t, []string{"sessions", "bounces", "bounceRate"}, tb.Columns())
assert.Equal(t, 1, tb.NumRows())
assert.NoError(t, tb.AddColumn("pageViews", datatable.Int, datatable.Values(1, 2, 3, 4, 5)))
assert.Equal(t, 4, tb.NumCols())
assert.Equal(t, 5, tb.NumRows())
fmt.Println(tb)
checkTable(t, tb,
"sessions", "bounces", "bounceRate", "pageViews",
120, nil, nil, 1,
nil, nil, nil, 2,
nil, nil, nil, 3,
nil, nil, nil, 4,
nil, nil, nil, 5,
)
}
func TestNewRow(t *testing.T) {
tb := datatable.New("test")
assert.NoError(t, tb.AddColumn("champ", datatable.String))
assert.Equal(t, 1, tb.NumCols())
assert.Equal(t, 0, tb.NumRows())
r := make(datatable.Row)
r["champ"] = "Malzahar"
tb.Append(r)
assert.Equal(t, 1, tb.NumRows())
tb.Append(nil)
assert.Equal(t, 1, tb.NumRows())
tb.Append()
assert.Equal(t, 1, tb.NumRows())
tb.Append(
tb.NewRow().Set("champ", "Xerath"),
tb.NewRow().Set("satan", "Teemo"), // wrong column => not set
tb.NewRow().Set("champ", "Ahri"),
)
checkTable(t, tb,
"champ",
"Malzahar",
"Xerath",
nil,
"Ahri",
)
tb.AddColumn("win", datatable.Int)
checkTable(t, tb,
"champ", "win",
"Malzahar", nil,
"Xerath", nil,
nil, nil,
"Ahri", nil,
)
tb.AddColumn("loose", datatable.Int, datatable.Values(3, 4, nil))
checkTable(t, tb,
"champ", "win", "loose",
"Malzahar", nil, 3,
"Xerath", nil, 4,
nil, nil, nil,
"Ahri", nil, nil,
)
}
func TestExprColumn(t *testing.T) {
tb := datatable.New("test")
tb.AddColumn("champ", datatable.String, datatable.Values("Malzahar", "Xerath", "Teemo"))
tb.AddColumn("champion", datatable.String, datatable.Expr("upper(`champ`)"))
tb.AddColumn("win", datatable.Int, datatable.Values(10, 20, 666))
tb.AddColumn("loose", datatable.Int, datatable.Values(6, 5, 666))
tb.AddColumn("winRate", datatable.String, datatable.Expr("(`win` * 100 / (`win` + `loose`)) ~ \" %\""))
tb.AddColumn("sum", datatable.Int, datatable.Expr("sum(`win`)"))
tb.AddColumn("ok", datatable.Bool, datatable.Expr("true"))
checkTable(t, tb,
"champ", "champion", "win", "loose", "winRate", "sum", "ok",
"Malzahar", "MALZAHAR", 10, 6, "62.5 %", 696, true,
"Xerath", "XERATH", 20, 5, "80 %", 696, true,
"Teemo", "TEEMO", 666, 666, "50 %", 696, true,
)
}
func TestAppendRow(t *testing.T) {
tb := datatable.New("test")
assert.NoError(t, tb.AddColumn("champ", datatable.String))
assert.NoError(t, tb.AddColumn("win", datatable.Int))
assert.NoError(t, tb.AddColumn("loose", datatable.Int))
assert.NoError(t, tb.AddColumn("winRate", datatable.Float64, datatable.Expr("(`win` * 100 / (`win` + `loose`))")))
assert.Error(t, tb.AddColumn("winRate", datatable.String, datatable.Expr("test")))
assert.NoError(t, tb.AppendRow("Xerath", 25, 15, "expr"))
assert.NoError(t, tb.AppendRow("Malzahar", 16, 16, nil))
assert.NoError(t, tb.AppendRow("Vel'Koz", 7, 5, 3))
checkTable(t, tb,
"champ", "win", "loose", "winRate",
"Xerath", 25, 15, 62.5,
"Malzahar", 16, 16, 50.0,
"Vel'Koz", 7, 5, 58.333333333333336,
)
}
func TestRows(t *testing.T) {
tb := datatable.New("test")
assert.NoError(t, tb.AddColumn("champ", datatable.String))
assert.NoError(t, tb.AddColumn("win", datatable.Int))
assert.NoError(t, tb.AddColumn("loose", datatable.Int, datatable.ColumnHidden(true)))
assert.NoError(t, tb.AddColumn("winRate", datatable.Float64, datatable.Expr("(`win` * 100 / (`win` + `loose`))")))
assert.Error(t, tb.AddColumn("winRate", datatable.String, datatable.Expr("test")))
assert.NoError(t, tb.AppendRow("Xerath", 25, 15, "expr"))
assert.NoError(t, tb.AppendRow("Malzahar", 16, 16, nil))
assert.NoError(t, tb.AppendRow("Vel'Koz", 7, 5, 3))
checkTable(t, tb,
"champ", "win", "winRate",
"Xerath", 25, 62.5,
"Malzahar", 16, 50.0,
"Vel'Koz", 7, 58.333333333333336,
)
for _, r := range tb.Rows() {
assert.Len(t, r, 3)
}
for _, r := range tb.Rows(datatable.ExportHidden(true)) {
assert.Len(t, r, 4)
}
}
func TestRow(t *testing.T) {
tb := datatable.New("test")
assert.NoError(t, tb.AddColumn("champ", datatable.String))
assert.NoError(t, tb.AddColumn("win", datatable.Int))
assert.NoError(t, tb.AddColumn("loose", datatable.Int, datatable.ColumnHidden(true)))
assert.NoError(t, tb.AddColumn("winRate", datatable.Float64, datatable.Expr("(`win` * 100 / (`win` + `loose`))")))
assert.Error(t, tb.AddColumn("winRate", datatable.String, datatable.Expr("test")))
assert.NoError(t, tb.AppendRow("Xerath", 25, 15, "expr"))
assert.NoError(t, tb.AppendRow("Malzahar", 16, 16, nil))
assert.NoError(t, tb.AppendRow("Vel'Koz", 7, 5, 3))
checkTable(t, tb,
"champ", "win", "winRate",
"Xerath", 25, 62.5,
"Malzahar", 16, 50.0,
"Vel'Koz", 7, 58.333333333333336,
)
r := tb.Row(0)
assert.Len(t, r, 3)
assert.Equal(t, r.Get("champ"), "Xerath")
assert.Equal(t, r.Get("win"), 25)
assert.Equal(t, r.Get("winRate"), 62.5)
assert.Nil(t, r.Get("loose"))
r = tb.Row(0, datatable.ExportHidden(true))
assert.Len(t, r, 4)
assert.Equal(t, r.Get("champ"), "Xerath")
assert.Equal(t, r.Get("win"), 25)
assert.Equal(t, r.Get("winRate"), 62.5)
assert.Equal(t, r.Get("loose"), 15)
}