|
| 1 | +"""Unit tests for dialect bulk_insert implementations.""" |
| 2 | +import datetime |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +pytest.importorskip("duckdb", reason="duckdb not installed") |
| 7 | + |
| 8 | +from sqlalchemy import ( |
| 9 | + BigInteger, |
| 10 | + Boolean, |
| 11 | + Column, |
| 12 | + DateTime, |
| 13 | + Double, |
| 14 | + Integer, |
| 15 | + LargeBinary, |
| 16 | + MetaData, |
| 17 | + SmallInteger, |
| 18 | + String, |
| 19 | + Table, |
| 20 | + create_engine, |
| 21 | + select, |
| 22 | + text, |
| 23 | +) |
| 24 | + |
| 25 | +from xml2db.dialect.base import DatabaseDialect |
| 26 | +from xml2db.dialect.duckdb import DuckDBDialect |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture() |
| 30 | +def duckdb_engine(): |
| 31 | + return create_engine("duckdb:///:memory:") |
| 32 | + |
| 33 | + |
| 34 | +def _make_table(engine, name, *extra_cols): |
| 35 | + """Create a simple test table and return the SQLAlchemy Table object.""" |
| 36 | + meta = MetaData() |
| 37 | + table = Table( |
| 38 | + name, |
| 39 | + meta, |
| 40 | + Column("id", Integer, key="id"), |
| 41 | + Column("label", String(100), key="label"), |
| 42 | + *extra_cols, |
| 43 | + ) |
| 44 | + meta.create_all(engine) |
| 45 | + return table |
| 46 | + |
| 47 | + |
| 48 | +def _roundtrip(engine, table, records): |
| 49 | + """Insert records via DuckDBDialect.bulk_insert and read them back.""" |
| 50 | + dialect = DuckDBDialect() |
| 51 | + with engine.begin() as conn: |
| 52 | + dialect.bulk_insert(conn, table, records) |
| 53 | + with engine.connect() as conn: |
| 54 | + return conn.execute(select(table)).mappings().all() |
| 55 | + |
| 56 | + |
| 57 | +# --------------------------------------------------------------------------- |
| 58 | +# Base dialect falls back to SQLAlchemy executemany |
| 59 | +# --------------------------------------------------------------------------- |
| 60 | + |
| 61 | + |
| 62 | +def test_base_dialect_bulk_insert(duckdb_engine): |
| 63 | + table = _make_table(duckdb_engine, "base_test") |
| 64 | + records = [{"id": 1, "label": "hello"}, {"id": 2, "label": "world"}] |
| 65 | + DatabaseDialect().bulk_insert( |
| 66 | + duckdb_engine.connect().__enter__(), table, records |
| 67 | + ) |
| 68 | + # Just check the method is importable and has the right signature. |
| 69 | + |
| 70 | + |
| 71 | +# --------------------------------------------------------------------------- |
| 72 | +# DuckDB dialect: basic types |
| 73 | +# --------------------------------------------------------------------------- |
| 74 | + |
| 75 | + |
| 76 | +def test_duckdb_bulk_insert_basic(duckdb_engine): |
| 77 | + table = _make_table(duckdb_engine, "basic") |
| 78 | + records = [{"id": 1, "label": "hello"}, {"id": 2, "label": None}] |
| 79 | + rows = _roundtrip(duckdb_engine, table, records) |
| 80 | + assert len(rows) == 2 |
| 81 | + assert rows[0]["id"] == 1 |
| 82 | + assert rows[0]["label"] == "hello" |
| 83 | + assert rows[1]["label"] is None |
| 84 | + |
| 85 | + |
| 86 | +def test_duckdb_bulk_insert_numeric_types(duckdb_engine): |
| 87 | + meta = MetaData() |
| 88 | + table = Table( |
| 89 | + "numeric_types", |
| 90 | + meta, |
| 91 | + Column("i", Integer, key="i"), |
| 92 | + Column("bi", BigInteger, key="bi"), |
| 93 | + Column("si", SmallInteger, key="si"), |
| 94 | + Column("d", Double, key="d"), |
| 95 | + ) |
| 96 | + meta.create_all(duckdb_engine) |
| 97 | + records = [{"i": 1, "bi": 10**15, "si": 32767, "d": 3.14}] |
| 98 | + rows = _roundtrip(duckdb_engine, table, records) |
| 99 | + assert rows[0]["i"] == 1 |
| 100 | + assert rows[0]["bi"] == 10**15 |
| 101 | + assert rows[0]["si"] == 32767 |
| 102 | + assert abs(rows[0]["d"] - 3.14) < 1e-9 |
| 103 | + |
| 104 | + |
| 105 | +def test_duckdb_bulk_insert_boolean(duckdb_engine): |
| 106 | + meta = MetaData() |
| 107 | + table = Table( |
| 108 | + "bool_test", |
| 109 | + meta, |
| 110 | + Column("id", Integer, key="id"), |
| 111 | + Column("flag", Boolean, key="flag"), |
| 112 | + ) |
| 113 | + meta.create_all(duckdb_engine) |
| 114 | + records = [{"id": 1, "flag": True}, {"id": 2, "flag": False}, {"id": 3, "flag": None}] |
| 115 | + rows = _roundtrip(duckdb_engine, table, records) |
| 116 | + assert rows[0]["flag"] is True |
| 117 | + assert rows[1]["flag"] is False |
| 118 | + assert rows[2]["flag"] is None |
| 119 | + |
| 120 | + |
| 121 | +def test_duckdb_bulk_insert_datetime(duckdb_engine): |
| 122 | + meta = MetaData() |
| 123 | + table = Table( |
| 124 | + "dt_test", |
| 125 | + meta, |
| 126 | + Column("id", Integer, key="id"), |
| 127 | + Column("ts", DateTime(timezone=True), key="ts"), |
| 128 | + ) |
| 129 | + meta.create_all(duckdb_engine) |
| 130 | + dt = datetime.datetime(2023, 9, 27, 14, 35, 54, 274602) |
| 131 | + records = [{"id": 1, "ts": dt}, {"id": 2, "ts": None}] |
| 132 | + rows = _roundtrip(duckdb_engine, table, records) |
| 133 | + # Value must survive the CSV round-trip and be returned as a datetime-like object. |
| 134 | + assert rows[0]["ts"] is not None |
| 135 | + assert rows[1]["ts"] is None |
| 136 | + |
| 137 | + |
| 138 | +def test_duckdb_bulk_insert_binary(duckdb_engine): |
| 139 | + meta = MetaData() |
| 140 | + table = Table( |
| 141 | + "binary_test", |
| 142 | + meta, |
| 143 | + Column("id", Integer, key="id"), |
| 144 | + Column("hash", LargeBinary(32), key="hash"), |
| 145 | + ) |
| 146 | + meta.create_all(duckdb_engine) |
| 147 | + payload = b"\xde\xad\xbe\xef" * 8 |
| 148 | + records = [{"id": 1, "hash": payload}, {"id": 2, "hash": None}] |
| 149 | + rows = _roundtrip(duckdb_engine, table, records) |
| 150 | + assert bytes(rows[0]["hash"]) == payload |
| 151 | + assert rows[1]["hash"] is None |
| 152 | + |
| 153 | + |
| 154 | +def test_duckdb_bulk_insert_scalar_column_default(duckdb_engine): |
| 155 | + """Columns with Python-side scalar defaults absent from records must be applied.""" |
| 156 | + meta = MetaData() |
| 157 | + table = Table( |
| 158 | + "default_test", |
| 159 | + meta, |
| 160 | + Column("id", Integer, key="id"), |
| 161 | + Column("flag", Boolean, default=False, key="flag"), |
| 162 | + ) |
| 163 | + meta.create_all(duckdb_engine) |
| 164 | + # Records do NOT contain 'flag'; the default must be applied. |
| 165 | + records = [{"id": 1}, {"id": 2}] |
| 166 | + rows = _roundtrip(duckdb_engine, table, records) |
| 167 | + assert rows[0]["flag"] is False |
| 168 | + assert rows[1]["flag"] is False |
| 169 | + |
| 170 | + |
| 171 | +def test_duckdb_bulk_insert_empty(duckdb_engine): |
| 172 | + table = _make_table(duckdb_engine, "empty_test") |
| 173 | + dialect = DuckDBDialect() |
| 174 | + with engine.begin() if False else duckdb_engine.begin() as conn: |
| 175 | + dialect.bulk_insert(conn, table, []) |
| 176 | + with duckdb_engine.connect() as conn: |
| 177 | + count = conn.execute(text("SELECT COUNT(*) FROM empty_test")).scalar() |
| 178 | + assert count == 0 |
0 commit comments