Skip to content

Commit 2162d59

Browse files
authored
Merge pull request #271 from RWTH-EBC/145-update-relationships-with-contextbrokerclient-update_entity
add ``update_entity_relationships`` to allow relationship update
2 parents 1737977 + 00e7d5d commit 2162d59

File tree

3 files changed

+126
-6
lines changed

3 files changed

+126
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### v0.4.2
22
- add: validation for JEXL based expression ([#260](https://github.com/RWTH-EBC/FiLiP/pull/260))
33
- add: tutorials for multi-entity ([#260](https://github.com/RWTH-EBC/FiLiP/pull/260))
4+
- add: add ``update_entity_relationships`` to allow relationship update ([#271](https://github.com/RWTH-EBC/FiLiP/pull/271))
45
- add: flag to determine the deletion of registration when clearing the CB ([#267](https://github.com/RWTH-EBC/FiLiP/pull/267))
56

67
### v0.4.1

filip/clients/ngsi_v2/cb.py

+61
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,36 @@ def update_entity(self, entity: ContextEntity, append_strict: bool = False):
546546
The request payload is an object representing the attributes to
547547
append or update.
548548
549+
Note:
550+
Update means overwriting the existing entity. If you want to
551+
manipulate you should rather use patch_entity.
552+
553+
Args:
554+
entity (ContextEntity):
555+
append_strict: If `False` the entity attributes are updated (if they
556+
previously exist) or appended (if they don't previously exist)
557+
with the ones in the payload.
558+
If `True` all the attributes in the payload not
559+
previously existing in the entity are appended. In addition
560+
to that, in case some of the attributes in the payload
561+
already exist in the entity, an error is returned.
562+
More precisely this means a strict append procedure.
563+
564+
Returns:
565+
None
566+
"""
567+
self.update_or_append_entity_attributes(
568+
entity_id=entity.id,
569+
entity_type=entity.type,
570+
attrs=entity.get_attributes(),
571+
append_strict=append_strict,
572+
)
573+
574+
def update_entity_properties(self, entity: ContextEntity, append_strict: bool = False):
575+
"""
576+
The request payload is an object representing the attributes, of any type
577+
but Relationship, to append or update.
578+
549579
Note:
550580
Update means overwriting the existing entity. If you want to
551581
manipulate you should rather use patch_entity.
@@ -571,6 +601,37 @@ def update_entity(self, entity: ContextEntity, append_strict: bool = False):
571601
append_strict=append_strict,
572602
)
573603

604+
def update_entity_relationships(self, entity: ContextEntity,
605+
append_strict: bool = False):
606+
"""
607+
The request payload is an object representing only the attributes, of type
608+
Relationship, to append or update.
609+
610+
Note:
611+
Update means overwriting the existing entity. If you want to
612+
manipulate you should rather use patch_entity.
613+
614+
Args:
615+
entity (ContextEntity):
616+
append_strict: If `False` the entity attributes are updated (if they
617+
previously exist) or appended (if they don't previously exist)
618+
with the ones in the payload.
619+
If `True` all the attributes in the payload not
620+
previously existing in the entity are appended. In addition
621+
to that, in case some of the attributes in the payload
622+
already exist in the entity, an error is returned.
623+
More precisely this means a strict append procedure.
624+
625+
Returns:
626+
None
627+
"""
628+
self.update_or_append_entity_attributes(
629+
entity_id=entity.id,
630+
entity_type=entity.type,
631+
attrs=entity.get_relationships(),
632+
append_strict=append_strict,
633+
)
634+
574635
def delete_entity(
575636
self,
576637
entity_id: str,

tests/clients/test_ngsi_v2_cb.py

+64-6
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@ def test_entity_update(self):
261261
entity_init = self.entity.model_copy(deep=True)
262262
attr_init = entity_init.get_attribute("temperature")
263263
attr_init.metadata = {
264-
"metadata_init": {
265-
"type": "Text",
266-
"value": "something"}
267-
}
264+
"metadata_init": {
265+
"type": "Text",
266+
"value": "something"}
267+
}
268268
attr_append = NamedContextAttribute(**{
269269
"name": 'pressure',
270270
"type": 'Number',
@@ -393,6 +393,62 @@ def test_entity_update(self):
393393
clear_all(fiware_header=self.fiware_header,
394394
cb_url=settings.CB_URL)
395395

396+
# 4) update only property or relationship
397+
if "update_entity_properties" or "update_entity_relationship":
398+
# post entity with a relationship attribute
399+
entity_init = self.entity.model_copy(deep=True)
400+
attrs = [
401+
NamedContextAttribute(name='in', type='Relationship', value='dummy1')]
402+
entity_init.add_attributes(attrs=attrs)
403+
client.post_entity(entity=entity_init, update=True)
404+
405+
# create entity that differs in both attributes
406+
entity_update = entity_init.model_copy(deep=True)
407+
attrs = [NamedContextAttribute(name='temperature',
408+
type='Number',
409+
value=21),
410+
NamedContextAttribute(name='in', type='Relationship',
411+
value='dummy2')]
412+
entity_update.update_attribute(attrs=attrs)
413+
414+
# update only properties and compare
415+
client.update_entity_properties(entity_update)
416+
entity_db = client.get_entity(entity_update.id)
417+
db_attrs = entity_db.get_attribute(attribute_name='temperature')
418+
update_attrs = entity_update.get_attribute(attribute_name='temperature')
419+
self.assertEqual(db_attrs, update_attrs)
420+
db_attrs = entity_db.get_attribute(attribute_name='in')
421+
update_attrs = entity_update.get_attribute(attribute_name='in')
422+
self.assertNotEqual(db_attrs, update_attrs)
423+
424+
# update only relationship and compare
425+
attrs = [
426+
NamedContextAttribute(name='temperature', type='Number', value=22)]
427+
entity_update.update_attribute(attrs=attrs)
428+
client.update_entity_relationships(entity_update)
429+
entity_db = client.get_entity(entity_update.id)
430+
self.assertEqual(entity_db.get_attribute(attribute_name='in'),
431+
entity_update.get_attribute(attribute_name='in'))
432+
self.assertNotEqual(entity_db.get_attribute(attribute_name='temperature'),
433+
entity_update.get_attribute(
434+
attribute_name='temperature'))
435+
436+
# change both, update both, compare
437+
attrs = [NamedContextAttribute(name='temperature',
438+
type='Number',
439+
value=23),
440+
NamedContextAttribute(name='in', type='Relationship',
441+
value='dummy3')]
442+
entity_update.update_attribute(attrs=attrs)
443+
client.update_entity(entity_update)
444+
entity_db = client.get_entity(entity_update.id)
445+
db_attrs = entity_db.get_attribute(attribute_name='in')
446+
update_attrs = entity_update.get_attribute(attribute_name='in')
447+
self.assertEqual(db_attrs, update_attrs)
448+
db_attrs = entity_db.get_attribute(attribute_name='temperature')
449+
update_attrs = entity_update.get_attribute(attribute_name='temperature')
450+
self.assertEqual(db_attrs, update_attrs)
451+
396452
@clean_test(fiware_service=settings.FIWARE_SERVICE,
397453
fiware_servicepath=settings.FIWARE_SERVICEPATH,
398454
cb_url=settings.CB_URL)
@@ -1460,9 +1516,11 @@ def test_send_receive_string(self):
14601516
self.client.post_entity(entity=entity)
14611517

14621518
testData = "hello_test"
1463-
self.client.update_attribute_value(entity_id="string_test", attr_name="data", value=testData)
1519+
self.client.update_attribute_value(entity_id="string_test", attr_name="data",
1520+
value=testData)
14641521

1465-
readback = self.client.get_attribute_value(entity_id="string_test", attr_name="data")
1522+
readback = self.client.get_attribute_value(entity_id="string_test",
1523+
attr_name="data")
14661524

14671525
self.assertEqual(testData, readback)
14681526

0 commit comments

Comments
 (0)