| 1 | """SQL layer tests via native witdb_sql_* exports.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import tempfile |
| 6 | from pathlib import Path |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from pywitdb._native.sql import sql_native_available |
| 11 | from pywitdb.sql import connect |
| 12 | |
| 13 | pytestmark = pytest.mark.skipif( |
| 14 | not sql_native_available(), reason="libwitdb SQL exports not built" |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | def test_native_sql_create_insert_query() -> None: |
| 19 | with tempfile.TemporaryDirectory() as tmp: |
| 20 | path = str(Path(tmp) / "sql_native.witdb") |
| 21 | with connect(path, backend="native", create=True) as conn: |
| 22 | conn.executescript( |
| 23 | "CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL);" |
| 24 | ) |
| 25 | conn.commit() |
| 26 | conn.execute("INSERT INTO t (name) VALUES (?)", ("native",)) |
| 27 | conn.commit() |
| 28 | row = conn.execute("SELECT name FROM t WHERE name = ?", ("native",)).fetchone() |
| 29 | assert row is not None |
| 30 | assert row["name"] == "native" |
| 31 | assert conn.last_insert_rowid() == 1 |
| 32 | |
| 33 | |
| 34 | def test_connect_auto_prefers_native_sql() -> None: |
| 35 | if not sql_native_available(): |
| 36 | pytest.skip("native SQL not available") |
| 37 | with tempfile.TemporaryDirectory() as tmp: |
| 38 | path = str(Path(tmp) / "auto_sql.witdb") |
| 39 | with connect(path, backend="auto", create=True) as conn: |
| 40 | from pywitdb._native.sql import NativeSqlConnection |
| 41 | |
| 42 | assert isinstance(conn, NativeSqlConnection) |
| 43 | |