Пример #1
0
def test_actions(actions_data):
    """Test that actions are properly parsed.

    1. Create json parser.
    2. Replace parse_action method of the parser so that it returns fake data.
    3. Create an entity parser with the parser.
    4. Parse fields.
    5. Check the parsed fields.
    """
    json_parser = JSONParser()
    json_parser.parse_action = actions_data.index

    parser = EntityParser(data={"actions": actions_data}, parser=json_parser)
    actual_actions = parser.parse_actions()
    assert actual_actions == tuple(range(len(actions_data))), "Wrong actions"
Пример #2
0
def test_parse(component_validator):
    """Test that entity data is properly parsed.

    1. Create an entity.
    2. Create an entity parser.
    3. Replace parser methods so that they return predefined data.
    4. Parse the entity.
    5. Check the entity data.
    """
    entity = Entity(
        classes=("parsed class 1", "parsed class 2"),
        properties={
            "property 1": 1,
            "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="action")],
        title="parsed title",
    )

    parser = EntityParser(data={}, parser=JSONParser())
    parser.parse_classes = lambda: entity.classes
    parser.parse_properties = lambda: entity.properties
    parser.parse_entities = lambda: entity.entities
    parser.parse_links = lambda: entity.links
    parser.parse_actions = lambda: entity.actions
    parser.parse_title = lambda: entity.title

    actual_entity = parser.parse()
    component_validator.validate_entity(actual_entity, entity)
Пример #3
0
def test_parse(component_validator):
    """Test that action data is properly parsed.

    1. Create an action.
    2. Create an action parser.
    3. Replace parser methods so that they return predefined data.
    4. Parse the action.
    5. Check the parsed data.
    """
    action = Action(
        name="parsed name",
        classes=("parsed class 1", "parsed class 2"),
        method=Method.PUT,
        target="/parsed/target",
        title="parsed title",
        media_type="application/parsed+type",
        fields=(Field(name="first"), Field(name="second")),
    )

    parser = ActionParser(data={}, parser=JSONParser())
    parser.parse_name = lambda: action.name
    parser.parse_classes = lambda: action.classes
    parser.parse_method = lambda: action.method
    parser.parse_target = lambda: action.target
    parser.parse_title = lambda: action.title
    parser.parse_media_type = lambda: action.media_type
    parser.parse_fields = lambda: action.fields

    actual_action = parser.parse()
    component_validator.validate_action(actual_action, action)
Пример #4
0
def test_links(links_data):
    """Test that links are properly parsed.

    1. Create json parser.
    2. Replace parse_link method of the parser so that it returns fake data.
    3. Create an embedded representation parser with the parser.
    4. Parse fields.
    5. Check the parsed fields.
    """
    json_parser = JSONParser()
    json_parser.parse_link = links_data.index

    parser = EmbeddedRepresentationParser(data={"links": links_data},
                                          parser=json_parser)
    actual_links = parser.parse_links()
    assert actual_links == tuple(range(len(links_data))), "Wrong links"
Пример #5
0
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)
Пример #6
0
def test_fields(fields_data):
    """Test that fields are properly parsed.

    1. Create an action parser.
    2. Replace parse_field of the json parser so that it returns fake data.
    3. Parse fields.
    4. Check the parsed fields.
    """
    json_parser = JSONParser()
    json_parser.parse_field = fields_data.index

    actual_fields = ActionParser(data={
        "fields": fields_data
    },
                                 parser=json_parser).parse_fields()
    assert actual_fields == tuple(range(len(fields_data))), "Wrong fields"
Пример #7
0
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)
Пример #8
0
def test_missing_fields():
    """Test that empty tuple is returned if action data don't have 'fields' key.

    1. Create an action parser for a dictionary without fields.
    2. Parse fields.
    3. Check that empty tuple is returned.
    """
    actual_fields = ActionParser(data={}, parser=JSONParser()).parse_fields()
    assert actual_fields == (), "Wrong fields"
Пример #9
0
def test_missing_actions():
    """Test that empty tuple is returned if entity data don't have 'actions' key.

    1. Create an entity parser for a dictionary without actions.
    2. Parse actions.
    3. Check that empty tuple is returned.
    """
    actual_actions = EntityParser(data={}, parser=JSONParser()).parse_actions()
    assert actual_actions == (), "Wrong actions"
Пример #10
0
def test_missing_method():
    """Test that GET method is returned if action data don't have 'method' key.

    1. Create an action parser for a dictionary without method.
    2. Parse method.
    3. Check that GET method is returned.
    """
    actual_method = ActionParser(data={}, parser=JSONParser()).parse_method()
    assert actual_method == Method.GET, "Wrong method"
Пример #11
0
def test_missing_title():
    """Test that None is returned if entity data don't have 'title' key.

    1. Create an entity parser for a dictionary without title.
    2. Parse title.
    3. Check that None is returned.
    """
    actual_title = EntityParser(data={}, parser=JSONParser()).parse_title()
    assert actual_title is None, "Wrong title"
Пример #12
0
def test_missing_links():
    """Test that empty tuple is returned if data of embedded representation don't have 'links' key.

    1. Create an embedded representation parser for a dictionary without links.
    2. Parse links.
    3. Check that empty tuple is returned.
    """
    actual_links = EmbeddedRepresentationParser(
        data={}, parser=JSONParser()).parse_links()
    assert actual_links == (), "Wrong links"
Пример #13
0
def test_missing_title():
    """Test that None is returned if data of embedded representation don't have 'title' key.

    1. Create an embedded representation parser for a dictionary without title.
    2. Parse title.
    3. Check that None is returned.
    """
    actual_title = EmbeddedRepresentationParser(
        data={}, parser=JSONParser()).parse_title()
    assert actual_title is None, "Wrong title"
Пример #14
0
def test_missing_properties():
    """Test that empty dictionary is returned if entity data don't have 'properties' key.

    1. Create an entity parser for a dictionary without properties.
    2. Parse properties.
    3. Check that empty dictionary is returned.
    """
    actual_properties = EntityParser(data={},
                                     parser=JSONParser()).parse_properties()
    assert actual_properties == {}, "Wrong properties"
Пример #15
0
def test_properties(properties, expected_properties):
    """Test that properties are properly parsed.

    1. Create an entity parser for a dictionary with specific properties.
    2. Parse properties.
    3. Check the parsed properties.
    """
    parser = EntityParser(data={"properties": properties}, parser=JSONParser())
    actual_properties = parser.parse_properties()
    assert actual_properties == expected_properties, "Wrong properties"
Пример #16
0
def test_target(target, expected_target):
    """Test that target is properly parsed.

    1. Create an action parser for dictionary with specific target.
    2. Parse a target.
    3. Check the parsed target.
    """
    actual_target = ActionParser(data={
        "href": target
    }, parser=JSONParser()).parse_target()
    assert actual_target == expected_target, "Wrong target"
Пример #17
0
def test_classes(classes, expected_classes):
    """Test that classes are properly parsed.

    1. Create an entity parser for a dictionary with specific classes.
    2. Parse classes.
    3. Check the parsed classes.
    """
    actual_classes = EntityParser(data={
        "class": classes
    }, parser=JSONParser()).parse_classes()
    assert actual_classes == expected_classes, "Wrong classes"
Пример #18
0
def test_title(title, expected_title):
    """Test that title is properly parsed.

    1. Create an entity parser for dictionary with specific title.
    2. Parse a title.
    3. Check the parsed title.
    """
    actual_title = EntityParser(data={
        "title": title
    }, parser=JSONParser()).parse_title()
    assert actual_title == expected_title, "Wrong title"
Пример #19
0
def test_classes(classes, expected_classes):
    """Test that classes are properly parsed.

    1. Create an embedded representation parser for a dictionary with specific classes.
    2. Parse classes.
    3. Check the parsed classes.
    """
    parser = EmbeddedRepresentationParser(data={"class": classes},
                                          parser=JSONParser())
    actual_classes = parser.parse_classes()
    assert actual_classes == expected_classes, "Wrong classes"
Пример #20
0
def test_title(title, expected_title):
    """Test that title is properly parsed.

    1. Create an embedded representation parser for dictionary with specific title.
    2. Parse a title.
    3. Check the parsed title.
    """
    parser = EmbeddedRepresentationParser(data={"title": title},
                                          parser=JSONParser())
    actual_title = parser.parse_title()
    assert actual_title == expected_title, "Wrong title"
Пример #21
0
def test_relations(relations, expected_relations):
    """Test that relations are properly parsed.

    1. Create an embedded representation parser for a dictionary with specific relations.
    2. Parse relations.
    3. Check the parsed classes.
    """
    parser = EmbeddedRepresentationParser(data={"rel": relations},
                                          parser=JSONParser())
    actual_relations = parser.parse_relations()
    assert actual_relations == expected_relations, "Wrong relations"
