Skip to content

Commit 7effe2c

Browse files
authored
test: microgen - _should_include_class() and _should_include_method() (#2321)
* updates docstrings for functions that handle filtering * adds tests for functions that handle filtering * refactors tests to use pytest.param()
1 parent 1466881 commit 7effe2c

File tree

2 files changed

+179
-2
lines changed

2 files changed

+179
-2
lines changed

scripts/microgenerator/generate.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,31 @@ def process_structure(
389389

390390

391391
def _should_include_class(class_name: str, class_filters: Dict[str, Any]) -> bool:
392-
"""Checks if a class should be included based on filter criteria."""
392+
"""Determines if a class should be included based on name filters.
393+
394+
Filters are defined in the configuration file and passed in the
395+
`class_filters` dictionary.
396+
397+
Args:
398+
class_name: The name of the class to check.
399+
class_filters: A dictionary containing filter rules:
400+
"include_suffixes": List of suffixes. If provided, the class name
401+
MUST end with one of these suffixes.
402+
"exclude_suffixes": List of suffixes. If provided, the class name
403+
MUST NOT end with any of these suffixes.
404+
405+
Returns:
406+
bool: True if the class should be included, False otherwise.
407+
408+
Example:
409+
>>> filters = {"include_suffixes": ["Client"], "exclude_suffixes": ["BaseClient"]}
410+
>>> _should_include_class("DatasetClient", filters)
411+
True
412+
>>> _should_include_class("BaseClient", filters)
413+
False
414+
>>> _should_include_class("SomeOtherClass", filters)
415+
False
416+
"""
393417
if class_filters.get("include_suffixes"):
394418
if not class_name.endswith(tuple(class_filters["include_suffixes"])):
395419
return False
@@ -400,7 +424,31 @@ def _should_include_class(class_name: str, class_filters: Dict[str, Any]) -> boo
400424

401425

402426
def _should_include_method(method_name: str, method_filters: Dict[str, Any]) -> bool:
403-
"""Checks if a method should be included based on filter criteria."""
427+
"""Determines if a method should be included based on name filters.
428+
429+
Filters are defined in the configuration file and passed in the
430+
`method_filters` dictionary.
431+
432+
Args:
433+
method_name: The name of the method to check.
434+
method_filters: A dictionary containing filter rules:
435+
"include_prefixes": List of prefixes. If provided, the method name
436+
MUST start with one of these prefixes.
437+
"exclude_prefixes": List of prefixes. If provided, the method name
438+
MUST NOT start with any of these prefixes.
439+
440+
Returns:
441+
bool: True if the method should be included, False otherwise.
442+
443+
Example:
444+
>>> filters = {"include_prefixes": ["get_", "list_"], "exclude_prefixes": ["_internal_"]}
445+
>>> _should_include_method("get_dataset", filters)
446+
True
447+
>>> _should_include_method("create_dataset", filters)
448+
False
449+
>>> _should_include_method("_internal_get_dataset", filters)
450+
False
451+
"""
404452
if method_filters.get("include_prefixes"):
405453
if not any(
406454
method_name.startswith(p) for p in method_filters["include_prefixes"]
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import pytest
2+
from scripts.microgenerator.generate import (
3+
_should_include_class,
4+
_should_include_method,
5+
)
6+
7+
8+
# Tests for _should_include_class
9+
@pytest.mark.parametrize(
10+
"class_name, filters, expected",
11+
[
12+
pytest.param("MyClass", {}, True, id="No filters"),
13+
pytest.param(
14+
"DatasetClient",
15+
{"include_suffixes": ["Client", "Service"]},
16+
True,
17+
id="Include suffix match",
18+
),
19+
pytest.param(
20+
"MyClass",
21+
{"include_suffixes": ["Client", "Service"]},
22+
False,
23+
id="Include suffix no match",
24+
),
25+
pytest.param(
26+
"MyBase",
27+
{"exclude_suffixes": ["Base", "Util"]},
28+
False,
29+
id="Exclude suffix match",
30+
),
31+
pytest.param(
32+
"MyClass",
33+
{"exclude_suffixes": ["Base", "Util"]},
34+
True,
35+
id="Exclude suffix no match",
36+
),
37+
pytest.param(
38+
"DatasetClient",
39+
{"include_suffixes": ["Client"], "exclude_suffixes": ["BaseClient"]},
40+
True,
41+
id="Mix include/exclude match",
42+
),
43+
pytest.param(
44+
"BaseClient",
45+
{"include_suffixes": ["Client"], "exclude_suffixes": ["BaseClient"]},
46+
False,
47+
id="Mix include/exclude no match",
48+
),
49+
pytest.param(
50+
"MyClass",
51+
{"include_suffixes": [], "exclude_suffixes": []},
52+
True,
53+
id="Empty filters",
54+
),
55+
],
56+
)
57+
def test_should_include_class(class_name, filters, expected):
58+
assert _should_include_class(class_name, filters) is expected
59+
60+
61+
# Tests for _should_include_method
62+
@pytest.mark.parametrize(
63+
"method_name, filters, expected",
64+
[
65+
pytest.param("my_method", {}, True, id="No filters"),
66+
pytest.param(
67+
"get_dataset",
68+
{"include_prefixes": ["get_", "list_"]},
69+
True,
70+
id="Include prefix match (get)",
71+
),
72+
pytest.param(
73+
"list_jobs",
74+
{"include_prefixes": ["get_", "list_"]},
75+
True,
76+
id="Include prefix match (list)",
77+
),
78+
pytest.param(
79+
"create_dataset",
80+
{"include_prefixes": ["get_", "list_"]},
81+
False,
82+
id="Include prefix no match",
83+
),
84+
pytest.param(
85+
"_private_method",
86+
{"exclude_prefixes": ["_", "internal_"]},
87+
False,
88+
id="Exclude prefix match (private)",
89+
),
90+
pytest.param(
91+
"internal_helper",
92+
{"exclude_prefixes": ["_", "internal_"]},
93+
False,
94+
id="Exclude prefix match (internal)",
95+
),
96+
pytest.param(
97+
"get_dataset",
98+
{"exclude_prefixes": ["_", "internal_"]},
99+
True,
100+
id="Exclude prefix no match",
101+
),
102+
pytest.param(
103+
"get_dataset",
104+
{"include_prefixes": ["get_"], "exclude_prefixes": ["get_internal_"]},
105+
True,
106+
id="Mix include/exclude match",
107+
),
108+
pytest.param(
109+
"get_internal_status",
110+
{"include_prefixes": ["get_"], "exclude_prefixes": ["get_internal_"]},
111+
False,
112+
id="Mix include/exclude no match (exclude wins)",
113+
),
114+
pytest.param(
115+
"list_datasets",
116+
{"include_prefixes": ["get_"], "exclude_prefixes": ["get_internal_"]},
117+
False,
118+
id="Mix include/exclude no match (include fails)",
119+
),
120+
pytest.param(
121+
"my_method",
122+
{"include_prefixes": [], "exclude_prefixes": []},
123+
True,
124+
id="Empty filters",
125+
),
126+
],
127+
)
128+
def test_should_include_method(method_name, filters, expected):
129+
assert _should_include_method(method_name, filters) is expected

0 commit comments

Comments
 (0)