@@ -587,3 +587,65 @@ class EntityB2:
587587 # This might require to store the (Python) model in the Store.
588588 # with pytest.raises(ValueError):
589589 # store_b.box(EntityA)
590+ def test_sync_dynamic_entities (env ):
591+ def create_entity (entity_name : str , dimensions : int , distance_type : VectorDistanceType , uid = 0 ):
592+ DynamicEntity = type (entity_name , (), {
593+ "id" : Id (),
594+ "name" : String (),
595+ "vector" : Float32Vector (index = HnswIndex (dimensions = dimensions , distance_type = distance_type ))
596+ })
597+ return Entity (uid = uid )(DynamicEntity ) # Apply @Entity decorator
598+
599+ CosineVectorEntity = create_entity ("CosineVectorEntity" ,
600+ dimensions = 2 ,
601+ distance_type = VectorDistanceType .COSINE )
602+ EuclideanVectorEntity = create_entity ("EuclideanVectorEntity" ,
603+ dimensions = 2 ,
604+ distance_type = VectorDistanceType .EUCLIDEAN )
605+ DotProductEntity = create_entity ("DotProductEntity" ,
606+ dimensions = 2 ,
607+ distance_type = VectorDistanceType .DOT_PRODUCT_NON_NORMALIZED )
608+ model = Model ()
609+ model .entity (CosineVectorEntity )
610+ model .entity (EuclideanVectorEntity )
611+ model .entity (DotProductEntity )
612+ assert env .sync (model )
613+ CosineVectorEntity_iduid = CosineVectorEntity ._iduid
614+
615+ store = env .create_store ()
616+ cosine_box = store .box (CosineVectorEntity )
617+ cosine_box .put (CosineVectorEntity (name = "CosineObj1" , vector = [2 , 1 ]))
618+ cosine_box .put (CosineVectorEntity (name = "CosineObj2" , vector = [- 6 , 0 ]))
619+ euclidean_box = store .box (EuclideanVectorEntity )
620+ euclidean_box .put (EuclideanVectorEntity (name = "EuclideanObj1" , vector = [5 , 4 ]))
621+ euclidean_box .put (EuclideanVectorEntity (name = "EuclideanObj2" , vector = [2 , - 6 ]))
622+ dot_product_box = store .box (DotProductEntity )
623+ dot_product_box .put (DotProductEntity (name = "DotProductObj1" , vector = [10 , 0 ]))
624+ assert cosine_box .get (1 ).name == "CosineObj1"
625+ assert cosine_box .get (2 ).name == "CosineObj2"
626+ assert euclidean_box .get (1 ).name == "EuclideanObj1"
627+ assert euclidean_box .get (2 ).name == "EuclideanObj2"
628+ assert dot_product_box .get (1 ).name == "DotProductObj1"
629+
630+ del cosine_box
631+ del euclidean_box
632+ del dot_product_box
633+ del store
634+
635+ # Rename CosineVectorEntity to MyCosineVectorEntity
636+ MyCosineVectorEntity = create_entity ("MyCosineVectorEntity" ,
637+ dimensions = 2 ,
638+ distance_type = VectorDistanceType .COSINE ,
639+ uid = CosineVectorEntity_iduid .uid )
640+ model = Model ()
641+ model .entity (MyCosineVectorEntity )
642+ model .entity (EuclideanVectorEntity )
643+ model .entity (DotProductEntity )
644+ assert env .sync (model )
645+ assert CosineVectorEntity_iduid == MyCosineVectorEntity ._iduid
646+
647+ # Check MyCosineVectorEntity objects are preserved after renaming
648+ store = env .create_store ()
649+ cosine_box = store .box (MyCosineVectorEntity )
650+ assert cosine_box .get (1 ).name == "CosineObj1"
651+ assert cosine_box .get (2 ).name == "CosineObj2"
0 commit comments