insertJSONPayload.py 15.0 KB
Newer Older
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
###################################################################
#           Copyright (c) 2021 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import sys
from util.log import *
from util.cases import *
from util.sql import *


class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor(), logSql)
        self._conn = conn

    def run(self):
        print("running {}".format(__file__))
        tdSql.execute("drop database if exists test")
        tdSql.execute("create database if not exists test precision 'us'")
        tdSql.execute('use test')


        ### Default format ###
34 35
        ### metric ###
        print("============= step0 : test metric  ================")
36
        payload = ['''
37 38
        {
	    "metric":	".stb.0.",
39
	    "timestamp":	1626006833610,
40 41 42 43 44 45 46 47
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
48
        ''']
49
        code = self._conn.schemaless_insert(payload, 2, None)
50
        print("schemaless_insert result {}".format(code))
51

52
        tdSql.query("describe `.stb.0.`")
53 54
        tdSql.checkRows(6)

55 56
        ### metric value ###
        print("============= step1 : test metric value types  ================")
57
        payload = ['''
58 59
        {
	    "metric":	"stb0_0",
60
	    "timestamp":	1626006833610,
61 62 63 64 65 66 67 68
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
69
        ''']
70
        code = self._conn.schemaless_insert(payload, 2, None)
71
        print("schemaless_insert result {}".format(code))
72 73

        tdSql.query("describe stb0_0")
74
        tdSql.checkData(1, 1, "DOUBLE")
75

76
        payload = ['''
77 78
        {
	    "metric":	"stb0_1",
79
	    "timestamp":	1626006833610,
80 81 82 83 84 85 86 87
	    "value":	true,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
88
        ''']
89
        code = self._conn.schemaless_insert(payload, 2, None)
90
        print("schemaless_insert result {}".format(code))
91 92 93 94

        tdSql.query("describe stb0_1")
        tdSql.checkData(1, 1, "BOOL")

95
        payload = ['''
96 97
        {
	    "metric":	"stb0_2",
98
	    "timestamp":	1626006833610,
99 100 101 102 103 104 105 106
	    "value":	false,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
107
        ''']
108
        code = self._conn.schemaless_insert(payload, 2, None)
109
        print("schemaless_insert result {}".format(code))
110 111 112 113

        tdSql.query("describe stb0_2")
        tdSql.checkData(1, 1, "BOOL")

114
        payload = ['''
115 116
        {
	    "metric":	"stb0_3",
117
	    "timestamp":	1626006833610,
118 119 120 121 122 123 124 125
	    "value":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>",
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
126
        ''']
127
        code = self._conn.schemaless_insert(payload, 2, None)
128
        print("schemaless_insert result {}".format(code))
129 130

        tdSql.query("describe stb0_3")
131
        tdSql.checkData(1, 1, "BINARY")
132

133
        payload = ['''
134 135
        {
	    "metric":	"stb0_4",
136
	    "timestamp":	1626006833610,
137 138 139 140 141 142 143 144
	    "value":	3.14,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
145
        ''']
146
        code = self._conn.schemaless_insert(payload, 2, None)
147
        print("schemaless_insert result {}".format(code))
148 149 150 151

        tdSql.query("describe stb0_4")
        tdSql.checkData(1, 1, "DOUBLE")

152
        payload = ['''
153 154
        {
	    "metric":	"stb0_5",
155
	    "timestamp":	1626006833610,
156 157 158 159 160 161 162 163
	    "value":	3.14E-2,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
164
        ''']
165
        code = self._conn.schemaless_insert(payload, 2, None)
166
        print("schemaless_insert result {}".format(code))
167 168 169 170 171 172 173

        tdSql.query("describe stb0_5")
        tdSql.checkData(1, 1, "DOUBLE")


        print("============= step2 : test timestamp  ================")
        ### timestamp 0 ###
174
        payload = ['''
175 176
        {
	    "metric":	"stb0_6",
177 178 179 180 181 182 183 184 185
	    "timestamp":	0,
	    "value":	123,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
186
        ''']
187
        code = self._conn.schemaless_insert(payload, 2, None)
188
        print("schemaless_insert result {}".format(code))
189

190 191 192 193 194 195 196 197 198 199 200 201 202 203
        ### timestamp 10 digits second ###
        payload = ['''
        {
	    "metric":	"stb0_7",
	    "timestamp":	1626006833,
	    "value":	123,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
        ''']
204
        code = self._conn.schemaless_insert(payload, 2, None)
205
        print("schemaless_insert result {}".format(code))
206

207 208
        print("============= step3 : test tags  ================")
        ### Default tag numeric types ###
