Coverage for tests/output/formatting/test_builtin.py: 100%

25 statements  

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

1from __future__ import annotations 

2 

3from typing import Any 

4from typing import Sequence 

5 

6import pytest 

7from harborapi.models.base import BaseModel 

8 

9from harbor_cli.output.formatting.builtin import bool_str 

10from harbor_cli.output.formatting.builtin import float_str 

11from harbor_cli.output.formatting.builtin import int_str 

12from harbor_cli.output.formatting.builtin import NONE_STR 

13from harbor_cli.output.formatting.builtin import plural_str 

14 

15 

16@pytest.mark.parametrize( 

17 "inp,expected,none_is_false", 

18 [ 

19 (True, "true", False), 

20 (False, "false", False), 

21 (None, NONE_STR, False), 

22 (None, "false", True), 

23 ], 

24) 

25def test_bool_str(inp: bool, expected: str, none_is_false: bool) -> None: 

26 assert bool_str(inp, none_is_false) == expected 

27 

28 

29@pytest.mark.parametrize( 

30 "inp,expected,precision", 

31 [ 

32 (1.0, "1.00", 2), 

33 (1.0, "1.0", 1), 

34 (1.0, "1", 0), 

35 (None, NONE_STR, 2), 

36 ], 

37) 

38def test_float_str(inp: float, expected: str, precision: int) -> None: 

39 assert float_str(inp, precision) == expected 

40 

41 

42@pytest.mark.parametrize( 

43 "inp,expected", 

44 [ 

45 (1, "1"), 

46 (None, NONE_STR), 

47 ], 

48) 

49def test_int_str(inp: int, expected: str) -> None: 

50 assert int_str(inp) == expected 

51 

52 

53class SomeModel(BaseModel): 

54 a: str = "a" 

55 b: int = 1 

56 

57 

58@pytest.mark.parametrize( 

59 "s, seq, expected", 

60 [ 

61 ("item", [], "items"), 

62 ("item", [1], "item"), 

63 ("item", [1, 2], "items"), 

64 ("letter", "", "letters"), 

65 ("letter", "a", "letter"), 

66 ("letter", "ab", "letters"), 

67 ("model", [], "models"), 

68 ("model", [SomeModel()], "model"), 

69 ("model", [SomeModel(), SomeModel()], "models"), 

70 ("vulnerability", [], "vulnerabilities"), 

71 ("vulnerability", ["HIGH"], "vulnerability"), 

72 ("vulnerability", ["HIGH", "LOW"], "vulnerabilities"), 

73 ], 

74) 

75def test_plural_str(s: str, seq: Sequence[Any], expected: str) -> None: 

76 assert plural_str(s, seq) == expected