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

50 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-08-27 18:25 +0000

1from enum import Enum 

2from typing import List 

3 

4import click 

5import pytest 

6 

7from tests.utils import Runner 

8from prisma.cli.utils import EnumChoice 

9 

10# TODO: doesn't look like this file is type checked by mypy 

11 

12 

13def test_no_args_outside_generation_warning(runner: Runner) -> None: 

14 """Running just `prisma` raises an error as it should only be ran by prisma internally""" 

15 result = runner.invoke([]) 

16 assert result.exit_code == 1 

17 assert result.output == ( 

18 'This command is only intended to be invoked internally. ' 

19 'Please run the following instead:\n' 

20 'prisma <command>\n' 

21 'e.g.\n' 

22 'prisma generate\n' 

23 ) 

24 

25 

26def test_invalid_command(runner: Runner) -> None: 

27 """Trying to run an unknown command errors""" 

28 result = runner.invoke(['py', 'unknown']) 

29 assert "Error: No such command 'unknown'" in result.output 

30 

31 

32@pytest.mark.parametrize('args', [['py'], ['py', '--help']]) 

33def test_custom_help(runner: Runner, args: List[str]) -> None: 

34 """Correct program name is output for help message""" 

35 result = runner.invoke(args) 

36 assert 'Usage: prisma py' in result.output 

37 assert 'Custom command line arguments specifically for Prisma Client Python.' in result.output 

38 

39 

40@pytest.mark.parametrize('args', [['-h'], ['--help']]) 

41def test_outputs_custom_commands_info(runner: Runner, args: List[str]) -> None: 

42 """Running `prisma --help` also outputs a message for our help command""" 

43 result = runner.invoke(args) 

44 assert 'Python Commands' in result.output 

45 assert 'For Prisma Client Python commands run prisma py --help' in result.output 

46 

47 

48def test_int_enum_choice() -> None: 

49 """Non-str subclassing enum cannot be used with EnumChoice""" 

50 

51 class MyEnum(int, Enum): 

52 bob = 1 

53 alice = 2 

54 

55 with pytest.raises(TypeError) as exc: 

56 EnumChoice(MyEnum) 

57 

58 assert exc.match('Enum does not subclass `str`') 

59 

60 

61def test_str_enum_choice(runner: Runner) -> None: 

62 """EnumChoice correctly accepts and rejects arguments""" 

63 

64 class MyEnum(str, Enum): 

65 bob = 'bob' 

66 alice = 'alice' 

67 

68 @click.command() 

69 @click.option( 

70 '--argument', 

71 type=EnumChoice(MyEnum), 

72 ) 

73 def cli(argument: str) -> None: 

74 if argument == MyEnum.bob: 

75 click.echo('is bob') 

76 elif argument == MyEnum.alice: 

77 click.echo('is alice') 

78 else: # pragma: no cover 

79 # this should never happen 

80 raise ValueError('Unknown enum value passed.') 

81 

82 result = runner.invoke(['--argument=bob'], cli=cli) 

83 assert result.output == 'is bob\n' 

84 

85 result = runner.invoke(['--argument=alice'], cli=cli) 

86 assert result.output == 'is alice\n' 

87 

88 result = runner.invoke(['--argument=invalid'], cli=cli) 

89 assert "Error: Invalid value for '--argument':" in result.output 

90 assert 'bob' in result.output 

91 assert 'alice' in result.output 

92 assert 'invalid' in result.output