209
        payload = ['''
210 211 212 213 214 215 216 217
        {
	    "metric":	"stb0_8",
	    "timestamp":	0,
	    "value":	123,
	    "tags":	{
		"t1":	123
	    }
        }
218
        ''']
219
        code = self._conn.schemaless_insert(payload, 2, None)
220
        print("schemaless_insert result {}".format(code))
221 222

        tdSql.query("describe stb0_8")
223
        tdSql.checkData(2, 1, "DOUBLE")
224

225
        payload = ['''
226 227 228 229 230 231 232 233
        {
	    "metric":	"stb0_9",
	    "timestamp":	0,
	    "value":	123,
	    "tags":	{
		"t1":	123.00
	    }
        }
234
        ''']
235
        code = self._conn.schemaless_insert(payload, 2, None)
236
        print("schemaless_insert result {}".format(code))
237 238 239 240

        tdSql.query("describe stb0_9")
        tdSql.checkData(2, 1, "DOUBLE")

241
        payload = ['''
242 243 244 245 246 247 248 249
        {
	    "metric":	"stb0_10",
	    "timestamp":	0,
	    "value":	123,
	    "tags":	{
		"t1":	123E-1
	    }
        }
250
        ''']
251
        code = self._conn.schemaless_insert(payload, 2, None)
252
        print("schemaless_insert result {}".format(code))
253 254 255

        tdSql.query("describe stb0_10")
        tdSql.checkData(2, 1, "DOUBLE")
256 257

        ### Nested format ###
258
        print("============= step4 : test nested format  ================")
259 260
        ### timestamp ###
        #seconds
261
        payload = ['''
262 263 264 265 266 267 268 269 270 271 272 273 274 275
        {
	    "metric":	"stb1_0",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
276
        ''']
277
        code = self._conn.schemaless_insert(payload, 2, None)
278
        print("schemaless_insert result {}".format(code))
279 280 281 282 283

        tdSql.query("select ts from stb1_0")
        tdSql.checkData(0, 0, "2021-07-11 20:33:53.000000")

        #milliseconds
284
        payload = ['''
285 286 287 288 289 290 291 292 293 294 295 296 297 298
        {
	    "metric":	"stb1_1",
	    "timestamp":	{
		"value":	1626006833610,
		"type":	"ms"
	    },
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
299
        ''']
300
        code = self._conn.schemaless_insert(payload, 2, None)
301
        print("schemaless_insert result {}".format(code))
302 303 304 305 306

        tdSql.query("select ts from stb1_1")
        tdSql.checkData(0, 0, "2021-07-11 20:33:53.610000")

        #microseconds
307
        payload = ['''
308 309 310 311 312 313 314 315 316 317 318 319 320 321
        {
	    "metric":	"stb1_2",
	    "timestamp":	{
		"value":	1626006833610123,
		"type":	"us"
	    },
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
322
        ''']
323
        code = self._conn.schemaless_insert(payload, 2, None)
324
        print("schemaless_insert result {}".format(code))
325 326 327 328 329

        tdSql.query("select ts from stb1_2")
        tdSql.checkData(0, 0, "2021-07-11 20:33:53.610123")

        #nanoseconds
330
        payload = ['''
331 332 333
        {
	    "metric":	"stb1_3",
	    "timestamp":	{
334
                "value":	1626006833610123321,
335 336 337 338 339 340 341 342 343 344
		"type":	"ns"
	    },
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
345
        ''']
346
        code = self._conn.schemaless_insert(payload, 2, None)
347
        print("schemaless_insert result {}".format(code))
348 349 350 351 352 353

        tdSql.query("select ts from stb1_3")
        tdSql.checkData(0, 0, "2021-07-11 20:33:53.610123")

        #now
        tdSql.execute('use test')
354
        payload = ['''
355 356 357 358 359 360 361 362 363 364 365 366 367 368
        {
	    "metric":	"stb1_4",
	    "timestamp":	{
		"value":	0,
		"type":	"ns"
	    },
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
369
        ''']
370
        code = self._conn.schemaless_insert(payload, 2, None)
371
        print("schemaless_insert result {}".format(code))
372 373

        ### metric value ###
374
        payload = ['''
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
        {
	    "metric":	"stb2_0",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	true,
		"type":	"bool"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
392
        ''']
393
        code = self._conn.schemaless_insert(payload, 2, None)
394
        print("schemaless_insert result {}".format(code))
395 396 397 398

        tdSql.query("describe stb2_0")
        tdSql.checkData(1, 1, "BOOL")

399
        payload = ['''
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
        {
	    "metric":	"stb2_1",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	127,
		"type":	"tinyint"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
417
        ''']
