2024-05-09 15:40:36 +02:00
|
|
|
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2025-05-26 17:22:51 +01:00
|
|
|
|
2024-02-22 12:52:04 +01:00
|
|
|
from typing import Any, Union
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2024-02-23 11:52:54 +01:00
|
|
|
from haystack import Document
|
2024-02-22 12:52:04 +01:00
|
|
|
from haystack.tracing import utils
|
|
|
|
|
|
|
|
|
|
|
|
class NonSerializableClass:
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return "NonSerializableClass"
|
|
|
|
|
|
|
|
|
|
|
|
class TestTypeCoercion:
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"raw_value,expected_tag_value",
|
|
|
|
[
|
|
|
|
(1, 1),
|
|
|
|
(1.0, 1.0),
|
|
|
|
(True, True),
|
|
|
|
(None, ""),
|
|
|
|
("string", "string"),
|
|
|
|
([1, 2, 3], "[1, 2, 3]"),
|
|
|
|
({"key": "value"}, '{"key": "value"}'),
|
|
|
|
(NonSerializableClass(), "NonSerializableClass"),
|
2024-02-23 11:52:54 +01:00
|
|
|
(
|
|
|
|
Document(id="1", content="text"),
|
2025-03-04 12:06:07 +01:00
|
|
|
'{"id": "1", "content": "text", "blob": null, "score": null, "embedding": null, "sparse_embedding": null}',
|
2024-02-23 11:52:54 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
[Document(id="1", content="text")],
|
2025-03-04 12:06:07 +01:00
|
|
|
'[{"id": "1", "content": "text", "blob": null, "score": null, "embedding": null, "sparse_embedding": null}]',
|
2024-02-23 11:52:54 +01:00
|
|
|
),
|
|
|
|
(
|
|
|
|
{"key": Document(id="1", content="text")},
|
2025-03-04 12:06:07 +01:00
|
|
|
'{"key": {"id": "1", "content": "text", "blob": null, "score": null, "embedding": null, "sparse_embedding": null}}',
|
2024-02-23 11:52:54 +01:00
|
|
|
),
|
2024-02-22 12:52:04 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_type_coercion(self, raw_value: Any, expected_tag_value: Union[bool, str, int, float]) -> None:
|
|
|
|
coerced_value = utils.coerce_tag_value(raw_value)
|
|
|
|
|
|
|
|
assert coerced_value == expected_tag_value
|