Test array with NULL values using psycopg3 binary protocol.
()
| 78 | return result |
| 79 | |
| 80 | def test_array_with_nulls(): |
| 81 | """Test array with NULL values using psycopg3 binary protocol.""" |
| 82 | |
| 83 | print("=" * 80) |
| 84 | print("Testing PostgreSQL array binary encoding with NULLs") |
| 85 | print("=" * 80) |
| 86 | |
| 87 | # Connect to pgsqlite server on port 5433 |
| 88 | conn_str = "host=localhost port=5433 user=postgres dbname=test" |
| 89 | |
| 90 | with psycopg.connect(conn_str) as conn: |
| 91 | with conn.cursor() as cur: |
| 92 | # Create test table |
| 93 | print("\n1. Creating test table...") |
| 94 | cur.execute(""" |
| 95 | CREATE TABLE IF NOT EXISTS array_test ( |
| 96 | id INTEGER PRIMARY KEY, |
| 97 | int_array INTEGER[] |
| 98 | ) |
| 99 | """) |
| 100 | |
| 101 | # Insert test data - array with NULLs |
| 102 | print("\n2. Inserting array with NULLs: [1, NULL, 3]") |
| 103 | cur.execute(""" |
| 104 | INSERT INTO array_test (id, int_array) |
| 105 | VALUES (1, ARRAY[1, NULL, 3]::INTEGER[]) |
| 106 | ON CONFLICT (id) DO UPDATE SET int_array = excluded.int_array |
| 107 | """) |
| 108 | |
| 109 | # Query with binary result format |
| 110 | print("\n3. Querying with binary protocol...") |
| 111 | cur.execute(""" |
| 112 | SELECT int_array FROM array_test WHERE id = 1 |
| 113 | """, binary=True) |
| 114 | |
| 115 | row = cur.fetchone() |
| 116 | |
| 117 | if row and row[0]: |
| 118 | # Get the raw binary data |
| 119 | raw_data = row[0] |
| 120 | |
| 121 | print(f"\n4. Received {len(raw_data)} bytes of binary data") |
| 122 | |
| 123 | # Decode and analyze the binary format |
| 124 | decoded = decode_binary_array(raw_data) |
| 125 | |
| 126 | print("\n5. Decoded array structure:") |
| 127 | print(f" - ndim: {decoded.get('ndim')}") |
| 128 | print(f" - dataoffset: {decoded.get('dataoffset')} (0x{decoded.get('dataoffset'):02x})") |
| 129 | print(f" - elemtype: {decoded.get('elemtype')} (OID for INT4)") |
| 130 | |
| 131 | if decoded.get('dimensions'): |
| 132 | print(f" - dimensions: {decoded['dimensions']}") |
| 133 | |
| 134 | if decoded.get('null_bitmap'): |
| 135 | print(f" - null_bitmap (hex): {decoded['null_bitmap']}") |
| 136 | print(f" - null_bitmap (binary): {decoded['null_bitmap_binary']}") |
| 137 |
no test coverage detected