Skip to content

Commit a97c743

Browse files
author
Sylvain MARIE
committed
Fixed filters so that they use the public API, and extended the tests so that they take into account the non-custom id. Added new filter has_tags
1 parent 69c73e4 commit a97c743

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

pytest_cases/filters.py

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
22

3-
from .case_info import CaseInfo
3+
from .case_funcs import get_case_id, get_case_tags
44

55

66
class CaseFilter(object):
@@ -41,36 +41,78 @@ def __invert__(self):
4141
)
4242

4343

44-
def has_tag(tag_name):
45-
"""Select cases that have the tag `tag_name`. See `@case(tags=...)` to add tags to a case."""
46-
return CaseFilter(lambda case: tag_name in CaseInfo.get_from(case).tags)
44+
def has_tags(*tag_names # type: str
45+
):
46+
"""
47+
Selects cases that have all tags in `tag_names`. See `@case(tags=...)` to add tags to a case.
48+
49+
:param tag_names:
50+
:return:
51+
"""
52+
53+
def _filter(case):
54+
return len(
55+
set(tag_names) - set(get_case_tags(case))
56+
) == 0
57+
58+
return CaseFilter(_filter)
59+
60+
61+
def has_tag(tag_name # type: str
62+
):
63+
"""
64+
Selects cases that have the tag `tag_name`. See `@case(tags=...)` to add tags to a case.
65+
66+
:param tag_name:
67+
:return:
68+
"""
69+
70+
def _filter(case):
71+
return tag_name in get_case_tags(case)
72+
73+
return CaseFilter(_filter)
4774

4875

49-
def id_has_prefix(prefix):
76+
def id_has_prefix(prefix # type: str
77+
):
5078
"""
51-
Select cases that have a case id prefix `prefix`.
79+
Selects cases that have a case id prefix `prefix`.
5280
5381
Note that this is not the prefix of the whole case function name, but the case id,
5482
possibly overridden with `@case(id=)`
5583
"""
56-
return CaseFilter(lambda case: CaseInfo.get_from(case).id.startswith(prefix))
84+
85+
def _filter(case):
86+
return get_case_id(case).startswith(prefix)
87+
88+
return CaseFilter(_filter)
5789

5890

59-
def id_has_suffix(suffix):
91+
def id_has_suffix(suffix # type: str
92+
):
6093
"""
61-
Select cases that have a case id suffix `suffix`.
94+
Selects cases that have a case id suffix `suffix`.
6295
6396
Note that this is not the suffix of the whole case function name, but the case id,
6497
possibly overridden with `@case(id=)`
6598
"""
66-
return CaseFilter(lambda case: CaseInfo.get_from(case).id.endswith(suffix))
6799

100+
def _filter(case):
101+
return get_case_id(case).endswith(suffix)
68102

69-
def id_match_regex(regex):
103+
return CaseFilter(_filter)
104+
105+
106+
def id_match_regex(regex # type: str
107+
):
70108
"""
71109
Select cases that have a case id matching `regex`.
72110
73111
Note that this is not a match of the whole case function name, but the case id,
74112
possibly overridden with `@case(id=)`
75113
"""
76-
return CaseFilter(lambda case: re.match(regex, CaseInfo.get_from(case).id))
114+
115+
def _filter(case):
116+
return re.match(regex, get_case_id(case))
117+
118+
return CaseFilter(_filter)

pytest_cases/tests/pytest_extension/parametrize_plus/test_filter.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def case_three():
2020
return 3
2121

2222

23-
@case(tags=[A, C], id="dom")
24-
def case_four():
23+
@case(tags=[A, C])
24+
def case_dom():
2525
return 4
2626

2727

@@ -46,6 +46,13 @@ def test_filter_with_and_relation(value):
4646
pass
4747

4848

49+
@parametrize_with_cases(
50+
argnames="value", cases=".", filter=filters.has_tags(B, C)
51+
)
52+
def test_filter_with_two_tags(value):
53+
pass
54+
55+
4956
@parametrize_with_cases(
5057
argnames="value", cases=".", filter=filters.has_tag(B) | filters.has_tag(C)
5158
)
@@ -74,6 +81,7 @@ def test_filter(module_results_dct):
7481
'test_filter_without_tag[tom]',
7582
'test_filter_without_tag[dom]',
7683
'test_filter_with_and_relation[toni]',
84+
'test_filter_with_two_tags[toni]',
7785
'test_filter_with_or_relation[tim]',
7886
'test_filter_with_or_relation[toni]',
7987
'test_filter_with_or_relation[dom]',

0 commit comments

Comments
 (0)