Mercurial > libervia-backend
comparison tests/unit/test_pubsub-cache.py @ 4037:524856bd7b19
massive refactoring to switch from camelCase to snake_case:
historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a
pre-PEP8 code, to use the same coding style as in Twisted.
However, snake_case is more readable and it's better to follow PEP8 best practices, so it
has been decided to move on full snake_case. Because Libervia has a huge codebase, this
ended with a ugly mix of camelCase and snake_case.
To fix that, this patch does a big refactoring by renaming every function and method
(including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case.
This is a massive change, and may result in some bugs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 08 Apr 2023 13:54:42 +0200 |
parents | fe9cb52f4a9c |
children | 4b842c1fb686 |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
25 | 25 |
26 class TestPubsubCache: | 26 class TestPubsubCache: |
27 | 27 |
28 @ed | 28 @ed |
29 async def test_cache_is_used_transparently(self, host, client): | 29 async def test_cache_is_used_transparently(self, host, client): |
30 """Cache is used when a pubsub getItems operation is done""" | 30 """Cache is used when a pubsub get_items operation is done""" |
31 items_ret = defer.Deferred() | 31 items_ret = defer.Deferred() |
32 items_ret.callback(([], {})) | 32 items_ret.callback(([], {})) |
33 client.pubsub_client.items = MagicMock(return_value=items_ret) | 33 client.pubsub_client.items = MagicMock(return_value=items_ret) |
34 host.memory.storage.getPubsubNode.return_value = None | 34 host.memory.storage.get_pubsub_node.return_value = None |
35 pubsub_node = host.memory.storage.setPubsubNode.return_value = PubsubNode( | 35 pubsub_node = host.memory.storage.set_pubsub_node.return_value = PubsubNode( |
36 sync_state = None | 36 sync_state = None |
37 ) | 37 ) |
38 with patch.object(host.plugins["PUBSUB_CACHE"], "cacheNode") as cacheNode: | 38 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node: |
39 await host.plugins["XEP-0060"].getItems( | 39 await host.plugins["XEP-0060"].get_items( |
40 client, | 40 client, |
41 None, | 41 None, |
42 "urn:xmpp:microblog:0", | 42 "urn:xmpp:microblog:0", |
43 ) | 43 ) |
44 assert cacheNode.call_count == 1 | 44 assert cache_node.call_count == 1 |
45 assert cacheNode.call_args.args[-1] == pubsub_node | 45 assert cache_node.call_args.args[-1] == pubsub_node |
46 | 46 |
47 @ed | 47 @ed |
48 async def test_cache_is_skipped_with_use_cache_false(self, host, client): | 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""" | 49 """Cache is skipped when 'use_cache' extra field is False""" |
50 items_ret = defer.Deferred() | 50 items_ret = defer.Deferred() |
51 items_ret.callback(([], {})) | 51 items_ret.callback(([], {})) |
52 client.pubsub_client.items = MagicMock(return_value=items_ret) | 52 client.pubsub_client.items = MagicMock(return_value=items_ret) |
53 host.memory.storage.getPubsubNode.return_value = None | 53 host.memory.storage.get_pubsub_node.return_value = None |
54 host.memory.storage.setPubsubNode.return_value = PubsubNode( | 54 host.memory.storage.set_pubsub_node.return_value = PubsubNode( |
55 sync_state = None | 55 sync_state = None |
56 ) | 56 ) |
57 with patch.object(host.plugins["PUBSUB_CACHE"], "cacheNode") as cacheNode: | 57 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node: |
58 await host.plugins["XEP-0060"].getItems( | 58 await host.plugins["XEP-0060"].get_items( |
59 client, | 59 client, |
60 None, | 60 None, |
61 "urn:xmpp:microblog:0", | 61 "urn:xmpp:microblog:0", |
62 extra = {C.KEY_USE_CACHE: False} | 62 extra = {C.KEY_USE_CACHE: False} |
63 ) | 63 ) |
64 assert not cacheNode.called | 64 assert not cache_node.called |
65 | 65 |
66 @ed | 66 @ed |
67 async def test_cache_is_not_used_when_no_cache(self, host, client): | 67 async def test_cache_is_not_used_when_no_cache(self, host, client): |
68 """Cache is skipped when 'pubsub_cache_strategy' is set to 'no_cache'""" | 68 """Cache is skipped when 'pubsub_cache_strategy' is set to 'no_cache'""" |
69 with host.use_option_and_reload(None, "pubsub_cache_strategy", "no_cache"): | 69 with host.use_option_and_reload(None, "pubsub_cache_strategy", "no_cache"): |
70 items_ret = defer.Deferred() | 70 items_ret = defer.Deferred() |
71 items_ret.callback(([], {})) | 71 items_ret.callback(([], {})) |
72 client.pubsub_client.items = MagicMock(return_value=items_ret) | 72 client.pubsub_client.items = MagicMock(return_value=items_ret) |
73 host.memory.storage.getPubsubNode.return_value = None | 73 host.memory.storage.get_pubsub_node.return_value = None |
74 host.memory.storage.setPubsubNode.return_value = PubsubNode( | 74 host.memory.storage.set_pubsub_node.return_value = PubsubNode( |
75 sync_state = None | 75 sync_state = None |
76 ) | 76 ) |
77 with patch.object(host.plugins["PUBSUB_CACHE"], "cacheNode") as cacheNode: | 77 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node: |
78 await host.plugins["XEP-0060"].getItems( | 78 await host.plugins["XEP-0060"].get_items( |
79 client, | 79 client, |
80 None, | 80 None, |
81 "urn:xmpp:microblog:0", | 81 "urn:xmpp:microblog:0", |
82 ) | 82 ) |
83 assert not cacheNode.called | 83 assert not cache_node.called |
84 | 84 |
85 | 85 |
86 @ed | 86 @ed |
87 async def test_no_pubsub_get_when_cache_completed(self, host, client): | 87 async def test_no_pubsub_get_when_cache_completed(self, host, client): |
88 """No pubsub get is emitted when items are fully cached""" | 88 """No pubsub get is emitted when items are fully cached""" |
89 items_ret = defer.Deferred() | 89 items_ret = defer.Deferred() |
90 items_ret.callback(([], {})) | 90 items_ret.callback(([], {})) |
91 client.pubsub_client.items = MagicMock(return_value=items_ret) | 91 client.pubsub_client.items = MagicMock(return_value=items_ret) |
92 host.memory.storage.getPubsubNode.return_value = PubsubNode( | 92 host.memory.storage.get_pubsub_node.return_value = PubsubNode( |
93 sync_state = SyncState.COMPLETED | 93 sync_state = SyncState.COMPLETED |
94 ) | 94 ) |
95 with patch.object( | 95 with patch.object( |
96 host.plugins["PUBSUB_CACHE"], | 96 host.plugins["PUBSUB_CACHE"], |
97 "getItemsFromCache" | 97 "get_items_from_cache" |
98 ) as getItemsFromCache: | 98 ) as get_items_from_cache: |
99 getItemsFromCache.return_value = ([], {}) | 99 get_items_from_cache.return_value = ([], {}) |
100 await host.plugins["XEP-0060"].getItems( | 100 await host.plugins["XEP-0060"].get_items( |
101 client, | 101 client, |
102 None, | 102 None, |
103 "urn:xmpp:microblog:0", | 103 "urn:xmpp:microblog:0", |
104 ) | 104 ) |
105 assert getItemsFromCache.call_count == 1 | 105 assert get_items_from_cache.call_count == 1 |
106 assert not client.pubsub_client.items.called | 106 assert not client.pubsub_client.items.called |
107 | 107 |
108 @ed | 108 @ed |
109 async def test_pubsub_get_when_cache_in_progress(self, host, client): | 109 async def test_pubsub_get_when_cache_in_progress(self, host, client): |
110 """Pubsub get is emitted when items are currently being cached""" | 110 """Pubsub get is emitted when items are currently being cached""" |
111 items_ret = defer.Deferred() | 111 items_ret = defer.Deferred() |
112 items_ret.callback(([], {})) | 112 items_ret.callback(([], {})) |
113 client.pubsub_client.items = MagicMock(return_value=items_ret) | 113 client.pubsub_client.items = MagicMock(return_value=items_ret) |
114 host.memory.storage.getPubsubNode.return_value = PubsubNode( | 114 host.memory.storage.get_pubsub_node.return_value = PubsubNode( |
115 sync_state = SyncState.IN_PROGRESS | 115 sync_state = SyncState.IN_PROGRESS |
116 ) | 116 ) |
117 with patch.object(host.plugins["PUBSUB_CACHE"], "analyseNode") as analyseNode: | 117 with patch.object(host.plugins["PUBSUB_CACHE"], "analyse_node") as analyse_node: |
118 analyseNode.return_value = {"to_sync": True} | 118 analyse_node.return_value = {"to_sync": True} |
119 with patch.object( | 119 with patch.object( |
120 host.plugins["PUBSUB_CACHE"], | 120 host.plugins["PUBSUB_CACHE"], |
121 "getItemsFromCache" | 121 "get_items_from_cache" |
122 ) as getItemsFromCache: | 122 ) as get_items_from_cache: |
123 getItemsFromCache.return_value = ([], {}) | 123 get_items_from_cache.return_value = ([], {}) |
124 assert client.pubsub_client.items.call_count == 0 | 124 assert client.pubsub_client.items.call_count == 0 |
125 await host.plugins["XEP-0060"].getItems( | 125 await host.plugins["XEP-0060"].get_items( |
126 client, | 126 client, |
127 None, | 127 None, |
128 "urn:xmpp:microblog:0", | 128 "urn:xmpp:microblog:0", |
129 ) | 129 ) |
130 assert not getItemsFromCache.called | 130 assert not get_items_from_cache.called |
131 assert client.pubsub_client.items.call_count == 1 | 131 assert client.pubsub_client.items.call_count == 1 |