Skip to content

Commit

Permalink
change the representation of UID (#251)
Browse files Browse the repository at this point in the history
* change the representation of UID

* fix the two tests that were missing

* this should work now

* missing test fix

* conftest get integration test var from env var with exception handling (#253)

* updated CI to have `CRIPT_STORAGE_TOKEN`

* if no env var then integration tests are true

* pass empty identifier list too

* uuidfy experiment/data

* thx mypy

---------

Co-authored-by: nh916 <navidhariri25@gmail.com>
  • Loading branch information
InnocentBug and nh916 authored Aug 10, 2023
1 parent 32145de commit db1c41d
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/cript/nodes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ def get_json(
is_patch: bool = False,
condense_to_uuid: Dict[str, Set[str]] = {
"Material": {"parent_material", "component"},
"Experiment": {"data"},
"Inventory": {"material"},
"Ingredient": {"material"},
"Property": {"component"},
Expand Down
6 changes: 3 additions & 3 deletions src/cript/nodes/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def default(self, obj):
pass
else:
if uid in NodeEncoder.handled_ids:
return {"node": obj._json_attrs.node, "uid": uid}
return {"uid": uid}

# When saving graphs, some nodes can be pre-saved.
# If that happens, we want to represent them as a UUID edge only
Expand Down Expand Up @@ -282,12 +282,12 @@ def __call__(self, node_str: Union[dict, str]) -> dict:
node_dict = dict(node_str) # type: ignore

# Handle UID objects.
if len(node_dict) == 2 and "uid" in node_dict and "node" in node_dict:
if len(node_dict) == 1 and "uid" in node_dict:
try:
return self._uid_cache[node_dict["uid"]]
except KeyError:
# TODO if we convince beartype to accept Proxy temporarily, enable return instead of raise
raise CRIPTDeserializationUIDError(node_dict["node"], node_dict["uid"])
raise CRIPTDeserializationUIDError("Unknown", node_dict["uid"])
# return _UIDProxy(node_dict["uid"])

try:
Expand Down
4 changes: 2 additions & 2 deletions src/cript/nodes/util/material_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _deserialize_flattened_material_identifiers(json_dict: Dict) -> Dict:
identifier_argument.append({identifier: json_dict[identifier]})
# delete identifiers from the API JSON response as they are added to the material node
del json_dict[identifier]
if len(identifier_argument) > 0:
json_dict["identifiers"] = identifier_argument

json_dict["identifiers"] = identifier_argument

return json_dict
16 changes: 15 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,23 @@

import cript


def _get_cript_tests_env() -> bool:
"""
Gets `CRIPT_TESTS` value from env variable and converts it to boolean.
If `CRIPT_TESTS` env var does not exist then it will default it to False.
"""
try:
has_integration_tests_enabled = os.getenv("CRIPT_TESTS").title().strip() == "True"
except AttributeError:
has_integration_tests_enabled = True

return has_integration_tests_enabled


# flip integration tests ON or OFF with this boolean
# automatically gets value env vars to run integration tests
HAS_INTEGRATION_TESTS_ENABLED: bool = os.getenv("CRIPT_TESTS").title() == "True"
HAS_INTEGRATION_TESTS_ENABLED: bool = _get_cript_tests_env()


@pytest.fixture(scope="session", autouse=True)
Expand Down
5 changes: 0 additions & 5 deletions tests/integration_test_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
import warnings

import pytest
from conftest import HAS_INTEGRATION_TESTS_ENABLED
Expand Down Expand Up @@ -96,8 +95,4 @@ def integrate_nodes_helper(cript_api: cript.API, project_node: cript.Project):
print("\n\n=================== Project Node Deserialized =========================")
print(my_project_from_api.get_json(sort_keys=False, condense_to_uuid={}, indent=2).json)
print("==============================================================")

print("\n\n\n######################################## TEST Passed ########################################\n\n\n")

warnings.warn("Please uncomment `integrate_nodes_helper` to test with the API")
pass
2 changes: 1 addition & 1 deletion tests/nodes/primary_nodes/test_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def test_experiment_json(simple_process_node, simple_computation_node, simple_co
],
}
],
"data": [{"node": ["Data"]}],
"data": [{}],
"funding": ["National Science Foundation", "IRIS", "NIST"],
"citation": [
{
Expand Down
4 changes: 2 additions & 2 deletions tests/nodes/primary_nodes/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def test_serialize_project_to_json(complex_project_node, complex_project_dict) -
expected_dict = complex_project_dict

# Since we condense those to UUID we remove them from the expected dict.
expected_dict["admin"] = [{"node": ["User"]}]
expected_dict["member"] = [{"node": ["User"]}]
expected_dict["admin"] = [{}]
expected_dict["member"] = [{}]

# comparing dicts instead of JSON strings because dict comparison is more accurate
serialized_project: dict = json.loads(complex_project_node.get_json(condense_to_uuid={}).json)
Expand Down
3 changes: 1 addition & 2 deletions tests/test_node_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_uid_deserialization(simple_algorithm_node, complex_parameter_node, simp
"type": "value",
"value": 5.0,
"unit": "GPa",
"computation": [{"node": ["Computation"], "uid": "_:9ddda2c0-ff8c-4ce3-beb0-e0cafb6169ef"}],
"computation": [{"uid": "_:9ddda2c0-ff8c-4ce3-beb0-e0cafb6169ef"}],
},
{
"node": ["Property"],
Expand All @@ -66,7 +66,6 @@ def test_uid_deserialization(simple_algorithm_node, complex_parameter_node, simp
"unit": "GPa",
"computation": [
{
"node": ["Computation"],
"uid": "_:9ddda2c0-ff8c-4ce3-beb0-e0cafb6169ef",
}
],
Expand Down

0 comments on commit db1c41d

Please sign in to comment.