コード例 #1
0
def test_marshal():
    """Test that data of an embedded representation is properly marshaled.

    1. Create an embedded representation.
    2. Create an embedded representation marshaler for the representation.
    3. Replace marshaler methods so that they return predefined data.
    4. Marshal the representation.
    5. Check the marshaled data.
    """
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=EmbeddedRepresentation(relations=["self"]),
    )
    marshaler.marshal_relations = lambda: "marshal_relations"
    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 = {
        "rel": "marshal_relations",
        "class": "marshal_classes",
        "properties": "marshal_properties",
        "entities": "marshal_entities",
        "links": "marshal_links",
        "actions": "marshal_actions",
        "title": "marshal_title",
    }
    assert actual_data == expected_data, "Embedded represenation is not properly marshaled"
コード例 #2
0
def test_missing_properties():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if an embedded representation does not have properties attribute.

    1. Create an embedded representation marshaler for an object without properties attribute.
    2. Try to call marshal_properties method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    # pylint: enable=line-too-long
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=object(),
    )
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_properties()

    expected_message = "Failed to get properties of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"
コード例 #3
0
def test_invalid_properties():
    # pylint: disable=line-too-long
    """Test that ValueError is raised if properties of an embedded representaion are not valid json object.

    1. Create an embedded representation marshaler for an object with invalid json object as
       its properties.
    2. Try to call marshal_properties method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    # pylint: enable=line-too-long
    PropertiesRepresentation = namedtuple("PropertiesRepresentation",
                                          "properties")
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=PropertiesRepresentation(properties=object()),
    )
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_properties()

    expected_message = "Failed to marshal properties of the embedded representation"
    assert error_info.value.args[0] == expected_message, "Wrong error"
コード例 #4
0
def test_properties(properties):
    """Test that properties are properly marshaled.

    1. Create an embedded representation marshaler for an object with specific properties.
    2. Marshal properties.
    3. Check the marshaled properties.
    """
    PropertiesRepresentation = namedtuple("PropertiesRepresentation",
                                          "properties")
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=PropertiesRepresentation(
            properties=properties),
    )
    actual_properties = json.dumps(marshaler.marshal_properties(),
                                   sort_keys=True)
    expected_properties = json.dumps(properties, sort_keys=True)
    assert actual_properties == expected_properties, "Wrong properties"