github.com/weaviate/weaviate@v1.24.6/test/acceptance_with_python/test_cyclic_refs.py (about) 1 import weaviate.classes as wvc 2 3 from conftest import CollectionFactory 4 5 6 def test_ref_with_cycle(collection_factory: CollectionFactory) -> None: 7 col = collection_factory( 8 properties=[wvc.config.Property(name="name", data_type=wvc.config.DataType.TEXT)] 9 ) 10 col.config.add_reference(wvc.config.ReferenceProperty(name="ref", target_collection=col.name)) 11 12 a = col.data.insert(properties={"name": "A"}) 13 b = col.data.insert(properties={"name": "B"}, references={"ref": a}) 14 col.data.reference_add(from_uuid=a, from_property="ref", to=b) 15 16 ret = col.query.fetch_objects( 17 return_references=[ 18 wvc.query.QueryReference( 19 link_on="ref", 20 return_properties="name", 21 return_references=[ 22 wvc.query.QueryReference( 23 link_on="ref", 24 return_properties="name", 25 return_metadata=wvc.query.MetadataQuery.full(), 26 ) 27 ], 28 return_metadata=wvc.query.MetadataQuery.full(), 29 ), 30 ], 31 ).objects 32 33 ret = sorted(ret, key=lambda x: x.properties["name"]) 34 assert ret[0].properties["name"] == "A" 35 assert ret[1].properties["name"] == "B" 36 assert ret[0].references["ref"].objects[0].properties["name"] == "B" 37 assert ret[1].references["ref"].objects[0].properties["name"] == "A" 38 39 40 def test_ref_with_multiple_cycle(collection_factory: CollectionFactory) -> None: 41 col = collection_factory( 42 properties=[wvc.config.Property(name="name", data_type=wvc.config.DataType.TEXT)] 43 ) 44 col.config.add_reference(wvc.config.ReferenceProperty(name="ref", target_collection=col.name)) 45 46 # Add objects with two cyclic paths 47 # c => b => a => c 48 # c => a => c 49 a = col.data.insert(properties={"name": "A"}) 50 b = col.data.insert(properties={"name": "B"}, references={"ref": a}) 51 c = col.data.insert(properties={"name": "C"}, references={"ref": [b, a]}) # has two refs 52 col.data.reference_add(from_uuid=a, from_property="ref", to=c) 53 54 ret = col.query.fetch_objects( 55 return_references=[ 56 wvc.query.QueryReference( 57 link_on="ref", 58 return_properties=["name"], 59 return_references=[ 60 wvc.query.QueryReference( 61 link_on="ref", 62 return_properties="name", 63 return_metadata=wvc.query.MetadataQuery.full(), 64 return_references=[ 65 wvc.query.QueryReference( 66 link_on="ref", 67 return_properties="name", 68 return_metadata=wvc.query.MetadataQuery.full(), 69 ) 70 ], 71 ) 72 ], 73 return_metadata=wvc.query.MetadataQuery.full(), 74 ), 75 ], 76 ).objects 77 78 # both paths are resolved correctly 79 ret = sorted(ret, key=lambda x: x.properties["name"]) 80 assert ret[0].properties["name"] == "A" 81 assert ret[1].properties["name"] == "B" 82 assert ret[2].properties["name"] == "C" 83 84 assert ret[0].references["ref"].objects[0].properties["name"] == "C" 85 assert ret[1].references["ref"].objects[0].properties["name"] == "A" 86 87 ret2_objects = sorted(ret[2].references["ref"].objects, key=lambda x: x.properties["name"]) 88 assert ret2_objects[0].properties["name"] == "A" 89 assert ret2_objects[1].properties["name"] == "B"