@kevin_men/websocket
Version:
TDengine Connector for nodejs and browser using WebSocket.
125 lines (124 loc) • 5.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const wsConnectorPool_1 = require("../../src/client/wsConnectorPool");
const config_1 = require("../../src/common/config");
const wsSql_1 = require("../../src/sql/wsSql");
beforeAll(async () => {
let dns = 'ws://localhost:6041';
let conf = new config_1.WSConfig(dns);
conf.setUser('root');
conf.setPwd('taosdata');
let wsSql = await wsSql_1.WsSql.open(conf);
await wsSql.exec('create database if not exists power KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;');
await wsSql.exec('use power');
await wsSql.exec('CREATE STABLE if not exists power.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 dsn = 'ws://root:taosdata@localhost:6041';
let wsSql = null;
let conf = new config_1.WSConfig(dsn);
conf.setDb('power');
wsSql = await wsSql_1.WsSql.open(conf);
expect(wsSql.state()).toBeGreaterThan(0);
await wsSql.close();
});
test('connect db with error', async () => {
expect.assertions(1);
let wsSql = null;
try {
let dsn = 'ws://root:taosdata@localhost:6041';
let conf = new config_1.WSConfig(dsn);
conf.setDb('jest');
wsSql = await wsSql_1.WsSql.open(conf);
}
catch (e) {
let err = e;
expect(err.message).toMatch('Database not exist');
}
finally {
if (wsSql) {
await wsSql.close();
}
}
});
test('get taosc version', async () => {
let dsn = 'ws://root:taosdata@localhost:6041';
let conf = new config_1.WSConfig(dsn);
let wsSql = await wsSql_1.WsSql.open(conf);
let version = await wsSql.version();
await wsSql.close();
console.log(version);
expect(version).toBeTruthy();
});
test('show databases', async () => {
let dsn = 'ws://root:taosdata@localhost:6041';
let conf = new config_1.WSConfig(dsn);
let wsSql = await wsSql_1.WsSql.open(conf);
let taosResult = await wsSql.exec('show databases');
await wsSql.close();
console.log(taosResult);
expect(taosResult).toBeTruthy();
});
test('create databases', async () => {
let dsn = 'ws://root:taosdata@localhost:6041';
let conf = new config_1.WSConfig(dsn);
let wsSql = await wsSql_1.WsSql.open(conf);
let taosResult = await wsSql.exec('create database if not exists power KEEP 3650 DURATION 10 BUFFER 16 WAL_LEVEL 1;');
await wsSql.close();
console.log(taosResult);
expect(taosResult).toBeTruthy();
});
test('create stable', async () => {
let dsn = 'ws://root:taosdata@localhost:6041';
let conf = new config_1.WSConfig(dsn);
let wsSql = await wsSql_1.WsSql.open(conf);
let taosResult = await wsSql.exec('use power');
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 dsn = 'ws://root:taosdata@localhost:6041';
let conf = new config_1.WSConfig(dsn);
let wsSql = await wsSql_1.WsSql.open(conf);
let taosResult = await wsSql.exec('use power');
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.SanFrancisco", 3) VALUES (NOW, 10.2, 219, 0.32)');
console.log(taosResult);
expect(taosResult.getAffectRows()).toBeGreaterThanOrEqual(1);
await wsSql.close();
});
test('query sql', async () => {
let dsn = 'ws://root:taosdata@localhost:6041';
let conf = new config_1.WSConfig(dsn);
let wsSql = await wsSql_1.WsSql.open(conf);
let taosResult = await wsSql.exec('use power');
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();
});
});
afterAll(async () => {
wsConnectorPool_1.WebSocketConnectionPool.instance().destroyed();
});