| 1 | """SQL layer tests via witdb-bridge.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import tempfile |
| 6 | from pathlib import Path |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from pywitdb._bridge.client import bridge_available |
| 11 | from pywitdb.sql import connect |
| 12 | |
| 13 | pytestmark = pytest.mark.skipif( |
| 14 | not bridge_available(), reason="witdb-bridge not built" |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | def test_sql_create_insert_query() -> None: |
| 19 | with tempfile.TemporaryDirectory() as tmp: |
| 20 | path = str(Path(tmp) / "sql.witdb") |
| 21 | with connect(path, 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 (?)", ("alice",)) |
| 27 | conn.commit() |
| 28 | cur = conn.execute("SELECT name FROM t WHERE name = ?", ("alice",)) |
| 29 | row = cur.fetchone() |
| 30 | assert row is not None |
| 31 | assert row["name"] == "alice" |
| 32 | assert conn.last_insert_rowid() == 1 |
| 33 | |
| 34 | |
| 35 | def test_sql_lastrowid_on_cursor() -> None: |
| 36 | with tempfile.TemporaryDirectory() as tmp: |
| 37 | path = str(Path(tmp) / "rowid.witdb") |
| 38 | with connect(path, create=True) as conn: |
| 39 | conn.executescript( |
| 40 | "CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT, v INT);" |
| 41 | ) |
| 42 | cur = conn.execute("INSERT INTO t (v) VALUES (?)", (42,)) |
| 43 | assert cur.lastrowid == 1 |
| 44 | |