Skip to content

Commit 4b8e2f2

Browse files
models can be now inspected as a package as well (#160)
* models can be now inspected as a package as well * tests altered a bit as new version of libraries give slightly different error messages
1 parent 1fa40a5 commit 4b8e2f2

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

fastapi_contrib/db/utils.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pkgutil
44
import pyclbr
55
import random
6+
import inspect
67

78
from typing import List
89

@@ -83,19 +84,22 @@ def get_models() -> list:
8384
models = []
8485
for app in settings.apps:
8586
app_path = f"{apps_folder_name}/{app}"
86-
modules = [f[1] for f in pkgutil.walk_packages(path=[app_path])]
87-
if "models" in modules:
87+
modules = [f for f in pkgutil.walk_packages(path=[app_path]) if f.name == 'models']
88+
if not modules:
89+
continue
90+
for module in modules:
8891
path_to_models = f"{apps_folder_name}.{app}.models"
89-
try:
90-
module_models = pyclbr.readmodule(path_to_models).keys()
91-
except (AttributeError, ImportError):
92-
logger.warning(
93-
f"Unable to read module attributes in {path_to_models}"
94-
)
95-
continue
96-
mudule = importlib.import_module(
97-
f"{apps_folder_name}.{app}.models"
98-
)
92+
mudule = importlib.import_module(path_to_models)
93+
if module.ispkg:
94+
module_models = [x[0] for x in inspect.getmembers(mudule, inspect.isclass)]
95+
else:
96+
try:
97+
module_models = pyclbr.readmodule(path_to_models).keys()
98+
except (AttributeError, ImportError):
99+
logger.warning(
100+
f"Unable to read module attributes in {path_to_models}"
101+
)
102+
continue
99103
models.extend([getattr(mudule, model) for model in module_models])
100104

101105
return list(filter(lambda x: issubclass(x, MongoDBModel), models))

tests/test_exception_handlers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ def test_exception_handler_when_choice_invalid():
244244
assert response["message"] == "Validation error."
245245
assert response["fields"] == [
246246
{
247-
"message": "One or more values provided are not valid.",
247+
"message": "Value is not a valid enumeration member; permitted: 'a', 'b', 'c'.",
248248
"name": "kind",
249249
"error_code": 400,
250250
}
@@ -262,7 +262,7 @@ def test_exception_handler_when_one_of_multi_choice_invalid():
262262
assert response["message"] == "Validation error."
263263
assert response["fields"] == [
264264
{
265-
"message": "One or more values provided are not valid.",
265+
"message": "Value is not a valid enumeration member; permitted: 'a', 'b', 'c'.",
266266
"name": "multi",
267267
"error_code": 400,
268268
}
@@ -280,7 +280,7 @@ def test_exception_handler_when_choice_default_and_received_invalid():
280280
assert response["message"] == "Validation error."
281281
assert response["fields"] == [
282282
{
283-
"message": "One or more values provided are not valid.",
283+
"message": "Value is not a valid enumeration member; permitted: 'a', 'b', 'c'.",
284284
"name": "kind",
285285
"error_code": 400,
286286
}
@@ -300,7 +300,7 @@ def test_exception_handler_when_choice_default_and_received_invalid():
300300
assert response["message"] == "Validation error."
301301
assert response["fields"] == [
302302
{
303-
"message": "One or more values provided are not valid.",
303+
"message": "Value is not a valid enumeration member; permitted: 'a', 'b', 'c'.",
304304
"name": "kind",
305305
"error_code": 400,
306306
},

0 commit comments

Comments
 (0)