Skip to content

Commit

Permalink
#150: Make requested changes on PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Jan 4, 2021
1 parent 01449cc commit e826e20
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 187 deletions.
2 changes: 0 additions & 2 deletions datagateway_api/common/backends.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys

from datagateway_api.common.backend import Backend
from datagateway_api.common.database.backend import DatabaseBackend
from datagateway_api.common.icat.backend import PythonICATBackend

Expand All @@ -27,6 +26,5 @@ def create_backend(backend_type):
else:
# Might turn to a warning so the abstract class can be tested?
sys.exit(f"Invalid config value '{backend_type}' for config option backend")
backend = Backend()

return backend
1 change: 0 additions & 1 deletion datagateway_api/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def __init__(self, path=Path(__file__).parent.parent.parent / "config.json"):
self.path = path
with open(self.path) as target:
self.config = json.load(target)
target.close()

def get_backend_type(self):
try:
Expand Down
3 changes: 3 additions & 0 deletions datagateway_api/common/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from datagateway_api.common.config import config


Expand All @@ -6,3 +8,4 @@ class Constants:
ACCEPTED_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
PYTHON_ICAT_DISTNCT_CONDITION = "!= null"
ICAT_PROPERTIES = config.get_icat_properties()
TEST_MOD_CREATE_DATETIME = datetime(2000, 1, 1)
21 changes: 19 additions & 2 deletions test/db/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from datagateway_api.common.constants import Constants
from datagateway_api.common.database.helpers import (
delete_row_by_id,
insert_row_into_table,
Expand All @@ -17,8 +18,8 @@

def set_meta_attributes(entity):
db_meta_attributes = {
"CREATE_TIME": datetime(2000, 1, 1),
"MOD_TIME": datetime(2000, 1, 1),
"CREATE_TIME": Constants.TEST_MOD_CREATE_DATETIME,
"MOD_TIME": Constants.TEST_MOD_CREATE_DATETIME,
"CREATE_ID": "test create id",
"MOD_ID": "test mod id",
}
Expand Down Expand Up @@ -110,3 +111,19 @@ def isis_specific_endpoint_data_db():
delete_row_by_id(FACILITYCYCLE, facility_cycle.ID)
delete_row_by_id(INVESTIGATION, investigation.ID)
delete_row_by_id(INSTRUMENT, instrument.ID)


@pytest.fixture()
def final_instrument_id(flask_test_app_db, valid_db_credentials_header):
final_instrument_result = flask_test_app_db.get(
'/instruments/findone?order="ID DESC"', headers=valid_db_credentials_header,
)
return final_instrument_result.json["ID"]


@pytest.fixture()
def final_facilitycycle_id(flask_test_app_db, valid_db_credentials_header):
final_facilitycycle_result = flask_test_app_db.get(
'/facilitycycles/findone?order="ID DESC"', headers=valid_db_credentials_header,
)
return final_facilitycycle_result.json["ID"]
1 change: 0 additions & 1 deletion test/db/endpoints/test_get_by_id_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def test_valid_get_with_id(
' Testing (DB)"}}',
headers=valid_db_credentials_header,
)
print(investigation_data.json)
test_data_id = investigation_data.json[0]["ID"]

test_response = flask_test_app_db.get(
Expand Down
3 changes: 1 addition & 2 deletions test/db/endpoints/test_get_with_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def test_valid_no_results_get_with_filters(
'testing purposes..."}}',
headers=valid_db_credentials_header,
)
print(test_response.json)

assert test_response.json == []

Expand All @@ -39,7 +38,7 @@ def test_valid_get_with_filters_distinct(
)

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

for title in expected:
Expand Down
48 changes: 12 additions & 36 deletions test/db/endpoints/test_table_endpoints_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ def test_valid_get_facility_cycles_with_filters(
valid_db_credentials_header,
isis_specific_endpoint_data_db,
):

print(int(isis_specific_endpoint_data_db[0]))
test_response = flask_test_app_db.get(
f"/instruments/{int(isis_specific_endpoint_data_db[0])}/facilitycycles",
headers=valid_db_credentials_header,
Expand All @@ -20,13 +18,8 @@ def test_valid_get_facility_cycles_with_filters(
assert test_response.json == [isis_specific_endpoint_data_db[1].to_dict()]

def test_invalid_get_facility_cycles_with_filters(
self, flask_test_app_db, valid_db_credentials_header,
self, flask_test_app_db, valid_db_credentials_header, final_instrument_id,
):
final_instrument_result = flask_test_app_db.get(
'/instruments/findone?order="ID DESC"', headers=valid_db_credentials_header,
)
final_instrument_id = final_instrument_result.json["ID"]

test_response = flask_test_app_db.get(
f"/instruments/{final_instrument_id + 100}/facilitycycles",
headers=valid_db_credentials_header,
Expand All @@ -48,13 +41,8 @@ def test_valid_get_facility_cycles_count_with_filters(
assert test_response.json == 1

def test_invalid_get_facility_cycles_count_with_filters(
self, flask_test_app_db, valid_db_credentials_header,
self, flask_test_app_db, valid_db_credentials_header, final_instrument_id,
):
final_instrument_result = flask_test_app_db.get(
'/instruments/findone?order="ID DESC"', headers=valid_db_credentials_header,
)
final_instrument_id = final_instrument_result.json["ID"]

test_response = flask_test_app_db.get(
f"/instruments/{final_instrument_id + 100}/facilitycycles/count",
headers=valid_db_credentials_header,
Expand All @@ -77,18 +65,12 @@ def test_valid_get_investigations_with_filters(
assert test_response.json == [isis_specific_endpoint_data_db[2].to_dict()]

def test_invalid_get_investigations_with_filters(
self, flask_test_app_db, valid_db_credentials_header,
self,
flask_test_app_db,
valid_db_credentials_header,
final_instrument_id,
final_facilitycycle_id,
):
final_instrument_result = flask_test_app_db.get(
'/instruments/findone?order="ID DESC"', headers=valid_db_credentials_header,
)
final_instrument_id = final_instrument_result.json["ID"]
final_facilitycycle_result = flask_test_app_db.get(
'/facilitycycles/findone?order="ID DESC"',
headers=valid_db_credentials_header,
)
final_facilitycycle_id = final_facilitycycle_result.json["ID"]

test_response = flask_test_app_db.get(
f"/instruments/{final_instrument_id + 100}/facilitycycles/"
f"{final_facilitycycle_id + 100}/investigations",
Expand All @@ -112,18 +94,12 @@ def test_valid_get_investigations_count_with_filters(
assert test_response.json == 1

def test_invalid_get_investigations_count_with_filters(
self, flask_test_app_db, valid_db_credentials_header,
self,
flask_test_app_db,
valid_db_credentials_header,
final_instrument_id,
final_facilitycycle_id,
):
final_instrument_result = flask_test_app_db.get(
'/instruments/findone?order="id DESC"', headers=valid_db_credentials_header,
)
final_instrument_id = final_instrument_result.json["ID"]
final_facilitycycle_result = flask_test_app_db.get(
'/facilitycycles/findone?order="ID DESC"',
headers=valid_db_credentials_header,
)
final_facilitycycle_id = final_facilitycycle_result.json["ID"]

test_response = flask_test_app_db.get(
f"/instruments/{final_instrument_id + 100}/facilitycycles/"
f"{final_facilitycycle_id + 100}/investigations/count",
Expand Down
43 changes: 21 additions & 22 deletions test/db/test_entity_helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime

import pytest

from datagateway_api.common.constants import Constants
from datagateway_api.common.database.models import (
DATAFILE,
DATAFILEFORMAT,
Expand All @@ -28,12 +27,12 @@ def datafile_entity(dataset_entity):
datafile.DATASET = dataset_entity
datafile.DATAFILEFORMAT = datafileformat
datafile.NAME = "test name"
datafile.MOD_TIME = datetime.datetime(2000, 1, 1)
datafile.CREATE_TIME = datetime.datetime(2000, 1, 1)
datafile.MOD_TIME = Constants.TEST_MOD_CREATE_DATETIME
datafile.CREATE_TIME = Constants.TEST_MOD_CREATE_DATETIME
datafile.CHECKSUM = "test checksum"
datafile.FILESIZE = 64
datafile.DATAFILEMODTIME = datetime.datetime(2000, 1, 1)
datafile.DATAFILECREATETIME = datetime.datetime(2000, 1, 1)
datafile.DATAFILEMODTIME = Constants.TEST_MOD_CREATE_DATETIME
datafile.DATAFILECREATETIME = Constants.TEST_MOD_CREATE_DATETIME
datafile.DATASET_ID = 1
datafile.DOI = "test doi"
datafile.DESCRIPTION = "test description"
Expand All @@ -50,18 +49,18 @@ def test_valid_to_dict(self, datafile_entity):
"ID": 1,
"LOCATION": "test location",
"NAME": "test name",
"MOD_TIME": str(datetime.datetime(2000, 1, 1)),
"MOD_TIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"CHECKSUM": "test checksum",
"FILESIZE": 64,
"DATAFILEMODTIME": str(datetime.datetime(2000, 1, 1)),
"DATAFILECREATETIME": str(datetime.datetime(2000, 1, 1)),
"DATAFILEMODTIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"DATAFILECREATETIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"DATASET_ID": 1,
"DOI": "test doi",
"DESCRIPTION": "test description",
"CREATE_ID": "test create id",
"MOD_ID": "test mod id",
"DATAFILEFORMAT_ID": 1,
"CREATE_TIME": str(datetime.datetime(2000, 1, 1)),
"CREATE_TIME": str(Constants.TEST_MOD_CREATE_DATETIME),
}

test_data = datafile_entity.to_dict()
Expand All @@ -76,18 +75,18 @@ def test_valid_to_dict(self, datafile_entity):
"ID": 1,
"LOCATION": "test location",
"NAME": "test name",
"MOD_TIME": str(datetime.datetime(2000, 1, 1)),
"MOD_TIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"CHECKSUM": "test checksum",
"FILESIZE": 64,
"DATAFILEMODTIME": str(datetime.datetime(2000, 1, 1)),
"DATAFILECREATETIME": str(datetime.datetime(2000, 1, 1)),
"DATAFILEMODTIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"DATAFILECREATETIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"DATASET_ID": 1,
"DOI": "test doi",
"DESCRIPTION": "test description",
"CREATE_ID": "test create id",
"MOD_ID": "test mod id",
"DATAFILEFORMAT_ID": 1,
"CREATE_TIME": str(datetime.datetime(2000, 1, 1)),
"CREATE_TIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"DATASET": {
"ID": None,
"CREATE_TIME": None,
Expand All @@ -114,18 +113,18 @@ def test_valid_to_dict(self, datafile_entity):
"ID": 1,
"LOCATION": "test location",
"NAME": "test name",
"MOD_TIME": str(datetime.datetime(2000, 1, 1)),
"MOD_TIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"CHECKSUM": "test checksum",
"FILESIZE": 64,
"DATAFILEMODTIME": str(datetime.datetime(2000, 1, 1)),
"DATAFILECREATETIME": str(datetime.datetime(2000, 1, 1)),
"DATAFILEMODTIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"DATAFILECREATETIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"DATASET_ID": 1,
"DOI": "test doi",
"DESCRIPTION": "test description",
"CREATE_ID": "test create id",
"MOD_ID": "test mod id",
"DATAFILEFORMAT_ID": 1,
"CREATE_TIME": str(datetime.datetime(2000, 1, 1)),
"CREATE_TIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"DATASET": {
"ID": None,
"CREATE_TIME": None,
Expand Down Expand Up @@ -180,18 +179,18 @@ def test_valid_update_from_dict(self, datafile_entity):
"ID": 1,
"LOCATION": "test location",
"NAME": "test name",
"MOD_TIME": str(datetime.datetime(2000, 1, 1)),
"MOD_TIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"CHECKSUM": "test checksum",
"FILESIZE": 64,
"DATAFILEMODTIME": str(datetime.datetime(2000, 1, 1)),
"DATAFILECREATETIME": str(datetime.datetime(2000, 1, 1)),
"DATAFILEMODTIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"DATAFILECREATETIME": str(Constants.TEST_MOD_CREATE_DATETIME),
"DATASET_ID": 1,
"DOI": "test doi",
"DESCRIPTION": "test description",
"CREATE_ID": "test create id",
"MOD_ID": "test mod id",
"DATAFILEFORMAT_ID": 1,
"CREATE_TIME": str(datetime.datetime(2000, 1, 1)),
"CREATE_TIME": str(Constants.TEST_MOD_CREATE_DATETIME),
}

datafile.update_from_dict(test_dict_data)
Expand Down
17 changes: 17 additions & 0 deletions test/icat/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,20 @@ def isis_specific_endpoint_data(icat_client):
icat_client.delete(instrument)
except ICATNoObjectError as e:
print(e)


@pytest.fixture()
def final_instrument_id(flask_test_app_icat, valid_icat_credentials_header):
final_instrument_result = flask_test_app_icat.get(
'/instruments/findone?order="id DESC"', headers=valid_icat_credentials_header,
)
return final_instrument_result.json["id"]


@pytest.fixture()
def final_facilitycycle_id(flask_test_app_icat, valid_icat_credentials_header):
final_facilitycycle_result = flask_test_app_icat.get(
'/facilitycycles/findone?order="id DESC"',
headers=valid_icat_credentials_header,
)
return final_facilitycycle_result.json["id"]
17 changes: 3 additions & 14 deletions test/icat/endpoints/test_create_icat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def test_valid_create_data(
):
create_investigations_json = [
{
"name": "Test Data for API Testing, Data Creation 1",
"name": f"Test Data for API Testing, Data Creation {i}",
"title": "Test data for the Python ICAT Backend on DataGateway API",
"summary": "Test data for DataGateway API testing",
"releaseDate": "2020-03-03 08:00:08",
Expand All @@ -17,19 +17,8 @@ def test_valid_create_data(
"doi": "DataGateway API Test DOI",
"facility": 1,
"type": 1,
},
{
"name": "Test Data for API Testing, Data Creation 2",
"title": "Test data for the Python ICAT Backend on DataGateway API",
"summary": "Test data for DataGateway API testing",
"releaseDate": "2020-03-03 08:00:08",
"startDate": "2020-02-02 09:00:09",
"endDate": "2020-02-03 10:00:10",
"visitId": "Data Creation Visit",
"doi": "DataGateway API Test DOI",
"facility": 1,
"type": 1,
},
}
for i in range(2)
]

test_response = flask_test_app_icat.post(
Expand Down
6 changes: 2 additions & 4 deletions test/icat/endpoints/test_get_with_filters_icat.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ def test_valid_get_with_filters_distinct(
)

expected = [
{
"title": f"Test data for the Python ICAT Backend on DataGateway API {i}"
for i in range(5)
},
{"title": f"Test data for the Python ICAT Backend on DataGateway API {i}"}
for i in range(5)
]

for title in expected:
Expand Down
Loading

0 comments on commit e826e20

Please sign in to comment.