Mercurial > libervia-backend
annotate tests/unit/test_pubsub-cache.py @ 4285:f1d0cde61af7
tests (unit): fix tests + black reformatting.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 14 Jul 2024 17:42:53 +0200 |
parents | 4b842c1fb686 |
children |
rev | line source |
---|---|
3624 | 1 #!/usr/bin/env python3 |
2 | |
3 # Libervia: an XMPP client | |
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 from twisted.internet import defer | |
20 from pytest_twisted import ensureDeferred as ed | |
21 from unittest.mock import MagicMock, patch | |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
22 from libervia.backend.memory.sqla import PubsubNode, SyncState |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
23 from libervia.backend.core.constants import Const as C |
3624 | 24 |
25 | |
26 class TestPubsubCache: | |
27 | |
28 @ed | |
29 async def test_cache_is_used_transparently(self, host, client): | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
30 """Cache is used when a pubsub get_items operation is done""" |
3624 | 31 items_ret = defer.Deferred() |
32 items_ret.callback(([], {})) | |
33 client.pubsub_client.items = MagicMock(return_value=items_ret) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
34 host.memory.storage.get_pubsub_node.return_value = None |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
35 pubsub_node = host.memory.storage.set_pubsub_node.return_value = PubsubNode( |
4285
f1d0cde61af7
tests (unit): fix tests + black reformatting.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
36 sync_state=None |
3624 | 37 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
38 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node: |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
39 await host.plugins["XEP-0060"].get_items( |
3624 | 40 client, |
41 None, | |
42 "urn:xmpp:microblog:0", | |
43 ) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
44 assert cache_node.call_count == 1 |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
45 assert cache_node.call_args.args[-1] == pubsub_node |
3624 | 46 |
47 @ed | |
48 async def test_cache_is_skipped_with_use_cache_false(self, host, client): | |
49 """Cache is skipped when 'use_cache' extra field is False""" | |
50 items_ret = defer.Deferred() | |
51 items_ret.callback(([], {})) | |
52 client.pubsub_client.items = MagicMock(return_value=items_ret) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
53 host.memory.storage.get_pubsub_node.return_value = None |
4285
f1d0cde61af7
tests (unit): fix tests + black reformatting.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
54 host.memory.storage.set_pubsub_node.return_value = PubsubNode(sync_state=None) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
55 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node: |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
56 await host.plugins["XEP-0060"].get_items( |
4285
f1d0cde61af7
tests (unit): fix tests + black reformatting.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
57 client, None, "urn:xmpp:microblog:0", extra={C.KEY_USE_CACHE: False} |
3624 | 58 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
59 assert not cache_node.called |
3624 | 60 |
61 @ed | |
62 async def test_cache_is_not_used_when_no_cache(self, host, client): | |
63 """Cache is skipped when 'pubsub_cache_strategy' is set to 'no_cache'""" | |
64 with host.use_option_and_reload(None, "pubsub_cache_strategy", "no_cache"): | |
65 items_ret = defer.Deferred() | |
66 items_ret.callback(([], {})) | |
67 client.pubsub_client.items = MagicMock(return_value=items_ret) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
68 host.memory.storage.get_pubsub_node.return_value = None |
4285
f1d0cde61af7
tests (unit): fix tests + black reformatting.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
69 host.memory.storage.set_pubsub_node.return_value = PubsubNode(sync_state=None) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
70 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node: |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
71 await host.plugins["XEP-0060"].get_items( |
3624 | 72 client, |
73 None, | |
74 "urn:xmpp:microblog:0", | |
75 ) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
76 assert not cache_node.called |
3624 | 77 |
78 @ed | |
79 async def test_no_pubsub_get_when_cache_completed(self, host, client): | |
80 """No pubsub get is emitted when items are fully cached""" | |
81 items_ret = defer.Deferred() | |
82 items_ret.callback(([], {})) | |
83 client.pubsub_client.items = MagicMock(return_value=items_ret) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
84 host.memory.storage.get_pubsub_node.return_value = PubsubNode( |
4285
f1d0cde61af7
tests (unit): fix tests + black reformatting.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
85 sync_state=SyncState.COMPLETED |
3624 | 86 ) |
87 with patch.object( | |
4285
f1d0cde61af7
tests (unit): fix tests + black reformatting.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
88 host.plugins["PUBSUB_CACHE"], "get_items_from_cache" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
89 ) as get_items_from_cache: |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
90 get_items_from_cache.return_value = ([], {}) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
91 await host.plugins["XEP-0060"].get_items( |
3624 | 92 client, |
93 None, | |
94 "urn:xmpp:microblog:0", | |
95 ) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
96 assert get_items_from_cache.call_count == 1 |
3624 | 97 assert not client.pubsub_client.items.called |
98 | |
99 @ed | |
100 async def test_pubsub_get_when_cache_in_progress(self, host, client): | |
101 """Pubsub get is emitted when items are currently being cached""" | |
102 items_ret = defer.Deferred() | |
103 items_ret.callback(([], {})) | |
104 client.pubsub_client.items = MagicMock(return_value=items_ret) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
105 host.memory.storage.get_pubsub_node.return_value = PubsubNode( |
4285
f1d0cde61af7
tests (unit): fix tests + black reformatting.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
106 sync_state=SyncState.IN_PROGRESS |
3624 | 107 ) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
108 with patch.object(host.plugins["PUBSUB_CACHE"], "analyse_node") as analyse_node: |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
109 analyse_node.return_value = {"to_sync": True} |
3624 | 110 with patch.object( |
4285
f1d0cde61af7
tests (unit): fix tests + black reformatting.
Goffi <goffi@goffi.org>
parents:
4071
diff
changeset
|
111 host.plugins["PUBSUB_CACHE"], "get_items_from_cache" |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
112 ) as get_items_from_cache: |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
113 get_items_from_cache.return_value = ([], {}) |
3624 | 114 assert client.pubsub_client.items.call_count == 0 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
115 await host.plugins["XEP-0060"].get_items( |
3624 | 116 client, |
117 None, | |
118 "urn:xmpp:microblog:0", | |
119 ) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3624
diff
changeset
|
120 assert not get_items_from_cache.called |
3624 | 121 assert client.pubsub_client.items.call_count == 1 |