Skip to content

Commit

Permalink
update 1:n and n:m filter tests to use RelationshipFilter syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
sabard committed Aug 2, 2022
1 parent 9aae6c7 commit e655b21
Showing 1 changed file with 104 additions and 2 deletions.
106 changes: 104 additions & 2 deletions graphene_sqlalchemy/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,38 @@ def test_filter_relationship_one_to_many(session):
add_test_data(session)
Query = create_schema(session)

# test contains
query = """
query {
reporter (filters: {
pets: {
name: {in: ["Garfield", "Snoopy"]}
contains: {
name: {in: ["Garfield", "Lassie"]}
}
}
}) {
lastName
}
}
"""
expected = {
"reporter": [{"lastName": "Doe"}, {"lastName": "Roe"}],
}
schema = graphene.Schema(query=Query)
result = schema.execute(query)
assert not result.errors
result = to_std_dicts(result.data)
assert result == expected

# test containsAllOf
query = """
query {
reporter (filters: {
pets: {
containsAllOf: [
name: {eq: "Garfield"},
name: {eq: "Snoopy"},
]
}
}) {
firstName
Expand All @@ -187,6 +214,28 @@ def test_filter_relationship_one_to_many(session):
result = to_std_dicts(result.data)
assert result == expected

# test containsExactly
query = """
query {
reporter (filters: {
pets: {
containsExactly: [
name: {eq: "Garfield"}
]
}
}) {
firstName
}
}
"""
expected = {
"reporter": [],
}
schema = graphene.Schema(query=Query)
result = schema.execute(query)
assert not result.errors
result = to_std_dicts(result.data)
assert result == expected

# Test a n:m relationship
def test_filter_relationship_many_to_many(session):
Expand All @@ -204,10 +253,42 @@ def test_filter_relationship_many_to_many(session):

Query = create_schema(session)

# test contains
query = """
query {
articles (filters: {
tags: {
contains: {
name: { in: ["sensational", "eye-grabbing"] }
}
}
}) {
headline
}
}
"""
expected = {
"articles": [
{"headline": "Woah! Another!"},
{"headline": "Article! Look!"},
],
}
schema = graphene.Schema(query=Query)
result = schema.execute(query)
assert not result.errors
result = to_std_dicts(result.data)
assert result == expected

# test containsAllOf
query = """
query {
articles (filters: {
tags: { name: { in: ["sensational", "eye-grabbing"] } }
tags: {
containsAllOf: [
{ tag: { name: { eq: "eye-grabbing" } } },
{ tag: { name: { eq: "sensational" } } },
]
}
}) {
headline
}
Expand All @@ -222,6 +303,27 @@ def test_filter_relationship_many_to_many(session):
result = to_std_dicts(result.data)
assert result == expected

# test containsExactly
query = """
query {
articles (filters: {
containsExactly: [
{ tag: { name: { eq: "sensational" } } }
]
}) {
headline
}
}
"""
expected = {
"articles": [{"headline": "Article! Look!"}],
}
schema = graphene.Schema(query=Query)
result = schema.execute(query)
assert not result.errors
result = to_std_dicts(result.data)
assert result == expected


# Test connecting filters with "and"
def test_filter_logic_and(session):
Expand Down

0 comments on commit e655b21

Please sign in to comment.