Coverage for tests/test_cli/test_version.py: 100%
53 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 re
2import json
4from _pytest.monkeypatch import MonkeyPatch
6from tests.utils import Runner
8HASH = re.compile(r'[a-f0-9]{40}')
9PLACEHOLDER = re.compile(r'.*')
10SEMANTIC_VERSION = re.compile(r'(\d?\d\.){2}\d?\da?')
11PATTERN = re.compile(
12 f'prisma : (?P<prisma>{SEMANTIC_VERSION.pattern})\n'
13 f'prisma client python : (?P<prisma_client_python>{SEMANTIC_VERSION.pattern})\n'
14 f'platform : (?P<platform>{PLACEHOLDER.pattern})\n'
15 f'expected engine version : (?P<expected_engine_version>{HASH.pattern})\n'
16 f'installed extras : (?P<installed_extras>{PLACEHOLDER.pattern})\n'
17 f'install path : (?P<install_path>{PLACEHOLDER.pattern})\n'
18 f'binary cache dir : (?P<binary_cache_dir>{PLACEHOLDER.pattern})\n'
19)
22def test_version(runner: Runner) -> None:
23 """Usage with no arguments"""
24 result = runner.invoke(['py', 'version'])
25 assert result.exit_code == 0
26 assert PATTERN.match(result.output) is not None, result.output
29def test_version_json(runner: Runner) -> None:
30 """--json flag produces valid json"""
31 result = runner.invoke(['py', 'version', '--json'])
32 assert result.exit_code == 0
34 data = json.loads(result.output)
35 assert SEMANTIC_VERSION.match(data['prisma'])
36 assert SEMANTIC_VERSION.match(data['prisma-client-python'])
37 assert PLACEHOLDER.match(data['platform'])
38 assert HASH.match(data['expected-engine-version'])
39 assert isinstance(data['installed-extras'], list)
40 assert PLACEHOLDER.match(data['binary-cache-dir'])
43def test_same_output(runner: Runner) -> None:
44 """The same information is output with and without the --json flag"""
45 result = runner.invoke(['py', 'version', '--json'])
46 assert result.exit_code == 0
47 data = json.loads(result.output)
49 result = runner.invoke(['py', 'version'])
50 assert result.exit_code == 0
51 output = result.output
53 match = PATTERN.match(output)
54 assert match is not None
56 # "-" is not a valid character in regex group names
57 groups = {k.replace('_', '-'): v for k, v in match.groupdict().items()}
58 data['installed-extras'] = str(data['installed-extras'])
59 assert groups == data
62def test_no_extras_installed(runner: Runner, monkeypatch: MonkeyPatch) -> None:
63 """Outputs empty list with no extras installed"""
64 from prisma.cli.commands import version
66 def patched_import_module(mod: str) -> None:
67 raise ImportError(mod)
69 monkeypatch.setattr(version, 'import_module', patched_import_module, raising=True)
71 result = runner.invoke(['py', 'version'])
72 assert result.exit_code == 0
74 match = PATTERN.match(result.output)
75 assert match is not None
76 assert match.group('installed_extras') == '[]'
79def test_no_extras_installed_json(runner: Runner, monkeypatch: MonkeyPatch) -> None:
80 """Outputs empty list with no extras installed and --json"""
81 from prisma.cli.commands import version
83 def patched_import_module(mod: str) -> None:
84 raise ImportError(mod)
86 monkeypatch.setattr(version, 'import_module', patched_import_module, raising=True)
88 result = runner.invoke(['py', 'version', '--json'])
89 assert result.exit_code == 0
91 data = json.loads(result.output)
92 assert data['installed-extras'] == []