Coverage for tests/test_cli/test_version.py: 100%

53 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-28 15:17 +0000

1import re 

2import json 

3 

4from _pytest.monkeypatch import MonkeyPatch 

5 

6from tests.utils import Runner 

7 

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) 

20 

21 

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 

27 

28 

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 

33 

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']) 

41 

42 

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) 

48 

49 result = runner.invoke(['py', 'version']) 

50 assert result.exit_code == 0 

51 output = result.output 

52 

53 match = PATTERN.match(output) 

54 assert match is not None 

55 

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 

60 

61 

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 

65 

66 def patched_import_module(mod: str) -> None: 

67 raise ImportError(mod) 

68 

69 monkeypatch.setattr(version, 'import_module', patched_import_module, raising=True) 

70 

71 result = runner.invoke(['py', 'version']) 

72 assert result.exit_code == 0 

73 

74 match = PATTERN.match(result.output) 

75 assert match is not None 

76 assert match.group('installed_extras') == '[]' 

77 

78 

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 

82 

83 def patched_import_module(mod: str) -> None: 

84 raise ImportError(mod) 

85 

86 monkeypatch.setattr(version, 'import_module', patched_import_module, raising=True) 

87 

88 result = runner.invoke(['py', 'version', '--json']) 

89 assert result.exit_code == 0 

90 

91 data = json.loads(result.output) 

92 assert data['installed-extras'] == []