From 819685ca074fa30b89cafe1463341fb2b9369c5e Mon Sep 17 00:00:00 2001 From: Trayan Azarov Date: Thu, 15 Feb 2024 03:25:01 +0200 Subject: [PATCH] fix: Adding id in the model for state validation - Problem to solve: same collection names in multiple tenants/db --- chromadb/test/property/test_collections.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/chromadb/test/property/test_collections.py b/chromadb/test/property/test_collections.py index ecb93630efa..a3d4c3e9877 100644 --- a/chromadb/test/property/test_collections.py +++ b/chromadb/test/property/test_collections.py @@ -1,3 +1,5 @@ +import math + import pytest import logging import hypothesis.strategies as st @@ -54,7 +56,7 @@ def create_coll( metadata=coll.metadata, embedding_function=coll.embedding_function, ) - self.set_model(coll.name, coll.metadata) + self.set_model(coll.name, coll.metadata,str(coll.id)) assert c.name == coll.name assert c.metadata == self.model[coll.name] @@ -85,7 +87,7 @@ def delete_coll(self, coll: strategies.Collection) -> None: @rule() def list_collections(self) -> None: colls = self.api.list_collections() - assert len(colls) == len(self.model) + assert len(colls) == len([c for c in self.model if not c.startswith("__id__")]) for c in colls: assert c.name in self.model @@ -163,7 +165,7 @@ def get_or_create_coll( coll.metadata = ( self.model[coll.name] if new_metadata is None else new_metadata ) - self.set_model(coll.name, coll.metadata) + self.set_model(coll.name, coll.metadata,str(coll.id)) # Update API c = self.api.get_or_create_collection( @@ -189,6 +191,9 @@ def modify_coll( new_metadata: types.Metadata, new_name: Optional[str], ) -> MultipleResults[strategies.Collection]: + # early exit if a col with name exists but with diff id, possibly in another tenant/db + if coll.name in self.model and f"__id__:{coll.id}" not in self.model: + return multiple() if coll.name not in self.model: with pytest.raises(Exception): c = self.api.get_collection(name=coll.name) @@ -218,7 +223,7 @@ def modify_coll( self.delete_from_model(coll.name) coll.name = new_name _name = new_name - self.set_model(_name, _metadata) # type: ignore + self.set_model(_name, _metadata, str(coll.id)) c.modify(metadata=new_metadata, name=new_name) c = self.api.get_collection(name=coll.name) @@ -228,14 +233,18 @@ def modify_coll( return multiple(coll) def set_model( - self, name: str, metadata: Optional[types.CollectionMetadata] + self, name: str, metadata: Optional[types.CollectionMetadata], id: Optional[str] = None ) -> None: model = self.model model[name] = metadata + if id is not None: + model[f"__id__:{id}"] = metadata - def delete_from_model(self, name: str) -> None: + def delete_from_model(self, name: str, id: Optional[str] = None) -> None: model = self.model del model[name] + if id is not None: + del model[f"__id__:{id}"] @property def model(self) -> Dict[str, Optional[types.CollectionMetadata]]: