Skip to content

Commit

Permalink
test: add initial tests for NestedWhereFilters #259
Browse files Browse the repository at this point in the history
- I will likely add more tests to this class in the future, particularly to cover a mixture of `SearchAPIWhereFilter` and `NestedWhereFilters`
  • Loading branch information
MRichards99 committed Nov 30, 2021
1 parent 583cbf2 commit 530f024
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions test/search_api/test_nested_where_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import pytest

from datagateway_api.src.search_api.filters import SearchAPIWhereFilter
from datagateway_api.src.search_api.nested_where_filters import NestedWhereFilters


class TestNestedWhereFilters:
@pytest.mark.parametrize(
"lhs, rhs, joining_operator, expected_where_clause",
[
pytest.param("A", "B", "AND", "(A AND B)", id="(A AND B)"),
pytest.param("A", "B", "OR", "(A OR B)", id="(A OR B)"),
pytest.param(
"(A AND B)",
"(C AND D)",
"AND",
"((A AND B) AND (C AND D))",
id="((A AND B) AND (C AND D))",
),
pytest.param(
"(A AND B)",
"(C AND D)",
"OR",
"((A AND B) OR (C AND D))",
id="((A AND B) OR (C AND D))",
),
pytest.param(
"(A OR B)",
"(C OR D)",
"OR",
"((A OR B) OR (C OR D))",
id="((A OR B) OR (C OR D))",
),
pytest.param(
"(A OR B)",
"(C OR D)",
"AND",
"((A OR B) AND (C OR D))",
id="((A OR B) AND (C OR D))",
),
pytest.param(
"(A AND B AND C) OR (C AND D)",
"(E AND F)",
"OR",
"((A AND B AND C) OR (C AND D) OR (E AND F))",
id="((A AND B AND C) OR (C AND D) OR (E AND F))",
),
pytest.param(
"(A AND B AND C) AND (C OR D)",
"(E AND F)",
"OR",
"((A AND B AND C) AND (C OR D) OR (E AND F))",
id="((A AND B AND C) AND (C OR D)) OR (E AND F))",
),
pytest.param(
"((A AND B AND C) AND (C OR D))",
"(E AND F)",
"OR",
"(((A AND B AND C) AND (C OR D)) OR (E AND F))",
id="(((A AND B AND C) AND (C OR D))) OR (E AND F))",
),
],
)
def test_str_filters(self, lhs, rhs, joining_operator, expected_where_clause):
test_nest = NestedWhereFilters(lhs, rhs, joining_operator)

where_clause = str(test_nest)

assert where_clause == expected_where_clause

@pytest.mark.parametrize(
"lhs, rhs, joining_operator, expected_where_clause",
[
pytest.param(
SearchAPIWhereFilter("name", "test name", "eq", "and"),
SearchAPIWhereFilter("id", 3, "eq", "and"),
"OR",
"(o.name = 'test name' OR o.id = '3')",
id="(o.name = 'test name' OR o.id = '3')",
),
],
)
def test_search_api_filters(
self, lhs, rhs, joining_operator, expected_where_clause,
):
test_nest = NestedWhereFilters(lhs, rhs, joining_operator)
where_clause = str(test_nest)
assert where_clause == expected_where_clause

@pytest.mark.parametrize(
"lhs, rhs, joining_operator, expected_where_clause",
[
pytest.param(
NestedWhereFilters("A", "B", "OR"),
NestedWhereFilters("C", "D", "AND"),
"OR",
"((A OR B) OR (C AND D))",
id="((A OR B) OR (C AND D))",
),
],
)
def test_nested_classes(
self, lhs, rhs, joining_operator, expected_where_clause,
):
test_nest = NestedWhereFilters(lhs, rhs, joining_operator)
where_clause = str(test_nest)
assert where_clause == expected_where_clause

0 comments on commit 530f024

Please sign in to comment.