File size: 1,459 Bytes
ce676f7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | # SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
#
# SPDX-License-Identifier: Apache-2.0
import pytest
from haystack.marshal.yaml import YamlMarshaller
class InvalidClass:
def __init__(self) -> None:
self.a = 1
self.b = None
self.c = "string"
@pytest.fixture
def yaml_data():
return {"key": "value", 1: 0.221, "list": [1, 2, 3], "tuple": (1, None, True), "dict": {"set": {False}}}
@pytest.fixture
def invalid_yaml_data():
return {"key": "value", 1: 0.221, "list": [1, 2, 3], "tuple": (1, InvalidClass(), True), "dict": {"set": {False}}}
@pytest.fixture
def serialized_yaml_str():
return """key: value
1: 0.221
list:
- 1
- 2
- 3
tuple: !!python/tuple
- 1
- null
- true
dict:
set: !!set
false: null
"""
def test_yaml_marshal(yaml_data, serialized_yaml_str):
marshaller = YamlMarshaller()
marshalled = marshaller.marshal(yaml_data)
assert isinstance(marshalled, str)
assert marshalled.strip().replace("\n", "") == serialized_yaml_str.strip().replace("\n", "")
def test_yaml_marshal_invalid_type(invalid_yaml_data):
with pytest.raises(TypeError, match="basic Python types"):
marshaller = YamlMarshaller()
marshalled = marshaller.marshal(invalid_yaml_data)
def test_yaml_unmarshal(yaml_data, serialized_yaml_str):
marshaller = YamlMarshaller()
unmarshalled = marshaller.unmarshal(serialized_yaml_str)
assert unmarshalled == yaml_data
|