Coverage for tests/test_config.py: 100%
46 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1import warnings
2from typing import cast
3from pathlib import Path
4from textwrap import dedent
6from mock import MagicMock
7from pytest_mock import MockerFixture
9from prisma.utils import temp_env_update
10from prisma._compat import model_fields, _get_field_env_var
11from prisma._config import Config, LazyConfigProxy
13from .utils import Testdir
16def test_lazy_proxy(mocker: MockerFixture) -> None:
17 """Laxy proxy only instantiates Config once"""
18 proxy = cast(Config, LazyConfigProxy())
20 # ignore deprecation warnings as the mocker implicitly accesses deprecated properties
21 mocked = cast(MagicMock, None)
22 with warnings.catch_warnings():
23 warnings.simplefilter('ignore')
24 mocked = mocker.patch.object(Config, 'load', spec=Config)
26 print(proxy.binary_cache_dir) # implicitly load the config
27 mocked.assert_called_once()
29 for _ in range(10):
30 print(proxy.expected_engine_version)
32 mocked.assert_called_once()
35def test_no_file(testdir: Testdir) -> None:
36 """Config loading works with no pyproject.toml present"""
37 path = Path('pyproject.toml')
38 assert not path.exists()
40 config = Config.load(path)
41 assert isinstance(config.prisma_version, str)
44def test_loading(testdir: Testdir) -> None:
45 """Config loading overrides defaults"""
46 testdir.makefile(
47 '.toml',
48 pyproject=dedent(
49 """
50 [tool.prisma]
51 prisma_version = '0.1.2.3'
52 """
53 ),
54 )
55 config = Config.load()
56 assert config.prisma_version == '0.1.2.3'
58 # updated options are used in computed options
59 assert '0.1.2.3' in str(config.binary_cache_dir)
62def test_allows_extra_keys(testdir: Testdir) -> None:
63 """Config loading allows unknown options to be present in the config file"""
64 testdir.makefile(
65 '.toml',
66 pyproject=dedent(
67 """
68 [tool.prisma]
69 foo = 'bar'
70 prisma_version = '0.3'
71 """
72 ),
73 )
74 config = Config.load()
75 assert config.prisma_version == '0.3'
76 assert not hasattr(config, 'foo')
79def test_env_var_takes_priority(testdir: Testdir) -> None:
80 """Any env variable present should be used instead of the given option"""
81 testdir.makefile(
82 '.toml',
83 pyproject=dedent(
84 """
85 [tool.prisma]
86 prisma_version = '0.3'
87 """
88 ),
89 )
90 config = Config.load()
91 assert config.prisma_version == '0.3'
93 with temp_env_update({'PRISMA_VERSION': '1.5'}):
94 config = Config.load()
95 assert config.prisma_version == '1.5'
98def test_every_option_loads_from_the_environment() -> None:
99 """Every config option must explicitly specify an environment variable to load from
100 to ensure that it can be easily set dynamically
101 """
102 for name, field in model_fields(Config).items():
103 assert (
104 _get_field_env_var(field, name=name) is not None
105 ), f'The {name} option does not specify an environment variable to load from'