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

    1. Create an action.
    2. Create an action marshaler for the action.
    3. Replace marshaler methods so that they return predefined data.
    4. Marshal the action.
    5. Check the marshaled data.
    """
    marshaler = ActionMarshaler(
        marshaler=JSONMarshaler(),
        action=Action(name="action", target="/target"),
    )
    marshaler.marshal_name = lambda: "marshal_name"
    marshaler.marshal_classes = lambda: "marshal_classes"
    marshaler.marshal_method = lambda: "marshal_method"
    marshaler.marshal_target = lambda: "marshal_target"
    marshaler.marshal_title = lambda: "marshal_title"
    marshaler.marshal_media_type = lambda: "marshal_media_type"
    marshaler.marshal_fields = lambda: "marshal_fields"

    actual_data = marshaler.marshal()
    expected_data = {
        "name": "marshal_name",
        "class": "marshal_classes",
        "method": "marshal_method",
        "href": "marshal_target",
        "title": "marshal_title",
        "type": "marshal_media_type",
        "fields": "marshal_fields",
    }
    assert actual_data == expected_data, "Action is not properly marshaled"
コード例 #2
0
ファイル: test_integration.py プロジェクト: KillAChicken/lila
def test_entity(component_validator):
    """Test that entity can be marshaled and parsed back.

    1. Create an entity.
    2. Create JSON parser and marshaler.
    3. Marshal the entity.
    4. Parse the data.
    5. Check that parsed entity has the same data as the original one.
    """
    entity = Entity(
        classes=("entity class 1", "entity class 2"),
        properties={
            "entity property 1": 1,
            "entity property 2": [1, 2]
        },
        entities=(
            EmbeddedLink(target="/embedded/link/target",
                         relations=["relation"]),
            EmbeddedRepresentation(relations=["relation"]),
        ),
        links=[Link(target="/link/target", relations=["relation"])],
        actions=[Action(target="/action/target", name="entity action")],
        title="entity title",
    )

    entity_data = JSONMarshaler().marshal_entity(entity)
    actual_entity = JSONParser().parse_entity(entity_data)

    component_validator.validate_entity(actual_entity, entity)
コード例 #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_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"
コード例 #5
0
ファイル: test_integration.py プロジェクト: KillAChicken/lila
def test_embedded_representation(component_validator):
    """Test that embedded representation can be marshaled and parsed back.

    1. Create an embedded representation.
    2. Create JSON parser and marshaler.
    3. Marshal the embedded representation.
    4. Parse the data.
    5. Check that parsed embedded representation has the same data as the original one.
    """
    representation = EmbeddedRepresentation(
        relations=["representation relation 1", "representation relation 2"],
        classes=("representation class 1", "representation class 2"),
        properties={
            "representation property 1": 1,
            "representation property 2": [1, 2]
        },
        entities=(
            EmbeddedLink(target="/embedded/link/target",
                         relations=["relation"]),
            EmbeddedRepresentation(relations=["relation"]),
        ),
        links=[Link(target="/link/target", relations=["relation"])],
        actions=[
            Action(target="/action/target", name="representation action")
        ],
        title="representation title",
    )

    representation_data = JSONMarshaler().marshal_embedded_representation(
        representation)
    actual_representation = JSONParser().parse_embedded_representation(
        representation_data)

    component_validator.validate_embedded_representation(
        actual_representation, representation)
コード例 #6
0
def test_links(links):
    """Test that links are properly marshaled.

    1. Create an entity marshaler.
    2. Replace marshal_link of the marshaler so that it returns fake data.
    3. Marshal links.
    4. Check the marshaled links.
    """
    json_marshaler = JSONMarshaler()
    json_marshaler.marshal_link = links.index

    LinksEntity = namedtuple("LinksEntity", "links")
    marshaler = EntityMarshaler(marshaler=json_marshaler,
                                entity=LinksEntity(links=links))

    actual_data = marshaler.marshal_links()
    assert actual_data == list(range(len(links))), "Wrong links"
コード例 #7
0
def test_actions(actions):
    """Test that actions are properly marshaled.

    1. Create an entity marshaler.
    2. Replace marshal_action of the marshaler so that it returns fake data.
    3. Marshal actions.
    4. Check the marshaled actions.
    """
    json_marshaler = JSONMarshaler()
    json_marshaler.marshal_action = actions.index

    ActionsEntity = namedtuple("ActionsEntity", "actions")
    marshaler = EntityMarshaler(marshaler=json_marshaler,
                                entity=ActionsEntity(actions=actions))

    actual_data = marshaler.marshal_actions()
    assert actual_data == list(range(len(actions))), "Wrong actions"
コード例 #8
0
def test_fields(fields):
    """Test that fields are properly marshaled.

    1. Create an action marshaler.
    2. Replace marshal_field of the marshaler so that it returns fake data.
    3. Marshal fields.
    4. Check the marshaled fields.
    """
    json_marshaler = JSONMarshaler()
    json_marshaler.marshal_field = fields.index

    FieldsAction = namedtuple("FieldsAction", "fields")
    marshaler = ActionMarshaler(marshaler=json_marshaler,
                                action=FieldsAction(fields=fields))

    actual_data = marshaler.marshal_fields()
    assert actual_data == list(range(len(fields))), "Wrong fields"
コード例 #9
0
def test_links(links):
    """Test that links are properly marshaled.

    1. Create an embedded representation marshaler.
    2. Replace marshal_link of the marshaler so that it returns fake data.
    3. Marshal links.
    4. Check the marshaled links.
    """
    json_marshaler = JSONMarshaler()
    json_marshaler.marshal_link = links.index

    LinksRepresentation = namedtuple("LinksRepresentation", "links")
    marshaler = RepresentationMarshaler(
        marshaler=json_marshaler,
        embedded_representation=LinksRepresentation(links=links),
    )

    actual_data = marshaler.marshal_links()
    assert actual_data == list(range(len(links))), "Wrong links"
コード例 #10
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"
コード例 #11
0
def test_default_field_marshaler(monkeypatch):
    """Test that json marshaler use FieldMarshaler to marshal a field.

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

    marshaled_field = JSONMarshaler().marshal_field(Field(name="field"))
    assert marshaled_field == "Marshaled field", "Wrong field data"
