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