Skip to content

Commit

Permalink
test: add basic valid tests for search API endpoints #266, #267, #268
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Jan 14, 2022
1 parent 1e38eae commit a17fb9a
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 1 deletion.
19 changes: 18 additions & 1 deletion test/search_api/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import json
from unittest.mock import mock_open, patch
from unittest.mock import MagicMock, mock_open, patch

from flask import Flask
import pytest

from datagateway_api.src.api_start_utils import (
create_api_endpoints,
create_app_infrastructure,
)
from datagateway_api.src.common.config import Config
from datagateway_api.src.search_api.panosc_mappings import PaNOSCMappings
from datagateway_api.src.search_api.query import SearchAPIQuery

Expand Down Expand Up @@ -132,3 +138,14 @@ def test_panosc_mappings(test_search_api_mappings_data):
"builtins.open", mock_open(read_data=json.dumps(test_search_api_mappings_data)),
):
return PaNOSCMappings("test/path")


@pytest.fixture(scope="package")
def flask_test_app_search_api(flask_test_app):
search_api_app = Flask(__name__)
search_api_app.config["TESTING"] = True

api, spec = create_app_infrastructure(search_api_app)
create_api_endpoints(search_api_app, api, spec)

yield search_api_app.test_client()
34 changes: 34 additions & 0 deletions test/search_api/endpoints/test_count_dataset_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest

from datagateway_api.src.common.config import Config


class TestSearchAPICountDatasetFilesEndpoint:
@pytest.mark.parametrize(
"pid, request_filter, expected_json",
[
pytest.param(
"0-8401-1070-7",
"{}",
{"count": 56},
id="Basic /datasets/{pid}/files/count request",
),
],
)
def test_valid_count_dataset_files_endpoint(
self, flask_test_app_search_api, pid, request_filter, expected_json,
):
test_response = flask_test_app_search_api.get(
f"{Config.config.search_api.extension}/datasets/{pid}/files/count"
f"?filter={request_filter}",
)

print(test_response)
print(test_response.json)

assert test_response.status_code == 200
assert test_response.json == expected_json

def test_invalid_count_dataset_files_endpoint(self):
# TODO - test for bad filter (where only)
pass
40 changes: 40 additions & 0 deletions test/search_api/endpoints/test_count_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import pytest

from datagateway_api.src.common.config import Config


class TestSearchAPICountEndpoint:
@pytest.mark.parametrize(
"endpoint_name, request_filter, expected_json",
[
pytest.param(
"datasets", "{}", {"count": 479}, id="Basic /datasets/count request",
),
pytest.param(
"documents", "{}", {"count": 239}, id="Basic /documents/count request",
),
pytest.param(
"instruments",
"{}",
{"count": 14},
id="Basic /instruments/count request",
),
],
)
def test_valid_count_endpoint(
self, flask_test_app_search_api, endpoint_name, request_filter, expected_json,
):
test_response = flask_test_app_search_api.get(
f"{Config.config.search_api.extension}/{endpoint_name}/count?filter="
f"{request_filter}",
)

print(test_response)
print(test_response.json)

assert test_response.status_code == 200
assert test_response.json == expected_json

def test_invalid_count_endpoint(self):
# TODO - test for bad filter (where only)
pass
49 changes: 49 additions & 0 deletions test/search_api/endpoints/test_get_dataset_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pytest

from datagateway_api.src.common.config import Config


class TestSearchAPIGetDatasetFilesEndpoint:
@pytest.mark.parametrize(
"pid, request_filter, expected_json",
[
pytest.param(
"0-8401-1070-7",
'{"limit": 2}',
[
{
"id": 1,
"name": "Datafile 1",
"path": "/hit/consumer/red.jpg",
"size": 199643799,
"dataset": None,
},
{
"id": 10060,
"name": "Datafile 10060",
"path": "/the/current/next.jpg",
"size": 124327237,
"dataset": None,
},
],
id="Basic /datasets/{pid}/files request",
),
],
)
def test_valid_get_dataset_files_endpoint(
self, flask_test_app_search_api, pid, request_filter, expected_json,
):
test_response = flask_test_app_search_api.get(
f"{Config.config.search_api.extension}/datasets/{pid}/files"
f"?filter={request_filter}",
)

print(test_response)
print(test_response.json)

assert test_response.status_code == 200
assert test_response.json == expected_json

def test_invalid_get_dataset_files_endpoint(self):
# TODO - test bad filter and bad pid
pass
99 changes: 99 additions & 0 deletions test/search_api/endpoints/test_get_entity_by_pid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import pytest

from datagateway_api.src.common.config import Config


