Files
romm/backend/tests/handler/database/test_base_handler.py
Stu Anderson 6fc323e24b chore(api): widen indexes for filtered search, MariaDB Fix (#5824)
* chore(api): add indexes to roms

* chore(api): add additional indexes

* chore(api): widen indexes on rom table

* perf(api): fast filtered and short searches on MariaDB, plus year, rating and game mode filter

* chore(api): add ANALYZE TABLE at end of migration

* pr-ready: drop the roms_search table

* pr-ready: code-review

* pr-ready: review-polish

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

* chore(api): use hand-coded list of columns

* pr-ready: code-review

Restore the schema before asserting in the 0163 schema tests, send
platform_ids in the endpoint test, and fix two stale comments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: review-polish

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Reword the comments code-review added to the 0163 schema tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(database): cover year_range_ms

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(database): raise the sort limit on MariaDB through the mysql driver

The listener keyed on the dialect name, which is mysql when
ROMM_DB_DRIVER=mysql reaches a MariaDB server. Check the dialect's
is_mariadb instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test: fix the sort limit tests on PostgreSQL and the _ms types

The sort limit tests read is_mariadb from the test engine, which
PostgreSQL's dialect lacks, so they now set it. _ms takes named date
parts again, since mypy rejects a star tuple before tzinfo.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com>
2026-12-02 18:58:05 -05:00

101 lines
2.7 KiB
Python

import pytest
import sqlalchemy as sa
from handler.database.base_handler import (
SORT_DATA_LIMIT_BYTES,
raise_sort_data_limit,
read_committed,
sync_engine,
)
class _Cursor:
def __init__(self, log_bin: int, binlog_format: str) -> None:
self.row = (log_bin, binlog_format)
self.executed: list[str] = []
def execute(self, statement: str) -> None:
self.executed.append(statement)
def fetchone(self) -> tuple[int, str]:
return self.row
def close(self) -> None: ...
class _Connection:
def __init__(self, cursor: _Cursor) -> None:
self._cursor = cursor
self.committed = False
def cursor(self) -> _Cursor:
return self._cursor
def commit(self) -> None:
self.committed = True
@pytest.mark.skipif(
sync_engine.dialect.name not in ("mariadb", "mysql"),
reason="PostgreSQL runs at READ COMMITTED by default",
)
def test_sessions_run_at_read_committed():
with sync_engine.connect() as conn:
assert conn.get_isolation_level() == "READ COMMITTED"
@pytest.mark.skipif(
sync_engine.dialect.name != "mariadb",
reason="only MariaDB sorts by row id when the sort key is long",
)
def test_mariadb_sessions_keep_sort_values_with_the_rows():
with sync_engine.connect() as conn:
value = conn.execute(sa.text("SELECT @@max_length_for_sort_data")).scalar()
assert value == SORT_DATA_LIMIT_BYTES
def test_raise_sort_data_limit_sets_the_session_variable(
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setattr(sync_engine.dialect, "is_mariadb", True, raising=False)
cursor = _Cursor(0, "ROW")
connection = _Connection(cursor)
raise_sort_data_limit(connection, None)
assert cursor.executed == [
f"SET SESSION max_length_for_sort_data = {SORT_DATA_LIMIT_BYTES}"
]
def test_raise_sort_data_limit_skips_a_mysql_server(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(sync_engine.dialect, "is_mariadb", False, raising=False)
cursor = _Cursor(0, "ROW")
raise_sort_data_limit(_Connection(cursor), None)
assert cursor.executed == []
@pytest.mark.parametrize(
("log_bin", "binlog_format", "sets_it"),
[
(0, "STATEMENT", True),
(1, "ROW", True),
(1, "MIXED", True),
(1, "STATEMENT", False),
],
)
def test_read_committed_unless_statement_binlog(
log_bin: int, binlog_format: str, sets_it: bool
):
cursor = _Cursor(log_bin, binlog_format)
connection = _Connection(cursor)
read_committed(connection, None)
set_isolation = "SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED"
assert (set_isolation in cursor.executed) is sets_it
assert connection.committed