Coverage for src/pytest_cases/filters.py: 88%

37 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-03-16 01:28 +0000

1# Authors: Sylvain MARIE <sylvain.marie@se.com> 

2# + All contributors to <https://github.com/smarie/python-pytest-cases> 

3# 

4# License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE> 

5import re 

6 

7from .case_funcs import get_case_id, get_case_tags 

8 

9 

10class CaseFilter(object): 

11 """ 

12 This class represents a case filter. You can use it in order to filter cases to be used by `parametrize_by_cases`. 

13 

14 `CaseFilter` implements logical operations "and" (`&`) "or" (`|`) and "not" (`~`). You can use it to define a 

15 composable filter from any callable receiving a single `case` argument and returning a boolean indicating if the 

16 `case` is selected. 

17 """ 

18 

19 def __init__(self, filter_function): 

20 self.filter_function = filter_function 

21 

22 def __call__(self, case): 

23 return self.filter_function(case) 

24 

25 def __and__(self, other): 

26 return CaseFilter( 

27 lambda case: self(case) and other(case) 

28 ) 

29 

30 def __rand__(self, other): 

31 return self & other 

32 

33 def __or__(self, other): 

34 return CaseFilter( 

35 lambda case: self(case) or other(case) 

36 ) 

37 

38 def __ror__(self, other): 

39 return self | other 

40 

41 def __invert__(self): 

42 return CaseFilter( 

43 lambda case: not self(case) 

44 ) 

45 

46 

47def has_tags(*tag_names # type: str 

48 ): 

49 """ 

50 Selects cases that have all tags in `tag_names`. See `@case(tags=...)` to add tags to a case. 

51 

52 :param tag_names: 

53 :return: 

54 """ 

55 

56 def _filter(case): 

57 return len( 

58 set(tag_names) - set(get_case_tags(case)) 

59 ) == 0 

60 

61 return CaseFilter(_filter) 

62 

63 

64def has_tag(tag_name # type: str 

65 ): 

66 """ 

67 Selects cases that have the tag `tag_name`. See `@case(tags=...)` to add tags to a case. 

68 

69 :param tag_name: 

70 :return: 

71 """ 

72 

73 def _filter(case): 

74 return tag_name in get_case_tags(case) 

75 

76 return CaseFilter(_filter) 

77 

78 

79def id_has_prefix(prefix # type: str 

80 ): 

81 """ 

82 Selects cases that have a case id prefix `prefix`. 

83 

84 Note that this is not the prefix of the whole case function name, but the case id, 

85 possibly overridden with `@case(id=)` 

86 """ 

87 

88 def _filter(case): 

89 return get_case_id(case).startswith(prefix) 

90 

91 return CaseFilter(_filter) 

92 

93 

94def id_has_suffix(suffix # type: str 

95 ): 

96 """ 

97 Selects cases that have a case id suffix `suffix`. 

98 

99 Note that this is not the suffix of the whole case function name, but the case id, 

100 possibly overridden with `@case(id=)` 

101 """ 

102 

103 def _filter(case): 

104 return get_case_id(case).endswith(suffix) 

105 

106 return CaseFilter(_filter) 

107 

108 

109def id_match_regex(regex # type: str 

110 ): 

111 """ 

112 Select cases that have a case id matching `regex`. 

113 

114 Note that this is not a match of the whole case function name, but the case id, 

115 possibly overridden with `@case(id=)` 

116 """ 

117 

118 def _filter(case): 

119 return re.match(regex, get_case_id(case)) 

120 

121 return CaseFilter(_filter)