Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions pinecone/control/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
IndexMetric,
IndexDimension,
)
from pinecone.models import ServerlessSpec, PodSpec

from pinecone.models import ServerlessSpec, PodSpec, IndexList, CollectionList

from pinecone.data import Index


class Pinecone:
def __init__(
self,
Expand Down Expand Up @@ -146,9 +144,10 @@ def get_remaining():
)
)

def list_indexes(self):
def list_indexes(self) -> IndexList:
"""Lists all indexes."""
return self.index_api.list_indexes()
response = self.index_api.list_indexes()
return IndexList(response)

def describe_index(self, name: str):
"""Describes a Pinecone index.
Expand Down Expand Up @@ -197,11 +196,11 @@ def create_collection(self, name: str, source: str):
api_instance = self.index_api
api_instance.create_collection(create_collection_request=CreateCollectionRequest(name=name, source=source))

def list_collections(self):
def list_collections(self) -> CollectionList:
"""List all collections"""
api_instance = self.index_api
response = api_instance.list_collections()
return response
return CollectionList(response)

def delete_collection(self, name: str):
"""Deletes a collection.
Expand Down
6 changes: 5 additions & 1 deletion pinecone/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from .collection_description import CollectionDescription
from .serverless_spec import ServerlessSpec
from .pod_spec import PodSpec
from .index_list import IndexList
from .collection_list import CollectionList

__all__ = [
'CollectionDescription',
Expand All @@ -10,5 +12,7 @@
'PodSpec',
'PodSpecDefinition',
'ServerlessSpec',
'ServerlessSpecDefinition'
'ServerlessSpecDefinition',
'IndexList',
'CollectionList'
]
24 changes: 24 additions & 0 deletions pinecone/models/collection_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pinecone.core.client.models import CollectionList as OpenAPICollectionList

class CollectionList:
def __init__(self, collection_list: OpenAPICollectionList):
self.collection_list = collection_list
self.current = 0

def __getitem__(self, key):
return self.collection_list.collections[key]

def __len__(self):
return len(self.collection_list.collections)

def __iter__(self):
return iter(self.collection_list.collections)

def __str__(self):
return str(self.collection_list)

def __repr__(self):
return repr(self.collection_list)

def __getattr__(self, attr):
return getattr(self.collection_list, attr)
24 changes: 24 additions & 0 deletions pinecone/models/index_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from pinecone.core.client.models import IndexList as OpenAPIIndexList

class IndexList:
def __init__(self, index_list: OpenAPIIndexList):
self.index_list = index_list
self.current = 0

def __getitem__(self, key):
return self.index_list.indexes[key]

def __len__(self):
return len(self.index_list.indexes)

def __iter__(self):
return iter(self.index_list.indexes)

def __str__(self):
return str(self.index_list)

def __repr__(self):
return repr(self.index_list)

def __getattr__(self, attr):
return getattr(self.index_list, attr)
30 changes: 30 additions & 0 deletions tests/unit/models/test_collection_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
from pinecone import CollectionList
from pinecone.core.client.models import CollectionList as OpenApiCollectionList, CollectionModel

@pytest.fixture
def collection_list_response():
return OpenApiCollectionList(
collections=[
CollectionModel(name='collection1', size=10000, status='Ready', dimension=1536, record_count=1000),
CollectionModel(name='collection2', size=20000, status='Ready', dimension=256, record_count=2000),
],
)

class TestCollectionList:
def test_collection_list_has_length(self, collection_list_response):
assert len(CollectionList(collection_list_response)) == 2

def test_collection_list_is_(self, collection_list_response):
icl = CollectionList(collection_list_response)
assert [i['name'] for i in icl] == ['collection1', 'collection2']
assert [i.record_count for i in icl] == [1000, 2000]

def test_collection_list_getitem(self, collection_list_response):
icl = CollectionList(collection_list_response)
assert icl[0] == collection_list_response.collections[0]
assert icl[1] == collection_list_response.collections[1]

def test_collection_list_proxies_methods(self, collection_list_response):
# Forward compatibility, in case we add more attributes to IndexList for pagination
assert CollectionList(collection_list_response).collection_list.collections == collection_list_response.collections
54 changes: 54 additions & 0 deletions tests/unit/models/test_index_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import pytest
from pinecone import IndexList
from pinecone.core.client.models import IndexList as OpenApiIndexList

@pytest.fixture
def index_list_response():
return OpenApiIndexList(
indexes=[
{
"name": "test-index-1",
"dimension": 2,
"metric": "cosine",
"spec": {
"pod": {
"environment": "us-west1-gcp",
"pod_type": "p1.x1",
"pods": 1,
"replicas": 1,
"shards": 1
}
}
},
{
"name": "test-index-2",
"dimension": 2,
"metric": "cosine",
"spec": {
"serverless": {
"cloud": "aws",
"region": "us-west-2"
}
}
},
], _check_type=False
)

class TestIndexList:
def test_index_list_has_length(self, index_list_response):
assert len(IndexList(index_list_response)) == 2

def test_index_list_is(self, index_list_response):
iil = IndexList(index_list_response)
assert [i['name'] for i in iil] == ['test-index-1', 'test-index-2']

def test_index_list_getitem(self, index_list_response):
iil = IndexList(index_list_response)
assert iil[0] == index_list_response.indexes[0]
assert iil[1] == index_list_response.indexes[1]

def test_index_list_proxies_methods(self, index_list_response):
# Forward compatibility, in case we add more attributes to IndexList for pagination
assert IndexList(index_list_response).index_list.indexes == index_list_response.indexes


18 changes: 18 additions & 0 deletions tests/unit/test_control.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import pytest
from pinecone import Pinecone, PodSpec, ServerlessSpec
from pinecone.core.client.models import IndexList, IndexModel
import time

@pytest.fixture
def index_list_response():
return IndexList(indexes=[
IndexModel(name="index1", dimension=10, metric="euclidean", status={"ready": True}, spec={}, _check_type=False),
IndexModel(name="index2", dimension=10, metric="euclidean", status={"ready": True}, spec={}, _check_type=False),
IndexModel(name="index3", dimension=10, metric="euclidean", status={"ready": True}, spec={}, _check_type=False),

])

class TestControl:
def test_default_host(self):
p = Pinecone(api_key="123-456-789")
Expand Down Expand Up @@ -50,3 +60,11 @@ def test_create_index_when_timeout_exceeded(self, mocker):
mocker.patch('time.sleep')

p.create_index(name="my-index", dimension=10, timeout=10, spec=PodSpec(environment="us-west1-gcp"))

def test_list_indexes_returns_iterable(self, mocker, index_list_response):
p = Pinecone(api_key="123-456-789")

mocker.patch.object(p.index_api, 'list_indexes', side_effect=[index_list_response])

response = p.list_indexes()
assert [i.name for i in response] == ["index1", "index2", "index3"]