"""Phase 4 integration tests — parameter binding (SQ_BIND). Tests cover ``?`` and ``:N`` placeholder styles, the supported Python type set (int, float, str, bool, None), and round-tripping through INSERT + SELECT to verify both encode AND decode paths. """ from __future__ import annotations import pytest import informix_db from tests.conftest import ConnParams pytestmark = pytest.mark.integration def _connect(conn_params: ConnParams) -> informix_db.Connection: return informix_db.connect( host=conn_params.host, port=conn_params.port, user=conn_params.user, password=conn_params.password, database=conn_params.database, server=conn_params.server, connect_timeout=10.0, read_timeout=10.0, ) def test_insert_with_qmark_params(conn_params: ConnParams) -> None: """``?`` placeholder style.""" with _connect(conn_params) as conn: cur = conn.cursor() cur.execute("CREATE TEMP TABLE t_p_a (id INTEGER, name VARCHAR(50))") cur.execute("INSERT INTO t_p_a VALUES (?, ?)", (42, "hello")) assert cur.rowcount == 1 cur.execute("SELECT id, name FROM t_p_a") assert cur.fetchall() == [(42, "hello")] def test_insert_with_numeric_params(conn_params: ConnParams) -> None: """``:1`` placeholder style (paramstyle="numeric").""" with _connect(conn_params) as conn: cur = conn.cursor() cur.execute("CREATE TEMP TABLE t_p_b (id INTEGER, name VARCHAR(50))") cur.execute("INSERT INTO t_p_b VALUES (:1, :2)", (99, "world")) assert cur.rowcount == 1 cur.execute("SELECT id, name FROM t_p_b") assert cur.fetchall() == [(99, "world")] def test_int_float_str_round_trip(conn_params: ConnParams) -> None: """All three core types in one INSERT, verified via SELECT.""" with _connect(conn_params) as conn: cur = conn.cursor() cur.execute("CREATE TEMP TABLE t_p_c (i INTEGER, f FLOAT, s VARCHAR(20))") cur.execute("INSERT INTO t_p_c VALUES (?, ?, ?)", (123, 4.5, "alpha")) cur.execute("INSERT INTO t_p_c VALUES (?, ?, ?)", (-7, -1.25, "beta")) cur.execute("SELECT i, f, s FROM t_p_c ORDER BY i") rows = cur.fetchall() assert rows == [(-7, -1.25, "beta"), (123, 4.5, "alpha")] def test_update_with_params(conn_params: ConnParams) -> None: """UPDATE with parameter values in both SET and WHERE clauses.""" with _connect(conn_params) as conn: cur = conn.cursor() cur.execute("CREATE TEMP TABLE t_p_d (id INTEGER, name VARCHAR(50))") cur.execute("INSERT INTO t_p_d VALUES (?, ?)", (1, "old")) cur.execute("INSERT INTO t_p_d VALUES (?, ?)", (2, "old")) cur.execute("UPDATE t_p_d SET name = ? WHERE id = ?", ("new", 2)) cur.execute("SELECT id, name FROM t_p_d ORDER BY id") assert cur.fetchall() == [(1, "old"), (2, "new")] def test_delete_with_param(conn_params: ConnParams) -> None: """DELETE with a parameter in the WHERE clause.""" with _connect(conn_params) as conn: cur = conn.cursor() cur.execute("CREATE TEMP TABLE t_p_e (id INTEGER, name VARCHAR(50))") for i in range(1, 6): cur.execute("INSERT INTO t_p_e VALUES (?, ?)", (i, f"row{i}")) cur.execute("DELETE FROM t_p_e WHERE id = ?", (3,)) cur.execute("SELECT id FROM t_p_e ORDER BY id") assert cur.fetchall() == [(1,), (2,), (4,), (5,)] def test_unsupported_param_type_raises(conn_params: ConnParams) -> None: """Phase 4 supports int/float/str/bool/None; other types raise.""" with _connect(conn_params) as conn: cur = conn.cursor() cur.execute("CREATE TEMP TABLE t_p_f (id INTEGER)") with pytest.raises(NotImplementedError, match="bytes"): cur.execute("INSERT INTO t_p_f VALUES (?)", (b"raw bytes",)) def test_parameterized_select_not_yet_supported(conn_params: ConnParams) -> None: """Parameterized SELECT lands in Phase 4.x — currently raises.""" with _connect(conn_params) as conn: cur = conn.cursor() with pytest.raises(informix_db.NotSupportedError, match=r"Phase 4\.x"): cur.execute("SELECT 1 FROM systables WHERE tabid = ?", (1,)) def test_dict_params_unsupported(conn_params: ConnParams) -> None: """Named parameters aren't supported — paramstyle is ``numeric``.""" with _connect(conn_params) as conn: cur = conn.cursor() cur.execute("CREATE TEMP TABLE t_p_g (id INTEGER)") with pytest.raises(informix_db.NotSupportedError, match="positional"): cur.execute("INSERT INTO t_p_g VALUES (:id)", {"id": 1})