-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cdd/tests/test_compound/test_openapi_emit_utils.py] Increase test co…
…verage via `test_rewrite_fk` ; [cdd/__init__.py] Bump version so coverage badge gets updated
- Loading branch information
1 parent
6b011d9
commit 8817003
Showing
4 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |