예제 #1
0
def test_actions(actions):
    """Check actions of an entity.

    1. Create an entity with different number of actions.
    2. Get actions of the entity.
    3. Check the actions.
    """
    entity = Entity(actions=actions)
    assert entity.actions == tuple(actions), "Wrong actions"
예제 #2
0
def test_default_links():
    """Check default set of links of an entity.

    1. Create an entity without specifying links.
    2. Get links of the entity.
    3. Check the links.
    """
    entity = Entity()
    assert entity.links == (), "Wrong links"
예제 #3
0
def test_title(title, expected_title):
    """Check title of an entity.

    1. Create an entity with different titles.
    2. Get title of the entity.
    3. Check the title.
    """
    entity = Entity(title=title)
    assert entity.title == expected_title, "Wrong title"
예제 #4
0
def test_links(links):
    """Check links of an entity.

    1. Create an entity with different number of links.
    2. Get links of the entity.
    3. Check the links.
    """
    entity = Entity(links=links)
    assert entity.links == tuple(links), "Wrong links"
예제 #5
0
def test_invalid_properties(properties, error_message):
    """Check that ValueError is raised if invalid properties are passed.

    1. Try to create an entity with invalid properties.
    2. Check that ValueError is raised.
    3. Check the error message.
    """
    with pytest.raises(ValueError) as error_info:
        Entity(properties=properties)

    assert error_info.value.args[0] == error_message, "Wrong error message"
예제 #6
0
def test_invalid_classes():
    """Check that ValueError is raised if invalid classes are passed.

    1. Try to create an entity with non-iterable classes.
    2. Check that ValueError is raised.
    3. Check the error message.
    """
    with pytest.raises(ValueError) as error_info:
        Entity(classes=1)

    assert error_info.value.args[
        0] == "Classes must be iterable with string values"
예제 #7
0
def test_incompatible_subentities(entities):
    """Check that ValueError is raised if at least one of the entities is of incompatible type.

    1. Try to create an entity with incompatible type of subentity.
    2. Check that ValueError is raised.
    3. Check the error message.
    """
    with pytest.raises(ValueError) as error_info:
        Entity(entities=entities)

    assert error_info.value.args[
        0] == "Some of the entities are of incompatible type"
예제 #8
0
def test_default_entity(monkeypatch):
    """Test that json marshaler use EntityMarshaler to marshal an entity.

    1. Create json marshaler.
    2. Replace marshal method of EntityMarshaler so that it returns fake data.
    3. Marshal an entity.
    4. Check that returned data are the same as ones from the mocked method.
    """
    monkeypatch.setattr(EntityMarshaler, "marshal", lambda self: "Marshaled entity")

    marshaled_entity = JSONMarshaler().marshal_entity(entity=Entity())
    assert marshaled_entity == "Marshaled entity", "Wrong entity data"
예제 #9
0
def test_custom_entity_marshaler():
    """Test that json marshaler use custom marshaler to marshal an entity.

    1. Create a sub-class of JSONMarshaler with redefined create_entity_marshaler factory.
    2. Create json marshaler from the sub-class.
    3. Marshal an entity.
    4. Check that custom marshaler is used to marshal an entity.
    """
    class _CustomEntityMarshaler(JSONMarshaler):
        create_entity_marshaler = _CustomMarshaler("Custom marshaled entity")

    marshaled_entity = _CustomEntityMarshaler().marshal_entity(entity=Entity())
    assert marshaled_entity == "Custom marshaled entity", "Wrong entity data"
예제 #10
0
def test_marshal_entity():
    """Check that NotImplementedError is raised if marshal_entity is called.

    1. Create an instance of Marshaler class.
    2. Try to marshal an entity.
    3. Check that NotImplementedError is raised.
    4. Check the error message.
    """
    entity = Entity()

    with pytest.raises(NotImplementedError) as error_info:
        Marshaler().marshal_entity(entity=entity)

    assert error_info.value.args[0] == "Marshaler does not support siren entity"
예제 #11
0
def test_default_properties():
    """Check default properties of an entity.

    1. Create an entity without specifying properties.
    2. Get properties of the entity.
    3. Check the properties.
    4. Change the retrieved dictionary.
    5. Get properties of the entity again.
    6. Check that properties have not been updated.
    """
    entity = Entity()
    actual_properties = entity.properties
    assert actual_properties == {}, "Wrong properties"

    actual_properties["new-key"] = "new value"
    assert entity.properties == {}, "Wrong properies"
예제 #12
0
def test_properties(properties, expected_properties):
    """Check properties of an entity.

    1. Create an entity with different properties.
    2. Get properties of the entity.
    3. Check the properties.
    4. Change the retrieved dictionary.
    5. Get properties of the entity again.
    6. Check that properties have not been updated.
    """
    entity = Entity(properties=properties)
    actual_properties = entity.properties
    assert actual_properties == expected_properties, "Wrong properties"

    actual_properties["new-key"] = "new value"
    assert entity.properties == expected_properties, "Wrong properies"
    3. Check the subentities.
    """
    representation = EmbeddedRepresentation(relations=DEFAULT_RELATIONS)
    assert representation.entities == (), "Wrong subentities"


@pytest.mark.parametrize(
    argnames="entities",
    argvalues=[
        [None, EmbeddedRepresentation(relations=["self"])],
        [
            EmbeddedRepresentation(relations=["first"]),
            "second",
            EmbeddedLink(relations="third", target="/third-link"),
        ],
        [Entity()],
    ],
    ids=[
        "First subentity",
        "Subentity in the middle",
        "Entity class",
    ],
)
def test_incompatible_subentities(entities):
    """Check that ValueError is raised if at least one of the entities is of incompatible type.

    1. Try to create an embedded representation with incompatible type of subentity.
    2. Check that ValueError is raised.
    3. Check the error message.
    """
    with pytest.raises(ValueError) as error_info: