-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Added checks for collection existance!
in order to avoid any errors raising and decoupled from ETL work.
- Loading branch information
1 parent
5c940ed
commit 05f78fb
Showing
3 changed files
with
123 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from unittest import TestCase | ||
|
||
from qdrant_client import models | ||
from tc_hivemind_backend.db.qdrant import QdrantSingleton | ||
from utils.qdrant_utils import QDrantUtils | ||
|
||
|
||
class TestQDrantAvailableCollection(TestCase): | ||
def setUp(self) -> None: | ||
self.community_id = "community_sample" | ||
self.qdrant_client = QdrantSingleton.get_instance().get_client() | ||
self.qdrant_utils = QDrantUtils(self.community_id) | ||
|
||
# deleting all collections | ||
collections = self.qdrant_client.get_collections() | ||
for col in collections.collections: | ||
self.qdrant_client.delete_collection(col.name) | ||
|
||
def test_no_collection_available(self): | ||
platform = "platform1" | ||
available = self.qdrant_utils.chech_collection_exist(platform) | ||
|
||
self.assertIsInstance(available, bool) | ||
self.assertFalse(available) | ||
|
||
def test_single_collection_available(self): | ||
platform = "platform1" | ||
collection_name = f"{self.community_id}_{platform}" | ||
self.qdrant_client.create_collection( | ||
collection_name, | ||
vectors_config=models.VectorParams( | ||
size=100, distance=models.Distance.COSINE | ||
), | ||
) | ||
available = self.qdrant_utils.chech_collection_exist(platform) | ||
|
||
self.assertIsInstance(available, bool) | ||
self.assertTrue(available) | ||
|
||
def test_multiple_collections_but_not_input(self): | ||
""" | ||
test if there was multiple collections available | ||
but it isn't the collection we want to check for | ||
""" | ||
platforms = ["platform1", "platform2", "platform3"] | ||
for plt in platforms: | ||
collection_name = f"{self.community_id}_{plt}" | ||
self.qdrant_client.create_collection( | ||
collection_name, | ||
vectors_config=models.VectorParams( | ||
size=100, distance=models.Distance.COSINE | ||
), | ||
) | ||
|
||
available = self.qdrant_utils.chech_collection_exist("platform4") | ||
|
||
self.assertIsInstance(available, bool) | ||
self.assertFalse(available) | ||
|
||
def test_multiple_collections_available_given_input(self): | ||
""" | ||
test multiple collections available with given input | ||
""" | ||
platforms = ["platform1", "platform2", "platform3"] | ||
for plt in platforms: | ||
collection_name = f"{self.community_id}_{plt}" | ||
self.qdrant_client.create_collection( | ||
collection_name, | ||
vectors_config=models.VectorParams( | ||
size=100, distance=models.Distance.COSINE | ||
), | ||
) | ||
|
||
available = self.qdrant_utils.chech_collection_exist( | ||
platforms[0], | ||
) | ||
|
||
self.assertIsInstance(available, bool) | ||
self.assertTrue(available) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from tc_hivemind_backend.db.qdrant import QdrantSingleton | ||
|
||
|
||
class QDrantUtils: | ||
def __init__(self, community_id: str) -> None: | ||
""" | ||
setup qdrant utils for a specific community | ||
Parameters | ||
------------ | ||
community_id : str | ||
the community we want to initialize the utils for | ||
""" | ||
self.qdrant_client = QdrantSingleton.get_instance().get_client() | ||
self.community_id = community_id | ||
|
||
def chech_collection_exist(self, platform_name: str) -> bool: | ||
""" | ||
check if the collection exist on qdrant database | ||
Parameters | ||
----------- | ||
platform_name : str | ||
the platform name we want to check for its collection availability | ||
Returns | ||
-------- | ||
available : bool | ||
if the collection was available True, else would be False | ||
""" | ||
collection_name = f"{self.community_id}_{platform_name}" | ||
available = self.qdrant_client.collection_exists(collection_name) | ||
return available |