|
| 1 | +import datetime |
| 2 | +import time |
| 3 | +import taosws |
| 4 | +from taosws import Consumer |
| 5 | +import os |
| 6 | + |
| 7 | + |
| 8 | +def test_blob_sql(): |
| 9 | + value = os.getenv("TEST_TD_3360") |
| 10 | + if value is not None: |
| 11 | + return |
| 12 | + |
| 13 | + conn = taosws.connect("ws://localhost:6041") |
| 14 | + cursor = conn.cursor() |
| 15 | + |
| 16 | + try: |
| 17 | + cursor.execute("drop database if exists test_1753269319") |
| 18 | + cursor.execute("create database test_1753269319") |
| 19 | + cursor.execute("use test_1753269319") |
| 20 | + cursor.execute("create table t0(ts timestamp, c1 blob)") |
| 21 | + cursor.execute("insert into t0 values(1752218982761, null)") |
| 22 | + cursor.execute("insert into t0 values(1752218982762, '')") |
| 23 | + cursor.execute("insert into t0 values(1752218982763, 'hello')") |
| 24 | + cursor.execute("insert into t0 values(1752218982764, '\\x12345678')") |
| 25 | + |
| 26 | + cursor.execute("select * from t0") |
| 27 | + rows = cursor.fetchall() |
| 28 | + |
| 29 | + assert len(rows) == 4 |
| 30 | + |
| 31 | + assert rows[0][0].timestamp() * 1000 == 1752218982761 |
| 32 | + assert rows[1][0].timestamp() * 1000 == 1752218982762 |
| 33 | + assert rows[2][0].timestamp() * 1000 == 1752218982763 |
| 34 | + assert rows[3][0].timestamp() * 1000 == 1752218982764 |
| 35 | + |
| 36 | + assert rows[0][1] is None |
| 37 | + assert rows[1][1] == b"" |
| 38 | + assert rows[2][1] == b"hello" |
| 39 | + assert rows[3][1] == b"\x124Vx" |
| 40 | + |
| 41 | + finally: |
| 42 | + cursor.execute("drop database test_1753269319") |
| 43 | + conn.close() |
| 44 | + |
| 45 | + |
| 46 | +def test_blob_stmt2(): |
| 47 | + value = os.getenv("TEST_TD_3360") |
| 48 | + if value is not None: |
| 49 | + return |
| 50 | + |
| 51 | + conn = taosws.connect("ws://localhost:6041") |
| 52 | + try: |
| 53 | + conn.execute("drop database if exists test_1753269333") |
| 54 | + conn.execute("create database test_1753269333") |
| 55 | + conn.execute("use test_1753269333") |
| 56 | + conn.execute("create table t0 (ts timestamp, c1 blob)") |
| 57 | + |
| 58 | + test_timestamps = [1726803356466, 1726803356467, 1726803356468, 1726803356469] |
| 59 | + test_blobs = [None, b"", b"hello", b"\x124Vx"] |
| 60 | + |
| 61 | + stmt2 = conn.stmt2_statement() |
| 62 | + stmt2.prepare("insert into t0 values (?, ?)") |
| 63 | + |
| 64 | + param = taosws.stmt2_bind_param_view( |
| 65 | + table_name="", |
| 66 | + tags=None, |
| 67 | + columns=[ |
| 68 | + taosws.millis_timestamps_to_column(test_timestamps), |
| 69 | + taosws.blob_to_column(test_blobs), |
| 70 | + ], |
| 71 | + ) |
| 72 | + stmt2.bind([param]) |
| 73 | + |
| 74 | + affected_rows = stmt2.execute() |
| 75 | + assert affected_rows == 4 |
| 76 | + |
| 77 | + stmt2.prepare("select * from t0 where ts > ?") |
| 78 | + |
| 79 | + param = taosws.stmt2_bind_param_view( |
| 80 | + table_name="", |
| 81 | + tags=None, |
| 82 | + columns=[taosws.millis_timestamps_to_column([1726803356465])], |
| 83 | + ) |
| 84 | + stmt2.bind([param]) |
| 85 | + stmt2.execute() |
| 86 | + |
| 87 | + result = stmt2.result_set() |
| 88 | + expected_results = list(zip(test_timestamps, test_blobs)) |
| 89 | + |
| 90 | + actual_results = [] |
| 91 | + for row in result: |
| 92 | + dt = datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S.%f %z") |
| 93 | + timestamp = int(dt.timestamp() * 1000) |
| 94 | + actual_results.append((timestamp, row[1])) |
| 95 | + |
| 96 | + assert actual_results == expected_results |
| 97 | + |
| 98 | + finally: |
| 99 | + conn.execute("drop database test_1753269333") |
| 100 | + conn.close() |
| 101 | + |
| 102 | + |
| 103 | +def test_blob_tmq(): |
| 104 | + value = os.getenv("TEST_TD_3360") |
| 105 | + if value is not None: |
| 106 | + return |
| 107 | + |
| 108 | + conn = taosws.connect("ws://localhost:6041") |
| 109 | + try: |
| 110 | + conn.execute("drop topic if exists topic_1753270984") |
| 111 | + conn.execute("drop database if exists test_1753270984") |
| 112 | + conn.execute("create database test_1753270984") |
| 113 | + conn.execute("create topic topic_1753270984 as database test_1753270984") |
| 114 | + conn.execute("use test_1753270984") |
| 115 | + conn.execute("create table t0 (ts timestamp, c1 int, c2 blob)") |
| 116 | + |
| 117 | + num = 100 |
| 118 | + |
| 119 | + sql = "insert into t0 values " |
| 120 | + for i in range(num): |
| 121 | + ts = 1726803356466 + i |
| 122 | + sql += f"({ts}, {i}, 'blob_{i}'), " |
| 123 | + conn.execute(sql) |
| 124 | + |
| 125 | + consumer = Consumer(dsn="ws://localhost:6041?group.id=10&auto.offset.reset=earliest") |
| 126 | + consumer.subscribe(["topic_1753270984"]) |
| 127 | + |
| 128 | + cnt = 0 |
| 129 | + |
| 130 | + while 1: |
| 131 | + message = consumer.poll(timeout=5.0) |
| 132 | + if message: |
| 133 | + for block in message: |
| 134 | + cnt += block.nrows() |
| 135 | + consumer.commit(message) |
| 136 | + else: |
| 137 | + break |
| 138 | + |
| 139 | + assert cnt == num |
| 140 | + |
| 141 | + consumer.unsubscribe() |
| 142 | + |
| 143 | + finally: |
| 144 | + time.sleep(3) |
| 145 | + conn.execute("drop topic topic_1753270984") |
| 146 | + conn.execute("drop database test_1753270984") |
| 147 | + conn.close() |
0 commit comments