class TestSearchAPIGetByPIDEndpoint:
@pytest.mark.parametrize(
"endpoint_name, pid, request_filter, expected_json",
[
pytest.param(
"datasets",
"0-8401-1070-7",
"{}",
{
"description": "Beat professional blue clear style have. Light"
" final summer. Or hour color maybe word side much team.\nMessage"
" weight official learn especially nature. Himself tax west.",
"modTime": "2006-08-24 01:28:06+00:00",
"modId": "user",
"startDate": "2000-10-13 00:00:00+00:00",
"endDate": "2000-02-10 00:00:00+00:00",
"createId": "user",
"complete": True,
"id": 2,
"name": "DATASET 2",
"doi": "0-8401-1070-7",
"createTime": "2013-04-01 10:56:52+00:00",
"location": "/subject/break.jpeg",
},
id="Basic /datasets/{pid} request",
),
pytest.param(
"documents",
"0-449-78690-0",
"{}",
{
"visitId": "42",
"modId": "user",
"name": "INVESTIGATION 1",
"createId": "user",
"createTime": "2002-11-27 06:20:36+00:00",
"doi": "0-449-78690-0",
"id": 1,
"summary": "Season identify professor happen third. Beat"
" professional blue clear style have. Light final summer.",
"endDate": "2000-07-09 00:00:00+00:00",
"modTime": "2005-04-30 19:41:49+00:00",
"releaseDate": "2000-07-05 00:00:00+00:00",
"startDate": "2000-04-03 00:00:00+00:00",
"title": "Including spend increase ability music skill former."
" Agreement director concern once technology sometimes someone"
" staff.\nSuccess pull bar. Laugh senior example.",
},
id="Basic /documents/{pid} request",
),
pytest.param(
"instruments",
"2",
"{}",
{
"description": "Former outside source play nearly Congress before"
" necessary. Allow want audience test laugh. Economic body person"
" general attorney. Effort weight prevent possible.",
"modId": "user",
"createTime": "2019-02-19 05:57:03+00:00",
"pid": None,
"createId": "user",
"type": "2",
"name": "INSTRUMENT 2",
"modTime": "2019-01-29 23:33:20+00:00",
"id": 2,
"fullName": "With piece reason late model. House office fly."
" International scene call deep answer audience baby true.\n"
"Indicate education across these. Opportunity design too.",
"url": "https://moore.org/",
},
id="Basic /instruments/{pid} request",
),
],
)
def test_valid_get_by_pid_endpoint(
self,
flask_test_app_search_api,
endpoint_name,
pid,
request_filter,
expected_json,
):
test_response = flask_test_app_search_api.get(
f"{Config.config.search_api.extension}/{endpoint_name}/{pid}?filter="
f"{request_filter}",
)

assert test_response.status_code == 200
assert test_response.json == expected_json

def test_invalid_get_by_pid_endpoint(self):
# TODO - test for bad filter and bad/unknown PID
pass
71 changes: 71 additions & 0 deletions test/search_api/endpoints/test_search_endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import pytest

from datagateway_api.src.common.config import Config


class TestSearchAPISearchEndpoint:
@pytest.mark.parametrize(
"endpoint_name, request_filter, expected_json",
[
pytest.param(
"datasets",
'{"limit": 2}',
[
{
"pid": "0-449-78690-0",
"title": "DATASET 1",
"creationDate": "2002-11-27T06:20:36+00:00",
"size": None,
"documents": [],
"techniques": [],
"instrument": None,
"files": [],
"parameters": [],
"samples": [],
},
{
"pid": "0-8401-1070-7",
"title": "DATASET 2",
"creationDate": "2013-04-01T10:56:52+00:00",
"size": None,
"documents": [],
"techniques": [],
"instrument": None,
"files": [],
"parameters": [],
"samples": [],
},
],
id="Basic /datasets request",
),
pytest.param(
"documents", '{"limit": 2}', [{}, {}], id="Basic /documents request",
),
# TODO - test data will need changing once facility has been fixed
pytest.param(
"instruments",
'{"limit": 2}',
[
{"pid": 1, "name": "INSTRUMENT 1", "datasets": None},
{"pid": 2, "name": "INSTRUMENT 2", "datasets": None},
],
id="Basic /instruments request",
),
],
)
def test_valid_search_endpoint(
self, flask_test_app_search_api, endpoint_name, request_filter, expected_json,
):
print(f"RF: {request_filter}")
test_response = flask_test_app_search_api.get(
f"{Config.config.search_api.extension}/{endpoint_name}?filter="
f"{request_filter}",
)
print(test_response)
print(test_response.json)

assert test_response.json == expected_json

def test_invalid_search_endpoint(self):
# TODO - test for bad filters
pass

0 comments on commit a17fb9a

Please sign in to comment.