17 lines
851 B
Python
17 lines
851 B
Python
import ast
|
|||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
|
||
|
|
def test_controllers_do_not_access_models_repositories_or_session_operations():
|
||
|
|
for path in Path("app/api/controllers").glob("*.py"):
|
||
|
|
tree = ast.parse(path.read_text(encoding="utf-8"))
|
||
|
|
for node in ast.walk(tree):
|
||
|
|
if isinstance(node, ast.ImportFrom):
|
||
|
|
assert not (node.module or "").startswith(("app.model", "app.repository")), path
|
||
|
|
if node.module == "sqlalchemy":
|
||
|
|
raise AssertionError(f"Controller imports SQL statements: {path}")
|
||
|
|
if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
|
||
|
|
target = node.func.value
|
||
|
|
if isinstance(target, ast.Name) and target.id == "session":
|
||
|
|
raise AssertionError(f"Controller operates on session: {path}:{node.lineno}")
|