From 86c6382be6f4fb63a37fdb19b9b1e7ce862e925f Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Mon, 27 Nov 2023 03:54:01 -0500 Subject: [PATCH 1/5] Fix integration tests --- pinecone/__init__.py | 4 +- pinecone/control/pinecone.py | 2 + tests/integration/conftest.py | 45 +++++++++++++------ .../test_create_index_api_errors.py | 40 ++++++++--------- ....py => test_create_index_sl_happy_path.py} | 0 .../integration/test_create_index_timeouts.py | 2 +- tests/integration/test_describe_index.py | 29 +++++++----- tests/integration/test_list_indexes.py | 21 +++++---- 8 files changed, 85 insertions(+), 58 deletions(-) rename tests/integration/{test_create_index_happy_path.py => test_create_index_sl_happy_path.py} (100%) diff --git a/pinecone/__init__.py b/pinecone/__init__.py index 4ccf6d4f3..91d143916 100644 --- a/pinecone/__init__.py +++ b/pinecone/__init__.py @@ -6,4 +6,6 @@ from .control.pinecone import Pinecone from .data.index import * from .models import * -# from .core.client.models import * \ No newline at end of file +from .core.client.models import ( + IndexModel, +) \ No newline at end of file diff --git a/pinecone/control/pinecone.py b/pinecone/control/pinecone.py index f39835d22..7c53653fe 100644 --- a/pinecone/control/pinecone.py +++ b/pinecone/control/pinecone.py @@ -83,6 +83,8 @@ def create_index( api_instance.create_index(create_index_request=CreateIndexRequest(name=name, dimension=dimension, metric=metric, spec=spec.asdict())) elif isinstance(spec, PodSpec): api_instance.create_index(create_index_request=CreateIndexRequest(name=name, dimension=dimension, metric=metric, spec=spec.asdict())) + else: + raise TypeError("spec must be of type dict, ServerlessSpec, or PodSpec") def is_ready(): status = self._get_status(name) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 09bfaba85..97c15ebe9 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -1,5 +1,6 @@ import pytest -from pinecone import Pinecone +import time +from pinecone import Pinecone, NotFoundException, ApiException from .helpers.helpers import generate_index_name, get_environment_var @pytest.fixture() @@ -13,11 +14,11 @@ def environment(): @pytest.fixture() def create_sl_index_params(index_name): - spec = { + spec = {"serverless": { 'cloud': 'aws', - 'region': 'us-east1' - } - return dict(name=index_name, dimension=10, metric='cosine', spec=spec, timeout=-1) + 'region': 'us-west-2' + }} + return dict(name=index_name, dimension=10, metric='cosine', spec=spec) @pytest.fixture() def create_pod_index_params(index_name, environment): @@ -33,36 +34,52 @@ def index_name(request): @pytest.fixture() def ready_sl_index(client, index_name, create_sl_index_params): - del create_sl_index_params['timeout'] + create_sl_index_params['timeout'] = None client.create_index(**create_sl_index_params) yield index_name client.delete_index(index_name, -1) @pytest.fixture() def notready_sl_index(client, index_name, create_sl_index_params): - client.create_index(**create_sl_index_params) + client.create_index(**create_sl_index_params, timeout=-1) yield index_name - client.delete_index(index_name, -1) @pytest.fixture() def ready_pod_index(client, index_name, create_pod_index_params): del create_pod_index_params['timeout'] client.create_index(**create_pod_index_params) yield index_name - client.delete_index(index_name, -1) @pytest.fixture() def notready_pod_index(client, index_name, create_pod_index_params): client.create_index(**create_pod_index_params) yield index_name - client.delete_index(index_name, -1) + +def delete_with_retry(client, index_name, retries=0, sleep_interval=5): + print('Deleting index ' + index_name + ', retry ' + str(retries) + ', next sleep interval ' + str(sleep_interval)) + try: + client.delete_index(index_name, -1) + except NotFoundException: + pass + except ApiException as e: + if e.error.code == 'PRECONDITON_FAILED': + if retries > 5: + raise 'Unable to delete index ' + index_name + time.sleep(sleep_interval) + delete_with_retry(client, index_name, retries + 1, sleep_interval * 2) + else: + print(e.__class__) + print(e) + raise 'Unable to delete index ' + index_name + except Exception as e: + print(e.__class__) + print(e) + raise 'Unable to delete index ' + index_name @pytest.fixture(autouse=True) def cleanup(client, index_name): yield - client.delete_index(index_name, -1) - try: client.delete_index(index_name, -1) except: @@ -73,8 +90,8 @@ def cleanup_all(): yield client = Pinecone() - for index in client.list_indexes().databases: + for index in client.list_indexes(): try: - client.delete_index(index.name, -1) + delete_with_retry(client, index.name) except: pass \ No newline at end of file diff --git a/tests/integration/test_create_index_api_errors.py b/tests/integration/test_create_index_api_errors.py index 1e7f56a7b..8eeb3f3db 100644 --- a/tests/integration/test_create_index_api_errors.py +++ b/tests/integration/test_create_index_api_errors.py @@ -1,31 +1,31 @@ import pytest -from pinecone import ApiException +from pinecone import ApiException, ApiValueError class TestCreateIndexApiErrorCases: - def test_create_index_with_invalid_name(self, client, create_index_params): - create_index_params['name'] = '-invalid-name' + def test_create_index_with_invalid_name(self, client, create_sl_index_params): + create_sl_index_params['name'] = '-invalid-name' with pytest.raises(ApiException): - client.create_index(**create_index_params) + client.create_index(**create_sl_index_params) - def test_create_index_invalid_metric(self, client, create_index_params): - create_index_params['metric'] = 'invalid' - with pytest.raises(ApiException): - client.create_index(**create_index_params) + def test_create_index_invalid_metric(self, client, create_sl_index_params): + create_sl_index_params['metric'] = 'invalid' + with pytest.raises(ApiValueError): + client.create_index(**create_sl_index_params) - def test_create_index_with_invalid_neg_dimension(self, client, create_index_params): - create_index_params['dimension'] = -1 - with pytest.raises(ApiException): - client.create_index(**create_index_params) + def test_create_index_with_invalid_neg_dimension(self, client, create_sl_index_params): + create_sl_index_params['dimension'] = -1 + with pytest.raises(ApiValueError): + client.create_index(**create_sl_index_params) - def test_create_index_that_already_exists(self, client, create_index_params): - client.create_index(**create_index_params) + def test_create_index_that_already_exists(self, client, create_sl_index_params): + client.create_index(**create_sl_index_params) with pytest.raises(ApiException): - client.create_index(**create_index_params) + client.create_index(**create_sl_index_params) @pytest.mark.skip(reason='Bug filed https://app.asana.com/0/1205078872348810/1205917627868143') - def test_create_index_w_incompatible_options(self, client, create_index_params): - create_index_params['pod_type'] = 'p1.x2' - create_index_params['environment'] = 'us-east1-gcp' - create_index_params['replicas'] = 2 + def test_create_index_w_incompatible_options(self, client, create_sl_index_params): + create_sl_index_params['pod_type'] = 'p1.x2' + create_sl_index_params['environment'] = 'us-east1-gcp' + create_sl_index_params['replicas'] = 2 with pytest.raises(ApiException): - client.create_index(**create_index_params) \ No newline at end of file + client.create_index(**create_sl_index_params) \ No newline at end of file diff --git a/tests/integration/test_create_index_happy_path.py b/tests/integration/test_create_index_sl_happy_path.py similarity index 100% rename from tests/integration/test_create_index_happy_path.py rename to tests/integration/test_create_index_sl_happy_path.py diff --git a/tests/integration/test_create_index_timeouts.py b/tests/integration/test_create_index_timeouts.py index e9d5b4137..cb7e3ba5c 100644 --- a/tests/integration/test_create_index_timeouts.py +++ b/tests/integration/test_create_index_timeouts.py @@ -2,7 +2,7 @@ class TestCreateIndexWithTimeout: def test_create_index_default_timeout(self, client, create_sl_index_params): - del create_sl_index_params['timeout'] + create_sl_index_params['timeout'] = None client.create_index(**create_sl_index_params) # Waits infinitely for index to be ready desc = client.describe_index(create_sl_index_params['name']) diff --git a/tests/integration/test_describe_index.py b/tests/integration/test_describe_index.py index 0e1cecb36..90c96bf3e 100644 --- a/tests/integration/test_describe_index.py +++ b/tests/integration/test_describe_index.py @@ -1,30 +1,37 @@ import pytest -from pinecone import IndexDescription +from pinecone import IndexModel class TestDescribeIndex: def test_describe_index_when_ready(self, client, ready_sl_index, create_sl_index_params): description = client.describe_index(ready_sl_index) - assert type(description) == IndexDescription + + assert type(description) == IndexModel assert description.name == ready_sl_index assert description.dimension == create_sl_index_params['dimension'] assert description.metric == create_sl_index_params['metric'] - assert description.capacity_mode == create_sl_index_params['capacity_mode'] + assert description.spec.serverless['cloud'] == create_sl_index_params['spec']['serverless']['cloud'] + assert description.spec.serverless['region'] == create_sl_index_params['spec']['serverless']['region'] + + assert type(description.host) == str + assert description.host != "" + assert ready_sl_index in description.host - assert type(description.status.host) == str - assert description.status.host != "" assert description.status.state == 'Ready' - assert ready_sl_index in description.status.host assert description.status.ready == True def test_describe_index_when_not_ready(self, client, notready_sl_index, create_sl_index_params): description = client.describe_index(notready_sl_index) - assert type(description) == IndexDescription + assert type(description) == IndexModel assert description.name == notready_sl_index assert description.dimension == create_sl_index_params['dimension'] assert description.metric == create_sl_index_params['metric'] + assert description.spec.serverless['cloud'] == create_sl_index_params['spec']['serverless']['cloud'] + assert description.spec.serverless['region'] == create_sl_index_params['spec']['serverless']['region'] + + assert type(description.host) == str + assert description.host != "" + assert notready_sl_index in description.host - assert type(description.status.host) == str - assert description.status.host != "" - assert notready_sl_index in description.status.host - assert description.status.ready == False \ No newline at end of file + assert description.status.ready == False + assert description.status.state in ['Ready', 'Initializing'] \ No newline at end of file diff --git a/tests/integration/test_list_indexes.py b/tests/integration/test_list_indexes.py index 66fa39e19..e3d7ae83c 100644 --- a/tests/integration/test_list_indexes.py +++ b/tests/integration/test_list_indexes.py @@ -1,23 +1,22 @@ from pinecone import IndexModel class TestListIndexes(): - def test_list_indexes_includes_ready_indexes(self, client, ready_index, create_index_params): + def test_list_indexes_includes_ready_indexes(self, client, ready_sl_index, create_sl_index_params): list_response = client.list_indexes() assert len(list_response.indexes) != 0 assert type(list_response.indexes[0]) == IndexModel - created_index = [index for index in list_response.indexes if index.name == ready_index][0] - assert created_index.name == ready_index - assert created_index.dimension == create_index_params['dimension'] - assert created_index.metric == create_index_params['metric'] - assert ready_index in created_index.host - assert created_index.capacity_mode == create_index_params['capacity_mode'] + created_index = [index for index in list_response.indexes if index.name == ready_sl_index][0] + assert created_index.name == ready_sl_index + assert created_index.dimension == create_sl_index_params['dimension'] + assert created_index.metric == create_sl_index_params['metric'] + assert ready_sl_index in created_index.host - def test_list_indexes_includes_not_ready_indexes(self, client, notready_index): + def test_list_indexes_includes_not_ready_indexes(self, client, notready_sl_index): list_response = client.list_indexes() assert len(list_response.indexes) != 0 assert type(list_response.indexes[0]) == IndexModel - created_index = [index for index in list_response.indexes if index.name == notready_index][0] - assert created_index.name == notready_index - assert notready_index in created_index.name + created_index = [index for index in list_response.indexes if index.name == notready_sl_index][0] + assert created_index.name == notready_sl_index + assert notready_sl_index in created_index.name From 9a259b925c53cc1ca534cd01e5eb92852b8c0427 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Mon, 27 Nov 2023 04:04:46 -0500 Subject: [PATCH 2/5] Fix headers error --- pinecone/core/client/rest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pinecone/core/client/rest.py b/pinecone/core/client/rest.py index 7e041c58a..9a51bb9b4 100644 --- a/pinecone/core/client/rest.py +++ b/pinecone/core/client/rest.py @@ -45,7 +45,7 @@ def __init__(self, resp): def getheaders(self): """Returns a dictionary of the response headers.""" - return self.urllib3_response.getheaders() + return self.urllib3_response.headers def getheader(self, name, default=None): """Returns a given response header.""" From 68d348bedaf6c131a0ffc78884cabbbc5add4cff Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Mon, 27 Nov 2023 04:19:52 -0500 Subject: [PATCH 3/5] Disable tests on staging --- .github/workflows/testing.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/testing.yaml b/.github/workflows/testing.yaml index 9279cacb1..748cb87d5 100644 --- a/.github/workflows/testing.yaml +++ b/.github/workflows/testing.yaml @@ -38,7 +38,9 @@ jobs: strategy: matrix: python-version: [3.8, 3.11] - pineconeEnv: ['prod', 'staging'] + pineconeEnv: + - prod + # - staging max-parallel: 1 fail-fast: false steps: From db2d136d25416322358b69273d820355b689deb6 Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Mon, 27 Nov 2023 05:14:03 -0500 Subject: [PATCH 4/5] Add upsert tests --- .github/workflows/testing.yaml | 4 --- tests/integration/conftest.py | 5 +++ tests/integration/test_upsert.py | 53 ++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 tests/integration/test_upsert.py diff --git a/.github/workflows/testing.yaml b/.github/workflows/testing.yaml index 748cb87d5..fd822de5f 100644 --- a/.github/workflows/testing.yaml +++ b/.github/workflows/testing.yaml @@ -60,8 +60,6 @@ jobs: env: PINECONE_CONTROLLER_HOST: 'https://api.pinecone.io' PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY }} - TEST_CAPACITY_MODE_1: ${{ secrets.TEST_CAPACITY_MODE_1 }} - TEST_CAPACITY_MODE_2: ${{ secrets.TEST_CAPACITY_MODE_2 }} GITHUB_BUILD_NUMBER: "${{ github.run_number }}-p-${{ matrix.python-version}}" - name: Run integration tests (REST, staging) @@ -70,8 +68,6 @@ jobs: env: PINECONE_CONTROLLER_HOST: 'https://api-staging.pinecone.io' PINECONE_API_KEY: ${{ secrets.PINECONE_API_KEY_STAGING }} - TEST_CAPACITY_MODE_1: ${{ secrets.TEST_CAPACITY_MODE_1 }} - TEST_CAPACITY_MODE_2: ${{ secrets.TEST_CAPACITY_MODE_2 }} GITHUB_BUILD_NUMBER: "${{ github.run_number }}-s-${{ matrix.python-version}}" units-grpc: diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 97c15ebe9..79b78419b 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -1,4 +1,5 @@ import pytest +import random import time from pinecone import Pinecone, NotFoundException, ApiException from .helpers.helpers import generate_index_name, get_environment_var @@ -27,6 +28,10 @@ def create_pod_index_params(index_name, environment): } return dict(name=index_name, dimension=10, metric='cosine', spec=spec, timeout=-1) +@pytest.fixture() +def random_vector(): + return [random.uniform(0, 1) for _ in range(10)] + @pytest.fixture() def index_name(request): test_name = request.node.name diff --git a/tests/integration/test_upsert.py b/tests/integration/test_upsert.py new file mode 100644 index 000000000..91aa90bdd --- /dev/null +++ b/tests/integration/test_upsert.py @@ -0,0 +1,53 @@ +import pytest +from pinecone import Vector, SparseValues + +class TestUpsert: + def test_upsert_sanity(self, client, ready_sl_index, random_vector): + idx = client.Index(ready_sl_index) + + # Tuples + idx.upsert(vectors=[('1', random_vector), ('2', random_vector), ('3', random_vector)]) + + # Tuples with metadata + idx.upsert(vectors=[('4', random_vector, {'key': 'value'}), ('5', random_vector, {'key': 'value2'})]) + + # Vector objects + idx.upsert(vectors=[Vector(id='6', values=random_vector)]) + idx.upsert(vectors=[Vector(id='7', values=random_vector, metadata={'key': 'value'})]) + + # Dict + idx.upsert(vectors=[{'id': '8', 'values': random_vector}]) + + # Dict with metadata + idx.upsert(vectors=[{'id': '8', 'values': random_vector, 'metadata': {'key': 'value'}}]) + + idx.describe_index_stats() + + def test_upsert_sparse_vectors(self, client, random_vector, create_sl_index_params, index_name): + create_sl_index_params['metric'] = 'dotproduct' + create_sl_index_params['timeout'] = 300 + client.create_index(**create_sl_index_params) + + idx = client.Index(index_name) + idx.upsert(vectors=[Vector(id='1', values=random_vector, sparse_values=SparseValues(values=[0.1, 0.2, 0.3], indices=[1, 2, 3]))]) + idx.upsert(vectors=[{'id': '8', 'values': random_vector, 'metadata': {'key': 'value'}, 'sparse_values': {'values': [0.1, 0.2, 0.3], 'indices': [1, 2, 3] }}]) + + def test_upsert_with_invalid_vector(self, client, ready_sl_index, random_vector): + idx = client.Index(ready_sl_index) + + with pytest.raises(TypeError): + # non-vector + idx.upsert(vectors=[('1', 'invalid_vector')]) + + with pytest.raises(TypeError): + # bogus metadata + idx.upsert(vectors=[('1', random_vector, 'invalid_metadata')]) + + with pytest.raises(TypeError): + # non-string id + idx.upsert(vectors=[(1, random_vector)]) + + with pytest.raises(TypeError): + idx.upsert(vectors=[{'id': 1, 'values': random_vector}]) + + \ No newline at end of file From 53e12db453ad37addd92c428c28c85ec177c0b3f Mon Sep 17 00:00:00 2001 From: Jen Hamon Date: Mon, 27 Nov 2023 05:37:46 -0500 Subject: [PATCH 5/5] Remove podtype enum --- .../model/configure_index_request_spec_pod.py | 14 -------------- pinecone/core/client/model/pod_spec.py | 18 ++---------------- 2 files changed, 2 insertions(+), 30 deletions(-) diff --git a/pinecone/core/client/model/configure_index_request_spec_pod.py b/pinecone/core/client/model/configure_index_request_spec_pod.py index 809c21c9f..a11a43757 100644 --- a/pinecone/core/client/model/configure_index_request_spec_pod.py +++ b/pinecone/core/client/model/configure_index_request_spec_pod.py @@ -55,20 +55,6 @@ class ConfigureIndexRequestSpecPod(ModelNormal): """ allowed_values = { - ('pod_type',): { - 'S1.X1': "s1.x1", - 'S1.X2': "s1.x2", - 'S1.X4': "s1.x4", - 'S1.X8': "s1.x8", - 'P1.X1': "p1.x1", - 'P1.X2': "p1.x2", - 'P1.X4': "p1.x4", - 'P1.X8': "p1.x8", - 'P2.X1': "p2.x1", - 'P2.X2': "p2.x2", - 'P2.X4': "p2.x4", - 'P2.X8': "p2.x8", - }, } validations = { diff --git a/pinecone/core/client/model/pod_spec.py b/pinecone/core/client/model/pod_spec.py index a8b89cbfe..62d897214 100644 --- a/pinecone/core/client/model/pod_spec.py +++ b/pinecone/core/client/model/pod_spec.py @@ -59,20 +59,6 @@ class PodSpec(ModelNormal): """ allowed_values = { - ('pod_type',): { - 'S1.X1': "s1.x1", - 'S1.X2': "s1.x2", - 'S1.X4': "s1.x4", - 'S1.X8': "s1.x8", - 'P1.X1': "p1.x1", - 'P1.X2': "p1.x2", - 'P1.X4': "p1.x4", - 'P1.X8': "p1.x8", - 'P2.X1': "p2.x1", - 'P2.X2': "p2.x2", - 'P2.X4': "p2.x4", - 'P2.X8': "p2.x8", - }, } validations = { @@ -150,7 +136,7 @@ def _from_openapi_data(cls, environment, *args, **kwargs): # noqa: E501 Keyword Args: replicas (int): The number of replicas. Replicas duplicate your index. They provide higher availability and throughput. Replicas can be scaled up or down as your needs change.. defaults to 1 # noqa: E501 shards (int): The number of shards. Shards split your data across multiple pods so you can fit more data into an index.. defaults to 1 # noqa: E501 - pod_type (str): The type of pod to use. One of `s1`, `p1`, or `p2` appended with `.` and one of `x1`, `x2`, `x4`, or `x8`.. defaults to "p1.x1", must be one of ["s1.x1", "s1.x2", "s1.x4", "s1.x8", "p1.x1", "p1.x2", "p1.x4", "p1.x8", "p2.x1", "p2.x2", "p2.x4", "p2.x8", ] # noqa: E501 + pod_type (str): The type of pod to use. One of `s1`, `p1`, or `p2` appended with `.` and one of `x1`, `x2`, `x4`, or `x8`.. defaults to "p1.x1" # noqa: E501 pods (int): The number of pods to be used in the index. This should be equal to `shards` x `replicas`.. defaults to 1 # noqa: E501 _check_type (bool): if True, values for parameters in openapi_types will be type checked and a TypeError will be @@ -249,7 +235,7 @@ def __init__(self, environment, *args, **kwargs): # noqa: E501 Keyword Args: replicas (int): The number of replicas. Replicas duplicate your index. They provide higher availability and throughput. Replicas can be scaled up or down as your needs change.. defaults to 1 # noqa: E501 shards (int): The number of shards. Shards split your data across multiple pods so you can fit more data into an index.. defaults to 1 # noqa: E501 - pod_type (str): The type of pod to use. One of `s1`, `p1`, or `p2` appended with `.` and one of `x1`, `x2`, `x4`, or `x8`.. defaults to "p1.x1", must be one of ["s1.x1", "s1.x2", "s1.x4", "s1.x8", "p1.x1", "p1.x2", "p1.x4", "p1.x8", "p2.x1", "p2.x2", "p2.x4", "p2.x8", ] # noqa: E501 + pod_type (str): The type of pod to use. One of `s1`, `p1`, or `p2` appended with `.` and one of `x1`, `x2`, `x4`, or `x8`.. defaults to "p1.x1" # noqa: E501 pods (int): The number of pods to be used in the index. This should be equal to `shards` x `replicas`.. defaults to 1 # noqa: E501 _check_type (bool): if True, values for parameters in openapi_types will be type checked and a TypeError will be