-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsql.test.ts
More file actions
303 lines (277 loc) · 10.6 KB
/
sql.test.ts
File metadata and controls
303 lines (277 loc) · 10.6 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { WebSocketConnectionPool } from "../../src/client/wsConnectorPool";
import { WSConfig } from "../../src/common/config";
import { WsSql } from "../../src/sql/wsSql";
import { Sleep } from "../utils";
import { setLevel } from "../../src/common/log";
let dns = "ws://localhost:6041";
let password1 = "Ab1!@#$%,.:?<>;~";
let password2 = "Bc%^&*()-_+=[]{}";
setLevel("debug");
beforeAll(async () => {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
let wsSql = await WsSql.open(conf);
await wsSql.exec("drop database if exists sql_test");
await wsSql.exec("drop database if exists sql_create");
await wsSql.exec(`CREATE USER user1 PASS '${password1}'`);
await wsSql.exec(`CREATE USER user2 PASS '${password2}'`);
await wsSql.exec(
"create database if not exists sql_test KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;"
);
await Sleep(100);
await wsSql.exec("use sql_test");
await wsSql.exec(
"CREATE STABLE if not exists meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);"
);
await wsSql.close();
});
describe("TDWebSocket.WsSql()", () => {
jest.setTimeout(20 * 1000);
test("normal connect", async () => {
let wsSql = null;
let conf: WSConfig = new WSConfig("");
conf.setUrl(dns);
conf.setUser("root");
conf.setPwd("taosdata");
conf.setDb("sql_test");
conf.setTimezone("America/New_York");
conf.setTimeOut(6000);
wsSql = await WsSql.open(conf);
expect(wsSql.state()).toBeGreaterThan(0);
let wsRows = await wsSql.query("select timezone()");
while (await wsRows.next()) {
let result = wsRows.getData();
console.log(result);
expect(result).toBeTruthy();
expect(JSON.stringify(result)).toContain("America/New_York");
}
await wsSql.close();
});
test("special characters connect1", async () => {
let wsSql = null;
let conf: WSConfig = new WSConfig(dns);
conf.setUser("user1");
conf.setPwd(password1);
wsSql = await WsSql.open(conf);
expect(wsSql.state()).toBeGreaterThan(0);
let version = await wsSql.version();
expect(version).not.toBeNull();
expect(version).not.toBeUndefined();
await wsSql.close();
});
test("special characters connect2", async () => {
let wsSql = null;
let conf: WSConfig = new WSConfig(dns);
conf.setUser("user2");
conf.setPwd(password2);
wsSql = await WsSql.open(conf);
expect(wsSql.state()).toBeGreaterThan(0);
let version = await wsSql.version();
expect(version).not.toBeNull();
expect(version).not.toBeUndefined();
await wsSql.close();
});
test("connect db with error", async () => {
expect.assertions(1);
let wsSql = null;
try {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
conf.setDb("jest");
wsSql = await WsSql.open(conf);
} catch (e) {
let err: any = e;
expect(err.message).toMatch("Database not exist");
} finally {
if (wsSql) {
await wsSql.close();
}
}
});
test("connect url", async () => {
let url =
"ws://root:taosdata@localhost:6041/information_schema?timezone=Asia/Shanghai";
let conf: WSConfig = new WSConfig(url);
let wsSql = await WsSql.open(conf);
let version = await wsSql.version();
console.log(version);
expect(version).toBeTruthy();
let wsRows = await wsSql.query("select timezone()");
while (await wsRows.next()) {
let result = wsRows.getData();
console.log(result);
expect(result).toBeTruthy();
expect(JSON.stringify(result)).toContain("Asia/Shanghai");
}
await wsSql.close();
});
test("get taosc version", async () => {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
let wsSql = await WsSql.open(conf);
let version = await wsSql.version();
await wsSql.close();
console.log(version);
expect(version).toBeTruthy();
});
test("show databases", async () => {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
let wsSql = await WsSql.open(conf);
let taosResult = await wsSql.exec("show databases");
await wsSql.close();
console.log(taosResult);
expect(taosResult).toBeTruthy();
});
test("create databases", async () => {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
let wsSql = await WsSql.open(conf);
let taosResult = await wsSql.exec(
"create database if not exists sql_create KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;"
);
await wsSql.close();
console.log(taosResult);
expect(taosResult).toBeTruthy();
});
test("create stable", async () => {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
let wsSql = await WsSql.open(conf);
let taosResult = await wsSql.exec("use sql_test");
console.log(taosResult);
expect(taosResult).toBeTruthy();
taosResult = await wsSql.exec(
"CREATE STABLE if not exists meters (ts timestamp, current float, voltage int, phase float) TAGS (location binary(64), groupId int);"
);
await wsSql.close();
console.log(taosResult);
expect(taosResult).toBeTruthy();
});
test("insert recoder", async () => {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
let wsSql = await WsSql.open(conf);
let taosResult = await wsSql.exec("use sql_test");
console.log(taosResult);
expect(taosResult).toBeTruthy();
taosResult = await wsSql.exec("describe meters");
console.log(taosResult);
taosResult = await wsSql.exec(
'INSERT INTO d1001 USING meters (location, groupid) TAGS ("California", 3) VALUES (NOW, 10.2, 219, 0.32)'
);
console.log(taosResult);
expect(taosResult.getAffectRows()).toBeGreaterThanOrEqual(1);
await wsSql.close();
});
test("query sql", async () => {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
let wsSql = await WsSql.open(conf);
let taosResult = await wsSql.exec("use sql_test");
console.log(taosResult);
expect(taosResult).toBeTruthy();
for (let i = 0; i < 10; i++) {
let wsRows = await wsSql.query("select * from meters limit 3");
expect(wsRows).toBeTruthy();
let meta = wsRows.getMeta();
expect(meta).toBeTruthy();
console.log("wsRow:meta:=>", meta);
while (await wsRows.next()) {
let result = await wsRows.getData();
expect(result).toBeTruthy();
}
await wsRows.close();
}
await wsSql.close();
});
test("query sql no getdata", async () => {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
let wsSql = await WsSql.open(conf);
let taosResult = await wsSql.exec("use sql_test");
console.log(taosResult);
expect(taosResult).toBeTruthy();
let wsRows = await wsSql.query("select * from meters");
await wsRows.close();
await wsSql.close();
});
test("timestamp order check", async () => {
const conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
const wsSql = await WsSql.open(conf);
await wsSql.exec("use sql_test");
await wsSql.exec("drop table if exists t_order");
await wsSql.exec("create table t_order (ts timestamp, c1 int)");
await wsSql.exec("insert into t_order values (1726803356466, 1)");
await wsSql.exec("insert into t_order values (1726803357466, 2)");
await wsSql.exec("insert into t_order values (1726803358466, 3)");
const expectRowsAsc = [
[1726803356466n, 1],
[1726803357466n, 2],
[1726803358466n, 3],
];
const expectRowsDesc = expectRowsAsc.slice().reverse();
const actualRowsAsc = [];
const actualRowsDesc = [];
const rowsAsc = await wsSql.query("select * from t_order order by ts asc");
while (await rowsAsc.next()) {
const data = rowsAsc.getData();
if (!data) break;
actualRowsAsc.push(data);
}
await rowsAsc.close();
expect(actualRowsAsc).toEqual(expectRowsAsc);
const rowsDesc = await wsSql.query("select * from t_order order by ts desc");
while (await rowsDesc.next()) {
const data = rowsDesc.getData();
if (!data) break;
actualRowsDesc.push(data);
}
await rowsDesc.close();
expect(actualRowsDesc).toEqual(expectRowsDesc);
await wsSql.close();
});
const maybeConnectorVersionTest = process.env.TEST_3360 ? test.skip : test;
maybeConnectorVersionTest("connector version info", async () => {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
let wsSql = await WsSql.open(conf);
await Sleep(2000);
let wsRows = await wsSql.query("show connections");
let hasNodejsWs = false;
while (await wsRows.next()) {
const data = wsRows.getData();
if (Array.isArray(data) && data.some(v => typeof v === "string" && v.includes("nodejs-ws"))) {
hasNodejsWs = true;
break;
}
}
expect(hasNodejsWs).toBe(true);
await wsRows.close();
await wsSql.close();
});
});
afterAll(async () => {
let conf: WSConfig = new WSConfig(dns);
conf.setUser("root");
conf.setPwd("taosdata");
let wsSql = await WsSql.open(conf);
await wsSql.exec("drop database sql_test");
await wsSql.exec("drop database sql_create");
await wsSql.exec("DROP USER user1;");
await wsSql.exec("DROP USER user2;");
await wsSql.close();
WebSocketConnectionPool.instance().destroyed();
});