Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
use a decorator for custom config
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Jul 11, 2019
1 parent 6ac7e50 commit 7880375
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions tests/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ class HomeserverTestCase(TestCase):
* override default_config, to return a modified configuration dictionary for use
by setup_test_homeserver.
* On a per-test basis, you can attach an 'extra_config' attribute, with a dictionary
containing additional configuration settings to be added to the basic config dict.
* On a per-test basis, you can use the @override_config decorator to give a
dictionary containing additional configuration settings to be added to the basic
config dict.
Attributes:
servlets (list[function]): List of servlet registration function.
Expand All @@ -185,9 +186,9 @@ class HomeserverTestCase(TestCase):
def __init__(self, methodName, *args, **kwargs):
super().__init__(methodName, *args, **kwargs)

# see if we have a custom config method for this test
# see if we have any additional config for this test
method = getattr(self, methodName)
self._extra_config = getattr(method, "extra_config", None)
self._extra_config = getattr(method, "_extra_config", None)

def setUp(self):
"""
Expand Down Expand Up @@ -299,8 +300,8 @@ def default_config(self, name="test"):
"""
config = default_config(name)

# apply any additional config which was specified as an extra_config attribute
# on the test method..
# apply any additional config which was specified via the override_config
# decorator.
if self._extra_config is not None:
config.update(self._extra_config)

Expand Down Expand Up @@ -562,3 +563,27 @@ def attempt_wrong_password_login(self, username, password):
)
self.render(request)
self.assertEqual(channel.code, 403, channel.result)


def override_config(extra_config):
"""A decorator which can be applied to test functions to give additional HS config
For use
For example:
class MyTestCase(HomeserverTestCase):
@override_config({"enable_registration": False, ...})
def test_foo(self):
...
Args:
extra_config(dict): Additional config settings to be merged into the default
config dict before instantiating the test homeserver.
"""

def decorator(func):
func._extra_config = extra_config
return func

return decorator

0 comments on commit 7880375

Please sign in to comment.