Skip to content

Commit

Permalink
[cdd/tests/test_compound/test_openapi_emit_utils.py] Increase test co…
Browse files Browse the repository at this point in the history
…verage via `test_rewrite_fk` ; [cdd/__init__.py] Bump version so coverage badge gets updated
  • Loading branch information
SamuelMarks committed Sep 29, 2023
1 parent 6b011d9 commit 8817003
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cdd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from logging import getLogger as get_logger

__author__ = "Samuel Marks"
__version__ = "0.0.99rc12"
__version__ = "0.0.99rc13"
__description__ = (
"Open API to/fro routes, models, and tests. "
"Convert between docstrings, classes, methods, argparse, pydantic, and SQLalchemy."
Expand Down
11 changes: 6 additions & 5 deletions cdd/compound/openapi/utils/emit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,10 @@ def rewrite_fk(symbol_to_module, column_assign):
Rewrite of the form:
```py
column_name = Column(
TableName0,
ForeignKey("TableName0"),
nullable=True,
)
TableName0,
ForeignKey("TableName0"),
nullable=True,
)
```
To the following, inferring that the primary key field is `id` by resolving the symbol and `ast.parse`ing it:
```py
Expand All @@ -656,7 +656,7 @@ def rewrite_fk(symbol_to_module, column_assign):
:type symbol_to_module: ```Dict[str,str]````
:param column_assign: `column_name = Column()` in SQLalchemy with unresolved foreign key
:type column_assign: ```Assign```d
:type column_assign: ```Assign```
:return: `Assign()` in SQLalchemy with resolved foreign key
:rtype: ```Assign```
Expand Down Expand Up @@ -908,6 +908,7 @@ def sqlalchemy_table_to_class(table_expr_ass):
"generate_create_from_attr_staticmethod",
"generate_repr_method",
"param_to_sqlalchemy_column_call",
"rewrite_fk",
"sqlalchemy_class_to_table",
"sqlalchemy_table_to_class",
"typ2column_type",
Expand Down
69 changes: 69 additions & 0 deletions cdd/tests/mocks/openapi_emit_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
OpenAPI emit_utils
"""

from ast import Assign, Call, Load, Name, Store, keyword

from cdd.shared.ast_utils import set_value

column_fk = Assign(
targets=[Name(id="column_name", ctx=Store())],
value=Call(
func=Name(id="Column", ctx=Load()),
args=[
Name(id="TableName0", ctx=Load()),
Call(
func=Name(id="ForeignKey", ctx=Load()),
args=[set_value("TableName0")],
keywords=[],
),
],
keywords=[keyword(arg="nullable", value=set_value(True))],
),
type_comment=None,
expr=None,
lineno=None,
)

column_fk_gold = Assign(
targets=[Name(id="column_name", ctx=Store())],
value=Call(
func=Name(id="Column", ctx=Load()),
args=[
Name(id="Integer", ctx=Load()),
Call(
func=Name(id="ForeignKey", ctx=Load()),
args=[set_value("table_name0.id")],
keywords=[],
),
],
keywords=[keyword(arg="nullable", value=set_value(True), identifier=None)],
),
type_comment=None,
expr=None,
lineno=None,
)

id_column = Assign(
targets=[Name(id="id", ctx=Store())],
value=Call(
func=Name(id="Column", ctx=Load()),
args=[Name(id="Integer", ctx=Load())],
keywords=[
keyword(arg="primary_key", value=set_value(True)),
keyword(
arg="server_default",
value=Call(
func=Name(id="Identity", ctx=Load()),
args=[],
keywords=[],
),
),
],
),
type_comment=None,
expr=None,
lineno=None,
)

__all__ = ["column_fk", "column_fk_gold", "id_column"]
56 changes: 56 additions & 0 deletions cdd/tests/test_compound/test_openapi_emit_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Tests OpenAPI emit_utils
"""

from copy import deepcopy
from os import mkdir, path
from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import patch

from cdd.compound.openapi.utils.emit_utils import rewrite_fk
from cdd.shared.ast_utils import set_value
from cdd.shared.source_transformer import to_code
from cdd.tests.mocks.openapi_emit_utils import column_fk, column_fk_gold, id_column
from cdd.tests.mocks.sqlalchemy import node_pk_tbl_class
from cdd.tests.utils_for_tests import run_ast_test, unittest_main


class TestOpenApiEmitUtils(TestCase):
"""Tests whether `openapi` can construct a `dict`"""

def test_rewrite_fk(self) -> None:
"""
Tests whether `rewrite_fk` produces `openapi_dict` given `model_paths` and `routes_paths`
"""
sqlalchemy_cls = deepcopy(node_pk_tbl_class)
sqlalchemy_cls.name = "TableName0"
sqlalchemy_cls.body[0].value = set_value("table_name0")
sqlalchemy_cls.body[1:] = [
column_fk,
id_column,
]

with TemporaryDirectory() as temp_dir:
mod_dir = path.join(temp_dir, "table_name0")
mkdir(mod_dir)
init_path = path.join(mod_dir, "__init__{}py".format(path.extsep))
with open(init_path, "wt") as f:
f.write(to_code(sqlalchemy_cls))
with patch(
"cdd.compound.openapi.utils.emit_utils.find_module_filepath",
lambda _, __: init_path,
):
gen_ast = rewrite_fk(
{"TableName0": "table_name0"},
column_fk,
)

run_ast_test(
self,
gen_ast=gen_ast,
gold=column_fk_gold,
)


unittest_main()

0 comments on commit 8817003

Please sign in to comment.