Skip to content

Commit

Permalink
Break up grpc unit tests into smaller files
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamon committed Jan 12, 2024
1 parent 0189031 commit f80b60e
Show file tree
Hide file tree
Showing 11 changed files with 1,024 additions and 759 deletions.
6 changes: 3 additions & 3 deletions pinecone/data/vector_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, item):
message = f"Found a tuple of length {len(item)} which is not supported. Vectors can be represented as tuples either the form (id, values, metadata) or (id, values). To pass sparse values please use either dicts or Vector objects as inputs."
super().__init__(message)

class SparseValuesTypeError(ValueError):
class SparseValuesTypeError(ValueError, TypeError):
def __init__(self):
message = "Found unexpected data in column `sparse_values`. Expected format is `'sparse_values': {'indices': List[int], 'values': List[float]}`."
super().__init__(message)
Expand All @@ -37,12 +37,12 @@ def __init__(self, sparse_values_dict):
message = f"Missing required keys in data in column `sparse_values`. Expected format is `'sparse_values': {{'indices': List[int], 'values': List[float]}}`. Found keys {list(sparse_values_dict.keys())}"
super().__init__(message)

class SparseValuesDictionaryExpectedError(ValueError):
class SparseValuesDictionaryExpectedError(ValueError, TypeError):
def __init__(self, sparse_values_dict):
message = f"Column `sparse_values` is expected to be a dictionary, found {type(sparse_values_dict)}"
super().__init__(message)

class MetadataDictionaryExpectedError(ValueError):
class MetadataDictionaryExpectedError(ValueError, TypeError):
def __init__(self, item):
message = f"Column `metadata` is expected to be a dictionary, found {type(item['metadata'])}"
super().__init__(message)
Expand Down
4 changes: 3 additions & 1 deletion pinecone/grpc/index_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@
UpdateResponse,
SparseValues as GRPCSparseValues,
)
from pinecone import Vector as NonGRPCVector
from pinecone.core.grpc.protos.vector_service_pb2_grpc import VectorServiceStub
from pinecone.utils import fix_tuple_length
from .base import GRPCIndexBase
from .future import PineconeGrpcFuture


__all__ = ["GRPCIndex", "GRPCVector", "GRPCQueryVector", "GRPCSparseValues"]

_logger = logging.getLogger(__name__)
Expand All @@ -52,7 +54,7 @@ def stub_class(self):

def upsert(
self,
vectors: Union[List[GRPCVector], List[tuple], List[dict]],
vectors: Union[List[GRPCVector], List[NonGRPCVector], List[tuple], List[dict]],
async_req: bool = False,
namespace: Optional[str] = None,
batch_size: Optional[int] = None,
Expand Down
25 changes: 19 additions & 6 deletions pinecone/grpc/vector_factory_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,23 @@ def _dict_to_vector(item) -> GRPCVector:

values = item.get("values")
if "values" in item:
item["values"] = convert_to_list(values)
try:
item["values"] = convert_to_list(values)
except TypeError as e:
raise TypeError(f"Column `values` is expected to be a list of floats") from e

sparse_values = item.get("sparse_values")
if sparse_values and not isinstance(sparse_values, GRPCSparseValues):
item["sparse_values"] = VectorFactoryGRPC._dict_to_sparse_values(sparse_values)

metadata = item.get("metadata")
if metadata:
if isinstance(metadata, Mapping):
if isinstance(metadata, dict):
item["metadata"] = dict_to_proto_struct(metadata)
elif not isinstance(metadata, Struct):
raise MetadataDictionaryExpectedError(item)
else:
item["metadata"] = dict_to_proto_struct({})

try:
return GRPCVector(**item)
Expand All @@ -100,10 +105,18 @@ def _dict_to_sparse_values(sparse_values_dict: Union[Dict, GRPCSparseValues, Non
if not {"indices", "values"}.issubset(sparse_values_dict):
raise SparseValuesMissingKeysError(sparse_values_dict)

indices = convert_to_list(sparse_values_dict.get("indices"))
values = convert_to_list(sparse_values_dict.get("values"))

try:
indices = convert_to_list(sparse_values_dict.get("indices"))
except TypeError as e:
raise SparseValuesTypeError() from e

try:
values = convert_to_list(sparse_values_dict.get("values"))
except TypeError as e:
raise SparseValuesTypeError() from e

try:
return GRPCSparseValues(indices=indices, values=values)
except TypeError:
raise SparseValuesTypeError()
except TypeError as e:
raise SparseValuesTypeError() from e
56 changes: 56 additions & 0 deletions tests/unit_grpc/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import pytest


@pytest.fixture
def vector_dim():
return 8


@pytest.fixture
def vals1(vector_dim):
return [0.1] * vector_dim


@pytest.fixture
def vals2(vector_dim):
return [0.2] * vector_dim


@pytest.fixture
def sparse_indices_1():
return [1, 8, 42]


@pytest.fixture
def sparse_values_1():
return [0.8, 0.9, 0.42]


@pytest.fixture
def sparse_indices_2():
return [1, 3, 5]


@pytest.fixture
def sparse_values_2():
return [0.7, 0.3, 0.31415]


@pytest.fixture
def md1():
return {"genre": "action", "year": 2021}


@pytest.fixture
def md2():
return {"genre": "documentary", "year": 2020}


@pytest.fixture
def filter1():
return {"genre": {"$in": ["action"]}}


@pytest.fixture
def filter2():
return {"year": {"$eq": 2020}}
Loading

0 comments on commit f80b60e

Please sign in to comment.