Skip to content

Commit

Permalink
Fix tests, add validation of dp value
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed Jul 18, 2024
1 parent 5818072 commit 343751c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
12 changes: 8 additions & 4 deletions pinecone/control/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,10 @@ def create_index(
def _parse_non_empty_args(args: List[Tuple[str, Any]]) -> Dict[str, Any]:
return {arg_name: val for arg_name, val in args if val is not None}

dp = DeletionProtection(deletion_protection)
if deletion_protection in ["enabled", "disabled"]:
dp = DeletionProtection(deletion_protection)
else:
raise ValueError("deletion_protection must be either 'enabled' or 'disabled'")

if isinstance(spec, dict):
if "serverless" in spec:
Expand Down Expand Up @@ -573,12 +576,13 @@ def configure_index(
"""
api_instance = self.index_api

description = self.describe_index(name=name)

if deletion_protection is None:
description = self.describe_index(name=name)
dp = DeletionProtection(description.deletion_protection)
else:
elif deletion_protection in ["enabled", "disabled"]:
dp = DeletionProtection(deletion_protection)
else:
raise ValueError("deletion_protection must be either 'enabled' or 'disabled'")

pod_config_args: Dict[str, Any] = {}
if pod_type:
Expand Down
3 changes: 2 additions & 1 deletion pinecone/models/index_list.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pinecone.core.openapi.control.models import IndexList as OpenAPIIndexList
from .index_model import IndexModel


class IndexList:
Expand All @@ -10,7 +11,7 @@ def names(self):
return [i["name"] for i in self.index_list.indexes]

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

def __len__(self):
return len(self.index_list.indexes)
Expand Down
16 changes: 14 additions & 2 deletions tests/integration/control/serverless/test_deletion_protection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,28 @@
class TestDeletionProtection:
def test_deletion_protection(self, client, create_sl_index_params):
name = create_sl_index_params["name"]
client.create_index(**create_sl_index_params, deletion_protection=True)
client.create_index(**create_sl_index_params, deletion_protection="enabled")
desc = client.describe_index(name)
assert desc.deletion_protection == "enabled"

with pytest.raises(Exception) as e:
client.delete_index(name)
assert "Deletion protection is enabled for this index" in str(e.value)

client.configure_index(name, deletion_protection=False)
client.configure_index(name, deletion_protection="disabled")
desc = client.describe_index(name)
assert desc.deletion_protection == "disabled"

client.delete_index(name)

@pytest.mark.parametrize("deletion_protection", ["invalid", None])
def test_deletion_protection_invalid_options(self, client, create_sl_index_params, deletion_protection):
with pytest.raises(Exception) as e:
client.create_index(**create_sl_index_params, deletion_protection=deletion_protection)
assert "deletion_protection must be either 'enabled' or 'disabled'" in str(e.value)

@pytest.mark.parametrize("deletion_protection", ["invalid"])
def test_configure_deletion_protection_invalid_options(self, client, create_sl_index_params, deletion_protection):
with pytest.raises(Exception) as e:
client.create_index(**create_sl_index_params, deletion_protection=deletion_protection)
assert "deletion_protection must be either 'enabled' or 'disabled'" in str(e.value)

0 comments on commit 343751c

Please sign in to comment.