annotate tests/unit/conftest.py @ 4326:5fd6a4dc2122

cli (output/std): use `rich` to output JSON.
author Goffi <goffi@goffi.org>
date Wed, 20 Nov 2024 11:38:44 +0100
parents f1d0cde61af7
children 699aa8788d98
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
4073
7c5654c54fed refactoring: rename `core.sat_main` to `core.main`
Goffi <goffi@goffi.org>
parents: 4072
diff changeset
24 from libervia.backend.core.main import LiberviaBackend
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
25 from libervia.backend.tools import async_trigger as trigger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
26 from libervia.backend.core import xmpp
3623
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()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4034
diff changeset
32 bridge.add_signal = MagicMock()
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4034
diff changeset
33 bridge.add_method = MagicMock()
3623
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
4232
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
42 class MockLiberviaBackend(LiberviaBackend):
3623
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 = {}
4285
f1d0cde61af7 tests (unit): fix tests + black reformatting.
Goffi <goffi@goffi.org>
parents: 4232
diff changeset
51 # map for short name to whole namespace,
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4034
diff changeset
52 # extended by plugins with register_namespace
3623
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
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4034
diff changeset
59 self.memory.config_get.side_effect = self.get_test_config
3623
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
3989
f5ba7594cced core (main): log Exception when `bridge_pi` fails
Goffi <goffi@goffi.org>
parents: 3623
diff changeset
63 defer.ensureDeferred(self._post_init())
3623
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()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4034
diff changeset
66 self._add_base_menus()
3623
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
4232
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
79 def register_namespace(self, short_name, namespace):
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
80 # Plugin classes may be instantiated several times in tests, so we do nothing in
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
81 # this method to avoid ConflictError.
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
82 pass
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
83
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
84 @contextmanager
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
85 def use_option_and_reload(self, section, name, value):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.set_test_config(section, name, value)
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 try:
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
89 yield self
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
90 finally:
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self.clear_test_config()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
92 self.reload_plugins()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
93
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
94 def reload_plugins(self):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
95 self.plugins.clear()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
96 self.trigger._TriggerManager__triggers.clear()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
97 self.ns_map = {
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
98 "x-data": xmpp.NS_X_DATA,
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
99 "disco#info": xmpp.NS_DISCO_INFO,
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 self._import_plugins()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
102
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
103 def _init(self):
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
104 pass
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
105
3989
f5ba7594cced core (main): log Exception when `bridge_pi` fails
Goffi <goffi@goffi.org>
parents: 3623
diff changeset
106 async def _post_init(self):
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
107 pass
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
108
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 @fixture(scope="session")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
111 def host(bridge, storage):
4232
0fbe5c605eb6 tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
Goffi <goffi@goffi.org>
parents: 4073
diff changeset
112 host = MockLiberviaBackend(bridge=bridge, storage=storage)
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
113 return host
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
114
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
115
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
116 @fixture
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
117 def client():
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
118 client = MagicMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
119 client.jid = jid.JID("test_user@test.example/123")
4034
9496f28dadff tests (unit): Add test for plugin XEP-0215:
Goffi <goffi@goffi.org>
parents: 3989
diff changeset
120 client.server_jid = jid.JID("test.example")
3623
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
121 client.pubsub_service = jid.JID("pubsub.test.example")
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
122 client.pubsub_client = AsyncMock()
495184d00360 tests: unit tests preparation with some fixtures
Goffi <goffi@goffi.org>
parents:
diff changeset
123 return client