コード例 #12
0
def test_default_link_marshaler(monkeypatch):
    """Test that json marshaler use LinkMarshaler to marshal a link.

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

    marshaled_link = JSONMarshaler().marshal_link(link=Link(relations=(), target="/target"))
    assert marshaled_link == "Marshaled link", "Wrong link data"
コード例 #13
0
def test_name(name, expected_name):
    """Test that name is properly marshaled.

    1. Create an action marshaler for an object with specific name.
    2. Marshal action's name.
    3. Check the marshaled name.
    """
    NameAction = namedtuple("NameAction", "name")
    marshaler = ActionMarshaler(marshaler=JSONMarshaler(),
                                action=NameAction(name=name))
    actual_name = marshaler.marshal_name()
    assert actual_name == expected_name, "Wrong name"
コード例 #14
0
def test_title(title, expected_title):
    """Test that title is properly marshaled.

    1. Create an action marshaler for an object with specific title.
    2. Marshal action's title.
    3. Check the marshaled title.
    """
    TitleAction = namedtuple("TitleAction", "title")
    marshaler = ActionMarshaler(marshaler=JSONMarshaler(),
                                action=TitleAction(title=title))

    actual_title = marshaler.marshal_title()
    assert actual_title == expected_title, "Wrong title"
コード例 #15
0
def test_target(target, expected_target):
    """Test that target is properly marshaled.

    1. Create an action marshaler for an object with specific target.
    2. Marshal action's target.
    3. Check the marshaled target.
    """
    TargetAction = namedtuple("TargetAction", "target")
    marshaler = ActionMarshaler(marshaler=JSONMarshaler(),
                                action=TargetAction(target=target))

    actual_target = marshaler.marshal_target()
    assert actual_target == expected_target, "Wrong target"
コード例 #16
0
def test_method(method):
    """Test that method is properly marshaled.

    1. Create an action marshaler for an object with specific method.
    2. Marshal action's method.
    3. Check the marshaled method.
    """
    MethodAction = namedtuple("MethodAction", "method")
    marshaler = ActionMarshaler(marshaler=JSONMarshaler(),
                                action=MethodAction(method=method))

    actual_method = marshaler.marshal_method()
    assert actual_method == Method(method).value, "Wrong method"
コード例 #17
0
def test_title(title, expected_title):
    """Test that title is properly marshaled.

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

    actual_title = marshaler.marshal_title()
    assert actual_title == expected_title, "Wrong title"