Пример #22
0
def test_name(name, expected_name):
    """Test that name is properly parsed.

    1. Create an action parser for dictionary with specific name.
    2. Parse a name.
    3. Check the parsed name.
    """
    actual_name = ActionParser(data={
        "name": name
    }, parser=JSONParser()).parse_name()
    assert actual_name == expected_name, "Wrong name"
Пример #23
0
def test_default_link_parser(monkeypatch):
    """Test that json parser use LinkParser to parse a link.

    1. Create json parser.
    2. Replace parse method of LinkParser so that it returns fake data.
    3. Parse a link.
    4. Check that returned link is the same as the one from the mocked method.
    """
    monkeypatch.setattr(LinkParser, "parse", lambda self: "Parsed link")

    parsed_link = JSONParser().parse_link({})
    assert parsed_link == "Parsed link", "Wrong link"
Пример #24
0
def test_missing_actions():
    # pylint: disable=line-too-long
    """Test that empty tuple is returned if data of embedded representation don't have 'actions' key.

    1. Create an embedded representation parser for a dictionary without actions.
    2. Parse actions.
    3. Check that empty tuple is returned.
    """
    # pylint: enable=line-too-long
    actual_actions = EmbeddedRepresentationParser(
        data={}, parser=JSONParser()).parse_actions()
    assert actual_actions == (), "Wrong actions"
Пример #25
0
def test_default_field_parser(monkeypatch):
    """Test that json parser use FieldParser to parse a field.

    1. Create json parser.
    2. Replace parse method of FieldParser so that it returns fake data.
    3. Parse a field.
    4. Check that returned field is the same as the one from the mocked method.
    """
    monkeypatch.setattr(FieldParser, "parse", lambda self: "Parsed field")

    parsed_field = JSONParser().parse_field({})
    assert parsed_field == "Parsed field", "Wrong field"
Пример #26
0
def test_missing_sub_entities():
    # pylint: disable=line-too-long
    """Test that empty tuple is returned if data of embedded representation don't have 'entities' key.

    1. Create an embedded representation parser for a dictionary without sub-entities.
    2. Parse sub-entities.
    3. Check that empty tuple is returned.
    """
    # pylint: enable=line-too-long
    parser = EmbeddedRepresentationParser(data={}, parser=JSONParser())
    actual_sub_entities = parser.parse_entities()
    assert actual_sub_entities == (), "Wrong sub-entities"
Пример #27
0
def test_default_entity(monkeypatch):
    """Test that json parser use EntityParser to parse an entity.

    1. Create json parser.
    2. Replace parse method of EntityParser so that it returns fake data.
    3. Parse an entity.
    4. Check that returned entity is the same as the one from the mocked method.
    """
    monkeypatch.setattr(EntityParser, "parse", lambda self: "Parsed entity")

    parsed_entity = JSONParser().parse_entity({})
    assert parsed_entity == "Parsed entity", "Wrong entity"
Пример #28
0
def test_default_action_parser(monkeypatch):
    """Test that json parser use ActionParser to parse an action.

    1. Create json parser.
    2. Replace parse method of ActionParser so that it returns fake data.
    3. Parse an action.
    4. Check that returned action is the same as the one from the mocked method.
    """
    monkeypatch.setattr(ActionParser, "parse", lambda self: "Parsed action")

    parsed_action = JSONParser().parse_action({})
    assert parsed_action == "Parsed action", "Wrong action"
Пример #29
0
def test_unobtainable_properties():
    """Test that ValueError is raised if properties can't be retrieved from entity data.

    1. Create an entity parser for a non-subscriptable object.
    2. Try to call parse_properties method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    with pytest.raises(ValueError) as error_info:
        EntityParser(data=None, parser=JSONParser()).parse_properties()

    assert error_info.value.args[
        0] == "Failed to get properties from entity data", "Wrong error"
Пример #30
0
def test_media_type(media_type, parsed_fields, expected_media_type):
    """Test that media type is properly parsed.

    1. Create an action parser for a dictionary with specific media type.
    2. Replace parse_fields of the parser so that it returns predefined parsed fields.
    3. Parse media type.
    3. Check parsed media type.
    """
    parser = ActionParser(data={"type": media_type}, parser=JSONParser())
    parser.parse_fields = lambda: parsed_fields

    actual_media_type = parser.parse_media_type()
    assert actual_media_type == expected_media_type, "Wrong media type"