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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3624
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
2
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: an XMPP client
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
5
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
10
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
15
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
18
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from twisted.internet import defer
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from pytest_twisted import ensureDeferred as ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
24
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
25
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class TestPubsubCache:
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
27
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
28 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
31 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
32 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
40 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
41 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
42 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
46
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
47 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
48 async def test_cache_is_skipped_with_use_cache_false(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
49 """Cache is skipped when 'use_cache' extra field is False"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
50 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
51 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
60
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
61 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
62 async def test_cache_is_not_used_when_no_cache(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
63 """Cache is skipped when 'pubsub_cache_strategy' is set to 'no_cache'"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
64 with host.use_option_and_reload(None, "pubsub_cache_strategy", "no_cache"):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
65 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
66 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
72 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
73 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
74 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
77
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
78 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
79 async def test_no_pubsub_get_when_cache_completed(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
80 """No pubsub get is emitted when items are fully cached"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
81 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
82 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
86 )
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
92 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
93 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
94 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
97 assert not client.pubsub_client.items.called
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
98
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
99 @ed
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
100 async def test_pubsub_get_when_cache_in_progress(self, host, client):
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
101 """Pubsub get is emitted when items are currently being cached"""
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
102 items_ret = defer.Deferred()
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
103 items_ret.callback(([], {}))
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
116 client,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
117 None,
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
118 "urn:xmpp:microblog:0",
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
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
fe9cb52f4a9c tests: pubsub cache tests
Goffi <goffi@goffi.org>
parents:
diff changeset
121 assert client.pubsub_client.items.call_count == 1