changeset 4374:90d476a80ce9

tests (unit/sqla): Add test for `keyword` filtering: rel 458
author Goffi <goffi@goffi.org>
date Fri, 06 Jun 2025 10:45:54 +0200
parents 5e48ae998976
children 42becd4b819f
files libervia/backend/core/xmpp.py pyproject.toml tests/unit/__init__.py tests/unit/test_memory_sqla.py
diffstat 4 files changed, 76 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/backend/core/xmpp.py	Wed Jun 04 17:52:18 2025 +0200
+++ b/libervia/backend/core/xmpp.py	Fri Jun 06 10:45:54 2025 +0200
@@ -16,7 +16,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-import asyncio
 import calendar
 from collections.abc import Iterable
 import copy
--- a/pyproject.toml	Wed Jun 04 17:52:18 2025 +0200
+++ b/pyproject.toml	Fri Jun 06 10:45:54 2025 +0200
@@ -134,6 +134,11 @@
     "aiosmtpd"
 ]
 
+[tool.pytest.ini_options]
+# We need to use AsyncIO reactor with pytest-twisted, otherwise AsyncIO calls like for
+# memory.sqla can't be done.
+addopts = "--reactor asyncio"
+
 
 [tool.hatch.version]
 path = "libervia/backend/__init__.py"
--- a/tests/unit/__init__.py	Wed Jun 04 17:52:18 2025 +0200
+++ b/tests/unit/__init__.py	Fri Jun 06 10:45:54 2025 +0200
@@ -0,0 +1,4 @@
+from pytest_twisted import init_asyncio_reactor
+
+
+init_asyncio_reactor()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/unit/test_memory_sqla.py	Fri Jun 06 10:45:54 2025 +0200
@@ -0,0 +1,67 @@
+#!/usr/bin/env python3
+
+# Libervia: an XMPP client
+# Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from unittest.mock import AsyncMock, MagicMock
+
+from pytest_twisted import ensureDeferred as ed
+from sqlalchemy.dialects import sqlite
+
+from libervia.backend.memory import sqla
+
+
+class TestSQLA:
+
+    @ed
+    async def test_keyword_filtering(self, monkeypatch):
+        monkeypatch.setattr(sqla, "profiles", {"test_profile": 1})
+        storage = sqla.Storage()
+
+        # We mock the SQLAlchemy session and result.
+        mock_session_context = AsyncMock()
+        mock_session_context.__aenter__ = AsyncMock()
+        mock_session_context.__aexit__ = AsyncMock()
+
+        storage.session = MagicMock(return_value=mock_session_context)
+
+        mock_session_instance = mock_session_context.__aenter__.return_value
+        mock_result = MagicMock()
+        mock_scalars = MagicMock()
+        mock_unique = MagicMock()
+        mock_unique.all.return_value = []
+        mock_scalars.unique.return_value = mock_unique
+        mock_result.scalars.return_value = mock_scalars
+        mock_session_instance.execute = AsyncMock(return_value=mock_result)
+
+        await storage.history_get(
+            from_jid=None,
+            to_jid=None,
+            filters={"keyword": "test"},
+            profile="test_profile"
+        )
+
+        # We check that the SQL query is as expected.
+        executed_stmt = mock_session_instance.execute.call_args[0][0]
+        compiled = executed_stmt.compile(
+            dialect=sqlite.dialect(),
+            compile_kwargs={"literal_binds": True}
+        )
+        sql = str(compiled)
+        assert "json_each" in sql
+        assert "keywords" in sql
+        assert "test" in sql
+        assert "EXISTS" in sql