Пример #1
0
def test_missing_properties():
    """Test that ValueError is raised if an entity does not have properties attribute.

    1. Create an entity 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.
    """
    marshaler = EntityMarshaler(marshaler=JSONMarshaler(), entity=object())
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_properties()

    assert error_info.value.args[
        0] == "Failed to get entity's properties", "Wrong error"
Пример #2
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"
Пример #3
0
def test_invalid_properties():
    """Test that ValueError is raised if entity's properties are not valid json object.

    1. Create an entity 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.
    """
    PropertiesEntity = namedtuple("PropertiesEntity", "properties")
    marshaler = EntityMarshaler(
        marshaler=JSONMarshaler(),
        entity=PropertiesEntity(properties=object()),
    )
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_properties()

    assert error_info.value.args[
        0] == "Failed to marshal entity's properties", "Wrong error"
Пример #4
0
def test_properties(properties):
    """Test that properties are properly marshaled.

    1. Create an entity marshaler for an object with specific properties.
    2. Marshal properties.
    3. Check the marshaled properties.
    """
    PropertiesEntity = namedtuple("PropertiesEntity", "properties")
    marshaler = EntityMarshaler(
        marshaler=JSONMarshaler(),
        entity=PropertiesEntity(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"