diff --git a/test/conftest.py b/test/conftest.py index d80c14b1..667a9506 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -1,35 +1,6 @@ -from datetime import datetime, timedelta -import json -from unittest.mock import mock_open, patch - -from flask import Flask -from icat.client import Client from icat.query import Query import pytest -from datagateway_api.src.api_start_utils import ( - create_api_endpoints, - create_app_infrastructure, -) -from datagateway_api.src.common.config import APIConfig, Config -from datagateway_api.src.datagateway_api.database.helpers import ( - delete_row_by_id, - insert_row_into_table, -) -from datagateway_api.src.datagateway_api.database.models import SESSION - - -@pytest.fixture(scope="package") -def icat_client(): - client = Client( - Config.config.datagateway_api.icat_url, - checkCert=Config.config.datagateway_api.icat_check_cert, - ) - client.login( - Config.config.test_mechanism, Config.config.test_user_credentials.dict(), - ) - return client - @pytest.fixture() def icat_query(icat_client): @@ -46,47 +17,6 @@ def invalid_credentials_header(): return {"Authorization": "Test"} -@pytest.fixture(scope="package") -def flask_test_app(): - """This is used to check the endpoints exist and have the correct HTTP methods""" - test_app = Flask(__name__) - api, spec = create_app_infrastructure(test_app) - create_api_endpoints(test_app, api, spec) - - yield test_app - - -@pytest.fixture(scope="package") -def flask_test_app_db(): - """ - This is in the common conftest file because this test app is also used in - non-backend specific tests - """ - db_app = Flask(__name__) - db_app.config["TESTING"] = True - db_app.config["TEST_BACKEND"] = "db" - - api, spec = create_app_infrastructure(db_app) - create_api_endpoints(db_app, api, spec) - db_app.app_context().push() - - yield db_app.test_client() - - -@pytest.fixture() -def valid_db_credentials_header(): - session = SESSION() - session.id = "Test" - session.expireDateTime = datetime.now() + timedelta(hours=1) - session.username = "Test User" - - insert_row_into_table(SESSION, session) - - yield {"Authorization": f"Bearer {session.id}"} - - delete_row_by_id(SESSION, "Test") - - @pytest.fixture() def test_config_data(): return { @@ -118,16 +48,3 @@ def test_config_data(): "test_user_credentials": {"username": "root", "password": "pw"}, "test_mechanism": "simple", } - - -@pytest.fixture() -def test_config(test_config_data): - with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))): - return APIConfig.load("test/path") - - -@pytest.fixture() -def test_config_without_search_api(test_config_data): - del test_config_data["search_api"] - with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))): - return APIConfig.load("test/path") diff --git a/test/datagateway_api/__init__.py b/test/integration/__init__.py similarity index 100% rename from test/datagateway_api/__init__.py rename to test/integration/__init__.py diff --git a/test/integration/conftest.py b/test/integration/conftest.py new file mode 100644 index 00000000..c8ac7fee --- /dev/null +++ b/test/integration/conftest.py @@ -0,0 +1,85 @@ +from datetime import datetime, timedelta +import json +from unittest.mock import mock_open, patch + +from flask import Flask +from icat.client import Client +from icat.query import Query +import pytest + +from datagateway_api.src.api_start_utils import ( + create_api_endpoints, + create_app_infrastructure, +) +from datagateway_api.src.common.config import APIConfig, Config +from datagateway_api.src.datagateway_api.database.helpers import ( + delete_row_by_id, + insert_row_into_table, +) +from datagateway_api.src.datagateway_api.database.models import SESSION + + +@pytest.fixture(scope="package") +def icat_client(): + client = Client( + Config.config.datagateway_api.icat_url, + checkCert=Config.config.datagateway_api.icat_check_cert, + ) + client.login( + Config.config.test_mechanism, Config.config.test_user_credentials.dict(), + ) + return client + + +@pytest.fixture(scope="package") +def flask_test_app(): + """This is used to check the endpoints exist and have the correct HTTP methods""" + test_app = Flask(__name__) + api, spec = create_app_infrastructure(test_app) + create_api_endpoints(test_app, api, spec) + + yield test_app + + +@pytest.fixture(scope="package") +def flask_test_app_db(): + """ + This is in the common conftest file because this test app is also used in + non-backend specific tests + """ + db_app = Flask(__name__) + db_app.config["TESTING"] = True + db_app.config["TEST_BACKEND"] = "db" + + api, spec = create_app_infrastructure(db_app) + create_api_endpoints(db_app, api, spec) + db_app.app_context().push() + + yield db_app.test_client() + + +@pytest.fixture() +def valid_db_credentials_header(): + session = SESSION() + session.id = "Test" + session.expireDateTime = datetime.now() + timedelta(hours=1) + session.username = "Test User" + + insert_row_into_table(SESSION, session) + + yield {"Authorization": f"Bearer {session.id}"} + + delete_row_by_id(SESSION, "Test") + + +@pytest.fixture() +def test_config(test_config_data): + with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))): + return APIConfig.load("test/path") + + +@pytest.fixture() +def test_config_without_search_api(test_config_data): + del test_config_data["search_api"] + with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))): + return APIConfig.load("test/path") diff --git a/test/search_api/__init__.py b/test/integration/datagateway_api/__init__.py similarity index 100% rename from test/search_api/__init__.py rename to test/integration/datagateway_api/__init__.py diff --git a/test/datagateway_api/db/conftest.py b/test/integration/datagateway_api/db/conftest.py similarity index 100% rename from test/datagateway_api/db/conftest.py rename to test/integration/datagateway_api/db/conftest.py diff --git a/test/datagateway_api/db/endpoints/test_count_with_filters_db.py b/test/integration/datagateway_api/db/endpoints/test_count_with_filters_db.py similarity index 100% rename from test/datagateway_api/db/endpoints/test_count_with_filters_db.py rename to test/integration/datagateway_api/db/endpoints/test_count_with_filters_db.py diff --git a/test/datagateway_api/db/endpoints/test_findone_db.py b/test/integration/datagateway_api/db/endpoints/test_findone_db.py similarity index 100% rename from test/datagateway_api/db/endpoints/test_findone_db.py rename to test/integration/datagateway_api/db/endpoints/test_findone_db.py diff --git a/test/datagateway_api/db/endpoints/test_get_by_id_db.py b/test/integration/datagateway_api/db/endpoints/test_get_by_id_db.py similarity index 100% rename from test/datagateway_api/db/endpoints/test_get_by_id_db.py rename to test/integration/datagateway_api/db/endpoints/test_get_by_id_db.py diff --git a/test/datagateway_api/db/endpoints/test_get_with_filters.py b/test/integration/datagateway_api/db/endpoints/test_get_with_filters.py similarity index 100% rename from test/datagateway_api/db/endpoints/test_get_with_filters.py rename to test/integration/datagateway_api/db/endpoints/test_get_with_filters.py diff --git a/test/datagateway_api/db/endpoints/test_ping_db.py b/test/integration/datagateway_api/db/endpoints/test_ping_db.py similarity index 100% rename from test/datagateway_api/db/endpoints/test_ping_db.py rename to test/integration/datagateway_api/db/endpoints/test_ping_db.py diff --git a/test/datagateway_api/db/endpoints/test_table_endpoints_db.py b/test/integration/datagateway_api/db/endpoints/test_table_endpoints_db.py similarity index 100% rename from test/datagateway_api/db/endpoints/test_table_endpoints_db.py rename to test/integration/datagateway_api/db/endpoints/test_table_endpoints_db.py diff --git a/test/datagateway_api/db/test_database_filter_utilities.py b/test/integration/datagateway_api/db/test_database_filter_utilities.py similarity index 100% rename from test/datagateway_api/db/test_database_filter_utilities.py rename to test/integration/datagateway_api/db/test_database_filter_utilities.py diff --git a/test/datagateway_api/db/test_entity_helper.py b/test/integration/datagateway_api/db/test_entity_helper.py similarity index 100% rename from test/datagateway_api/db/test_entity_helper.py rename to test/integration/datagateway_api/db/test_entity_helper.py diff --git a/test/datagateway_api/db/test_requires_session_id.py b/test/integration/datagateway_api/db/test_requires_session_id.py similarity index 100% rename from test/datagateway_api/db/test_requires_session_id.py rename to test/integration/datagateway_api/db/test_requires_session_id.py diff --git a/test/datagateway_api/icat/conftest.py b/test/integration/datagateway_api/icat/conftest.py similarity index 96% rename from test/datagateway_api/icat/conftest.py rename to test/integration/datagateway_api/icat/conftest.py index cd0aac73..18ea25e8 100644 --- a/test/datagateway_api/icat/conftest.py +++ b/test/integration/datagateway_api/icat/conftest.py @@ -11,8 +11,12 @@ create_app_infrastructure, ) from datagateway_api.src.common.config import Config -from test.datagateway_api.icat.endpoints.test_create_icat import TestICATCreateData -from test.datagateway_api.icat.test_query import prepare_icat_data_for_assertion +from test.integration.datagateway_api.icat.endpoints.test_create_icat import ( + TestICATCreateData, +) +from test.integration.datagateway_api.icat.test_query import ( + prepare_icat_data_for_assertion, +) @pytest.fixture() diff --git a/test/datagateway_api/icat/endpoints/test_count_with_filters_icat.py b/test/integration/datagateway_api/icat/endpoints/test_count_with_filters_icat.py similarity index 100% rename from test/datagateway_api/icat/endpoints/test_count_with_filters_icat.py rename to test/integration/datagateway_api/icat/endpoints/test_count_with_filters_icat.py diff --git a/test/datagateway_api/icat/endpoints/test_create_icat.py b/test/integration/datagateway_api/icat/endpoints/test_create_icat.py similarity index 98% rename from test/datagateway_api/icat/endpoints/test_create_icat.py rename to test/integration/datagateway_api/icat/endpoints/test_create_icat.py index 646d4a48..3442759c 100644 --- a/test/datagateway_api/icat/endpoints/test_create_icat.py +++ b/test/integration/datagateway_api/icat/endpoints/test_create_icat.py @@ -1,7 +1,9 @@ import pytest from datagateway_api.src.common.config import Config -from test.datagateway_api.icat.test_query import prepare_icat_data_for_assertion +from test.integration.datagateway_api.icat.test_query import ( + prepare_icat_data_for_assertion, +) class TestICATCreateData: diff --git a/test/datagateway_api/icat/endpoints/test_delete_by_id_icat.py b/test/integration/datagateway_api/icat/endpoints/test_delete_by_id_icat.py similarity index 100% rename from test/datagateway_api/icat/endpoints/test_delete_by_id_icat.py rename to test/integration/datagateway_api/icat/endpoints/test_delete_by_id_icat.py diff --git a/test/datagateway_api/icat/endpoints/test_findone_icat.py b/test/integration/datagateway_api/icat/endpoints/test_findone_icat.py similarity index 92% rename from test/datagateway_api/icat/endpoints/test_findone_icat.py rename to test/integration/datagateway_api/icat/endpoints/test_findone_icat.py index fd57253b..70097c88 100644 --- a/test/datagateway_api/icat/endpoints/test_findone_icat.py +++ b/test/integration/datagateway_api/icat/endpoints/test_findone_icat.py @@ -1,5 +1,7 @@ from datagateway_api.src.common.config import Config -from test.datagateway_api.icat.test_query import prepare_icat_data_for_assertion +from test.integration.datagateway_api.icat.test_query import ( + prepare_icat_data_for_assertion, +) class TestICATFindone: diff --git a/test/datagateway_api/icat/endpoints/test_get_by_id_icat.py b/test/integration/datagateway_api/icat/endpoints/test_get_by_id_icat.py similarity index 95% rename from test/datagateway_api/icat/endpoints/test_get_by_id_icat.py rename to test/integration/datagateway_api/icat/endpoints/test_get_by_id_icat.py index 44cc79d8..cbe16a3b 100644 --- a/test/datagateway_api/icat/endpoints/test_get_by_id_icat.py +++ b/test/integration/datagateway_api/icat/endpoints/test_get_by_id_icat.py @@ -1,5 +1,7 @@ from datagateway_api.src.common.config import Config -from test.datagateway_api.icat.test_query import prepare_icat_data_for_assertion +from test.integration.datagateway_api.icat.test_query import ( + prepare_icat_data_for_assertion, +) class TestICATGetByID: diff --git a/test/datagateway_api/icat/endpoints/test_get_with_filters_icat.py b/test/integration/datagateway_api/icat/endpoints/test_get_with_filters_icat.py similarity index 96% rename from test/datagateway_api/icat/endpoints/test_get_with_filters_icat.py rename to test/integration/datagateway_api/icat/endpoints/test_get_with_filters_icat.py index 9d3a9a2d..a868ec1d 100644 --- a/test/datagateway_api/icat/endpoints/test_get_with_filters_icat.py +++ b/test/integration/datagateway_api/icat/endpoints/test_get_with_filters_icat.py @@ -1,7 +1,9 @@ import pytest from datagateway_api.src.common.config import Config -from test.datagateway_api.icat.test_query import prepare_icat_data_for_assertion +from test.integration.datagateway_api.icat.test_query import ( + prepare_icat_data_for_assertion, +) class TestICATGetWithFilters: diff --git a/test/datagateway_api/icat/endpoints/test_ping_icat.py b/test/integration/datagateway_api/icat/endpoints/test_ping_icat.py similarity index 100% rename from test/datagateway_api/icat/endpoints/test_ping_icat.py rename to test/integration/datagateway_api/icat/endpoints/test_ping_icat.py diff --git a/test/datagateway_api/icat/endpoints/test_table_endpoints_icat.py b/test/integration/datagateway_api/icat/endpoints/test_table_endpoints_icat.py similarity index 97% rename from test/datagateway_api/icat/endpoints/test_table_endpoints_icat.py rename to test/integration/datagateway_api/icat/endpoints/test_table_endpoints_icat.py index 58a1345b..f6e29ccb 100644 --- a/test/datagateway_api/icat/endpoints/test_table_endpoints_icat.py +++ b/test/integration/datagateway_api/icat/endpoints/test_table_endpoints_icat.py @@ -1,5 +1,7 @@ from datagateway_api.src.common.config import Config -from test.datagateway_api.icat.test_query import prepare_icat_data_for_assertion +from test.integration.datagateway_api.icat.test_query import ( + prepare_icat_data_for_assertion, +) class TestICATableEndpoints: diff --git a/test/datagateway_api/icat/endpoints/test_update_by_id_icat.py b/test/integration/datagateway_api/icat/endpoints/test_update_by_id_icat.py similarity index 94% rename from test/datagateway_api/icat/endpoints/test_update_by_id_icat.py rename to test/integration/datagateway_api/icat/endpoints/test_update_by_id_icat.py index a9e52b8f..e9f4af0e 100644 --- a/test/datagateway_api/icat/endpoints/test_update_by_id_icat.py +++ b/test/integration/datagateway_api/icat/endpoints/test_update_by_id_icat.py @@ -1,5 +1,7 @@ from datagateway_api.src.common.config import Config -from test.datagateway_api.icat.test_query import prepare_icat_data_for_assertion +from test.integration.datagateway_api.icat.test_query import ( + prepare_icat_data_for_assertion, +) class TestUpdateByID: diff --git a/test/datagateway_api/icat/endpoints/test_update_multiple_icat.py b/test/integration/datagateway_api/icat/endpoints/test_update_multiple_icat.py similarity index 98% rename from test/datagateway_api/icat/endpoints/test_update_multiple_icat.py rename to test/integration/datagateway_api/icat/endpoints/test_update_multiple_icat.py index 6c7de86b..41f92ef8 100644 --- a/test/datagateway_api/icat/endpoints/test_update_multiple_icat.py +++ b/test/integration/datagateway_api/icat/endpoints/test_update_multiple_icat.py @@ -1,7 +1,9 @@ import pytest from datagateway_api.src.common.config import Config -from test.datagateway_api.icat.test_query import prepare_icat_data_for_assertion +from test.integration.datagateway_api.icat.test_query import ( + prepare_icat_data_for_assertion, +) class TestUpdateMultipleEntities: diff --git a/test/datagateway_api/icat/filters/test_distinct_filter.py b/test/integration/datagateway_api/icat/filters/test_distinct_filter.py similarity index 100% rename from test/datagateway_api/icat/filters/test_distinct_filter.py rename to test/integration/datagateway_api/icat/filters/test_distinct_filter.py diff --git a/test/datagateway_api/icat/filters/test_include_filter.py b/test/integration/datagateway_api/icat/filters/test_include_filter.py similarity index 100% rename from test/datagateway_api/icat/filters/test_include_filter.py rename to test/integration/datagateway_api/icat/filters/test_include_filter.py diff --git a/test/datagateway_api/icat/filters/test_limit_filter.py b/test/integration/datagateway_api/icat/filters/test_limit_filter.py similarity index 100% rename from test/datagateway_api/icat/filters/test_limit_filter.py rename to test/integration/datagateway_api/icat/filters/test_limit_filter.py diff --git a/test/datagateway_api/icat/filters/test_order_filter.py b/test/integration/datagateway_api/icat/filters/test_order_filter.py similarity index 100% rename from test/datagateway_api/icat/filters/test_order_filter.py rename to test/integration/datagateway_api/icat/filters/test_order_filter.py diff --git a/test/datagateway_api/icat/filters/test_skip_filter.py b/test/integration/datagateway_api/icat/filters/test_skip_filter.py similarity index 100% rename from test/datagateway_api/icat/filters/test_skip_filter.py rename to test/integration/datagateway_api/icat/filters/test_skip_filter.py diff --git a/test/datagateway_api/icat/filters/test_where_filter.py b/test/integration/datagateway_api/icat/filters/test_where_filter.py similarity index 100% rename from test/datagateway_api/icat/filters/test_where_filter.py rename to test/integration/datagateway_api/icat/filters/test_where_filter.py diff --git a/test/datagateway_api/icat/test_helpers.py b/test/integration/datagateway_api/icat/test_helpers.py similarity index 100% rename from test/datagateway_api/icat/test_helpers.py rename to test/integration/datagateway_api/icat/test_helpers.py diff --git a/test/datagateway_api/icat/test_icat_client.py b/test/integration/datagateway_api/icat/test_icat_client.py similarity index 100% rename from test/datagateway_api/icat/test_icat_client.py rename to test/integration/datagateway_api/icat/test_icat_client.py diff --git a/test/datagateway_api/icat/test_lru_cache.py b/test/integration/datagateway_api/icat/test_lru_cache.py similarity index 100% rename from test/datagateway_api/icat/test_lru_cache.py rename to test/integration/datagateway_api/icat/test_lru_cache.py diff --git a/test/datagateway_api/icat/test_query.py b/test/integration/datagateway_api/icat/test_query.py similarity index 100% rename from test/datagateway_api/icat/test_query.py rename to test/integration/datagateway_api/icat/test_query.py diff --git a/test/datagateway_api/icat/test_session_handling.py b/test/integration/datagateway_api/icat/test_session_handling.py similarity index 100% rename from test/datagateway_api/icat/test_session_handling.py rename to test/integration/datagateway_api/icat/test_session_handling.py diff --git a/test/datagateway_api/test_backends.py b/test/integration/datagateway_api/test_backends.py similarity index 100% rename from test/datagateway_api/test_backends.py rename to test/integration/datagateway_api/test_backends.py diff --git a/test/datagateway_api/test_query_filter_factory.py b/test/integration/datagateway_api/test_query_filter_factory.py similarity index 100% rename from test/datagateway_api/test_query_filter_factory.py rename to test/integration/datagateway_api/test_query_filter_factory.py diff --git a/test/integration/search_api/__init__.py b/test/integration/search_api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/search_api/conftest.py b/test/integration/search_api/conftest.py similarity index 100% rename from test/search_api/conftest.py rename to test/integration/search_api/conftest.py diff --git a/test/search_api/endpoints/test_count_dataset_files.py b/test/integration/search_api/endpoints/test_count_dataset_files.py similarity index 100% rename from test/search_api/endpoints/test_count_dataset_files.py rename to test/integration/search_api/endpoints/test_count_dataset_files.py diff --git a/test/search_api/endpoints/test_count_endpoint.py b/test/integration/search_api/endpoints/test_count_endpoint.py similarity index 100% rename from test/search_api/endpoints/test_count_endpoint.py rename to test/integration/search_api/endpoints/test_count_endpoint.py diff --git a/test/search_api/endpoints/test_get_dataset_files.py b/test/integration/search_api/endpoints/test_get_dataset_files.py similarity index 100% rename from test/search_api/endpoints/test_get_dataset_files.py rename to test/integration/search_api/endpoints/test_get_dataset_files.py diff --git a/test/search_api/endpoints/test_get_entity_by_pid.py b/test/integration/search_api/endpoints/test_get_entity_by_pid.py similarity index 100% rename from test/search_api/endpoints/test_get_entity_by_pid.py rename to test/integration/search_api/endpoints/test_get_entity_by_pid.py diff --git a/test/search_api/endpoints/test_search_endpoint.py b/test/integration/search_api/endpoints/test_search_endpoint.py similarity index 100% rename from test/search_api/endpoints/test_search_endpoint.py rename to test/integration/search_api/endpoints/test_search_endpoint.py diff --git a/test/search_api/filters/test_search_api_include_filter.py b/test/integration/search_api/filters/test_search_api_include_filter.py similarity index 100% rename from test/search_api/filters/test_search_api_include_filter.py rename to test/integration/search_api/filters/test_search_api_include_filter.py diff --git a/test/search_api/filters/test_search_api_limit_filter.py b/test/integration/search_api/filters/test_search_api_limit_filter.py similarity index 100% rename from test/search_api/filters/test_search_api_limit_filter.py rename to test/integration/search_api/filters/test_search_api_limit_filter.py diff --git a/test/search_api/filters/test_search_api_skip_filter.py b/test/integration/search_api/filters/test_search_api_skip_filter.py similarity index 100% rename from test/search_api/filters/test_search_api_skip_filter.py rename to test/integration/search_api/filters/test_search_api_skip_filter.py diff --git a/test/search_api/filters/test_search_api_where_filter.py b/test/integration/search_api/filters/test_search_api_where_filter.py similarity index 100% rename from test/search_api/filters/test_search_api_where_filter.py rename to test/integration/search_api/filters/test_search_api_where_filter.py diff --git a/test/search_api/test_error_handling.py b/test/integration/search_api/test_error_handling.py similarity index 100% rename from test/search_api/test_error_handling.py rename to test/integration/search_api/test_error_handling.py diff --git a/test/search_api/test_nested_where_filters.py b/test/integration/search_api/test_nested_where_filters.py similarity index 100% rename from test/search_api/test_nested_where_filters.py rename to test/integration/search_api/test_nested_where_filters.py diff --git a/test/search_api/test_search_api_query.py b/test/integration/search_api/test_search_api_query.py similarity index 100% rename from test/search_api/test_search_api_query.py rename to test/integration/search_api/test_search_api_query.py diff --git a/test/search_api/test_search_api_query_filter_factory.py b/test/integration/search_api/test_search_api_query_filter_factory.py similarity index 100% rename from test/search_api/test_search_api_query_filter_factory.py rename to test/integration/search_api/test_search_api_query_filter_factory.py diff --git a/test/search_api/test_session_handler.py b/test/integration/search_api/test_session_handler.py similarity index 100% rename from test/search_api/test_session_handler.py rename to test/integration/search_api/test_session_handler.py diff --git a/test/integration/test_config.py b/test/integration/test_config.py new file mode 100644 index 00000000..122c2f8a --- /dev/null +++ b/test/integration/test_config.py @@ -0,0 +1,9 @@ +class TestAPIConfig: + def test_load_with_valid_config_data(self, test_config): + backend_type = test_config.datagateway_api.backend + assert backend_type == "db" + + def test_set_backend_type(self, test_config): + test_config.datagateway_api.set_backend_type("backend_name_changed") + + assert test_config.datagateway_api.backend == "backend_name_changed" diff --git a/test/test_endpoint_rules.py b/test/integration/test_endpoint_rules.py similarity index 100% rename from test/test_endpoint_rules.py rename to test/integration/test_endpoint_rules.py diff --git a/test/test_filter_order_handler.py b/test/integration/test_filter_order_handler.py similarity index 100% rename from test/test_filter_order_handler.py rename to test/integration/test_filter_order_handler.py diff --git a/test/test_get_filters_from_query.py b/test/integration/test_get_filters_from_query.py similarity index 91% rename from test/test_get_filters_from_query.py rename to test/integration/test_get_filters_from_query.py index 4e232db0..02f98d47 100644 --- a/test/test_get_filters_from_query.py +++ b/test/integration/test_get_filters_from_query.py @@ -1,6 +1,6 @@ import pytest -from datagateway_api.src.common.exceptions import ApiError, FilterError +from datagateway_api.src.common.exceptions import FilterError from datagateway_api.src.common.helpers import get_filters_from_query_string from datagateway_api.src.datagateway_api.database.filters import ( DatabaseDistinctFieldFilter, @@ -60,7 +60,3 @@ def test_valid_search_api_filter(self, flask_test_app_db): filters = get_filters_from_query_string("search_api", "Dataset") assert len(filters) == 2 - - def test_invalid_api_type(self): - with pytest.raises(ApiError): - get_filters_from_query_string("unknown_api") diff --git a/test/test_get_session_id_from_auth_header.py b/test/integration/test_get_session_id_from_auth_header.py similarity index 100% rename from test/test_get_session_id_from_auth_header.py rename to test/integration/test_get_session_id_from_auth_header.py diff --git a/test/unit/__init__.py b/test/unit/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/datagateway_api/test_openapi.py b/test/unit/datagateway_api/test_openapi.py similarity index 100% rename from test/datagateway_api/test_openapi.py rename to test/unit/datagateway_api/test_openapi.py diff --git a/test/unit/search_api/conftest.py b/test/unit/search_api/conftest.py new file mode 100644 index 00000000..d5b46c85 --- /dev/null +++ b/test/unit/search_api/conftest.py @@ -0,0 +1,115 @@ +import json +from unittest.mock import mock_open, patch + + +import pytest +from datagateway_api.src.search_api.panosc_mappings import PaNOSCMappings + + +@pytest.fixture() +def test_search_api_mappings_data(): + return { + "Affiliation": { + "base_icat_entity": "Affiliation", + "id": "id", + "name": "name", + "address": "fullReference", + "city": "", + "country": "", + "members": {"Member": "user.user.investigationUsers"}, + }, + "Dataset": { + "base_icat_entity": "Dataset", + "pid": ["doi", "id"], + "title": "name", + "isPublic": "", + "creationDate": "createTime", + "size": "", + "documents": {"Document": "investigation"}, + "techniques": {"Technique": "datasetTechniques.technique"}, + "instrument": {"Instrument": "datasetInstruments.instrument"}, + "files": {"File": "datafiles"}, + "parameters": {"Parameter": "parameters"}, + "samples": {"Sample": "sample"}, + }, + "Document": { + "base_icat_entity": "Investigation", + "pid": ["doi", "id"], + "isPublic": "", + "type": "type.name", + "title": "name", + "summary": "summary", + "doi": "doi", + "startDate": "startDate", + "endDate": "endDate", + "releaseDate": "releaseDate", + "license": "", + "keywords": "keywords.name", + "datasets": {"Dataset": "datasets"}, + "members": {"Member": "investigationUsers"}, + "parameters": {"Parameter": "parameters"}, + }, + "File": { + "base_icat_entity": "Datafile", + "id": "id", + "name": "name", + "path": "location", + "size": "fileSize", + "dataset": {"Dataset": "dataset"}, + }, + "Instrument": { + "base_icat_entity": "Instrument", + "pid": ["pid", "id"], + "name": "name", + "facility": "facility.name", + "datasets": {"Dataset": "datasetInstruments.dataset"}, + }, + "Member": { + "base_icat_entity": "InvestigationUser", + "id": "id", + "role": "role", + "document": {"Document": "investigation"}, + "person": {"Person": "user"}, + "affiliation": {"Affiliation": "user.dataPublicationUsers.affiliations"}, + }, + "Parameter": { + "base_icat_entity": ["InvestigationParameter", "DatasetParameter"], + "id": "id", + "name": "type.name", + "value": ["numericValue", "stringValue", "dateTimeValue"], + "unit": "type.units", + "dataset": {"Dataset": "dataset"}, + "document": {"Document": "investigation"}, + }, + "Person": { + "base_icat_entity": "User", + "id": "id", + "fullName": "fullName", + "orcid": "orcidId", + "researcherId": "", + "firstName": "givenName", + "lastName": "familyName", + "members": {"Member": "investigationUsers"}, + }, + "Sample": { + "base_icat_entity": "Sample", + "name": "name", + "pid": ["pid", "id"], + "description": "parameters.type.description", + "datasets": {"Dataset": "datasets"}, + }, + "Technique": { + "base_icat_entity": "Technique", + "pid": ["pid", "id"], + "name": "name", + "datasets": {"Dataset": "datasetTechniques.dataset"}, + }, + } + + +@pytest.fixture() +def test_panosc_mappings(test_search_api_mappings_data): + with patch( + "builtins.open", mock_open(read_data=json.dumps(test_search_api_mappings_data)), + ): + return PaNOSCMappings("test/path") diff --git a/test/search_api/test_models.py b/test/unit/search_api/test_models.py similarity index 100% rename from test/search_api/test_models.py rename to test/unit/search_api/test_models.py diff --git a/test/search_api/test_panosc_mappings.py b/test/unit/search_api/test_panosc_mappings.py similarity index 90% rename from test/search_api/test_panosc_mappings.py rename to test/unit/search_api/test_panosc_mappings.py index 9871618d..5040865e 100644 --- a/test/search_api/test_panosc_mappings.py +++ b/test/unit/search_api/test_panosc_mappings.py @@ -1,5 +1,3 @@ -from unittest.mock import patch - import pytest from datagateway_api.src.common.exceptions import FilterError, SearchAPIError @@ -11,31 +9,6 @@ def test_valid_load_mappings(self, test_panosc_mappings): test_mappings = PaNOSCMappings() assert test_mappings.mappings == test_panosc_mappings.mappings - @pytest.mark.parametrize( - "search_api_config_flag", - [ - pytest.param(True, id="Search API config present"), - pytest.param(False, id="No search API config"), - ], - ) - def test_invalid_load_mappings( - self, test_config, test_config_without_search_api, search_api_config_flag, - ): - if search_api_config_flag: - current_test_config = test_config - else: - current_test_config = test_config_without_search_api - - with patch( - "datagateway_api.src.common.config.Config.config", current_test_config, - ): - if search_api_config_flag: - with pytest.raises(SystemExit): - PaNOSCMappings("bad/path") - else: - # Shouldn't SysExit if a user isn't using the search API - PaNOSCMappings("bad/path") - @pytest.mark.parametrize( "panosc_entity_name, field_name, expected_panosc_entity_name" ", expected_icat_field_name", diff --git a/test/test_base_query_filter_factory.py b/test/unit/test_base_query_filter_factory.py similarity index 100% rename from test/test_base_query_filter_factory.py rename to test/unit/test_base_query_filter_factory.py diff --git a/test/test_config.py b/test/unit/test_config.py similarity index 91% rename from test/test_config.py rename to test/unit/test_config.py index c22e07b8..f9646bf8 100644 --- a/test/test_config.py +++ b/test/unit/test_config.py @@ -7,10 +7,6 @@ class TestAPIConfig: - def test_load_with_valid_config_data(self, test_config): - backend_type = test_config.datagateway_api.backend - assert backend_type == "db" - def test_load_with_no_config_data(self): with patch("builtins.open", mock_open(read_data="{}")): with pytest.raises(SystemExit): @@ -61,11 +57,6 @@ def test_load_with_same_api_extensions(self, test_config_data): with pytest.raises(SystemExit): APIConfig.load("test/path") - def test_set_backend_type(self, test_config): - test_config.datagateway_api.set_backend_type("backend_name_changed") - - assert test_config.datagateway_api.backend == "backend_name_changed" - @pytest.mark.parametrize( "input_extension, expected_extension", [ diff --git a/test/test_date_handler.py b/test/unit/test_date_handler.py similarity index 100% rename from test/test_date_handler.py rename to test/unit/test_date_handler.py diff --git a/test/test_exceptions.py b/test/unit/test_exceptions.py similarity index 100% rename from test/test_exceptions.py rename to test/unit/test_exceptions.py diff --git a/test/test_get_entity_object.py b/test/unit/test_get_entity_object.py similarity index 100% rename from test/test_get_entity_object.py rename to test/unit/test_get_entity_object.py diff --git a/test/unit/test_get_filters_from_query.py b/test/unit/test_get_filters_from_query.py new file mode 100644 index 00000000..09327d30 --- /dev/null +++ b/test/unit/test_get_filters_from_query.py @@ -0,0 +1,10 @@ +import pytest + +from datagateway_api.src.common.exceptions import ApiError +from datagateway_api.src.common.helpers import get_filters_from_query_string + + +class TestGetFiltersFromQueryString: + def test_invalid_api_type(self): + with pytest.raises(ApiError): + get_filters_from_query_string("unknown_api") diff --git a/test/test_is_valid_json.py b/test/unit/test_is_valid_json.py similarity index 100% rename from test/test_is_valid_json.py rename to test/unit/test_is_valid_json.py diff --git a/test/test_map_distinct_attrs.py b/test/unit/test_map_distinct_attrs.py similarity index 100% rename from test/test_map_distinct_attrs.py rename to test/unit/test_map_distinct_attrs.py diff --git a/test/test_queries_records.py b/test/unit/test_queries_records.py similarity index 100% rename from test/test_queries_records.py rename to test/unit/test_queries_records.py diff --git a/test/test_query_filter.py b/test/unit/test_query_filter.py similarity index 100% rename from test/test_query_filter.py rename to test/unit/test_query_filter.py