From 88170031861eeba151f89a39f4f9edfa1db5caef Mon Sep 17 00:00:00 2001 From: Samuel Marks <807580+SamuelMarks@users.noreply.github.com> Date: Thu, 28 Sep 2023 23:10:11 -0400 Subject: [PATCH] [cdd/tests/test_compound/test_openapi_emit_utils.py] Increase test coverage via `test_rewrite_fk` ; [cdd/__init__.py] Bump version so coverage badge gets updated --- cdd/__init__.py | 2 +- cdd/compound/openapi/utils/emit_utils.py | 11 +-- cdd/tests/mocks/openapi_emit_utils.py | 69 +++++++++++++++++++ .../test_compound/test_openapi_emit_utils.py | 56 +++++++++++++++ 4 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 cdd/tests/mocks/openapi_emit_utils.py create mode 100644 cdd/tests/test_compound/test_openapi_emit_utils.py diff --git a/cdd/__init__.py b/cdd/__init__.py index 05be7985..e39fb97d 100644 --- a/cdd/__init__.py +++ b/cdd/__init__.py @@ -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." diff --git a/cdd/compound/openapi/utils/emit_utils.py b/cdd/compound/openapi/utils/emit_utils.py index 64c0032d..40927bc5 100644 --- a/cdd/compound/openapi/utils/emit_utils.py +++ b/cdd/compound/openapi/utils/emit_utils.py @@ -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 @@ -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``` @@ -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", diff --git a/cdd/tests/mocks/openapi_emit_utils.py b/cdd/tests/mocks/openapi_emit_utils.py new file mode 100644 index 00000000..3790e149 --- /dev/null +++ b/cdd/tests/mocks/openapi_emit_utils.py @@ -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"] diff --git a/cdd/tests/test_compound/test_openapi_emit_utils.py b/cdd/tests/test_compound/test_openapi_emit_utils.py new file mode 100644 index 00000000..3b5b509a --- /dev/null +++ b/cdd/tests/test_compound/test_openapi_emit_utils.py @@ -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()