insertJSONPayload.py 16.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
###################################################################
#           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 *
18
from util.types import TDSmlProtocolType, TDSmlTimestampType
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34


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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

475
        payload = ['''
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
        {
	    "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_.!@#$%^&*:;,./?|+-=()[]{}<>"
	    }
        }
493
        ''']
494
        code = self._conn.schemaless_insert(payload, TDSmlProtocolType.JSON.value, TDSmlTimestampType.NOT_CONFIGURED.value)
495
        print("schemaless_insert result {}".format(code))
496 497 498 499

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

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

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

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

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

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

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

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

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

        ### tag value ###

602
        payload = ['''
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 651
        {
	    "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"
		}
	    }
        }
652
        ''']
653
        code = self._conn.schemaless_insert(payload, TDSmlProtocolType.JSON.value, TDSmlTimestampType.NOT_CONFIGURED.value)
654
        print("schemaless_insert result {}".format(code))
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

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