Skip to content

Commit

Permalink
[cdd/__init__.py] Bump version ; [cdd/tests/{test_ast_utils,test_emit…
Browse files Browse the repository at this point in the history
…/test_emit_sqlalchemy_utils,test_gen}.py] Increase test coverage
  • Loading branch information
SamuelMarks committed Jan 9, 2023
1 parent ee58e7f commit cd15d9a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 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.92"
__version__ = "0.0.93"
__description__ = (
"Open API to/fro routes, models, and tests. "
"Convert between docstrings, classes, methods, argparse, and SQLalchemy."
Expand Down
2 changes: 1 addition & 1 deletion cdd/tests/test_ast_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ def test_infer_imports_with_sqlalchemy(self) -> None:
identifier=None,
identifier_name=None,
),
("Boolean", "Column", "JSON", "String"),
("Boolean", "Column", "Enum", "JSON", "String"),
)
),
level=0,
Expand Down
33 changes: 33 additions & 0 deletions cdd/tests/test_emit/test_emit_sqlalchemy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from cdd.emit.utils.sqlalchemy_utils import (
ensure_has_primary_key,
param_to_sqlalchemy_column_call,
update_args_infer_typ_sqlalchemy,
)
from cdd.tests.mocks.ir import (
intermediate_repr_empty,
Expand Down Expand Up @@ -100,5 +101,37 @@ def test_param_to_sqlalchemy_column_call_when_foreign_key(self) -> None:
gold=node_fk_call,
)

def test_update_args_infer_typ_sqlalchemy_when_simple_array(self) -> None:
"""Tests that SQLalchemy can infer the typ from a simple array"""
args = []
update_args_infer_typ_sqlalchemy(
{"items": {"type": "string"}, "typ": ""}, args, "", False, {}
)
self.assertEqual(len(args), 1)
run_ast_test(
self,
args[0],
gold=Call(
func=Name(id="ARRAY", ctx=Load()),
args=[Name(id="String", ctx=Load())],
keywords=[],
expr=None,
expr_func=None,
),
)

def test_update_args_infer_typ_sqlalchemy_when_simple_union(self) -> None:
"""Tests that SQLalchemy can infer the typ from a simple Union"""
args = []
update_args_infer_typ_sqlalchemy(
{"typ": "Union[string | Small]"}, args, "", False, {}
)
self.assertEqual(len(args), 1)
run_ast_test(
self,
args[0],
gold=Name(id="Small", ctx=Load()),
)


unittest_main()
45 changes: 42 additions & 3 deletions cdd/tests/test_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from shutil import rmtree
from tempfile import mkdtemp
from unittest import TestCase
from unittest.mock import patch
from unittest.mock import MagicMock, patch

import cdd.emit.class_
import cdd.parse.function
Expand All @@ -32,7 +32,7 @@
from cdd.pure_utils import rpartial
from cdd.source_transformer import to_code
from cdd.tests.mocks.methods import function_adder_ast
from cdd.tests.utils_for_tests import run_ast_test
from cdd.tests.utils_for_tests import run_ast_test, unittest_main

method_adder_ast = deepcopy(function_adder_ast)
method_adder_ast.body[0] = Expr(set_value(" C class (mocked!) "))
Expand Down Expand Up @@ -319,8 +319,47 @@ def test_gen_with_imports_from_file_and_prepended_import(self) -> None:
gold=gold,
)

def test_gen_phase_mocked(self) -> None:
"""Tests that different phases are branched to correctly (doesn't test their internals though)"""
with patch(
"cdd.gen.update_with_imports_from_columns", new_callable=MagicMock()
) as phase1_func:
gen(
name_tpl="{name}",
input_mapping="",
parse_name=None,
emit_name="sqlalchemy",
output_filename="",
phase=1,
)
self.assertEqual(phase1_func.call_count, 1)

with patch(
"cdd.gen.update_fk_for_file", new_callable=MagicMock()
) as phase2_func:
gen(
name_tpl="{name}",
input_mapping="",
parse_name=None,
emit_name="sqlalchemy",
output_filename="",
phase=2,
)
self.assertEqual(phase2_func.call_count, 1)

self.assertRaises(
NotImplementedError,
gen,
name_tpl="{name}",
input_mapping="",
parse_name=None,
emit_name="sqlalchemy",
output_filename="",
phase=33,
)


# unittest_main()
unittest_main()
# mock_class = ClassDef(
# name="ClassyB",
# bases=tuple(),
Expand Down

0 comments on commit cd15d9a

Please sign in to comment.