Skip to content

Commit

Permalink
test RelationshipFilter exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
sabard committed Mar 4, 2024
1 parent 4080d20 commit 7d87235
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 19 deletions.
13 changes: 8 additions & 5 deletions graphene_sqlalchemy/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def execute_filters(
field_filter_type = input_field.type
else:
field_filter_type = cls._meta.fields[field].type
# raise Exception

# TODO we need to save the relationship props in the meta fields array
# to conduct joins and alias the joins (in case there are duplicate joins: A->B A->C B->C)
if field == "and":
Expand Down Expand Up @@ -428,7 +428,8 @@ def __init_subclass_with_meta__(
cls, base_type_filter=None, model=None, _meta=None, **options
):
if not base_type_filter:
raise Exception("Relationship Filters must be specific to an object type")
raise TypeError("Relationship Filters must be specific to an object type.")

# Init meta options class if it doesn't exist already
if not _meta:
_meta = InputObjectTypeOptions(cls)
Expand All @@ -440,9 +441,11 @@ def __init_subclass_with_meta__(

# Generate Graphene Fields from the filter functions based on type hints
for field_name, _annotations in filter_functions:
assert (
"val" in _annotations
), "Each filter method must have a value field with valid type annotations"
if "val" not in _annotations:
raise TypeError(
"Each filter method must have a 'val' field with valid type annotations."
)

# If type is generic, replace with actual type of filter class
if is_list(_annotations["val"]):
relationship_filters.update(
Expand Down
30 changes: 29 additions & 1 deletion graphene_sqlalchemy/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from graphene import Connection, relay

from ..fields import SQLAlchemyConnectionField
from ..filters import FloatFilter
from ..filters import FloatFilter, RelationshipFilter
from ..types import ORMField, SQLAlchemyObjectType
from .models import (
Article,
Expand Down Expand Up @@ -1199,3 +1199,31 @@ async def test_additional_filters(session):
schema = graphene.Schema(query=Query)
result = await schema.execute_async(query, context_value={"session": session})
assert_and_raise_result(result, expected)


# Test that exceptions are called correctly
@pytest.mark.asyncio
async def test_filter_relationship_no_base_type(session):
with pytest.raises(
TypeError,
match=r"(.*)Relationship Filters must be specific to an object type.(.*)",
):
RelationshipFilter.create_type(
"InvalidRelationshipFilter", base_type_filter=None, model=Article
)


@pytest.mark.asyncio
async def test_filter_(session):
with pytest.raises(
TypeError,
match=r"(.*)Each filter method must have a 'val' field with valid type annotations.(.*)",
):

class InvalidFilter(FloatFilter):
class Meta:
graphene_type = graphene.Float

@classmethod
def invalid_filter(cls, query, field) -> bool:
return False
22 changes: 9 additions & 13 deletions graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,15 @@ def get_or_create_relationship_filter(
relationship_filter = registry.get_relationship_filter_for_base_type(base_type)

if not relationship_filter:
try:
base_type_filter = registry.get_filter_for_base_type(base_type)
relationship_filter = RelationshipFilter.create_type(
f"{base_type.__name__}RelationshipFilter",
base_type_filter=base_type_filter,
model=base_type._meta.model,
)
registry.register_relationship_filter_for_base_type(
base_type, relationship_filter
)
except Exception as e:
print("e")
raise e
base_type_filter = registry.get_filter_for_base_type(base_type)
relationship_filter = RelationshipFilter.create_type(
f"{base_type.__name__}RelationshipFilter",
base_type_filter=base_type_filter,
model=base_type._meta.model,
)
registry.register_relationship_filter_for_base_type(
base_type, relationship_filter
)

return relationship_filter

Expand Down

0 comments on commit 7d87235

Please sign in to comment.