Coverage for app / backend / src / tests / test_config.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.14.0, created at 2026-05-24 18:25 +0000

1from typing import Any 

2 

3import pytest 

4 

5from couchers.config import CONFIG_OPTIONS, check_config 

6 

7 

8def _complete_config(dev: bool) -> dict[str, Any]: 

9 """Build a config dict with every CONFIG_OPTIONS key populated with a valid, truthy value. 

10 

11 This mirrors what make_config() produces when every env var is set, so check_config() must be 

12 able to run against it without touching any key outside CONFIG_OPTIONS. 

13 """ 

14 cfg: dict[str, Any] = {} 

15 for name, type_, *_ in CONFIG_OPTIONS: 

16 if type_ is bool: 

17 cfg[name] = True 

18 elif type_ is int: 

19 cfg[name] = 1 

20 elif type_ is bytes: 

21 cfg[name] = b"x" 

22 elif isinstance(type_, list): 

23 cfg[name] = type_[0] 

24 else: 

25 cfg[name] = "x" 

26 

27 cfg["DEV"] = dev 

28 if not dev: 

29 # production invariants that aren't satisfiable by a generic truthy value 

30 cfg["BASE_URL"] = "https://example.com" 

31 cfg["ENABLE_EMAIL"] = True 

32 cfg["IN_TEST"] = False 

33 return cfg 

34 

35 

36@pytest.mark.parametrize("dev", [True, False]) 

37def test_check_config_only_references_known_keys(dev): 

38 """check_config() must only access config keys that are declared in CONFIG_OPTIONS. 

39 

40 A reference to a key that was removed from CONFIG_OPTIONS (e.g. a toggle migrated to a feature 

41 flag) would raise KeyError at app boot but is invisible to the rest of the test suite, since 

42 check_config() only runs in app.py's startup path. Exercising it here catches that. 

43 """ 

44 check_config(_complete_config(dev=dev))