Beispiel #1
0
    def test_encode_unserializable(value):
        """ Test that an unserializable object raises an error. """
        # Arrange

        # Act & Assert
        with pytest.raises(ValueError):
            default_encode_value(value)
Beispiel #2
0
    def test_encode_collection_types(value):
        """ Test that a class with custom-encoded types does not encode collection. """
        # Arrange

        # Act
        result = default_encode_value(value)

        # Assert
        assert result == value
Beispiel #3
0
    def test_encode_numpy_recurse(value, expected) -> None:
        """
        Test recursive encoding of numpy objects
        """
        # Arrange

        # Act
        result = default_encode_value(value)

        # Assert
        assert result == expected
Beispiel #4
0
    def test_encode_class():
        """ Test that a class is encoded as expected. """
        # Arrange
        value = SimpleNamespace(a=1, b="banana", c=None)
        expected = dict(a=1, b="banana", c=None)

        # Act
        result = default_encode_value(value)

        # Assert
        assert result == expected
Beispiel #5
0
    def test_encode_serializable_types():
        """ Test that a class with custom-encoded types does not encode these types. """
        # Arrange
        from datetime import datetime

        complex_value = 3 + 4j
        datetime_value = datetime(2020, 10, 8)
        value = SimpleNamespace(complex=complex_value, datetime=datetime_value)
        expected = dict(complex=complex_value, datetime=datetime_value)

        # Act
        result = default_encode_value(value)

        # Assert
        assert result == expected
Beispiel #6
0
    def test_encode_unserializable_recurse():
        """ Test that a class with un-serializable attributes does not error. """
        # Arrange
        value = SimpleNamespace(
            class_=SimpleNamespace(good="class", bad=type),
            dict_=dict(good="dict", bad=dict),
            list_=["list", list],
        )
        expected = dict(class_=dict(good="class"), dict_=dict(good="dict"), list_=["list"])

        # Act
        result = default_encode_value(value)

        # Assert
        assert result == expected
Beispiel #7
0
    def test_encode_recursive_object():
        """ Test that recursive objects are handled gracefully. """
        # Arrange
        value = SimpleNamespace(class_=None, dict_=None, list_=None)
        value.class_ = value
        value.dict_ = dict(normal="string", recursive=value)
        value.list_ = [value, 1]
        expected = dict(dict_=dict(normal="string"), list_=[1])

        # Act
        import warnings

        with warnings.catch_warnings(record=True) as record:
            result = default_encode_value(value)

        # Assert
        assert result == expected
        assert all("recursive" in warn.message.args[0] for warn in record)
Beispiel #8
0
    def test_encode_class_recursive():
        """ Test that a class is recursively encoded as expected. """
        # Arrange
        value = SimpleNamespace(
            class_=SimpleNamespace(a=1, b="banana", c=None),
            dict_=dict(key_1="value_1", key_2="value_2"),
            list_=["item_1", "item_2"],
        )
        expected = dict(
            class_=dict(a=1, b="banana", c=None),
            dict_=dict(key_1="value_1", key_2="value_2"),
            list_=["item_1", "item_2"],
        )

        # Act
        result = default_encode_value(value)

        # Assert
        assert result == expected
Beispiel #9
0
    def test_encode_custom_encoder():
        """ Test that a class that specifies a custom encoder
        gets encoded using that method.
        """
        # Arrange
        encoding = "CUSTOM ENCODING"

        class ClassWithCustomEncoder:
            """ Example class with custom encoder. """

            # noinspection PyMethodMayBeStatic
            def __snapshot__(self):
                """ Custom encoder for snappiershot. """
                return encoding

        value = ClassWithCustomEncoder()

        # Act
        result = default_encode_value(value)

        # Assert
        assert result == encoding