-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsdbbuilder_test.go
241 lines (208 loc) · 4.83 KB
/
tsdbbuilder_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package tsbuilder_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
"github.com/tkcrm/tsbuilder"
"github.com/tkcrm/tsbuilder/tsfuncs"
)
func Test_Create(t *testing.T) {
tests := []struct {
name string
stable string
tags map[string]any
expect string
}{
{
name: "table_name",
stable: "s_table_name",
tags: map[string]any{
"test": tsfuncs.Binary("16"),
"test2": tsfuncs.Binary("24"),
"test3": 3,
},
expect: "CREATE TABLE IF NOT EXISTS table_name USING s_table_name (test, test2, test3) TAGS (BINARY(16), BINARY(24), 3);",
},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
b := tsbuilder.NewCreateTableBuilder().
TableName(tc.name).
STable(tc.stable).
Tags(tc.tags)
sql, err := b.Build()
require.NoError(t, err)
require.Equal(t, tc.expect, sql)
})
}
}
func Test_Insert(t *testing.T) {
b := tsbuilder.NewInsertBuilder()
// add table 1
b.AddTable("test_table_1").
Using("s_table_name").
Columns("column_1", "column_2", "column_3").
Values(1, 2, 3).
Values(1, 2, 3).
Values(1, 2, 3)
// replace table 1
b.AddTable("test_table_1").
Using("s_table_name_2").
Columns("column_1", "column_2", "column_3").
Values(1, 2, tsfuncs.Now()).
Values(1, 2, tsfuncs.Abs("4321")).
Values(1, 2, nil)
// add table 2
b.AddTable("test_table_2").
Using("s_table_name").
Tags(map[string]any{
"tag_1": 1.1,
"tag_2": 2,
"tag_3": 3,
}).
Columns("column_1", "column_2", "column_3").
Values(1, 2, 3).
Values(1, 2, 3).
Values(1, 2, 3)
sql, err := b.Build()
if err != nil {
t.Fatal(err)
}
fmt.Println(sql)
}
func Test_Delete(t *testing.T) {
tests := []struct {
from string
wheres []string
expect string
}{
{
from: "dbName.test_table",
wheres: []string{"asasd > asd", "asdfasdf <= 1212"},
expect: "DELETE FROM dbName.test_table WHERE asasd > asd AND asdfasdf <= 1212;",
},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
b := tsbuilder.NewDeleteBuilder().
From(tc.from).
Where(tc.wheres...)
sql, err := b.Build()
require.NoError(t, err)
require.Equal(t, tc.expect, sql)
})
}
}
func Test_Select(t *testing.T) {
limit := uint32(10)
offset := uint32(0)
tests := []struct {
columns []string
from string
wheres []string
orderBy string
limit uint32
offset uint32
expect string
}{
{
columns: []string{"col_1", "col_2", "col_3"},
from: "dbName.test_table",
wheres: []string{"asasd > asd", "asdfasdf <= 1212"},
orderBy: "ts desc",
limit: 10,
offset: 0,
expect: "SELECT col_1, col_2, col_3 FROM dbName.test_table WHERE asasd > asd AND asdfasdf <= 1212 ORDER BY ts desc LIMIT 10 OFFSET 0;",
},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
b := tsbuilder.NewSelectBuilder().
Columns(tc.columns...).
From(tc.from).
Where(tc.wheres...).
OrderBy(tc.orderBy).
Limit(&limit).
Offset(&offset)
sql, err := b.Build()
require.NoError(t, err)
require.Equal(t, tc.expect, sql)
})
}
}
func Test_Database(t *testing.T) {
tests := []struct {
name string
options []string
expect string
}{
{
name: "db_name",
options: []string{"adsasd 12", "fasasdas true"},
expect: "CREATE DATABASE IF NOT EXISTS db_name adsasd 12 fasasdas true;",
},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
b := tsbuilder.NewDatabaseBuilder().
Name(tc.name).
Options(tc.options...)
sql, err := b.Build()
require.NoError(t, err)
require.Equal(t, tc.expect, sql)
})
}
}
func Test_STable(t *testing.T) {
tests := []struct {
name string
definitions []string
tags map[string]any
options []string
expect string
}{
{
name: "s_table_name",
definitions: []string{"vasdvasdv", "caqwqdw"},
tags: map[string]any{
"tag_1": 1,
"tag_2": 2,
"tag_3": 3,
},
options: []string{"adsasd 12", "fasasdas true"},
expect: "CREATE STABLE IF NOT EXISTS s_table_name (vasdvasdv, caqwqdw) TAGS (tag_1 1, tag_2 2, tag_3 3) adsasd 12 fasasdas true;",
},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
b := tsbuilder.NewSTableBuilder().
Name(tc.name).
Definitions(tc.definitions...).
Tags(tc.tags).
Options(tc.options...)
sql, err := b.Build()
require.NoError(t, err)
require.Equal(t, tc.expect, sql)
})
}
}
func Test_DropTable(t *testing.T) {
tests := []struct {
tables []string
expect string
}{
{
tables: []string{"db_name.test_table", "db_name.test_table_2"},
expect: "DROP TABLE IF EXISTS db_name.test_table, IF EXISTS db_name.test_table_2;",
},
}
for _, tc := range tests {
t.Run("", func(t *testing.T) {
b := tsbuilder.NewDropTableBuilder().
Tables(tc.tables...)
sql, err := b.Build()
require.NoError(t, err)
require.Equal(t, tc.expect, sql)
})
}
}