comparison tests/unit/conftest.py @ 3623:495184d00360

tests: unit tests preparation with some fixtures
author Goffi <goffi@goffi.org>
date Mon, 02 Aug 2021 21:52:17 +0200
parents
children f5ba7594cced
comparison
equal deleted inserted replaced
3622:d4cb99e7e838 3623:495184d00360
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 contextlib import contextmanager
20 from unittest.mock import MagicMock, AsyncMock
21 from pytest import fixture
22 from twisted.internet import defer
23 from twisted.words.protocols.jabber import jid
24 from sat.core.sat_main import SAT
25 from sat.tools import async_trigger as trigger
26 from sat.core import xmpp
27
28
29 @fixture(scope="session")
30 def bridge():
31 bridge = AsyncMock()
32 bridge.addSignal = MagicMock()
33 bridge.addMethod = MagicMock()
34 return bridge
35
36
37 @fixture(scope="session")
38 def storage():
39 return AsyncMock()
40
41
42 class MockSAT(SAT):
43
44 def __init__(self, bridge, storage):
45 self._cb_map = {}
46 self._menus = {}
47 self._menus_paths = {}
48 self._test_config = {}
49 self.profiles = {}
50 self.plugins = {}
51 # map for short name to whole namespace,
52 # extended by plugins with registerNamespace
53 self.ns_map = {
54 "x-data": xmpp.NS_X_DATA,
55 "disco#info": xmpp.NS_DISCO_INFO,
56 }
57 self.memory = MagicMock()
58 self.memory.storage = storage
59 self.memory.getConfig.side_effect = self.get_test_config
60
61 self.trigger = trigger.TriggerManager()
62 self.bridge = bridge
63 defer.ensureDeferred(self._postInit())
64 self.common_cache = AsyncMock()
65 self._import_plugins()
66 self._addBaseMenus()
67 self.initialised = defer.Deferred()
68 self.initialised.callback(None)
69
70 def get_test_config(self, section, name, default=None):
71 return self._test_config.get((section or None, name), default)
72
73 def set_test_config(self, section, name, value):
74 self._test_config[(section or None, name)] = value
75
76 def clear_test_config(self):
77 self._test_config.clear()
78
79 @contextmanager
80 def use_option_and_reload(self, section, name, value):
81 self.set_test_config(section, name, value)
82 self.reload_plugins()
83 try:
84 yield self
85 finally:
86 self.clear_test_config()
87 self.reload_plugins()
88
89 def reload_plugins(self):
90 self.plugins.clear()
91 self.trigger._TriggerManager__triggers.clear()
92 self.ns_map = {
93 "x-data": xmpp.NS_X_DATA,
94 "disco#info": xmpp.NS_DISCO_INFO,
95 }
96 self._import_plugins()
97
98 def _init(self):
99 pass
100
101 async def _postInit(self):
102 pass
103
104
105 @fixture(scope="session")
106 def host(bridge, storage):
107 host = MockSAT(bridge=bridge, storage=storage)
108 return host
109
110
111 @fixture
112 def client():
113 client = MagicMock()
114 client.jid = jid.JID("test_user@test.example/123")
115 client.pubsub_service = jid.JID("pubsub.test.example")
116 client.pubsub_client = AsyncMock()
117 return client