예제 #1
0
def test_non_marshalable_sub_entities(sub_entity):
    """Test that ValueError is raised if one of sub-entities of the entity is not marshallable.

    1. Create json marshaler that raises exception when either marshal_embedded_link or
       marshal_embedded_representation is called.
    2. Create an entity marshaler.
    3. Try to call marshal_entities method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    class _SubEntityErrorMarshaler(JSONMarshaler):
        def marshal_embedded_link(self, embedded_link):
            raise Exception()

        def marshal_embedded_representation(self, embedded_representation):
            raise Exception()

    SubEntitiesEntity = namedtuple("SubEntitiesEntity", "entities")
    marshaler = EntityMarshaler(
        marshaler=_SubEntityErrorMarshaler(),
        entity=SubEntitiesEntity(entities=[sub_entity]),
    )
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_entities()

    assert error_info.value.args[
        0] == "Failed to marshal sub-entities of the entity", "Wrong error"
예제 #2
0
def test_missing_sub_entities():
    """Test that ValueError is raised if an entity does not have entities attribute.

    1. Create an entity marshaler for an object without entities attribute.
    2. Try to call marshal_entities method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=object())
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_entities()

    assert error_info.value.args[
        0] == "Failed to get sub-entities of the entity", "Wrong error"
예제 #3
0
def test_marshal():
    """Test that entity data is properly marshaled.

    1. Create an entity.
    2. Create an entity marshaler for the entity.
    3. Replace marshaler methods so that they return predefined data.
    4. Marshal the entity.
    5. Check the marshaled data.
    """
    marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=Entity())
    marshaler.marshal_classes = lambda: "marshal_classes"
    marshaler.marshal_properties = lambda: "marshal_properties"
    marshaler.marshal_entities = lambda: "marshal_entities"
    marshaler.marshal_links = lambda: "marshal_links"
    marshaler.marshal_actions = lambda: "marshal_actions"
    marshaler.marshal_title = lambda: "marshal_title"

    actual_data = marshaler.marshal()
    expected_data = {
        "class": "marshal_classes",
        "properties": "marshal_properties",
        "entities": "marshal_entities",
        "links": "marshal_links",
        "actions": "marshal_actions",
        "title": "marshal_title",
    }
    assert actual_data == expected_data, "Entity is not properly marshaled"
예제 #4
0
def test_sub_entities(sub_entities):
    """Test that sub-entities are properly marshaled.

    1. Create json marshaler with overridden marshal_embedded_link and
       marshal_embedded_representation.
    2. Create an entity marshaler.
    3. Marshal sub-entities.
    4. Check the marshaled sub-entities.
    """
    class _SubEntitiesMarshaler(JSONMarshaler):
        def marshal_embedded_link(self, embedded_link):
            if not hasattr(embedded_link, "target"):
                pytest.fail(
                    "Try to mershal embedded link instead of embedded representation"
                )
            return sub_entities.index(embedded_link)

        def marshal_embedded_representation(self, embedded_representation):
            if hasattr(embedded_representation, "target"):
                pytest.fail(
                    "Try to marshal embedded representation instead of embedded link"
                )
            return sub_entities.index(embedded_representation)

    SubEntitiesEntity = namedtuple("SubEntitiesEntity", "entities")
    marshaler = EntityMarshaler(
        marshaler=_SubEntitiesMarshaler(),
        entity=SubEntitiesEntity(entities=sub_entities),
    )

    actual_data = marshaler.marshal_entities()
    assert actual_data == list(range(len(sub_entities))), "Wrong entities"
예제 #5
0
def test_non_iterable_sub_entities():
    """Test that ValueError is raised if an entity has a non-iterable object as its sub-entities.

    1. Create an entity marshaler for an object with non-iterable sub-entities.
    2. Try to call marshal_entities method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    SubEntitiesEntity = namedtuple("SubEntitiesEntity", "entities")
    marshaler = EntityMarshaler(marshaler=JSONMarshaler(),
                                entity=SubEntitiesEntity(entities=None))
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_entities()

    assert error_info.value.args[
        0] == "Failed to iterate over sub-entities of the entity", (
            "Wrong error")