コード例 #18
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"
コード例 #19
0
def test_missing_classes():
    """Test that ValueError is raised if an action does not have classes attribute.

    1. Create an action 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 = ActionMarshaler(marshaler=JSONMarshaler(), action=object())
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_classes()

    assert error_info.value.args[
        0] == "Failed to get action's classes", "Wrong error"
コード例 #20
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"
コード例 #21
0
def test_default_embedded_link_marshaler(monkeypatch):
    """Test that json marshaler use EmbeddedLinkMarshaler to marshal an embedded link.

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

    marshaled_link = JSONMarshaler().marshal_embedded_link(
        embedded_link=EmbeddedLink(relations=["self"], target="/target"),
        )
    assert marshaled_link == "Marshaled embedded link", "Wrong data of the embedded link"
コード例 #22
0
def test_default_action_marshaler(monkeypatch):
    """Test that json marshaler use ActionMarshaler to marshal an action.

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

    marshaled_action = JSONMarshaler().marshal_action(
        action=Action(name="action", target="/target"),
        )
    assert marshaled_action == "Marshaled action", "Wrong action data"
コード例 #23
0
def test_media_type(media_type, expected_media_type):
    """Test that media type is properly marshaled.

    1. Create an action marshaler for an object with specific media type.
    2. Marshal action's media type.
    3. Check the marshaled media type.
    """
    MediaTypeAction = namedtuple("MediaTypeAction", "media_type")
    marshaler = ActionMarshaler(
        marshaler=JSONMarshaler(),
        action=MediaTypeAction(media_type=media_type),
    )

    actual_media_type = marshaler.marshal_media_type()
    assert actual_media_type == expected_media_type, "Wrong media type"
コード例 #24
0
def test_title(title, expected_title):
    """Test that title is properly marshaled.

    1. Create an embedded representation marshaler for an object with specific title.
    2. Marshal title.
    3. Check the marshaled title.
    """
    TitleRepresentation = namedtuple("TitleRepresentation", "title")
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=TitleRepresentation(title=title),
    )

    actual_title = marshaler.marshal_title()
    assert actual_title == expected_title, "Wrong title"
コード例 #25
0
def test_missing_title():
    """Test that ValueError is raised if an embedded representation does not have title attribute.

    1. Create an embedded representation marshaler for an object without title attribute.
    2. Try to call marshal_title method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    marshaler = RepresentationMarshaler(marshaler=JSONMarshaler(),
                                        embedded_representation=object())
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_title()

    expected_error = "Failed to get title of the embedded representation"
    assert error_info.value.args[0] == expected_error, "Wrong error"
コード例 #26
0
def test_classes(classes, expected_classes):
    """Test that classes are properly marshaled.

    1. Create an embedded representation marshaler for an object with specific classes.
    2. Marshal classes.
    3. Check the marshaled classes.
    """
    ClassesRepresentation = namedtuple("ClassesRepresentation", "classes")
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=ClassesRepresentation(classes=classes),
    )

    actual_classes = marshaler.marshal_classes()
    assert actual_classes == expected_classes, "Wrong classes"
コード例 #27
0
def test_relations(relations, expected_relations):
    """Test that relations are properly marshaled.

    1. Create an embedded representation marshaler for an object with specific relations.
    2. Marshal relations.
    3. Check the marshaled relations.
    """
    RelationsRepresentation = namedtuple("RelationsRepresentation",
                                         "relations")
    marshaler = RepresentationMarshaler(
        marshaler=JSONMarshaler(),
        embedded_representation=RelationsRepresentation(relations=relations),
    )

    actual_relations = marshaler.marshal_relations()
    assert actual_relations == expected_relations, "Wrong relations"
コード例 #28
0
def test_non_iterable_classes():
    """Test that ValueError is raised if an action provides a non-iterable object as its classes.

    1. Create an action 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.
    """
    ClassesAction = namedtuple("ClassesAction", "classes")
    marshaler = ActionMarshaler(marshaler=JSONMarshaler(),
                                action=ClassesAction(classes=None))
    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_classes()

    assert error_info.value.args[
        0] == "Failed to iterate over action's classes", "Wrong error"
コード例 #29
0
def test_non_iterable_actions():
    """Test that ValueError is raised if an entity provides a non-iterable object as its actions.

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

    assert error_info.value.args[
        0] == "Failed to iterate over entity's actions", "Wrong error"
コード例 #30
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"