test.js 5.3 KB
Newer Older
S
StoneT2000 已提交
1
const taos = require('../tdengine');
2
var conn = taos.connect({host:"127.0.0.1", user:"root", password:"taosdata", config:"/etc/taos",port:10});
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var c1 = conn.cursor();
let stime = new Date();
let interval = 1000;

function convertDateToTS(date) {
  let tsArr = date.toISOString().split("T")
  return "\"" + tsArr[0] + " " + tsArr[1].substring(0, tsArr[1].length-1) + "\"";
}
function R(l,r) {
  return Math.random() * (r - l) - r;
}
function randomBool() {
  if (Math.random() <  0.5) {
    return true;
  }
  return false;
}
S
StoneT2000 已提交
20 21

// Initialize
22
//c1.execute('drop database td_connector_test;');
S
StoneT2000 已提交
23
c1.execute('create database if not exists td_connector_test;');
24 25
c1.execute('use td_connector_test;')
c1.execute('create table if not exists all_types (ts timestamp, _int int, _bigint bigint, _float float, _double double, _binary binary(40), _smallint smallint, _tinyint tinyint, _bool bool, _nchar nchar(40));');
26
c1.execute('create table if not exists stabletest (ts timestamp, v1 int, v2 int, v3 int, v4 double) tags (id int, location binary(20));')
S
StoneT2000 已提交
27 28 29 30

// Shell Test : The following uses the cursor to imitate the taos shell

// Insert
L
liu0x54 已提交
31
for (let i = 0; i < 10000; i++) {
32
  let insertData = ["now+" + i + "s", // Timestamp
33 34 35
                    parseInt( R(-Math.pow(2,31) + 1 , Math.pow(2,31) - 1) ), // Int
                    parseInt( R(-Math.pow(2,31) + 1 , Math.pow(2,31) - 1) ), // BigInt
                    parseFloat( R(-3.4E38, 3.4E38) ), // Float
S
StoneT2000 已提交
36
                    parseFloat( R(-1.7E30, 1.7E30) ), // Double
37 38 39 40
                    "\"Long Binary\"", // Binary
                    parseInt( R(-32767, 32767) ), // Small Int
                    parseInt( R(-127, 127) ), // Tiny Int
                    randomBool(),
S
StoneT2000 已提交
41
                    "\"Nchars\""]; // Bool
S
StoneT2000 已提交
42
  c1.execute('insert into td_connector_test.all_types values(' + insertData.join(',') + ' );', {quiet:true});
L
liu0x54 已提交
43
  if (i % 1000 == 0) {
S
StoneT2000 已提交
44 45
    console.log("Insert # " , i);
  }
46 47
}

S
StoneT2000 已提交
48
// Select
49
console.log('select * from td_connector_test.all_types limit 3 offset 100;');
L
liu0x54 已提交
50
c1.execute('select * from td_connector_test.all_types limit 2 offset 100;');
51

52
var d = c1.fetchall();
S
StoneT2000 已提交
53
console.log(c1.fields);
54
console.log(d);
L
liu0x54 已提交
55

S
StoneT2000 已提交
56
// Functions
57
console.log('select count(*), avg(_int), sum(_float), max(_bigint), min(_double) from td_connector_test.all_types;')
58 59
c1.execute('select count(*), avg(_int), sum(_float), max(_bigint), min(_double) from td_connector_test.all_types;');
var d = c1.fetchall();
S
StoneT2000 已提交
60
console.log(c1.fields);
61 62
console.log(d);

63
// Immediate Execution like the Shell
S
StoneT2000 已提交
64

L
liu0x54 已提交
65 66 67
c1.query('select count(*), stddev(_double), min(_tinyint) from all_types where _tinyint > 50 and _int < 0;', true).then(function(result){
  result.pretty();
})
L
liu0x54 已提交
68

