@@ -170,6 +170,99 @@ def test_reload(live_c8y):
170170 assert 'c8y_AdditionalFragment' not in obj2 .fragments
171171
172172
173+ @pytest .fixture (name = 'asset_hierarchy_root_id' , scope = 'module' )
174+ def fix_asset_hierarchy_root_id (module_factory ):
175+ """Provide a (read-only) sample asset hierarchy for corresponding tests.
176+
177+ This fixture creates a root object with a child of each kind (asset,
178+ device, addition). Each of the children references to another 'addition'
179+ child to create a multi-level hierarchy.
180+
181+ It is automatically cleaned up after testing.
182+ """
183+ name = RandomNameGenerator .random_name ()
184+ obj = module_factory (ManagedObject (name = f'Root-{ name } ' , type = f'Root-{ name } ' ))
185+
186+ addition = module_factory (ManagedObject (name = f'Addition-{ name } ' , type = f'Addition-{ name } ' ))
187+ asset = module_factory (ManagedObject (name = f'Asset-{ name } ' , type = f'Asset-{ name } ' ))
188+ device = module_factory (Device (name = f'Device-{ name } ' , type = f'Device-{ name } ' ))
189+ obj .add_child_addition (addition )
190+ obj .add_child_asset (asset )
191+ obj .add_child_device (device )
192+
193+ sub_addition = module_factory (ManagedObject (name = f'SubAddition-{ name } ' , type = f'Addition-{ name } ' ))
194+ addition .add_child_addition (sub_addition )
195+ asset .add_child_addition (sub_addition )
196+ device .add_child_addition (sub_addition )
197+
198+ return obj .id
199+
200+
201+ def test_references (live_c8y : CumulocityApi , asset_hierarchy_root_id ):
202+ """Verify that parent references are handles as expected.
203+
204+ This test uses the "asset_hierarchy" fixture which defines a root
205+ with children of each kind.
206+ """
207+ root_id = asset_hierarchy_root_id
208+
209+ # (1) ignore children and parents
210+ result = live_c8y .inventory .get (root_id , with_children = False )
211+ assert not result .child_assets
212+ assert not result .child_devices
213+ assert not result .child_additions
214+ assert not result .parent_assets
215+ assert not result .parent_devices
216+ assert not result .parent_additions
217+
218+ # (2) include children, with names
219+ result = live_c8y .inventory .get (root_id , with_children = True , skip_children_names = False )
220+ # -> the root object references one of each
221+ assert len (result .child_assets ) == 1
222+ assert len (result .child_devices ) == 1
223+ assert len (result .child_additions ) == 1
224+ # -> including their names
225+ assert result .child_assets [0 ].name
226+ assert result .child_devices [0 ].name
227+ assert result .child_additions [0 ].name
228+ # -> but no parents
229+ assert not result .parent_assets
230+ assert not result .parent_devices
231+ assert not result .parent_additions
232+
233+ # (3) include children, no names
234+ result = live_c8y .inventory .get (root_id , with_children = True , skip_children_names = True )
235+ # -> the root object references one of each
236+ assert len (result .child_assets ) == 1
237+ assert len (result .child_devices ) == 1
238+ assert len (result .child_additions ) == 1
239+ # -> including their names
240+ assert not result .child_assets [0 ].name
241+ assert not result .child_devices [0 ].name
242+ assert not result .child_additions [0 ].name
243+
244+
245+ @pytest .mark .parametrize ('child_type' , ['asset' , 'device' , 'addition' ])
246+ def test_parent_references (live_c8y : CumulocityApi , asset_hierarchy_root_id , child_type ):
247+ """Verify that parent references are handles as expected.
248+
249+ This test uses the "asset_hierarchy" fixture which defines a root
250+ with children of each kind. Each kind has another "addition" child.
251+ """
252+ root = live_c8y .inventory .get (asset_hierarchy_root_id , with_children = True )
253+ child = root .__dict__ [f'child_{ child_type } s' ][0 ]
254+
255+ # read child with references
256+ result = live_c8y .inventory .get (child .id , with_children = True , with_parents = True )
257+ # parent (root) is linked by the child's type
258+ parents = result .__dict__ [f'parent_{ child_type } s' ]
259+ assert len (parents ) == 1
260+ assert parents [0 ].id == root .id
261+ assert parents [0 ].name == root .name
262+ # each child as an 'addition' child
263+ assert len (result .child_additions ) == 1
264+
265+
173266def test_deletion (live_c8y : CumulocityApi , safe_create ):
174267 """Verify that deletion works as expected.
175268
0 commit comments