Skip to content

Commit 7411514

Browse files
committed
adjust python transit handling to avoid flakiness
1 parent 2d83a1c commit 7411514

2 files changed

Lines changed: 34 additions & 29 deletions

File tree

python/tests/conftest.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
import psycopg as pg
44
import asyncio
55
import os
6+
import time
7+
import random
68

79
XTDB_HOST = os.environ.get("XTDB_HOST", "xtdb")
810

11+
def _generate_table_name():
12+
"""Generate a unique table name using timestamp and random suffix."""
13+
return f"test_table_{int(time.time() * 1000)}_{random.randint(0, 99999)}"
14+
915
# Default DB params without transit fallback (for JSON and basic tests)
1016
DB_PARAMS = {
1117
"host": XTDB_HOST,
@@ -44,14 +50,14 @@ async def conn_transit():
4450
@pytest_asyncio.fixture
4551
async def clean_table(conn):
4652
"""Create a clean test table."""
47-
table_name = f"test_table_{id(conn)}"
53+
table_name = _generate_table_name()
4854
yield table_name
4955
# Cleanup happens automatically in XTDB (ephemeral container)
5056

5157
@pytest_asyncio.fixture
5258
async def clean_table_transit(conn_transit):
5359
"""Create a clean test table for transit connection."""
54-
table_name = f"test_table_{id(conn_transit)}"
60+
table_name = _generate_table_name()
5561
yield table_name
5662
# Cleanup happens automatically in XTDB (ephemeral container)
5763

python/tests/test_transit.py

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,32 @@ async def test_transit_verify_with_unmarshalling(conn_transit, clean_table_trans
264264
# Store the original parsed data for later comparison
265265
original_data = []
266266

267+
# Custom transit-JSON reader (handles the subset we need)
268+
def parse_transit_value(val):
269+
if isinstance(val, list) and len(val) > 0:
270+
if val[0] == "^ ":
271+
# It's a map: ["^ ", "~:key1", val1, "~:key2", val2, ...]
272+
result = {}
273+
for i in range(1, len(val), 2):
274+
if i + 1 >= len(val):
275+
break
276+
k = val[i]
277+
v = val[i + 1]
278+
# Convert keyword keys
279+
if isinstance(k, str) and k.startswith("~:"):
280+
k = k[2:]
281+
# Recursively parse values
282+
result[k] = parse_transit_value(v)
283+
return result
284+
else:
285+
# It's an array - parse each element
286+
return [parse_transit_value(item) for item in val]
287+
elif isinstance(val, str) and val.startswith("~t"):
288+
# Transit date - keep as ISO string
289+
return val[2:]
290+
else:
291+
return val
292+
267293
# Step 1: Insert using OID 16384 approach
268294
for line in lines:
269295
line = line.strip()
@@ -273,33 +299,6 @@ async def test_transit_verify_with_unmarshalling(conn_transit, clean_table_trans
273299
# Parse the COMPLETE transit-JSON with custom parser
274300
# This demonstrates that you don't need transit-python2 for reading transit-JSON
275301
raw_json = json.loads(line)
276-
277-
# Custom transit-JSON reader (handles the subset we need)
278-
def parse_transit_value(val):
279-
if isinstance(val, list) and len(val) > 0:
280-
if val[0] == "^ ":
281-
# It's a map: ["^ ", "~:key1", val1, "~:key2", val2, ...]
282-
result = {}
283-
for i in range(1, len(val), 2):
284-
if i + 1 >= len(val):
285-
break
286-
k = val[i]
287-
v = val[i + 1]
288-
# Convert keyword keys
289-
if isinstance(k, str) and k.startswith("~:"):
290-
k = k[2:]
291-
# Recursively parse values
292-
result[k] = parse_transit_value(v)
293-
return result
294-
else:
295-
# It's an array - parse each element
296-
return [parse_transit_value(item) for item in val]
297-
elif isinstance(val, str) and val.startswith("~t"):
298-
# Transit date - keep as ISO string
299-
return val[2:]
300-
else:
301-
return val
302-
303302
parsed_dict = parse_transit_value(raw_json)
304303
original_data.append(parsed_dict)
305304

0 commit comments

Comments
 (0)