Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

221 ngsi ld migrate v2 datamodels in ld #234

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions filip/models/ngsi_ld/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,10 +732,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)