Skip to content

Commit cdf37a8

Browse files
authored
Melhorando teste de imports (#389)
* Melhorando teste de imports * Atualizando configurações do ruff As que estavam antes foram deprecadas
1 parent 7e7a45f commit cdf37a8

File tree

4 files changed

+190
-59
lines changed

4 files changed

+190
-59
lines changed

brutils/__init__.py

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# flake8: noqa: F401
2-
1+
# CEP Imports
32
from brutils.cep import (
43
format_cep,
54
get_address_from_cep,
@@ -14,6 +13,8 @@
1413
from brutils.cep import (
1514
remove_symbols as remove_symbols_cep,
1615
)
16+
17+
# CNPJ Imports
1718
from brutils.cnpj import (
1819
format_cnpj,
1920
)
@@ -26,6 +27,8 @@
2627
from brutils.cnpj import (
2728
remove_symbols as remove_symbols_cnpj,
2829
)
30+
31+
# CPF Imports
2932
from brutils.cpf import (
3033
format_cpf,
3134
)
@@ -38,7 +41,11 @@
3841
from brutils.cpf import (
3942
remove_symbols as remove_symbols_cpf,
4043
)
44+
45+
# Email Import
4146
from brutils.email import is_valid as is_valid_email
47+
48+
# Legal Process Imports
4249
from brutils.legal_process import (
4350
format_legal_process,
4451
)
@@ -51,6 +58,8 @@
5158
from brutils.legal_process import (
5259
remove_symbols as remove_symbols_legal_process,
5360
)
61+
62+
# License Plate Imports
5463
from brutils.license_plate import (
5564
convert_to_mercosul as convert_license_plate_to_mercosul,
5665
)
@@ -69,6 +78,8 @@
6978
from brutils.license_plate import (
7079
remove_symbols as remove_symbols_license_plate,
7180
)
81+
82+
# Phone Imports
7283
from brutils.phone import (
7384
format_phone,
7485
remove_international_dialing_code,
@@ -80,6 +91,8 @@
8091
from brutils.phone import (
8192
is_valid as is_valid_phone,
8293
)
94+
95+
# PIS Imports
8396
from brutils.pis import (
8497
format_pis,
8598
)
@@ -92,6 +105,8 @@
92105
from brutils.pis import (
93106
remove_symbols as remove_symbols_pis,
94107
)
108+
109+
# Voter ID Imports
95110
from brutils.voter_id import (
96111
format_voter_id,
97112
)
@@ -101,3 +116,53 @@
101116
from brutils.voter_id import (
102117
is_valid as is_valid_voter_id,
103118
)
119+
120+
# Defining __all__ to expose the public methods
121+
__all__ = [
122+
# CEP
123+
"format_cep",
124+
"get_address_from_cep",
125+
"get_cep_information_from_address",
126+
"generate_cep",
127+
"is_valid_cep",
128+
"remove_symbols_cep",
129+
# CNPJ
130+
"format_cnpj",
131+
"generate_cnpj",
132+
"is_valid_cnpj",
133+
"remove_symbols_cnpj",
134+
# CPF
135+
"format_cpf",
136+
"generate_cpf",
137+
"is_valid_cpf",
138+
"remove_symbols_cpf",
139+
# Email
140+
"is_valid_email",
141+
# Legal Process
142+
"format_legal_process",
143+
"generate_legal_process",
144+
"is_valid_legal_process",
145+
"remove_symbols_legal_process",
146+
# License Plate
147+
"convert_license_plate_to_mercosul",
148+
"format_license_plate",
149+
"generate_license_plate",
150+
"get_format_license_plate",
151+
"is_valid_license_plate",
152+
"remove_symbols_license_plate",
153+
# Phone
154+
"format_phone",
155+
"remove_international_dialing_code",
156+
"remove_symbols_phone",
157+
"generate_phone",
158+
"is_valid_phone",
159+
# PIS
160+
"format_pis",
161+
"generate_pis",
162+
"is_valid_pis",
163+
"remove_symbols_pis",
164+
# Voter ID
165+
"format_voter_id",
166+
"generate_voter_id",
167+
"is_valid_voter_id",
168+
]

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ ruff = ">=0.5.0,<0.7.0"
3636

3737
[tool.ruff]
3838
line-length = 80
39-
extend-select = ["I"]
39+
lint.extend-select = ["I"]
4040

41-
[tool.ruff.per-file-ignores]
41+
[tool.ruff.lint.per-file-ignores]
4242
"__init__.py" = ["F401"]
4343

4444
[build-system]

tests/test_import.py

Lines changed: 0 additions & 55 deletions
This file was deleted.

tests/test_imports.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import importlib
2+
import inspect
3+
import pkgutil
4+
import unittest
5+
6+
7+
def get_public_functions(module):
8+
"""Get all public functions and methods in the module."""
9+
return [
10+
name
11+
for name, obj in inspect.getmembers(module, inspect.isfunction)
12+
if not is_private_function(name) and inspect.getmodule(obj) == module
13+
] + [
14+
name
15+
for name, obj in inspect.getmembers(module, inspect.ismethod)
16+
if obj.__module__ == module.__name__ and not name.startswith("_")
17+
]
18+
19+
20+
def get_imported_methods(module):
21+
"""Get all names in the module's namespace."""
22+
return [
23+
name
24+
for name in dir(module)
25+
if not is_private_function(name) and not is_standard_function(name)
26+
]
27+
28+
29+
def is_private_function(name):
30+
"""Check if a function is private."""
31+
return name.startswith("_")
32+
33+
34+
def is_standard_function(name):
35+
"""Check if a function name is a standard or built-in function."""
36+
return name in dir(__builtins__) or (
37+
name.startswith("__") and name.endswith("__")
38+
)
39+
40+
41+
class TestImports(unittest.TestCase):
42+
@classmethod
43+
def setUpClass(cls):
44+
"""Set up the package and its __init__.py module."""
45+
cls.package_name = "brutils"
46+
cls.package = importlib.import_module(cls.package_name)
47+
cls.init_module = importlib.import_module(
48+
f"{cls.package_name}.__init__"
49+
)
50+
51+
cls.all_public_methods = []
52+
cls.imported_methods = []
53+
54+
# Iterate over all submodules and collect their public methods
55+
for _, module_name, _ in pkgutil.walk_packages(
56+
cls.package.__path__, cls.package.__name__ + "."
57+
):
58+
module = importlib.import_module(module_name)
59+
cls.all_public_methods.extend(get_public_functions(module))
60+
61+
# Collect imported methods from __init__.py
62+
cls.imported_methods = get_imported_methods(cls.init_module)
63+
64+
# Filter out standard or built-in functions
65+
cls.filtered_public_methods = [
66+
method
67+
for method in cls.all_public_methods
68+
if not is_standard_function(method)
69+
]
70+
cls.filtered_imported_methods = [
71+
method
72+
for method in cls.imported_methods
73+
if not is_standard_function(method)
74+
]
75+
76+
# Remove specific old methods
77+
cls.filtered_public_methods = [
78+
method
79+
for method in cls.filtered_public_methods
80+
if method not in {"sieve", "display", "validate"}
81+
]
82+
83+
# Ensure all public methods are included in __all__
84+
cls.all_defined_names = dir(cls.init_module)
85+
cls.public_methods_in_all = getattr(cls.init_module, "__all__", [])
86+
87+
cls.missing_imports = [
88+
method
89+
for method in cls.filtered_public_methods
90+
if method not in cls.filtered_imported_methods
91+
]
92+
93+
def test_public_methods_in_imports(self):
94+
"""Test that all public methods are imported or aliased."""
95+
aliases_imports = [
96+
method
97+
for method in self.filtered_imported_methods
98+
if method not in self.filtered_public_methods
99+
]
100+
101+
diff = len(self.missing_imports) - len(aliases_imports)
102+
103+
if diff != 0:
104+
self.fail(
105+
f"{diff} public method(s) missing from imports at __init__.py. You need to import the new brutils features methods inside the brutils/__init__.py file"
106+
)
107+
108+
def test_public_methods_in_all(self):
109+
"""Test that all public methods are included in __all__."""
110+
missing_in_all = set(self.filtered_imported_methods) - set(
111+
self.public_methods_in_all
112+
)
113+
114+
if missing_in_all:
115+
self.fail(
116+
f"Public method(s) missing from __all__: {missing_in_all}. You need to add the new brutils features methods names to the list __all__ in the brutils/__init__.py file"
117+
)
118+
119+
120+
if __name__ == "__main__":
121+
unittest.main()

0 commit comments

Comments
 (0)