418
        code = self._conn.schemaless_insert(payload, 2, None)
419
        print("schemaless_insert result {}".format(code))
420 421 422 423

        tdSql.query("describe stb2_1")
        tdSql.checkData(1, 1, "TINYINT")

424
        payload = ['''
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
        {
	    "metric":	"stb2_2",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	32767,
		"type":	"smallint"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
442
        ''']
443
        code = self._conn.schemaless_insert(payload, 2, None)
444
        print("schemaless_insert result {}".format(code))
445 446 447 448

        tdSql.query("describe stb2_2")
        tdSql.checkData(1, 1, "SMALLINT")

449
        payload = ['''
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
        {
	    "metric":	"stb2_3",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	2147483647,
		"type":	"int"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
467
        ''']
468
        code = self._conn.schemaless_insert(payload, 2, None)
469
        print("schemaless_insert result {}".format(code))
470 471 472 473

        tdSql.query("describe stb2_3")
        tdSql.checkData(1, 1, "INT")

474
        payload = ['''
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
        {
	    "metric":	"stb2_4",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	9.2233720368547758e+18,
		"type":	"bigint"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
492
        ''']
493
        code = self._conn.schemaless_insert(payload, 2, None)
494
        print("schemaless_insert result {}".format(code))
495 496 497 498

        tdSql.query("describe stb2_4")
        tdSql.checkData(1, 1, "BIGINT")

499
        payload = ['''
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
        {
	    "metric":	"stb2_5",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	11.12345,
		"type":	"float"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
517
        ''']
518
        code = self._conn.schemaless_insert(payload, 2, None)
519
        print("schemaless_insert result {}".format(code))
520 521 522 523

        tdSql.query("describe stb2_5")
        tdSql.checkData(1, 1, "FLOAT")

524
        payload = ['''
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
        {
	    "metric":	"stb2_6",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	22.123456789,
		"type":	"double"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
542
        ''']
543
        code = self._conn.schemaless_insert(payload, 2, None)
544
        print("schemaless_insert result {}".format(code))
545 546 547 548

        tdSql.query("describe stb2_6")
        tdSql.checkData(1, 1, "DOUBLE")

549
        payload = ['''
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
        {
	    "metric":	"stb2_7",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>",
		"type":	"binary"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
567
        ''']
568
        code = self._conn.schemaless_insert(payload, 2, None)
569
        print("schemaless_insert result {}".format(code))
570 571 572 573

        tdSql.query("describe stb2_7")
        tdSql.checkData(1, 1, "BINARY")

574
        payload = ['''
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
        {
	    "metric":	"stb2_8",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	"你好",
		"type":	"nchar"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
592
        ''']
593
        code = self._conn.schemaless_insert(payload, 2, None)
594
        print("schemaless_insert result {}".format(code))
595 596 597 598 599 600

        tdSql.query("describe stb2_8")
        tdSql.checkData(1, 1, "NCHAR")

        ### tag value ###

601
        payload = ['''
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
        {
	    "metric":	"stb3_0",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	"hello",
		"type":	"nchar"
	    },
	    "tags":	{
		"t1":	{
			"value":	true,
			"type":	"bool"
		},
		"t2":	{
			"value":	127,
			"type":	"tinyint"
		},
		"t3":	{
			"value":	32767,
			"type":	"smallint"
		},
		"t4":	{
			"value":	2147483647,
			"type":	"int"
		},
		"t5":	{
			"value":	9.2233720368547758e+18,
			"type":	"bigint"
		},
		"t6":	{
			"value":	11.12345,
			"type":	"float"
		},
		"t7":	{
			"value":	22.123456789,
			"type":	"double"
		},
		"t8":	{
			"value":	"binary_val",
			"type":	"binary"
		},
		"t9":	{
			"value":	"你好",
			"type":	"nchar"
		}
	    }
        }
651
        ''']
652
        code = self._conn.schemaless_insert(payload, 2, None)
653
        print("schemaless_insert result {}".format(code))
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

        tdSql.query("describe stb3_0")
        tdSql.checkData(2, 1, "BOOL")
        tdSql.checkData(3, 1, "TINYINT")
        tdSql.checkData(4, 1, "SMALLINT")
        tdSql.checkData(5, 1, "INT")
        tdSql.checkData(6, 1, "BIGINT")
        tdSql.checkData(7, 1, "FLOAT")
        tdSql.checkData(8, 1, "DOUBLE")
        tdSql.checkData(9, 1, "BINARY")
        tdSql.checkData(10, 1, "NCHAR")


    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase())