Skip to content

Commit d0d40de

Browse files
fix batch reference serialization mutation
1 parent c307d97 commit d0d40de

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

test/collection/test_batch.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import pytest
44

55
from weaviate.collections.batch.grpc_batch import _validate_props
6-
from weaviate.collections.classes.batch import MAX_STORED_RESULTS, BatchObjectReturn
6+
from weaviate.collections.classes.batch import MAX_STORED_RESULTS, BatchObjectReturn, BatchReference
77
from weaviate.exceptions import WeaviateInsertInvalidPropertyError
8+
from weaviate.types import BEACON
89

910

1011
def test_batch_object_return_add() -> None:
@@ -53,3 +54,30 @@ def test_validate_props_raises_for_top_level_vector() -> None:
5354
def test_validate_props_raises_for_nested_vector() -> None:
5455
with pytest.raises(WeaviateInsertInvalidPropertyError):
5556
_validate_props({"vector": [0.1, 0.2]}, nested=True)
57+
58+
59+
@pytest.mark.parametrize(
60+
("to_object_collection", "expected_to"),
61+
[
62+
(None, f"{BEACON}28f3f61b-b524-45e0-9bbe-2c1550bf73d2"),
63+
("Target", f"{BEACON}Target/28f3f61b-b524-45e0-9bbe-2c1550bf73d2"),
64+
],
65+
)
66+
def test_batch_reference_to_internal_is_idempotent(
67+
to_object_collection: str | None, expected_to: str
68+
) -> None:
69+
ref = BatchReference(
70+
from_object_collection="Source",
71+
from_object_uuid="1c9cd584-88fe-5010-83d0-017cb3fcb446",
72+
from_property_name="link",
73+
to_object_collection=to_object_collection,
74+
to_object_uuid="28f3f61b-b524-45e0-9bbe-2c1550bf73d2",
75+
index=0,
76+
)
77+
78+
first = ref._to_internal()
79+
second = ref._to_internal()
80+
81+
assert ref.to_object_collection == to_object_collection
82+
assert first.to == expected_to
83+
assert second.to == expected_to

weaviate/collections/classes/batch.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,13 @@ def _validate_uuids(cls, v: UUID) -> str:
138138
return get_valid_uuid(v)
139139

140140
def _to_internal(self) -> _BatchReference:
141-
if self.to_object_collection is None:
142-
self.to_object_collection = ""
143-
else:
144-
self.to_object_collection = self.to_object_collection + "/"
141+
to_object_collection = (
142+
"" if self.to_object_collection is None else f"{self.to_object_collection}/"
143+
)
145144
return _BatchReference(
146145
from_uuid=str(self.from_object_uuid),
147146
from_=f"{BEACON}{self.from_object_collection}/{self.from_object_uuid}/{self.from_property_name}",
148-
to=f"{BEACON}{self.to_object_collection}{str(self.to_object_uuid)}",
147+
to=f"{BEACON}{to_object_collection}{str(self.to_object_uuid)}",
149148
to_uuid=str(self.to_object_uuid),
150149
tenant=self.tenant,
151150
index=self.index,

0 commit comments

Comments
 (0)