S
StoneT2000 已提交
69 70 71
c1.query('select _tinyint, _bool from all_types where _tinyint > 50 and _int < 0 limit 50;', true).then(function(result){
  result.pretty();
})
L
liu0x54 已提交
72

73 74 75 76
c1.query('select stddev(_double), stddev(_bigint), stddev(_float) from all_types;', true).then(function(result){
  result.pretty();
})
c1.query('select stddev(_double), stddev(_bigint), stddev(_float) from all_types interval(1m) limit 100;', true).then(function(result){
S
StoneT2000 已提交
77 78 79
  result.pretty();
})

80
// Binding arguments, and then using promise
81
var q = c1.query('select _nchar from td_connector_test.all_types where ts >= ? and _int > ? limit 100 offset 40;').bind(new Date(1231), 100)
82 83
console.log(q.query);
q.execute().then(function(r) {
S
StoneT2000 已提交
84 85 86
  r.pretty();
});

87 88 89 90 91 92 93 94 95 96
var q = c1.query('select * from td_connector_test.weather');
console.log(q.query);
q.execute().then(function(r) {
	//console.log(r);
  	r.pretty();
});

function sleep(sleepTime) {
    for(var start = +new Date; +new Date - start <= sleepTime; ) { }
}
97

98
sleep(10000);
99 100 101

// Raw Async Testing (Callbacks, not promises)
function cb2(param, result, rowCount, rd) {
L
liu0x54 已提交
102
  console.log('CB2 Callbacked!');  
103
  console.log("RES *", result);
L
liu0x54 已提交
104
  console.log("Async fetched", rowCount, " rows");
105
  console.log("Passed Param: ", param);
L
liu0x54 已提交
106 107
  console.log("Fields ", rd.fields);
  console.log("Data ", rd.data);
108 109
}
function cb1(param,result,code) {
L
liu0x54 已提交
110 111
  console.log('CB1 Callbacked!');
  console.log("RES * ", result);
112
  console.log("Status: ", code);
L
liu0x54 已提交
113 114
  console.log("Passed Param ", param);
  c1.fetchall_a(result, cb2, param);
115 116 117 118 119
}

c1.execute_a("describe td_connector_test.all_types;", cb1, {myparam:3.141});

function cb4(param, result, rowCount, rd) {
L
liu0x54 已提交
120
  console.log('CB4 Callbacked!');  
121 122 123 124 125 126 127 128 129
  console.log("RES *", result);
  console.log("Async fetched", rowCount, "rows");
  console.log("Passed Param: ", param);
  console.log("Fields", rd.fields);
  console.log("Data", rd.data);
}
// Without directly calling fetchall_a
var thisRes;
function cb3(param,result,code) {
L
liu0x54 已提交
130
  console.log('CB3 Callbacked!');
131
  console.log("RES *", result);
L
liu0x54 已提交
132
  console.log("Status:", code);
133 134 135 136 137 138
  console.log("Passed Param", param);
  thisRes = result;
}
//Test calling execute and fetchall seperately and not through callbacks
var param = c1.execute_a("describe td_connector_test.all_types;", cb3, {e:2.718});
console.log("Passed Param outside of callback: ", param);
L
liu0x54 已提交
139
console.log(param);
140 141 142 143
setTimeout(function(){
  c1.fetchall_a(thisRes, cb4, param);
},100);

144

145
// Async through promises
L
liu0x54 已提交
146
var aq = c1.query('select count(*) from td_connector_test.all_types;',false);
147 148
aq.execute_a().then(function(data) {
  data.pretty();
L
liu0x54 已提交
149
});
150 151 152 153 154

c1.query('describe td_connector_test.stabletest').execute_a().then(function(r){
	r.pretty()
});

155 156
setTimeout(function(){
  c1.query('drop database td_connector_test;');
L
liu0x54 已提交
157
},200);
158

L
liu0x54 已提交
159 160
setTimeout(function(){
  conn.close();
161
},2000);