Skip to content

Commit 524fc8c

Browse files
committed
tests: add tests for string fallback removal of hybrid property
chore: change the exception types Signed-off-by: Erik Wrede <erikwrede2@gmail.com>
1 parent 9bf07dc commit 524fc8c

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

graphene_sqlalchemy/converter.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,9 @@ def convert_sqlalchemy_type(
247247
return type_arg
248248

249249
# No valid type found, warn and fall back to graphene.String
250-
raise Exception(
251-
"Don't know how to convert the SQLAlchemy field %s (%s, %s)"
250+
raise TypeError(
251+
"Don't know how to convert the SQLAlchemy field %s (%s, %s). "
252+
"Please add a type converter or set the type manually using ORMField(type_=your_type)"
252253
% (column, column.__class__ or "no column provided", type_arg)
253254
)
254255

@@ -562,6 +563,12 @@ def convert_sqlalchemy_hybrid_property_bare_str(type_arg: str, **kwargs):
562563

563564
def convert_hybrid_property_return_type(hybrid_prop):
564565
# Grab the original method's return type annotations from inside the hybrid property
565-
return_type_annotation = hybrid_prop.fget.__annotations__.get("return", str)
566+
return_type_annotation = hybrid_prop.fget.__annotations__.get("return", None)
567+
if not return_type_annotation:
568+
raise TypeError(
569+
"Cannot convert hybrid property type {} to a valid graphene type. "
570+
"Please make sure to annotate the return type of the hybrid property or use the "
571+
"type_ attribute of ORMField to set the type.".format(hybrid_prop)
572+
)
566573

567574
return convert_sqlalchemy_type(return_type_annotation)

graphene_sqlalchemy/tests/models.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ class Reporter(Base):
8888
favorite_article = relationship("Article", uselist=False, lazy="selectin")
8989

9090
@hybrid_property
91-
def hybrid_prop_with_doc(self):
91+
def hybrid_prop_with_doc(self) -> str:
9292
"""Docstring test"""
9393
return self.first_name
9494

9595
@hybrid_property
96-
def hybrid_prop(self):
96+
def hybrid_prop(self) -> str:
9797
return self.first_name
9898

9999
@hybrid_property
@@ -253,13 +253,6 @@ def hybrid_prop_first_shopping_cart_item(self) -> ShoppingCartItem:
253253
def hybrid_prop_shopping_cart_item_list(self) -> List[ShoppingCartItem]:
254254
return [ShoppingCartItem(id=1), ShoppingCartItem(id=2)]
255255

256-
# Unsupported Type
257-
# fixme move this somewhere else
258-
#
259-
# @hybrid_property
260-
# def hybrid_prop_unsupported_type_tuple(self) -> Tuple[str, str]:
261-
# return "this will actually", "be a string"
262-
263256
# Self-references
264257

265258
@hybrid_property

graphene_sqlalchemy/tests/test_converter.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import enum
22
import sys
3-
from typing import Dict, Union
3+
from typing import Dict, Tuple, Union
44

55
import pytest
66
import sqlalchemy_utils as sqa_utils
@@ -79,6 +79,30 @@ def prop_method() -> int:
7979
assert get_hybrid_property_type(prop_method).type == graphene.Int
8080

8181

82+
def test_hybrid_unknown_annotation():
83+
@hybrid_property
84+
def hybrid_prop(self):
85+
return "This should fail"
86+
87+
with pytest.raises(
88+
TypeError,
89+
match=r"(.*)Please make sure to annotate the return type of the hybrid property or use the "
90+
"type_ attribute of ORMField to set the type.(.*)",
91+
):
92+
get_hybrid_property_type(hybrid_prop)
93+
94+
95+
def test_hybrid_prop_no_type_annotation():
96+
@hybrid_property
97+
def hybrid_prop(self) -> Tuple[str, str]:
98+
return "This should Fail because", "we don't support Tuples in GQL"
99+
100+
with pytest.raises(
101+
TypeError, match=r"(.*)Don't know how to convert the SQLAlchemy field(.*)"
102+
):
103+
get_hybrid_property_type(hybrid_prop)
104+
105+
82106
@pytest.mark.skipif(
83107
sys.version_info < (3, 10), reason="|-Style Unions are unsupported in python < 3.10"
84108
)

0 commit comments

Comments
 (0)