insertJSONPayload.py 14.2 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 39 40 41 42 43 44 45 46 47
        {
	    "metric":	".stb.0.",
	    "timestamp":	1626006833610123,
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
48 49 50
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
51 52 53 54

        tdSql.query("describe _stb_0_")
        tdSql.checkRows(6)

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

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

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

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

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

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

114
        payload = ['''
115 116 117 118 119 120 121 122 123 124 125
        {
	    "metric":	"stb0_3",
	    "timestamp":	1626006833610123,
	    "value":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>",
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
126 127 128
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines 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 137 138 139 140 141 142 143 144
	    "timestamp":	1626006833610123,
	    "value":	3.14,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
145 146 147
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
148 149 150 151

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

152
        payload = ['''
153 154 155 156 157 158 159 160 161 162 163
        {
	    "metric":	"stb0_5",
	    "timestamp":	1626006833610123,
	    "value":	3.14E-2,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
164 165 166
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines 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 188
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
189 190


191 192
        print("============= step3 : test tags  ================")
        ### Default tag numeric types ###
193
        payload = ['''
194 195 196 197 198 199 200 201
        {
	    "metric":	"stb0_8",
	    "timestamp":	0,
	    "value":	123,
	    "tags":	{
		"t1":	123
	    }
        }
202 203 204
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
205 206 207 208

        tdSql.query("describe stb0_8")
        tdSql.checkData(2, 1, "BIGINT")

209
        payload = ['''
210 211 212 213 214 215 216 217
        {
	    "metric":	"stb0_9",
	    "timestamp":	0,
	    "value":	123,
	    "tags":	{
		"t1":	123.00
	    }
        }
218 219 220
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
221 222 223 224

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

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

        tdSql.query("describe stb0_10")
        tdSql.checkData(2, 1, "DOUBLE")
240 241

        ### Nested format ###
242
        print("============= step4 : test nested format  ================")
243 244
        ### timestamp ###
        #seconds
245
        payload = ['''
246 247 248 249 250 251 252 253 254 255 256 257 258 259
        {
	    "metric":	"stb1_0",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
260 261 262
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
263 264 265 266 267

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

        #milliseconds
268
        payload = ['''
269 270 271 272 273 274 275 276 277 278 279 280 281 282
        {
	    "metric":	"stb1_1",
	    "timestamp":	{
		"value":	1626006833610,
		"type":	"ms"
	    },
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
283 284 285
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
286 287 288 289 290

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

        #microseconds
291
        payload = ['''
292 293 294 295 296 297 298 299 300 301 302 303 304 305
        {
	    "metric":	"stb1_2",
	    "timestamp":	{
		"value":	1626006833610123,
		"type":	"us"
	    },
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
306 307 308
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
309 310 311 312 313

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

        #nanoseconds
314
        payload = ['''
315 316 317 318 319 320 321 322 323 324 325 326 327 328
        {
	    "metric":	"stb1_3",
	    "timestamp":	{
                "value":	1.6260068336101233e+18,
		"type":	"ns"
	    },
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
329 330 331
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
332 333 334 335 336 337

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

        #now
        tdSql.execute('use test')
338
        payload = ['''
339 340 341 342 343 344 345 346 347 348 349 350 351 352
        {
	    "metric":	"stb1_4",
	    "timestamp":	{
		"value":	0,
		"type":	"ns"
	    },
	    "value":	10,
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
353 354 355
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
356 357

        ### metric value ###
358
        payload = ['''
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
        {
	    "metric":	"stb2_0",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	true,
		"type":	"bool"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
376 377 378
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
379 380 381 382

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

383
        payload = ['''
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
        {
	    "metric":	"stb2_1",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	127,
		"type":	"tinyint"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
401 402 403
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
404 405 406 407

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

408
        payload = ['''
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
        {
	    "metric":	"stb2_2",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	32767,
		"type":	"smallint"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
426 427 428
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
429 430 431 432

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

433
        payload = ['''
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
        {
	    "metric":	"stb2_3",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	2147483647,
		"type":	"int"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
451 452 453
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
454 455 456 457

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

458
        payload = ['''
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
        {
	    "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_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
476 477 478
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
479 480 481 482

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

483
        payload = ['''
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
        {
	    "metric":	"stb2_5",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	11.12345,
		"type":	"float"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
501 502 503
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
504 505 506 507

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

508
        payload = ['''
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
        {
	    "metric":	"stb2_6",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	22.123456789,
		"type":	"double"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
526 527 528
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
529 530 531 532

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

533
        payload = ['''
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
        {
	    "metric":	"stb2_7",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>",
		"type":	"binary"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
551 552 553
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
554 555 556 557

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

558
        payload = ['''
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
        {
	    "metric":	"stb2_8",
	    "timestamp":	{
		"value":	1626006833,
		"type":	"s"
	    },
	    "value":	{
		"value":	"你好",
		"type":	"nchar"
	    },
	    "tags":	{
		"t1":	true,
		"t2":	false,
		"t3":	10,
		"t4":	"123_abc_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
576 577 578
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
579 580 581 582 583 584

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

        ### tag value ###

585
        payload = ['''
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 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
        {
	    "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"
		}
	    }
        }
635 636 637
        ''']
        code = self._conn.insert_lines(payload, 2)
        print("insert_lines result {}".format(code))
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657

        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())