Coverage for tests/commands/cli/test_find.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-09 12:09 +0100

1from __future__ import annotations 

2 

3import pytest 

4import typer 

5 

6from ..._utils import Parameter 

7from harbor_cli.commands.cli.find import _do_find 

8from harbor_cli.commands.cli.find import MatchStrategy 

9from harbor_cli.models import CommandSummary 

10 

11# Tests for the underlying _do_find() function 

12 

13# TODO: more precise testing of this function 

14def test__do_find(mock_ctx: typer.Context) -> None: 

15 res = _do_find( 

16 mock_ctx, 

17 query=["ldap"], 

18 strategy=MatchStrategy.PARTIAL_RATIO, 

19 limit=None, 

20 min_score=75, 

21 names=True, 

22 descriptions=True, 

23 ) 

24 assert len(res) > 0 

25 assert all(isinstance(r, CommandSummary) for r in res) 

26 

27 

28# Tests for the find command 

29 

30 

31def test_find_no_args(invoke) -> None: 

32 result = invoke(["find"]) 

33 assert result.exit_code == 0 

34 # No argument is help 

35 assert result.output == invoke(["find", "--help"]).output 

36 

37 

38def test_find_query(invoke) -> None: 

39 result = invoke(["find", "ldap"]) 

40 assert result.exit_code == 0 

41 assert result.output != "" 

42 

43 

44@pytest.mark.parametrize( 

45 "limit", 

46 [ 

47 Parameter("--limit", "1"), 

48 Parameter("--limit", "0", ok=False), 

49 Parameter("--limit", "-1", ok=False), 

50 Parameter("--limit", "1000"), 

51 ], 

52) 

53@pytest.mark.parametrize("names", [Parameter("--names"), Parameter("--no-names")]) 

54@pytest.mark.parametrize( 

55 "descriptions", [Parameter("--descriptions"), Parameter("--no-descriptions")] 

56) 

57def test_find_param( 

58 invoke, 

59 output_format_arg: list[str], 

60 limit: Parameter, 

61 names: Parameter, 

62 descriptions: Parameter, 

63) -> None: 

64 result = invoke( 

65 [ 

66 *output_format_arg, 

67 "find", 

68 "ldap", 

69 *limit.as_arg, 

70 *descriptions.as_arg, 

71 *names.as_arg, 

72 ] 

73 ) 

74 if any([not p.ok for p in [limit, names, descriptions]]): 

75 assert result.exit_code != 0, result.output 

76 else: 

77 assert result.exit_code == 0, result.output 

78 

79 

80def test_strategies_exist() -> None: 

81 assert len(list(MatchStrategy)) > 0 

82 

83 

84@pytest.mark.parametrize("strategy", list(MatchStrategy)) 

85def test_find_strategies(invoke, strategy: MatchStrategy) -> None: 

86 # Test all valid strategies 

87 result = invoke(["find", "ldap", "--strategy", strategy.value]) 

88 assert result.exit_code == 0 

89 

90 

91def test_find_invalid_strategy(invoke) -> None: 

92 # Invalid strategy 

93 invalid_strat = "random" 

94 with pytest.raises(ValueError): 

95 MatchStrategy(invalid_strat) # assert this is invalid 

96 result = invoke(["find", "ldap", "--strategy", invalid_strat]) 

97 assert result.exit_code != 0