|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +import pytest |
| 4 | + |
| 5 | +from pinecone.db_data.sparse_values_factory import SparseValuesFactory |
| 6 | +from pinecone import SparseValues |
| 7 | +from pinecone.core.openapi.db_data.models import SparseValues as OpenApiSparseValues |
| 8 | +from pinecone.db_data.errors import ( |
| 9 | + SparseValuesTypeError, |
| 10 | + SparseValuesMissingKeysError, |
| 11 | + SparseValuesDictionaryExpectedError, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class TestSparseValuesFactory: |
| 16 | + """Test SparseValuesFactory for REST API (db_data module).""" |
| 17 | + |
| 18 | + def test_build_when_none_returns_none(self): |
| 19 | + """Test that None input returns None.""" |
| 20 | + assert SparseValuesFactory.build(None) is None |
| 21 | + |
| 22 | + def test_build_when_passed_openapi_sparse_values(self): |
| 23 | + """Test that OpenApiSparseValues are returned unchanged.""" |
| 24 | + sv = OpenApiSparseValues(indices=[0, 2], values=[0.1, 0.3]) |
| 25 | + actual = SparseValuesFactory.build(sv) |
| 26 | + assert actual == sv |
| 27 | + assert actual is sv |
| 28 | + |
| 29 | + def test_build_when_given_sparse_values_dataclass(self): |
| 30 | + """Test conversion from SparseValues dataclass to OpenApiSparseValues.""" |
| 31 | + sv = SparseValues(indices=[0, 2], values=[0.1, 0.3]) |
| 32 | + actual = SparseValuesFactory.build(sv) |
| 33 | + expected = OpenApiSparseValues(indices=[0, 2], values=[0.1, 0.3]) |
| 34 | + assert isinstance(actual, OpenApiSparseValues) |
| 35 | + assert actual.indices == expected.indices |
| 36 | + assert actual.values == expected.values |
| 37 | + |
| 38 | + @pytest.mark.parametrize( |
| 39 | + "input_dict", |
| 40 | + [ |
| 41 | + {"indices": [2], "values": [0.3]}, |
| 42 | + {"indices": [88, 102], "values": [-0.1, 0.3]}, |
| 43 | + {"indices": [0, 2, 4], "values": [0.1, 0.3, 0.5]}, |
| 44 | + {"indices": [0, 2, 4, 6], "values": [0.1, 0.3, 0.5, 0.7]}, |
| 45 | + ], |
| 46 | + ) |
| 47 | + def test_build_when_valid_dictionary(self, input_dict): |
| 48 | + """Test building from valid dictionary input.""" |
| 49 | + actual = SparseValuesFactory.build(input_dict) |
| 50 | + expected = OpenApiSparseValues(indices=input_dict["indices"], values=input_dict["values"]) |
| 51 | + assert actual.indices == expected.indices |
| 52 | + assert actual.values == expected.values |
| 53 | + |
| 54 | + @pytest.mark.parametrize( |
| 55 | + "input_dict", |
| 56 | + [ |
| 57 | + {"indices": np.array([0, 2]), "values": [0.1, 0.3]}, |
| 58 | + {"indices": [0, 2], "values": np.array([0.1, 0.3])}, |
| 59 | + {"indices": np.array([0, 2]), "values": np.array([0.1, 0.3])}, |
| 60 | + {"indices": pd.array([0, 2]), "values": [0.1, 0.3]}, |
| 61 | + {"indices": [0, 2], "values": pd.array([0.1, 0.3])}, |
| 62 | + {"indices": pd.array([0, 2]), "values": pd.array([0.1, 0.3])}, |
| 63 | + ], |
| 64 | + ) |
| 65 | + def test_build_when_special_data_types(self, input_dict): |
| 66 | + """Test that the factory handles numpy/pandas arrays correctly.""" |
| 67 | + actual = SparseValuesFactory.build(input_dict) |
| 68 | + expected = OpenApiSparseValues(indices=[0, 2], values=[0.1, 0.3]) |
| 69 | + assert actual.indices == expected.indices |
| 70 | + assert actual.values == expected.values |
| 71 | + |
| 72 | + @pytest.mark.parametrize( |
| 73 | + "input_dict", |
| 74 | + [{"indices": [2], "values": [0.3, 0.3]}, {"indices": [88, 102], "values": [-0.1]}], |
| 75 | + ) |
| 76 | + def test_build_when_list_sizes_dont_match(self, input_dict): |
| 77 | + """Test that mismatched indices and values lengths raise ValueError.""" |
| 78 | + with pytest.raises( |
| 79 | + ValueError, match="Sparse values indices and values must have the same length" |
| 80 | + ): |
| 81 | + SparseValuesFactory.build(input_dict) |
| 82 | + |
| 83 | + @pytest.mark.parametrize( |
| 84 | + "input_dict", |
| 85 | + [ |
| 86 | + {"indices": [2.0], "values": [0.3]}, |
| 87 | + {"indices": ["2"], "values": [0.3]}, |
| 88 | + {"indices": np.array([2.0]), "values": [0.3]}, |
| 89 | + {"indices": pd.array([2.0]), "values": [0.3]}, |
| 90 | + ], |
| 91 | + ) |
| 92 | + def test_build_when_non_integer_indices(self, input_dict): |
| 93 | + """Test that non-integer indices raise SparseValuesTypeError.""" |
| 94 | + with pytest.raises(SparseValuesTypeError): |
| 95 | + SparseValuesFactory.build(input_dict) |
| 96 | + |
| 97 | + @pytest.mark.parametrize( |
| 98 | + "input_dict", [{"indices": [2], "values": ["3.2"]}, {"indices": [2], "values": [True]}] |
| 99 | + ) |
| 100 | + def test_build_when_non_float_values(self, input_dict): |
| 101 | + """Test that non-float values raise SparseValuesTypeError.""" |
| 102 | + with pytest.raises(SparseValuesTypeError): |
| 103 | + SparseValuesFactory.build(input_dict) |
| 104 | + |
| 105 | + def test_build_when_missing_indices_key(self): |
| 106 | + """Test that missing 'indices' key raises SparseValuesMissingKeysError.""" |
| 107 | + input_dict = {"values": [0.1, 0.3]} |
| 108 | + with pytest.raises(SparseValuesMissingKeysError) as exc_info: |
| 109 | + SparseValuesFactory.build(input_dict) |
| 110 | + assert "indices" in str(exc_info.value) |
| 111 | + |
| 112 | + def test_build_when_missing_values_key(self): |
| 113 | + """Test that missing 'values' key raises SparseValuesMissingKeysError.""" |
| 114 | + input_dict = {"indices": [0, 2]} |
| 115 | + with pytest.raises(SparseValuesMissingKeysError) as exc_info: |
| 116 | + SparseValuesFactory.build(input_dict) |
| 117 | + assert "values" in str(exc_info.value) |
| 118 | + |
| 119 | + def test_build_when_missing_both_keys(self): |
| 120 | + """Test that missing both keys raises SparseValuesMissingKeysError.""" |
| 121 | + input_dict = {} |
| 122 | + with pytest.raises(SparseValuesMissingKeysError) as exc_info: |
| 123 | + SparseValuesFactory.build(input_dict) |
| 124 | + assert "indices" in str(exc_info.value) or "values" in str(exc_info.value) |
| 125 | + |
| 126 | + def test_build_when_not_a_dictionary(self): |
| 127 | + """Test that non-dictionary input raises SparseValuesDictionaryExpectedError.""" |
| 128 | + with pytest.raises(SparseValuesDictionaryExpectedError) as exc_info: |
| 129 | + SparseValuesFactory.build("not a dict") |
| 130 | + assert "dictionary" in str(exc_info.value).lower() |
| 131 | + |
| 132 | + with pytest.raises(SparseValuesDictionaryExpectedError): |
| 133 | + SparseValuesFactory.build(123) |
| 134 | + |
| 135 | + with pytest.raises(SparseValuesDictionaryExpectedError): |
| 136 | + SparseValuesFactory.build([1, 2, 3]) |
| 137 | + |
| 138 | + def test_build_when_empty_indices_list(self): |
| 139 | + """Test that empty indices list is handled correctly.""" |
| 140 | + input_dict = {"indices": [], "values": []} |
| 141 | + actual = SparseValuesFactory.build(input_dict) |
| 142 | + expected = OpenApiSparseValues(indices=[], values=[]) |
| 143 | + assert actual.indices == expected.indices |
| 144 | + assert actual.values == expected.values |
| 145 | + |
| 146 | + def test_build_when_empty_values_list(self): |
| 147 | + """Test that empty values list is handled correctly.""" |
| 148 | + input_dict = {"indices": [], "values": []} |
| 149 | + actual = SparseValuesFactory.build(input_dict) |
| 150 | + expected = OpenApiSparseValues(indices=[], values=[]) |
| 151 | + assert actual.indices == expected.indices |
| 152 | + assert actual.values == expected.values |
0 commit comments