annotate tests/unit/conftest.py @ 3635:7bc443253b7c

merge branche "@"
author Goffi <goffi@goffi.org>
date Sat, 28 Aug 2021 15:32:58 +0200
parents 495184d00360
children f5ba7594cced
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
2
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: an XMPP client
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
5
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
10
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
15
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
18
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from contextlib import contextmanager
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from unittest.mock import MagicMock, AsyncMock
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from pytest import fixture
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.internet import defer
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import jid
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.sat_main import SAT
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.tools import async_trigger as trigger
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.core import xmpp
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
27
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
28
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
29 @fixture(scope="session")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
30 def bridge():
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
31 bridge = AsyncMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
32 bridge.addSignal = MagicMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
33 bridge.addMethod = MagicMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
34 return bridge
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
35
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
36
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
37 @fixture(scope="session")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
38 def storage():
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
39 return AsyncMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
40
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
41
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
42 class MockSAT(SAT):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
43
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def __init__(self, bridge, storage):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self._cb_map = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self._menus = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self._menus_paths = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self._test_config = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.profiles = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
50 self.plugins = {}
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
51 # map for short name to whole namespace,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
52 # extended by plugins with registerNamespace
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self.ns_map = {
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "x-data": xmpp.NS_X_DATA,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "disco#info": xmpp.NS_DISCO_INFO,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
56 }
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.memory = MagicMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
58 self.memory.storage = storage
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.memory.getConfig.side_effect = self.get_test_config
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
60
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.trigger = trigger.TriggerManager()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.bridge = bridge
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
63 defer.ensureDeferred(self._postInit())
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.common_cache = AsyncMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
65 self._import_plugins()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
66 self._addBaseMenus()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.initialised = defer.Deferred()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.initialised.callback(None)
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
69
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def get_test_config(self, section, name, default=None):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
71 return self._test_config.get((section or None, name), default)
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
72
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
73 def set_test_config(self, section, name, value):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
74 self._test_config[(section or None, name)] = value
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
75
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
76 def clear_test_config(self):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
77 self._test_config.clear()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
78
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
79 @contextmanager
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
80 def use_option_and_reload(self, section, name, value):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
81 self.set_test_config(section, name, value)
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
82 self.reload_plugins()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
83 try:
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
84 yield self
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
85 finally:
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.clear_test_config()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
87 self.reload_plugins()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
88
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
89 def reload_plugins(self):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.plugins.clear()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self.trigger._TriggerManager__triggers.clear()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
92 self.ns_map = {
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
93 "x-data": xmpp.NS_X_DATA,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
94 "disco#info": xmpp.NS_DISCO_INFO,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
95 }
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
96 self._import_plugins()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
97
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
98 def _init(self):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
99 pass
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
100
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
101 async def _postInit(self):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
102 pass
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
103
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
104
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
105 @fixture(scope="session")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
106 def host(bridge, storage):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
107 host = MockSAT(bridge=bridge, storage=storage)
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return host
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
109
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
110
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
111 @fixture
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
112 def client():
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
113 client = MagicMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
114 client.jid = jid.JID("test_user@test.example/123")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
115 client.pubsub_service = jid.JID("pubsub.test.example")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
116 client.pubsub_client = AsyncMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
117 return client