Mercurial > libervia-backend
annotate tests/unit/test_memory_sqla.py @ 4376:7ac28a270b7f default tip
memory (sqla): Add a filter for `origin` in `history_get`:
rel 459
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 08 Jun 2025 17:16:58 +0200 |
parents | 90d476a80ce9 |
children |
rev | line source |
---|---|
4374
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia: an XMPP client |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org) |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 from unittest.mock import AsyncMock, MagicMock |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from pytest_twisted import ensureDeferred as ed |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sqlalchemy.dialects import sqlite |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from libervia.backend.memory import sqla |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 class TestSQLA: |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 @ed |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 async def test_keyword_filtering(self, monkeypatch): |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 monkeypatch.setattr(sqla, "profiles", {"test_profile": 1}) |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 storage = sqla.Storage() |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 # We mock the SQLAlchemy session and result. |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 mock_session_context = AsyncMock() |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 mock_session_context.__aenter__ = AsyncMock() |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 mock_session_context.__aexit__ = AsyncMock() |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 storage.session = MagicMock(return_value=mock_session_context) |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 mock_session_instance = mock_session_context.__aenter__.return_value |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 mock_result = MagicMock() |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 mock_scalars = MagicMock() |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 mock_unique = MagicMock() |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 mock_unique.all.return_value = [] |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 mock_scalars.unique.return_value = mock_unique |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 mock_result.scalars.return_value = mock_scalars |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 mock_session_instance.execute = AsyncMock(return_value=mock_result) |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 await storage.history_get( |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 from_jid=None, |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 to_jid=None, |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 filters={"keyword": "test"}, |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 profile="test_profile" |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 ) |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 # We check that the SQL query is as expected. |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 executed_stmt = mock_session_instance.execute.call_args[0][0] |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 compiled = executed_stmt.compile( |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 dialect=sqlite.dialect(), |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 compile_kwargs={"literal_binds": True} |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 ) |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 sql = str(compiled) |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 assert "json_each" in sql |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 assert "keywords" in sql |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 assert "test" in sql |
90d476a80ce9
tests (unit/sqla): Add test for `keyword` filtering:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 assert "EXISTS" in sql |