Skip to content

Commit

Permalink
[cdd/tests/mocks/sqlalchemy.py] Add mock for create_from_attr ; [cd…
Browse files Browse the repository at this point in the history
…d/tests/test_sqlalchemy/test_emit_sqlalchemy_utils.py] Add `test_generate_create_from_attr_staticmethod`
  • Loading branch information
SamuelMarks committed Sep 18, 2023
1 parent 65e8311 commit e13def3
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 2 deletions.
1 change: 1 addition & 0 deletions cdd/compound/openapi/utils/emit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,7 @@ def sqlalchemy_table_to_class(table_expr_ass):

__all__ = [
"ensure_has_primary_key",
"generate_create_from_attr_staticmethod",
"generate_repr_method",
"param_to_sqlalchemy_column_call",
"sqlalchemy_class_to_table",
Expand Down
94 changes: 94 additions & 0 deletions cdd/tests/mocks/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
Attribute,
Call,
ClassDef,
Compare,
DictComp,
Expr,
FunctionDef,
IsNot,
Load,
Module,
Name,
Return,
Store,
Tuple,
arguments,
comprehension,
keyword,
)
from copy import deepcopy
from functools import partial
from itertools import chain, islice
from textwrap import indent

Expand Down Expand Up @@ -610,13 +616,101 @@ def __repr__(self):
type_ignores=[],
)

create_from_attr_mock = FunctionDef(
name="create_from_attr",
args=arguments(
posonlyargs=[],
args=[set_arg("record")],
kwonlyargs=[],
kw_defaults=[],
defaults=[],
),
body=[
Expr(
value=set_value(
"\n".join(
map(
partial(indent, prefix=tab * 2),
(
"",
"Construct an instance from an object with identical columns (as attributes) as this `class`/`Table`",
tab * 2,
":return: A new instance made from the input object's attributes",
":rtype: ```foo```",
tab * 2,
),
)
)
)
),
Return(
value=Call(
func=Name(id="foo", ctx=Load()),
args=[],
keywords=[
keyword(
value=DictComp(
key=Name(id="attr", ctx=Load()),
value=Call(
func=Name(id="getattr", ctx=Load()),
args=[
Name(id="node", ctx=Load()),
Name(id="attr", ctx=Load()),
],
keywords=[],
),
generators=[
comprehension(
target=Name(id="attr", ctx=Store()),
iter=Tuple(
elts=[
set_value("id"),
set_value("not_pk_id"),
],
ctx=Load(),
),
ifs=[
Compare(
left=Call(
func=Name(id="getattr", ctx=Load()),
args=[
Name(
id="node",
ctx=Load(),
),
Name(
id="attr",
ctx=Load(),
),
set_value(None),
],
keywords=[],
),
ops=[IsNot()],
comparators=[set_value(None)],
)
],
is_async=0,
)
],
)
)
],
)
),
],
lineno=None,
decorator_list=[Name(id="staticmethod", ctx=Load())],
)

__all__ = [
"config_decl_base_ast",
"config_decl_base_str",
"config_hybrid_ast",
"config_tbl_with_comments_ast",
"config_tbl_with_comments_ast",
"config_tbl_with_comments_str",
"create_from_attr_mock",
"dataset_primary_key_column_assign",
"element_pk_fk_ass",
"empty_with_inferred_pk_column_assign",
Expand Down
5 changes: 4 additions & 1 deletion cdd/tests/test_emit/test_emitters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def test_emitters_root(self) -> None:
EMITTERS,
all_dunder_for_module(
path.dirname(path.dirname(path.dirname(__file__))),
("sqlalchemy_table",),
(
"sqlalchemy_hybrid",
"sqlalchemy_table",
),
),
)

Expand Down
5 changes: 4 additions & 1 deletion cdd/tests/test_parse/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ def test_parsers_root(self) -> None:
PARSERS,
all_dunder_for_module(
path.dirname(path.dirname(path.dirname(__file__))),
("sqlalchemy_table",),
(
"sqlalchemy_hybrid",
"sqlalchemy_table",
),
),
)

Expand Down
20 changes: 20 additions & 0 deletions cdd/tests/test_sqlalchemy/test_emit_sqlalchemy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

from cdd.compound.openapi.utils.emit_utils import (
ensure_has_primary_key,
generate_create_from_attr_staticmethod,
param_to_sqlalchemy_column_call,
sqlalchemy_class_to_table,
sqlalchemy_table_to_class,
Expand All @@ -42,6 +43,7 @@
from cdd.tests.mocks.sqlalchemy import (
config_hybrid_ast,
config_tbl_with_comments_ast,
create_from_attr_mock,
element_pk_fk_ass,
node_fk_call,
node_pk_tbl_ass,
Expand Down Expand Up @@ -107,6 +109,24 @@ def test_ensure_has_primary_key_from_id(self) -> None:
ir["params"]["id"]["doc"] = "[PK] {}".format(ir["params"]["id"]["doc"])
self.assertDictEqual(res, ir)

def test_generate_create_from_attr_staticmethod(self):
"""Tests that `generate_create_from_attr` staticmethod is correctly constructed"""
run_ast_test(
self,
generate_create_from_attr_staticmethod(
OrderedDict(
(
("id", {"doc": "My doc", "typ": "str"}),
("not_pk_id", {"doc": "", "typ": "str"}),
)
),
cls_name="foo",
docstring_format="rest",
),
create_from_attr_mock,
skip_black=True,
)

def test_param_to_sqlalchemy_column_call_when_sql_constraints(self) -> None:
"""Tests that with SQL constraints the SQLalchemy column is correctly generated"""
run_ast_test(
Expand Down

0 comments on commit e13def3

Please sign in to comment.