Skip to content

Commit

Permalink
Merge pull request #234 from RWTH-EBC/221-ngsi-ld-migrate-V2-datamode…
Browse files Browse the repository at this point in the history
…ls-in-ld

221 ngsi ld migrate v2 datamodels in ld
  • Loading branch information
djs0109 authored Sep 17, 2024
2 parents 0803507 + 317cfb5 commit 47ac79a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions filip/models/ngsi_ld/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,10 +769,15 @@ def get_context(self):
Returns: context of the entity as list
"""
found_list = False
for key, value in self.model_dump(exclude_unset=True).items():
if key not in ContextLDEntity.model_fields:
if isinstance(value, list):
found_list = True
return value
if not found_list:
logging.warning("No context in entity")
return None


class ActionTypeLD(str, Enum):
Expand Down
51 changes: 51 additions & 0 deletions tests/models/test_ngsi_ld_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,50 @@ def setUp(self) -> None:
}
self.entity2_dict.update(self.entity2_props_dict)
self.entity2_dict.update(self.entity2_rel_dict)
self.entity3_dict = {
"id": "urn:ngsi-ld:Vehicle:test1243",
"type": "Vehicle",
"isParked": {
"type": "Relationship",
"object": "urn:ngsi-ld:OffStreetParking:Downtown1",
"observedAt": "2017-07-29T12:00:04Z",
"providedBy": {
"type": "Relationship",
"object": "urn:ngsi-ld:Person:Bob"
}
}
}
# The entity for testing the nested structure of properties
self.entity_sub_props_dict = {
"id": "urn:ngsi-ld:Vehicle:test1243",
"type": "Vehicle",
"prop1": {
"type": "Property",
"value": 1,
"sub_property": {
"type": "Property",
"value": 10,
"sub_sub_property": {
"type": "Property",
"value": 100
}
},
"sub_properties_list": [
{
"sub_prop_1": {
"value": 100,
"type": "Property"
}
},
{
"sub_prop_2": {
"value": 200,
"type": "Property"
}
}
],
}
}

def test_cb_attribute(self) -> None:
"""
Expand Down Expand Up @@ -235,3 +279,10 @@ def test_get_context(self):

self.assertEqual(self.entity1_context,
context_entity1)

# test here if entity without context can be validated and get_context works accordingly:
entity3 = ContextLDEntity(**self.entity3_dict)
context_entity3 = entity3.get_context()

self.assertEqual(None,
context_entity3)

0 comments on commit 47ac79a

Please sign in to comment.