Skip to content

Commit

Permalink
#150: Add get with filter tests for DB backend
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 4, 2020
1 parent b2af27d commit 1b91217
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
74 changes: 74 additions & 0 deletions test/db/endpoints/test_get_with_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import pytest


class TestDBGetWithFilters:
def test_valid_get_with_filters(
self,
flask_test_app_db,
valid_db_credentials_header,
single_investigation_test_data_db,
):
test_response = flask_test_app_db.get(
'/investigations?where={"TITLE": {"like": "Title for DataGateway API'
' Testing (DB)"}}',
headers=valid_db_credentials_header,
)

assert test_response.json == [single_investigation_test_data_db.to_dict()]

def test_valid_no_results_get_with_filters(
self, flask_test_app_db, valid_db_credentials_header,
):
test_response = flask_test_app_db.get(
'/investigations?where={"TITLE": {"eq": "This filter should cause a 404 for'
'testing purposes..."}}',
headers=valid_db_credentials_header,
)
print(test_response.json)

assert test_response.json == []

@pytest.mark.usefixtures("multiple_investigation_test_data_db")
def test_valid_get_with_filters_distinct(
self, flask_test_app_db, valid_db_credentials_header,
):
test_response = flask_test_app_db.get(
'/investigations?where={"TITLE": {"like": "Title for DataGateway API'
' Testing (DB)"}}&distinct="TITLE"',
headers=valid_db_credentials_header,
)

expected = [
{"TITLE": f"Title for DataGateway API Testing (DB) {i}" for i in range(5)},
]

for title in expected:
assert title in test_response.json

def test_limit_skip_merge_get_with_filters(
self,
flask_test_app_db,
valid_db_credentials_header,
multiple_investigation_test_data_db,
):
skip_value = 1
limit_value = 2

test_response = flask_test_app_db.get(
'/investigations?where={"TITLE": {"like": "Title for DataGateway API'
' Testing (DB)"}}'
f'&skip={skip_value}&limit={limit_value}&order="ID ASC"',
headers=valid_db_credentials_header,
)

# Copy required to ensure data is deleted at the end of the test
investigation_test_data_copy = multiple_investigation_test_data_db.copy()
filtered_investigation_data = []
filter_count = 0
while filter_count < limit_value:
filtered_investigation_data.append(
investigation_test_data_copy.pop(skip_value).to_dict(),
)
filter_count += 1

assert test_response.json == filtered_investigation_data
2 changes: 1 addition & 1 deletion test/icat/endpoints/test_create_icat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from test.icat.test_query import prepare_icat_data_for_assertion


class TestCreateData:
class TestICATCreateData:
def test_valid_create_data(
self, flask_test_app_icat, valid_icat_credentials_header,
):
Expand Down
2 changes: 1 addition & 1 deletion test/icat/endpoints/test_get_with_filters_icat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from test.icat.test_query import prepare_icat_data_for_assertion


class TestGetWithFilters:
class TestICATGetWithFilters:
def test_valid_get_with_filters(
self,
flask_test_app_icat,
Expand Down

0 comments on commit 1b91217

Please sign in to comment.