Skip to content

Commit

Permalink
#150: Add function to remove ICAT meta attributes and new test
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Nov 23, 2020
1 parent df48005 commit 367eebb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
8 changes: 5 additions & 3 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import uuid

from icat.client import Client
from icat.entity import Entity
from icat.query import Query
import pytest

from datagateway_api.common.config import config
from test.icat.test_query import remove_meta_attributes


@pytest.fixture(scope="package")
Expand Down Expand Up @@ -33,9 +33,11 @@ def single_investigation_test_data(icat_client):
investigation.facility = icat_client.get("Facility", 1)
investigation.type = icat_client.get("InvestigationType", 1)
investigation.create()

investigation_dict = investigation.as_dict()
remove_meta_attributes(investigation_dict)

meta_attributes = Entity.MetaAttr
for attr in meta_attributes:
investigation_dict.pop(attr)

yield [investigation_dict]

Expand Down
46 changes: 34 additions & 12 deletions test/icat/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,28 @@
from datagateway_api.common.icat.query import ICATQuery


def remove_meta_attributes(entity_dict):
def prepare_icat_data_for_assertion(data):
"""
Remove meta attributes from ICAT data. Meta attributes contain data about data
creation/modification, and should be removed to ensure correct assertion values
:param data: ICAT data containing meta attributes such as modTime
:type data: :class:`dict` or an inherited version of :class:`icat.entity.Entity`
"""
assertable_data = []
meta_attributes = Entity.MetaAttr
for attr in meta_attributes:
entity_dict.pop(attr)

for entity in data:
# Convert to dictionary if an ICAT entity object
if isinstance(entity, Entity):
entity = entity.as_dict()

for attr in meta_attributes:
entity.pop(attr)

assertable_data.append(entity)

return assertable_data


class TestICATQuery:
Expand All @@ -33,11 +51,7 @@ def test_valid_query_exeuction(
test_data_filter.apply_filter(test_query.query)
query_data = test_query.execute_query(icat_client)

query_output_dicts = []
for entity in query_data:
entity_dict = entity.as_dict()
remove_meta_attributes(entity_dict)
query_output_dicts.append(entity_dict)
query_output_dicts = prepare_icat_data_for_assertion(query_data)

assert query_output_dicts == single_investigation_test_data

Expand All @@ -51,11 +65,19 @@ def test_valid_count_query_execution(self, icat_client):
def test_valid_distinct_query_execution(self, icat_client):
pass

def test_json_format_execution_output(self, icat_client):
pass
def test_json_format_execution_output(
self, icat_client, single_investigation_test_data,
):
test_query = ICATQuery(icat_client, "Investigation")
test_data_filter = PythonICATWhereFilter(
"title", "Test data for the Python ICAT Backend on DataGateway API", "eq",
)
test_data_filter.apply_filter(test_query.query)
query_data = test_query.execute_query(icat_client, True)

def test_icat_execution_output(self, icat_client):
pass
query_output_json = prepare_icat_data_for_assertion(query_data)

assert query_output_json == single_investigation_test_data

# gap in function testing

Expand Down

0 comments on commit 367eebb

Please sign in to comment.