comparison tests/unit/test_pubsub-cache.py @ 4285:f1d0cde61af7 default tip @

tests (unit): fix tests + black reformatting.
author Goffi <goffi@goffi.org>
date Sun, 14 Jul 2024 17:42:53 +0200
parents 4b842c1fb686
children
comparison
equal deleted inserted replaced
4284:3a550e9a2b55 4285:f1d0cde61af7
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.get_pubsub_node.return_value = None 34 host.memory.storage.get_pubsub_node.return_value = None
35 pubsub_node = host.memory.storage.set_pubsub_node.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"], "cache_node") as cache_node: 38 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node:
39 await host.plugins["XEP-0060"].get_items( 39 await host.plugins["XEP-0060"].get_items(
40 client, 40 client,
41 None, 41 None,
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.get_pubsub_node.return_value = None 53 host.memory.storage.get_pubsub_node.return_value = None
54 host.memory.storage.set_pubsub_node.return_value = PubsubNode( 54 host.memory.storage.set_pubsub_node.return_value = PubsubNode(sync_state=None)
55 sync_state = None
56 )
57 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node: 55 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node:
58 await host.plugins["XEP-0060"].get_items( 56 await host.plugins["XEP-0060"].get_items(
59 client, 57 client, None, "urn:xmpp:microblog:0", extra={C.KEY_USE_CACHE: False}
60 None,
61 "urn:xmpp:microblog:0",
62 extra = {C.KEY_USE_CACHE: False}
63 ) 58 )
64 assert not cache_node.called 59 assert not cache_node.called
65 60
66 @ed 61 @ed
67 async def test_cache_is_not_used_when_no_cache(self, host, client): 62 async def test_cache_is_not_used_when_no_cache(self, host, client):
69 with host.use_option_and_reload(None, "pubsub_cache_strategy", "no_cache"): 64 with host.use_option_and_reload(None, "pubsub_cache_strategy", "no_cache"):
70 items_ret = defer.Deferred() 65 items_ret = defer.Deferred()
71 items_ret.callback(([], {})) 66 items_ret.callback(([], {}))
72 client.pubsub_client.items = MagicMock(return_value=items_ret) 67 client.pubsub_client.items = MagicMock(return_value=items_ret)
73 host.memory.storage.get_pubsub_node.return_value = None 68 host.memory.storage.get_pubsub_node.return_value = None
74 host.memory.storage.set_pubsub_node.return_value = PubsubNode( 69 host.memory.storage.set_pubsub_node.return_value = PubsubNode(sync_state=None)
75 sync_state = None
76 )
77 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node: 70 with patch.object(host.plugins["PUBSUB_CACHE"], "cache_node") as cache_node:
78 await host.plugins["XEP-0060"].get_items( 71 await host.plugins["XEP-0060"].get_items(
79 client, 72 client,
80 None, 73 None,
81 "urn:xmpp:microblog:0", 74 "urn:xmpp:microblog:0",
82 ) 75 )
83 assert not cache_node.called 76 assert not cache_node.called
84 77
85
86 @ed 78 @ed
87 async def test_no_pubsub_get_when_cache_completed(self, host, client): 79 async def test_no_pubsub_get_when_cache_completed(self, host, client):
88 """No pubsub get is emitted when items are fully cached""" 80 """No pubsub get is emitted when items are fully cached"""
89 items_ret = defer.Deferred() 81 items_ret = defer.Deferred()
90 items_ret.callback(([], {})) 82 items_ret.callback(([], {}))
91 client.pubsub_client.items = MagicMock(return_value=items_ret) 83 client.pubsub_client.items = MagicMock(return_value=items_ret)
92 host.memory.storage.get_pubsub_node.return_value = PubsubNode( 84 host.memory.storage.get_pubsub_node.return_value = PubsubNode(
93 sync_state = SyncState.COMPLETED 85 sync_state=SyncState.COMPLETED
94 ) 86 )
95 with patch.object( 87 with patch.object(
96 host.plugins["PUBSUB_CACHE"], 88 host.plugins["PUBSUB_CACHE"], "get_items_from_cache"
97 "get_items_from_cache"
98 ) as get_items_from_cache: 89 ) as get_items_from_cache:
99 get_items_from_cache.return_value = ([], {}) 90 get_items_from_cache.return_value = ([], {})
100 await host.plugins["XEP-0060"].get_items( 91 await host.plugins["XEP-0060"].get_items(
101 client, 92 client,
102 None, 93 None,
110 """Pubsub get is emitted when items are currently being cached""" 101 """Pubsub get is emitted when items are currently being cached"""
111 items_ret = defer.Deferred() 102 items_ret = defer.Deferred()
112 items_ret.callback(([], {})) 103 items_ret.callback(([], {}))
113 client.pubsub_client.items = MagicMock(return_value=items_ret) 104 client.pubsub_client.items = MagicMock(return_value=items_ret)
114 host.memory.storage.get_pubsub_node.return_value = PubsubNode( 105 host.memory.storage.get_pubsub_node.return_value = PubsubNode(
115 sync_state = SyncState.IN_PROGRESS 106 sync_state=SyncState.IN_PROGRESS
116 ) 107 )
117 with patch.object(host.plugins["PUBSUB_CACHE"], "analyse_node") as analyse_node: 108 with patch.object(host.plugins["PUBSUB_CACHE"], "analyse_node") as analyse_node:
118 analyse_node.return_value = {"to_sync": True} 109 analyse_node.return_value = {"to_sync": True}
119 with patch.object( 110 with patch.object(
120 host.plugins["PUBSUB_CACHE"], 111 host.plugins["PUBSUB_CACHE"], "get_items_from_cache"
121 "get_items_from_cache"
122 ) as get_items_from_cache: 112 ) as get_items_from_cache:
123 get_items_from_cache.return_value = ([], {}) 113 get_items_from_cache.return_value = ([], {})
124 assert client.pubsub_client.items.call_count == 0 114 assert client.pubsub_client.items.call_count == 0
125 await host.plugins["XEP-0060"].get_items( 115 await host.plugins["XEP-0060"].get_items(
126 client, 116 client,