From d0d40debb51ee3584a77308716d7faf271a45e18 Mon Sep 17 00:00:00 2001 From: pragnyanramtha Date: Sun, 17 May 2026 01:18:18 +0000 Subject: [PATCH] fix batch reference serialization mutation --- test/collection/test_batch.py | 30 ++++++++++++++++++++++++++- weaviate/collections/classes/batch.py | 9 ++++---- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/test/collection/test_batch.py b/test/collection/test_batch.py index 0a2cda954..29bcb9997 100644 --- a/test/collection/test_batch.py +++ b/test/collection/test_batch.py @@ -3,8 +3,9 @@ import pytest from weaviate.collections.batch.grpc_batch import _validate_props -from weaviate.collections.classes.batch import MAX_STORED_RESULTS, BatchObjectReturn +from weaviate.collections.classes.batch import MAX_STORED_RESULTS, BatchObjectReturn, BatchReference from weaviate.exceptions import WeaviateInsertInvalidPropertyError +from weaviate.types import BEACON def test_batch_object_return_add() -> None: @@ -53,3 +54,30 @@ def test_validate_props_raises_for_top_level_vector() -> None: def test_validate_props_raises_for_nested_vector() -> None: with pytest.raises(WeaviateInsertInvalidPropertyError): _validate_props({"vector": [0.1, 0.2]}, nested=True) + + +@pytest.mark.parametrize( + ("to_object_collection", "expected_to"), + [ + (None, f"{BEACON}28f3f61b-b524-45e0-9bbe-2c1550bf73d2"), + ("Target", f"{BEACON}Target/28f3f61b-b524-45e0-9bbe-2c1550bf73d2"), + ], +) +def test_batch_reference_to_internal_is_idempotent( + to_object_collection: str | None, expected_to: str +) -> None: + ref = BatchReference( + from_object_collection="Source", + from_object_uuid="1c9cd584-88fe-5010-83d0-017cb3fcb446", + from_property_name="link", + to_object_collection=to_object_collection, + to_object_uuid="28f3f61b-b524-45e0-9bbe-2c1550bf73d2", + index=0, + ) + + first = ref._to_internal() + second = ref._to_internal() + + assert ref.to_object_collection == to_object_collection + assert first.to == expected_to + assert second.to == expected_to diff --git a/weaviate/collections/classes/batch.py b/weaviate/collections/classes/batch.py index eb8d0181a..088d76e6f 100644 --- a/weaviate/collections/classes/batch.py +++ b/weaviate/collections/classes/batch.py @@ -138,14 +138,13 @@ def _validate_uuids(cls, v: UUID) -> str: return get_valid_uuid(v) def _to_internal(self) -> _BatchReference: - if self.to_object_collection is None: - self.to_object_collection = "" - else: - self.to_object_collection = self.to_object_collection + "/" + to_object_collection = ( + "" if self.to_object_collection is None else f"{self.to_object_collection}/" + ) return _BatchReference( from_uuid=str(self.from_object_uuid), from_=f"{BEACON}{self.from_object_collection}/{self.from_object_uuid}/{self.from_property_name}", - to=f"{BEACON}{self.to_object_collection}{str(self.to_object_uuid)}", + to=f"{BEACON}{to_object_collection}{str(self.to_object_uuid)}", to_uuid=str(self.to_object_uuid), tenant=self.tenant, index=self.index,