Skip to content

Commit d9cfbf8

Browse files
committed
normalize_data no longer suppresses keys with value None. Fixes #101
1 parent 71c39be commit d9cfbf8

4 files changed

Lines changed: 18 additions & 6 deletions

File tree

fairgraph/kgobject.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(
9595
self._raw_remote_data = data # for debugging
9696
if data:
9797
self.remote_data = normalize_data(
98-
self.to_jsonld(include_empty_properties=True, embed_linked_nodes=False),
98+
self.to_jsonld(include_empty_properties=False, embed_linked_nodes=False),
9999
data.get("@context", self.context)
100100
)
101101

@@ -737,7 +737,10 @@ def save(
737737
activity_log.update(item=self, delta=None, space=space, entry_type="no-op")
738738
else:
739739
# update
740-
local_data = normalize_data(self.to_jsonld(embed_linked_nodes=False), self.context)
740+
local_data = normalize_data(
741+
self.to_jsonld(include_empty_properties=False, embed_linked_nodes=False),
742+
self.context
743+
)
741744
if replace:
742745
logger.info(f" - replacing - {self.__class__.__name__}(id={self.id})")
743746
if activity_log:
@@ -793,7 +796,10 @@ def save(
793796
activity_log.update(item=self, delta=None, space=space, entry_type="no-op")
794797
else:
795798
# create new
796-
local_data = normalize_data(self.to_jsonld(embed_linked_nodes=False), self.context)
799+
local_data = normalize_data(
800+
self.to_jsonld(include_empty_properties=False, embed_linked_nodes=False),
801+
self.context
802+
)
797803
logger.info(" - creating instance with data {}".format(local_data))
798804
if self.id and self.id.startswith("http"):
799805
instance_id = self.uuid

fairgraph/utility.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ def normalize_data(data: Union[None, JSONdict], context: Dict[str, Any]) -> Unio
218218

219219
if hasattr(value, "__len__") and len(value) == 0:
220220
pass
221-
elif value is None:
222-
pass
223221
elif expanded_key == "@id":
224222
if value.startswith("http"):
225223
# do not take local ids, e.g., those starting with "_"

test/test_base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,18 +485,24 @@ def test_modified_data(self):
485485
self._construct_embedded_object_required_properties(-18),
486486
self._construct_embedded_object_required_properties(19),
487487
]
488+
obj.an_optional_list_of_linked_objects = None
488489
expected = {
489490
"https://openminds.ebrains.eu/vocab/aRequiredString": "pomme",
490491
"https://openminds.ebrains.eu/vocab/anOptionalListOfEmbeddedObjects": [
491492
{
492493
"@type": "https://openminds.ebrains.eu/mock/MockEmbeddedObject",
494+
"https://openminds.ebrains.eu/vocab/aDate": None,
493495
"https://openminds.ebrains.eu/vocab/aNumber": -18,
496+
"https://openminds.ebrains.eu/vocab/aString": None,
494497
},
495498
{
496499
"@type": "https://openminds.ebrains.eu/mock/MockEmbeddedObject",
500+
"https://openminds.ebrains.eu/vocab/aDate": None,
497501
"https://openminds.ebrains.eu/vocab/aNumber": 19,
502+
"https://openminds.ebrains.eu/vocab/aString": None,
498503
},
499504
],
505+
"https://openminds.ebrains.eu/vocab/anOptionalListOfLinkedObjects": None
500506
}
501507
assert obj.modified_data() == expected
502508

@@ -594,7 +600,7 @@ def instance_from_full_uri(self, id, use_cache=True, release_status="in progress
594600
# not just those that have changed
595601
"https://openminds.ebrains.eu/vocab/aNumber": 41,
596602
"https://openminds.ebrains.eu/vocab/aString": "forty one",
597-
# "https://openminds.ebrains.eu/vocab/aDate": None,
603+
"https://openminds.ebrains.eu/vocab/aDate": None,
598604
},
599605
}
600606
assert new_obj.modified_data() == expected

test/test_utility.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def test_normalize_data():
8282
},
8383
"familyName": "Oakenshield",
8484
"givenName": "Thorin",
85+
"alternateName": None
8586
}
8687
context = {"@vocab": "https://openminds.ebrains.eu/vocab/"}
8788
expected = {
@@ -96,6 +97,7 @@ def test_normalize_data():
9697
},
9798
"https://openminds.ebrains.eu/vocab/familyName": "Oakenshield",
9899
"https://openminds.ebrains.eu/vocab/givenName": "Thorin",
100+
"https://openminds.ebrains.eu/vocab/alternateName": None
99101
}
100102
assert normalize_data(data, context) == expected
101103

0 commit comments

Comments
 (0)