|
| 1 | +import os |
| 2 | +import pytest |
| 3 | + |
| 4 | +if os.environ.get("USE_GRPC") == "true": |
| 5 | + from pinecone.grpc import PineconeGrpcFuture |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.skipif( |
| 9 | + os.getenv("USE_GRPC") != "true", reason="PineconeGrpcFutures only returned from grpc client" |
| 10 | +) |
| 11 | +class TestFetchFuture: |
| 12 | + def setup_method(self): |
| 13 | + self.expected_dimension = 2 |
| 14 | + |
| 15 | + def test_fetch_multiple_by_id(self, idx, namespace): |
| 16 | + target_namespace = namespace |
| 17 | + |
| 18 | + results = idx.fetch(ids=["1", "2", "4"], namespace=target_namespace, async_req=True) |
| 19 | + assert isinstance(results, PineconeGrpcFuture) |
| 20 | + |
| 21 | + from concurrent.futures import wait, FIRST_COMPLETED |
| 22 | + |
| 23 | + done, _ = wait([results], return_when=FIRST_COMPLETED) |
| 24 | + |
| 25 | + results = done.pop().result() |
| 26 | + assert results.usage is not None |
| 27 | + assert results.usage["read_units"] is not None |
| 28 | + assert results.usage["read_units"] > 0 |
| 29 | + |
| 30 | + assert results.namespace == target_namespace |
| 31 | + assert len(results.vectors) == 3 |
| 32 | + assert results.vectors["1"].id == "1" |
| 33 | + assert results.vectors["2"].id == "2" |
| 34 | + # Metadata included, if set |
| 35 | + assert results.vectors["1"].metadata is None |
| 36 | + assert results.vectors["2"].metadata is None |
| 37 | + assert results.vectors["4"].metadata is not None |
| 38 | + assert results.vectors["4"].metadata["genre"] == "action" |
| 39 | + assert results.vectors["4"].metadata["runtime"] == 120 |
| 40 | + # Values included |
| 41 | + assert results.vectors["1"].values is not None |
| 42 | + assert len(results.vectors["1"].values) == self.expected_dimension |
| 43 | + |
| 44 | + def test_fetch_single_by_id(self, idx, namespace): |
| 45 | + target_namespace = namespace |
| 46 | + |
| 47 | + future = idx.fetch(ids=["1"], namespace=target_namespace, async_req=True) |
| 48 | + |
| 49 | + from concurrent.futures import wait, FIRST_COMPLETED |
| 50 | + |
| 51 | + done, _ = wait([future], return_when=FIRST_COMPLETED) |
| 52 | + results = done.pop().result() |
| 53 | + |
| 54 | + assert results.namespace == target_namespace |
| 55 | + assert len(results.vectors) == 1 |
| 56 | + assert results.vectors["1"].id == "1" |
| 57 | + assert results.vectors["1"].metadata is None |
| 58 | + assert results.vectors["1"].values is not None |
| 59 | + assert len(results.vectors["1"].values) == self.expected_dimension |
| 60 | + |
| 61 | + def test_fetch_nonexistent_id(self, idx, namespace): |
| 62 | + target_namespace = namespace |
| 63 | + |
| 64 | + # Fetch id that is missing |
| 65 | + future = idx.fetch(ids=["100"], namespace=target_namespace, async_req=True) |
| 66 | + |
| 67 | + from concurrent.futures import wait, FIRST_COMPLETED |
| 68 | + |
| 69 | + done, _ = wait([future], return_when=FIRST_COMPLETED) |
| 70 | + results = done.pop().result() |
| 71 | + |
| 72 | + assert results.namespace == target_namespace |
| 73 | + assert len(results.vectors) == 0 |
| 74 | + |
| 75 | + def test_fetch_nonexistent_namespace(self, idx): |
| 76 | + target_namespace = "nonexistent-namespace" |
| 77 | + |
| 78 | + # Fetch from namespace with no vectors |
| 79 | + future = idx.fetch(ids=["1"], namespace=target_namespace, async_req=True) |
| 80 | + |
| 81 | + from concurrent.futures import wait, FIRST_COMPLETED |
| 82 | + |
| 83 | + done, _ = wait([future], return_when=FIRST_COMPLETED) |
| 84 | + results = done.pop().result() |
| 85 | + |
| 86 | + assert results.namespace == target_namespace |
| 87 | + assert len(results.vectors) == 0 |
| 88 | + |
| 89 | + def test_fetch_unspecified_namespace(self, idx): |
| 90 | + # Fetch without specifying namespace gives default namespace results |
| 91 | + future = idx.fetch(ids=["1", "4"], async_req=True) |
| 92 | + |
| 93 | + from concurrent.futures import wait, FIRST_COMPLETED |
| 94 | + |
| 95 | + done, _ = wait([future], return_when=FIRST_COMPLETED) |
| 96 | + results = done.pop().result() |
| 97 | + |
| 98 | + assert results.namespace == "" |
| 99 | + assert results.vectors["1"].id == "1" |
| 100 | + assert results.vectors["1"].values is not None |
| 101 | + assert results.vectors["4"].metadata is not None |
0 commit comments