mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 01:58:41 +01:00
* fix(roms): read filter values with .all() so the GC never frees the cursor Looping over the filter-values Result directly makes SQLAlchemy 2.1 memoize an iterator closure that captures the Result, which puts the CursorResult, its execution context and the mariadb cursor in a reference cycle. The cyclic GC then frees them on whatever thread triggers a collection, and under concurrent gallery requests that segfaults the mariadb connector (1.1.14, Python 3.14) inside the pool checkout. .all() never builds that closure, so nothing is left for the GC. * fix(roms): address pr-ready code review * style(roms): pr-ready polish * fix(db): read every iterated query result with .all() The same reference cycle that segfaulted the filter values also hits smart collection matching on every save, the screenshot binding check, playlist and favourite adds, the recommendation reads and the stopword probe. The IGDB related-games stream keeps yield_per and closes its Result with a with block. A source scan test now fails on any Result iterated directly. * fix(roms): address Greptile review Keep gc.garbage entries that predate the filter-values cursor test: only the objects this test collects are inspected and removed, so a runner that already saves garbage keeps its list intact (Greptile, test_rom_facets.py). * pr-ready: code-review fixes Keep a generator's begin_session open until it finishes, read the streamed IGDB scan by partition, and widen the iterated-Result guard to names, unpacking, yield from and iterating methods. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * pr-ready: simplify Share the app-source AST scan between the two guard tests, fold the generator session's two branches into one nullcontext, and drop the call-site comment the guard test already carries. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * test(db): flag iterated legacy session.query() results Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Georges-Antoine Assi <contact@me.gantoine.com> Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
147 lines
4.4 KiB
Python
147 lines
4.4 KiB
Python
from collections.abc import Iterator
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
from sqlalchemy import text
|
|
from sqlalchemy.exc import OperationalError
|
|
|
|
from handler.database.base_handler import sync_engine
|
|
from utils import fulltext
|
|
from utils.database import is_postgresql
|
|
from utils.fulltext import (
|
|
DEFAULT_FULLTEXT_SETTINGS,
|
|
FulltextSettings,
|
|
fulltext_settings,
|
|
read_fulltext_settings,
|
|
split_fulltext_words,
|
|
)
|
|
|
|
IS_FULLTEXT_ENGINE = not is_postgresql(sync_engine)
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def fresh_settings() -> Iterator[None]:
|
|
fulltext._server_fulltext_settings.cache_clear()
|
|
yield
|
|
fulltext._server_fulltext_settings.cache_clear()
|
|
|
|
|
|
def _conn_reporting(
|
|
min_token_size: int = 3,
|
|
*,
|
|
stopwords_enabled: bool = True,
|
|
user_table: str | None = None,
|
|
server_table: str | None = None,
|
|
) -> MagicMock:
|
|
conn = MagicMock()
|
|
conn.execute.return_value.one.return_value = (
|
|
min_token_size,
|
|
84,
|
|
int(stopwords_enabled),
|
|
user_table,
|
|
server_table,
|
|
)
|
|
conn.scalars.return_value.all.return_value = ["Foo", "bar", None]
|
|
return conn
|
|
|
|
|
|
def _engine_connecting(conn: MagicMock) -> MagicMock:
|
|
engine = MagicMock()
|
|
engine.engine.name = "mariadb"
|
|
engine.connect.return_value.__enter__.return_value = conn
|
|
return engine
|
|
|
|
|
|
@pytest.mark.skipif(not IS_FULLTEXT_ENGINE, reason="InnoDB full-text only")
|
|
def test_reads_the_servers_settings():
|
|
with sync_engine.connect() as conn:
|
|
min_token_size: int = conn.execute(
|
|
text("SELECT @@innodb_ft_min_token_size")
|
|
).scalar_one()
|
|
settings = read_fulltext_settings(conn)
|
|
|
|
assert settings.min_token_size == min_token_size
|
|
assert "the" in settings.stopwords
|
|
|
|
|
|
@pytest.mark.skipif(IS_FULLTEXT_ENGINE, reason="PostgreSQL has no FULLTEXT index")
|
|
def test_postgresql_uses_the_defaults():
|
|
assert fulltext_settings(sync_engine) == DEFAULT_FULLTEXT_SETTINGS
|
|
|
|
|
|
def test_skips_fulltext_and_retries_when_the_server_is_unreachable():
|
|
engine = _engine_connecting(MagicMock())
|
|
engine.connect.side_effect = OperationalError("SELECT", {}, Exception("2013"))
|
|
|
|
assert fulltext_settings(engine) is None
|
|
assert fulltext_settings(engine) is None
|
|
|
|
assert engine.connect.call_count == 2
|
|
|
|
|
|
def test_keeps_the_token_sizes_when_the_stopwords_are_unreadable():
|
|
conn = _conn_reporting(4)
|
|
conn.scalars.side_effect = OperationalError("SELECT", {}, Exception("1227"))
|
|
|
|
assert read_fulltext_settings(conn) == DEFAULT_FULLTEXT_SETTINGS._replace(
|
|
min_token_size=4
|
|
)
|
|
|
|
|
|
def test_reads_a_custom_stopword_table():
|
|
conn = _conn_reporting(2, server_table="romm/stopwords")
|
|
|
|
settings = read_fulltext_settings(conn)
|
|
|
|
query = conn.scalars.call_args.args[0]
|
|
assert "FROM romm.stopwords" in str(query)
|
|
assert settings == FulltextSettings(2, 84, frozenset({"foo", "bar"}))
|
|
|
|
|
|
def test_prefers_the_user_stopword_table():
|
|
conn = _conn_reporting(user_table="romm/mine", server_table="romm/stopwords")
|
|
|
|
read_fulltext_settings(conn)
|
|
|
|
assert "FROM romm.mine" in str(conn.scalars.call_args.args[0])
|
|
|
|
|
|
def test_unreadable_custom_stopwords_skip_fulltext_and_retry():
|
|
conn = _conn_reporting(server_table="romm/stopwords")
|
|
conn.scalars.side_effect = OperationalError("SELECT", {}, Exception("1142"))
|
|
|
|
engine = _engine_connecting(conn)
|
|
|
|
assert fulltext_settings(engine) is None
|
|
assert fulltext_settings(engine) is None
|
|
assert conn.scalars.call_count == 2
|
|
|
|
|
|
def test_disabled_stopwords_leave_none():
|
|
conn = _conn_reporting(stopwords_enabled=False)
|
|
|
|
assert read_fulltext_settings(conn) == FulltextSettings(3, 84, frozenset())
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("term", "expected"),
|
|
[
|
|
("final fantasy 7", (["final", "fantasy"], ["7"])),
|
|
("the legend", (["legend"], ["the"])),
|
|
# InnoDB reads "dr." as the 2-letter token "dr".
|
|
("dr. mario", (["mario"], ["dr."])),
|
|
("pokemon: red", (["pokemon", "red"], [])),
|
|
("spider-man x-2", (["spider", "man"], ["x-2"])),
|
|
("mario's", (["mario's"], [])),
|
|
("a" * 85, ([], ["a" * 85])),
|
|
],
|
|
)
|
|
def test_split_fulltext_words(term: str, expected: tuple[list[str], list[str]]):
|
|
assert split_fulltext_words(term.split(), DEFAULT_FULLTEXT_SETTINGS) == expected
|
|
|
|
|
|
def test_split_fulltext_words_follows_the_token_size():
|
|
settings = DEFAULT_FULLTEXT_SETTINGS._replace(min_token_size=2)
|
|
|
|
assert split_fulltext_words(["ff", "of"], settings) == (["ff"], ["of"])
|