Coverage for /Users/davegaeddert/Developer/dropseed/plain/plain-pytest/plain/pytest/plugin.py: 94%

1 import pytest

2 from plain.runtime import settings as plain_settings

3 from plain.runtime import setup

4 from plain.test.client import Client, RequestFactory

5

6

7 def pytest_configure(config):

8 # Run Plain setup before anything else

9 setup()

10

11

12@pytest.fixture(autouse=True, scope="session")

13 def _allowed_hosts_testserver():

14 # Add testserver to ALLOWED_HOSTS so the test client can make requests

15 plain_settings.ALLOWED_HOSTS = [*plain_settings.ALLOWED_HOSTS, "testserver"]

16

17

18@pytest.fixture

19 def client() -> Client:

20 """A Plain test client instance."""

21 return Client()

22

23

24@pytest.fixture

25 def request_factory() -> RequestFactory:

26 """A Plain RequestFactory instance."""

27 return RequestFactory()

28

29

30@pytest.fixture

31 def settings():

32 class SettingsProxy:

33 def __init__(self):

34 self._original = {}

35

36 def __getattr__(self, name):

37 return getattr(plain_settings, name)

38

39 def __setattr__(self, name, value):

40 if name.startswith("_"):

41 super().__setattr__(name, value)

42 else:

43 if name not in self._original:

44 self._original[name] = getattr(plain_settings, name, None)

45 setattr(plain_settings, name, value)

46

47 def _restore(self):

48 for key, value in self._original.items():

49 setattr(plain_settings, key, value)

50

51 proxy = SettingsProxy()

52 yield proxy

53 proxy._restore()