|
2 | 2 |
|
3 | 3 | import ast |
4 | 4 |
|
| 5 | +from pydantic import ConfigDict, Field, model_serializer |
5 | 6 | from pytest import fixture, mark, raises |
6 | 7 |
|
7 | 8 | from semantic_kernel.connectors.in_memory import InMemoryCollection, InMemoryStore |
@@ -35,6 +36,209 @@ async def test_upsert(collection): |
35 | 36 | assert collection.inner_storage == {"testid": record} |
36 | 37 |
|
37 | 38 |
|
| 39 | +@mark.parametrize("batch", [False, True]) |
| 40 | +@mark.parametrize("model_kind", ["dict", "dataclass", "pydantic"]) |
| 41 | +async def test_upsert_with_key_storage_name(definition, dataclass_vector_data_model, record_type, batch, model_kind): |
| 42 | + definition.key_field.storage_name = "stored_id" |
| 43 | + model_type = {"dict": dict, "dataclass": dataclass_vector_data_model, "pydantic": record_type}[model_kind] |
| 44 | + collection = InMemoryCollection(collection_name="test", record_type=model_type, definition=definition) |
| 45 | + records = [ |
| 46 | + model_type(id="first", content="first content", vector=[1.0, 0.0, 0.0, 0.0, 0.0]), |
| 47 | + model_type(id="second", content="second content", vector=[0.0, 1.0, 0.0, 0.0, 0.0]), |
| 48 | + ] |
| 49 | + |
| 50 | + keys = await collection.upsert(records if batch else records[0]) |
| 51 | + assert keys == (["first", "second"] if batch else "first") |
| 52 | + assert await collection.get("first", include_vectors=True) == records[0] |
| 53 | + if batch: |
| 54 | + assert await collection.get(["first", "second"], include_vectors=True) == records |
| 55 | + |
| 56 | + results = await collection.search(vector=[1.0, 0.0, 0.0, 0.0, 0.0], include_vectors=True) |
| 57 | + found = [result.record async for result in results.results] |
| 58 | + assert found == (records if batch else records[:1]) |
| 59 | + |
| 60 | + await collection.delete("first") |
| 61 | + assert await collection.get("first") is None |
| 62 | + if batch: |
| 63 | + assert await collection.get("second", include_vectors=True) == records[1] |
| 64 | + |
| 65 | + |
| 66 | +@mark.parametrize("batch", [False, True]) |
| 67 | +async def test_upsert_when_storage_name_matches_logical_field(definition, record_type, batch): |
| 68 | + definition.key_field.storage_name = "content" |
| 69 | + collection = InMemoryCollection(collection_name="test", record_type=record_type, definition=definition) |
| 70 | + record = record_type(id="first", content="unrelated", vector=[1.0, 0.0, 0.0, 0.0, 0.0]) |
| 71 | + |
| 72 | + keys = await collection.upsert([record] if batch else record) |
| 73 | + assert keys == (["first"] if batch else "first") |
| 74 | + assert "first" in collection.inner_storage |
| 75 | + assert "unrelated" not in collection.inner_storage |
| 76 | + |
| 77 | + await collection.delete("first") |
| 78 | + assert collection.inner_storage == {} |
| 79 | + |
| 80 | + |
| 81 | +@mark.parametrize("batch", [False, True]) |
| 82 | +@mark.parametrize("serializer", ["default", "serialize_none", "model_serialize_none"]) |
| 83 | +async def test_upsert_when_key_storage_name_matches_another_logical_field(definition, record_type, batch, serializer): |
| 84 | + definition.key_field.storage_name = "content" |
| 85 | + if serializer == "serialize_none": |
| 86 | + definition.serialize = lambda record, **kwargs: None |
| 87 | + elif serializer == "model_serialize_none": |
| 88 | + |
| 89 | + class CustomRecord(record_type): |
| 90 | + def serialize(self, **kwargs): |
| 91 | + return None |
| 92 | + |
| 93 | + record_type = CustomRecord |
| 94 | + collection = InMemoryCollection(collection_name="test", record_type=record_type, definition=definition) |
| 95 | + records = [ |
| 96 | + record_type(id="first", content="unrelated first", vector=[1.0, 0.0, 0.0, 0.0, 0.0]), |
| 97 | + record_type(id="second", content="unrelated second", vector=[0.0, 1.0, 0.0, 0.0, 0.0]), |
| 98 | + ] |
| 99 | + |
| 100 | + keys = await collection.upsert(records if batch else records[0]) |
| 101 | + assert keys == (["first", "second"] if batch else "first") |
| 102 | + assert set(collection.inner_storage) == ({"first", "second"} if batch else {"first"}) |
| 103 | + assert collection.inner_storage["first"] == records[0].model_dump() |
| 104 | + |
| 105 | + await collection.delete(["first", "second"] if batch else "first") |
| 106 | + assert collection.inner_storage == {} |
| 107 | + |
| 108 | + |
| 109 | +@mark.parametrize("batch", [False, True]) |
| 110 | +@mark.parametrize("model_kind", ["dict", "dataclass"]) |
| 111 | +async def test_upsert_when_another_storage_name_matches_key(definition, dataclass_vector_data_model, batch, model_kind): |
| 112 | + definition.key_field.storage_name = "stored_id" |
| 113 | + definition.fields[1].storage_name = "id" |
| 114 | + model_type = dict if model_kind == "dict" else dataclass_vector_data_model |
| 115 | + collection = InMemoryCollection(collection_name="test", record_type=model_type, definition=definition) |
| 116 | + records = [ |
| 117 | + model_type(id="first", content="unrelated first", vector=[1.0, 0.0, 0.0, 0.0, 0.0]), |
| 118 | + model_type(id="second", content="unrelated second", vector=[0.0, 1.0, 0.0, 0.0, 0.0]), |
| 119 | + ] |
| 120 | + |
| 121 | + keys = await collection.upsert(records if batch else records[0]) |
| 122 | + assert keys == (["first", "second"] if batch else "first") |
| 123 | + assert await collection.get("first", include_vectors=True) == records[0] |
| 124 | + if batch: |
| 125 | + assert await collection.get(["first", "second"], include_vectors=True) == records |
| 126 | + |
| 127 | + await collection.delete(["first", "second"] if batch else "first") |
| 128 | + assert collection.inner_storage == {} |
| 129 | + |
| 130 | + |
| 131 | +@mark.parametrize("batch", [False, True]) |
| 132 | +@mark.parametrize("serializer", ["to_dict", "serialize", "model_serialize", "model_dump", "model_serializer"]) |
| 133 | +async def test_upsert_with_pydantic_custom_storage_serialization(definition, record_type, batch, serializer): |
| 134 | + definition.key_field.storage_name = "stored_id" |
| 135 | + content_name = "id" if serializer in {"serialize", "model_serialize"} else "stored_content" |
| 136 | + definition.fields[1].storage_name = content_name |
| 137 | + if serializer == "model_serialize": |
| 138 | + |
| 139 | + class CustomRecord(record_type): |
| 140 | + def serialize(self, **kwargs): |
| 141 | + return {"stored_id": self.id, content_name: self.content, "vector": self.vector} |
| 142 | + |
| 143 | + model_type = CustomRecord |
| 144 | + elif serializer == "model_dump": |
| 145 | + |
| 146 | + class CustomDumpRecord(record_type): |
| 147 | + def model_dump(self, **kwargs): |
| 148 | + return {"stored_id": self.id, content_name: self.content, "vector": self.vector} |
| 149 | + |
| 150 | + model_type = CustomDumpRecord |
| 151 | + elif serializer == "model_serializer": |
| 152 | + |
| 153 | + class CustomSerializedRecord(record_type): |
| 154 | + @model_serializer |
| 155 | + def serialize_model(self): |
| 156 | + return {"stored_id": self.id, content_name: self.content, "vector": self.vector} |
| 157 | + |
| 158 | + model_type = CustomSerializedRecord |
| 159 | + else: |
| 160 | + model_type = record_type |
| 161 | + setattr( |
| 162 | + definition, |
| 163 | + serializer, |
| 164 | + lambda record, **kwargs: {"stored_id": record.id, content_name: record.content, "vector": record.vector}, |
| 165 | + ) |
| 166 | + collection = InMemoryCollection(collection_name="test", record_type=model_type, definition=definition) |
| 167 | + records = [ |
| 168 | + model_type(id="first", content="unrelated first", vector=[1.0, 0.0, 0.0, 0.0, 0.0]), |
| 169 | + model_type(id="second", content="unrelated second", vector=[0.0, 1.0, 0.0, 0.0, 0.0]), |
| 170 | + ] |
| 171 | + |
| 172 | + keys = await collection.upsert(records if batch else records[0]) |
| 173 | + assert keys == (["first", "second"] if batch else "first") |
| 174 | + assert set(collection.inner_storage) == ({"first", "second"} if batch else {"first"}) |
| 175 | + |
| 176 | + await collection.delete(["first", "second"] if batch else "first") |
| 177 | + assert collection.inner_storage == {} |
| 178 | + |
| 179 | + |
| 180 | +@mark.parametrize("batch", [False, True]) |
| 181 | +async def test_upsert_with_pydantic_serialization_aliases(definition, record_type, batch): |
| 182 | + definition.key_field.storage_name = "stored_id" |
| 183 | + definition.fields[1].storage_name = "id" |
| 184 | + |
| 185 | + class AliasedRecord(record_type): |
| 186 | + model_config = ConfigDict(serialize_by_alias=True) |
| 187 | + id: str = Field(serialization_alias="stored_id") |
| 188 | + content: str = Field(serialization_alias="id") |
| 189 | + |
| 190 | + collection = InMemoryCollection(collection_name="test", record_type=AliasedRecord, definition=definition) |
| 191 | + records = [ |
| 192 | + AliasedRecord(id="first", content="unrelated first", vector=[1.0, 0.0, 0.0, 0.0, 0.0]), |
| 193 | + AliasedRecord(id="second", content="unrelated second", vector=[0.0, 1.0, 0.0, 0.0, 0.0]), |
| 194 | + ] |
| 195 | + |
| 196 | + keys = await collection.upsert(records if batch else records[0]) |
| 197 | + assert keys == (["first", "second"] if batch else "first") |
| 198 | + assert set(collection.inner_storage) == ({"first", "second"} if batch else {"first"}) |
| 199 | + |
| 200 | + await collection.delete(["first", "second"] if batch else "first") |
| 201 | + assert collection.inner_storage == {} |
| 202 | + |
| 203 | + |
| 204 | +@mark.parametrize("batch", [False, True]) |
| 205 | +@mark.parametrize("serializer", ["model_dump", "model_serializer", "to_dict"]) |
| 206 | +async def test_upsert_with_custom_logical_serialization(definition, record_type, batch, serializer): |
| 207 | + definition.key_field.storage_name = "content" |
| 208 | + definition.fields[1].storage_name = "id" |
| 209 | + if serializer == "model_dump": |
| 210 | + |
| 211 | + class CustomRecord(record_type): |
| 212 | + def model_dump(self, **kwargs): |
| 213 | + return super().model_dump(**kwargs) |
| 214 | + |
| 215 | + model_type = CustomRecord |
| 216 | + elif serializer == "model_serializer": |
| 217 | + |
| 218 | + class CustomRecord(record_type): |
| 219 | + @model_serializer(mode="wrap") |
| 220 | + def serialize_model(self, handler): |
| 221 | + return handler(self) |
| 222 | + |
| 223 | + model_type = CustomRecord |
| 224 | + else: |
| 225 | + model_type = record_type |
| 226 | + definition.to_dict = lambda record, **kwargs: record.model_dump() |
| 227 | + |
| 228 | + collection = InMemoryCollection(collection_name="test", record_type=model_type, definition=definition) |
| 229 | + records = [ |
| 230 | + model_type(id="first", content="unrelated first", vector=[1.0, 0.0, 0.0, 0.0, 0.0]), |
| 231 | + model_type(id="second", content="unrelated second", vector=[0.0, 1.0, 0.0, 0.0, 0.0]), |
| 232 | + ] |
| 233 | + |
| 234 | + keys = await collection.upsert(records if batch else records[0]) |
| 235 | + assert keys == (["first", "second"] if batch else "first") |
| 236 | + assert set(collection.inner_storage) == ({"first", "second"} if batch else {"first"}) |
| 237 | + |
| 238 | + await collection.delete(["first", "second"] if batch else "first") |
| 239 | + assert collection.inner_storage == {} |
| 240 | + |
| 241 | + |
38 | 242 | async def test_get(collection): |
39 | 243 | record = {"id": "testid", "content": "test content", "vector": [0.1, 0.2, 0.3, 0.4, 0.5]} |
40 | 244 | await collection.upsert(record) |
|
0 commit comments