Esempio n. 1
0
def test_missing_classes():
    """Test that ValueError is raised if an entity does not have classes attribute.

    1. Create an entity marshaler for an object without classes attribute.
    2. Try to call marshal_classes 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_classes()

    assert error_info.value.args[
        0] == "Failed to get entity's classes", "Wrong error"
Esempio n. 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"
Esempio n. 3
0
def test_non_iterable_classes():
    """Test that ValueError is raised if an entity provides a non-iterable object as its classes.

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

    assert error_info.value.args[
        0] == "Failed to iterate over entity's classes", "Wrong error"
Esempio n. 4
0
def test_classes(classes, expected_classes):
    """Test that classes are properly marshaled.

    1. Create an entity marshaler for an object with specific classes.
    2. Marshal classes.
    3. Check the marshaled classes.
    """
    ClassesEntity = namedtuple("ClassesEntity", "classes")
    marshaler = EntityMarshaler(marshaler=JSONMarshaler(),
                                entity=ClassesEntity(classes=classes))

    actual_classes = marshaler.marshal_classes()
    assert actual_classes == expected_classes, "Wrong classes"