Skip to content

Commit

Permalink
Initial reorganisation
Browse files Browse the repository at this point in the history
Tests have been organised into ones that immediately work with no icat stack and ones that don't.
  • Loading branch information
Reillyhewitson committed Nov 22, 2022
1 parent 71d81ac commit 185e304
Show file tree
Hide file tree
Showing 74 changed files with 247 additions and 133 deletions.
83 changes: 0 additions & 83 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
File renamed without changes.
85 changes: 85 additions & 0 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -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")
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions test/integration/test_config.py
Original file line number Diff line number Diff line change
@@ -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"
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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")
Empty file added test/unit/__init__.py
Empty file.
File renamed without changes.
Loading

0 comments on commit 185e304

Please sign in to comment.