Skip to content

Commit

Permalink
Melhorando teste de imports
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamaia committed Sep 12, 2024
1 parent 4947074 commit ee5a896
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 55 deletions.
67 changes: 67 additions & 0 deletions brutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# flake8: noqa: F401

# CEP Imports
from brutils.cep import (
format_cep,
get_address_from_cep,
Expand All @@ -14,6 +15,8 @@
from brutils.cep import (
remove_symbols as remove_symbols_cep,
)

# CNPJ Imports
from brutils.cnpj import (
format_cnpj,
)
Expand All @@ -26,6 +29,8 @@
from brutils.cnpj import (
remove_symbols as remove_symbols_cnpj,
)

# CPF Imports
from brutils.cpf import (
format_cpf,
)
Expand All @@ -38,7 +43,11 @@
from brutils.cpf import (
remove_symbols as remove_symbols_cpf,
)

# Email Import
from brutils.email import is_valid as is_valid_email

# Legal Process Imports
from brutils.legal_process import (
format_legal_process,
)
Expand All @@ -51,6 +60,8 @@
from brutils.legal_process import (
remove_symbols as remove_symbols_legal_process,
)

# License Plate Imports
from brutils.license_plate import (
convert_to_mercosul as convert_license_plate_to_mercosul,
)
Expand All @@ -69,6 +80,8 @@
from brutils.license_plate import (
remove_symbols as remove_symbols_license_plate,
)

# Phone Imports
from brutils.phone import (
format_phone,
remove_international_dialing_code,
Expand All @@ -80,6 +93,8 @@
from brutils.phone import (
is_valid as is_valid_phone,
)

# PIS Imports
from brutils.pis import (
format_pis,
)
Expand All @@ -92,6 +107,8 @@
from brutils.pis import (
remove_symbols as remove_symbols_pis,
)

# Voter ID Imports
from brutils.voter_id import (
format_voter_id,
)
Expand All @@ -101,3 +118,53 @@
from brutils.voter_id import (
is_valid as is_valid_voter_id,
)

# Defining __all__ to expose the public methods
__all__ = [
# CEP
"format_cep",
"get_address_from_cep",
"get_cep_information_from_address",
"generate_cep",
"is_valid_cep",
"remove_symbols_cep",
# CNPJ
"format_cnpj",
"generate_cnpj",
"is_valid_cnpj",
"remove_symbols_cnpj",
# CPF
"format_cpf",
"generate_cpf",
"is_valid_cpf",
"remove_symbols_cpf",
# Email
"is_valid_email",
# Legal Process
"format_legal_process",
"generate_legal_process",
"is_valid_legal_process",
"remove_symbols_legal_process",
# License Plate
"convert_license_plate_to_mercosul",
"format_license_plate",
"generate_license_plate",
"get_format_license_plate",
"is_valid_license_plate",
"remove_symbols_license_plate",
# Phone
"format_phone",
"remove_international_dialing_code",
"remove_symbols_phone",
"generate_phone",
"is_valid_phone",
# PIS
"format_pis",
"generate_pis",
"is_valid_pis",
"remove_symbols_pis",
# Voter ID
"format_voter_id",
"generate_voter_id",
"is_valid_voter_id",
]
55 changes: 0 additions & 55 deletions tests/test_import.py

This file was deleted.

121 changes: 121 additions & 0 deletions tests/test_imports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import importlib
import inspect
import pkgutil
import unittest


def get_public_functions(module):
"""Get all public functions and methods in the module."""
return [
name
for name, obj in inspect.getmembers(module, inspect.isfunction)
if not is_private_function(name) and inspect.getmodule(obj) == module
] + [
name
for name, obj in inspect.getmembers(module, inspect.ismethod)
if obj.__module__ == module.__name__ and not name.startswith("_")
]


def get_imported_methods(module):
"""Get all names in the module's namespace."""
return [
name
for name in dir(module)
if not is_private_function(name) and not is_standard_function(name)
]


def is_private_function(name):
"""Check if a function is private."""
return name.startswith("_")


def is_standard_function(name):
"""Check if a function name is a standard or built-in function."""
return name in dir(__builtins__) or (
name.startswith("__") and name.endswith("__")
)


class TestImports(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""Set up the package and its __init__.py module."""
cls.package_name = "brutils"
cls.package = importlib.import_module(cls.package_name)
cls.init_module = importlib.import_module(
f"{cls.package_name}.__init__"
)

cls.all_public_methods = []
cls.imported_methods = []

# Iterate over all submodules and collect their public methods
for _, module_name, _ in pkgutil.walk_packages(
cls.package.__path__, cls.package.__name__ + "."
):
module = importlib.import_module(module_name)
cls.all_public_methods.extend(get_public_functions(module))

# Collect imported methods from __init__.py
cls.imported_methods = get_imported_methods(cls.init_module)

# Filter out standard or built-in functions
cls.filtered_public_methods = [
method
for method in cls.all_public_methods
if not is_standard_function(method)
]
cls.filtered_imported_methods = [
method
for method in cls.imported_methods
if not is_standard_function(method)
]

# Remove specific old methods
cls.filtered_public_methods = [
method
for method in cls.filtered_public_methods
if method not in {"sieve", "display", "validate"}
]

# Ensure all public methods are included in __all__
cls.all_defined_names = dir(cls.init_module)
cls.public_methods_in_all = getattr(cls.init_module, "__all__", [])

cls.missing_imports = [
method
for method in cls.filtered_public_methods
if method not in cls.filtered_imported_methods
]

def test_public_methods_in_imports(self):
"""Test that all public methods are imported or aliased."""
aliases_imports = [
method
for method in self.filtered_imported_methods
if method not in self.filtered_public_methods
]

diff = len(self.missing_imports) - len(aliases_imports)

if diff != 0:
self.fail(
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"
)

def test_public_methods_in_all(self):
"""Test that all public methods are included in __all__."""
missing_in_all = set(self.filtered_imported_methods) - set(
self.public_methods_in_all
)

if missing_in_all:
self.fail(
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"
)


if __name__ == "__main__":
unittest.main()

0 comments on commit ee5a896

Please sign in to comment.