Skip to content

Commit

Permalink
Merge pull request #271 from RWTH-EBC/145-update-relationships-with-c…
Browse files Browse the repository at this point in the history
…ontextbrokerclient-update_entity

add ``update_entity_relationships`` to allow relationship update
  • Loading branch information
djs0109 authored Apr 30, 2024
2 parents 1737977 + 00e7d5d commit 2162d59
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### v0.4.2
- add: validation for JEXL based expression ([#260](https://github.com/RWTH-EBC/FiLiP/pull/260))
- add: tutorials for multi-entity ([#260](https://github.com/RWTH-EBC/FiLiP/pull/260))
- add: add ``update_entity_relationships`` to allow relationship update ([#271](https://github.com/RWTH-EBC/FiLiP/pull/271))
- add: flag to determine the deletion of registration when clearing the CB ([#267](https://github.com/RWTH-EBC/FiLiP/pull/267))

### v0.4.1
Expand Down
61 changes: 61 additions & 0 deletions filip/clients/ngsi_v2/cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,36 @@ def update_entity(self, entity: ContextEntity, append_strict: bool = False):
The request payload is an object representing the attributes to
append or update.
Note:
Update means overwriting the existing entity. If you want to
manipulate you should rather use patch_entity.
Args:
entity (ContextEntity):
append_strict: If `False` the entity attributes are updated (if they
previously exist) or appended (if they don't previously exist)
with the ones in the payload.
If `True` all the attributes in the payload not
previously existing in the entity are appended. In addition
to that, in case some of the attributes in the payload
already exist in the entity, an error is returned.
More precisely this means a strict append procedure.
Returns:
None
"""
self.update_or_append_entity_attributes(
entity_id=entity.id,
entity_type=entity.type,
attrs=entity.get_attributes(),
append_strict=append_strict,
)

def update_entity_properties(self, entity: ContextEntity, append_strict: bool = False):
"""
The request payload is an object representing the attributes, of any type
but Relationship, to append or update.
Note:
Update means overwriting the existing entity. If you want to
manipulate you should rather use patch_entity.
Expand All @@ -571,6 +601,37 @@ def update_entity(self, entity: ContextEntity, append_strict: bool = False):
append_strict=append_strict,
)

def update_entity_relationships(self, entity: ContextEntity,
append_strict: bool = False):
"""
The request payload is an object representing only the attributes, of type
Relationship, to append or update.
Note:
Update means overwriting the existing entity. If you want to
manipulate you should rather use patch_entity.
Args:
entity (ContextEntity):
append_strict: If `False` the entity attributes are updated (if they
previously exist) or appended (if they don't previously exist)
with the ones in the payload.
If `True` all the attributes in the payload not
previously existing in the entity are appended. In addition
to that, in case some of the attributes in the payload
already exist in the entity, an error is returned.
More precisely this means a strict append procedure.
Returns:
None
"""
self.update_or_append_entity_attributes(
entity_id=entity.id,
entity_type=entity.type,
attrs=entity.get_relationships(),
append_strict=append_strict,
)

def delete_entity(
self,
entity_id: str,
Expand Down
70 changes: 64 additions & 6 deletions tests/clients/test_ngsi_v2_cb.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ def test_entity_update(self):
entity_init = self.entity.model_copy(deep=True)
attr_init = entity_init.get_attribute("temperature")
attr_init.metadata = {
"metadata_init": {
"type": "Text",
"value": "something"}
}
"metadata_init": {
"type": "Text",
"value": "something"}
}
attr_append = NamedContextAttribute(**{
"name": 'pressure',
"type": 'Number',
Expand Down Expand Up @@ -393,6 +393,62 @@ def test_entity_update(self):
clear_all(fiware_header=self.fiware_header,
cb_url=settings.CB_URL)

# 4) update only property or relationship
if "update_entity_properties" or "update_entity_relationship":
# post entity with a relationship attribute
entity_init = self.entity.model_copy(deep=True)
attrs = [
NamedContextAttribute(name='in', type='Relationship', value='dummy1')]
entity_init.add_attributes(attrs=attrs)
client.post_entity(entity=entity_init, update=True)

# create entity that differs in both attributes
entity_update = entity_init.model_copy(deep=True)
attrs = [NamedContextAttribute(name='temperature',
type='Number',
value=21),
NamedContextAttribute(name='in', type='Relationship',
value='dummy2')]
entity_update.update_attribute(attrs=attrs)

# update only properties and compare
client.update_entity_properties(entity_update)
entity_db = client.get_entity(entity_update.id)
db_attrs = entity_db.get_attribute(attribute_name='temperature')
update_attrs = entity_update.get_attribute(attribute_name='temperature')
self.assertEqual(db_attrs, update_attrs)
db_attrs = entity_db.get_attribute(attribute_name='in')
update_attrs = entity_update.get_attribute(attribute_name='in')
self.assertNotEqual(db_attrs, update_attrs)

# update only relationship and compare
attrs = [
NamedContextAttribute(name='temperature', type='Number', value=22)]
entity_update.update_attribute(attrs=attrs)
client.update_entity_relationships(entity_update)
entity_db = client.get_entity(entity_update.id)
self.assertEqual(entity_db.get_attribute(attribute_name='in'),
entity_update.get_attribute(attribute_name='in'))
self.assertNotEqual(entity_db.get_attribute(attribute_name='temperature'),
entity_update.get_attribute(
attribute_name='temperature'))

# change both, update both, compare
attrs = [NamedContextAttribute(name='temperature',
type='Number',
value=23),
NamedContextAttribute(name='in', type='Relationship',
value='dummy3')]
entity_update.update_attribute(attrs=attrs)
client.update_entity(entity_update)
entity_db = client.get_entity(entity_update.id)
db_attrs = entity_db.get_attribute(attribute_name='in')
update_attrs = entity_update.get_attribute(attribute_name='in')
self.assertEqual(db_attrs, update_attrs)
db_attrs = entity_db.get_attribute(attribute_name='temperature')
update_attrs = entity_update.get_attribute(attribute_name='temperature')
self.assertEqual(db_attrs, update_attrs)

@clean_test(fiware_service=settings.FIWARE_SERVICE,
fiware_servicepath=settings.FIWARE_SERVICEPATH,
cb_url=settings.CB_URL)
Expand Down Expand Up @@ -1460,9 +1516,11 @@ def test_send_receive_string(self):
self.client.post_entity(entity=entity)

testData = "hello_test"
self.client.update_attribute_value(entity_id="string_test", attr_name="data", value=testData)
self.client.update_attribute_value(entity_id="string_test", attr_name="data",
value=testData)

readback = self.client.get_attribute_value(entity_id="string_test", attr_name="data")
readback = self.client.get_attribute_value(entity_id="string_test",
attr_name="data")

self.assertEqual(testData, readback)

Expand Down

0 comments on commit 2162d59

Please sign in to comment.