Exemplo n.º 1
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)
Exemplo n.º 2
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"
Exemplo n.º 3
0
def test_missing_media_type(parsed_fields, expected_media_type):
    """Test that None is returned if action data don't have 'type' key.

    1. Create an action parser for a dictionary without media type.
    2. Replace parse_fields of the parser so that it returns predefined parsed fields.
    3. Parse media type.
    3. Check that default value is returned.
    """
    parser = ActionParser(data={}, 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"
Exemplo n.º 4
0
    def create_action_parser(self, data):
        """Factory method to create a parser for an action.

        :param data: action data to parse.
        :returns: :class:`ActionParser <lila.serialization.json.action.ActionParser>`.
        """
        return ActionParser(data=data, parser=self)
Exemplo n.º 5
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"
Exemplo n.º 6
0
def test_missing_title():
    """Test that None is returned if action data don't have 'title' key.

    1. Create an action parser for a dictionary without title.
    2. Parse title.
    3. Check that None is returned.
    """
    actual_title = ActionParser(data={}, parser=JSONParser()).parse_title()
    assert actual_title is None, "Wrong title"
Exemplo n.º 7
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"
Exemplo n.º 8
0
def test_title(title, expected_title):
    """Test that title is properly parsed.

    1. Create an action parser for dictionary with specific title.
    2. Parse a title.
    3. Check the parsed title.
    """
    actual_title = ActionParser(data={
        "title": title
    }, parser=JSONParser()).parse_title()
    assert actual_title == expected_title, "Wrong title"
Exemplo n.º 9
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"
Exemplo n.º 10
0
def test_classes(classes, expected_classes):
    """Test that classes are properly parsed.

    1. Create an action parser for a dictionary with specific classes.
    2. Parse classes.
    3. Check the parsed classes.
    """
    actual_classes = ActionParser(data={
        "class": classes
    }, parser=JSONParser()).parse_classes()
    assert actual_classes == expected_classes, "Wrong classes"
Exemplo n.º 11
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"
Exemplo n.º 12
0
def test_unobtainable_classes():
    """Test that ValueError is raised if classes can't be retrieved from action data.

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

    assert error_info.value.args[
        0] == "Failed to get classes from action data", "Wrong error"
Exemplo n.º 13
0
def test_method():
    """Test that method are properly parsed.

    1. Create an action parser for a dictionary with specific method.
    2. Parse method.
    3. Check the parsed method.
    """
    method = random.choice(list(Method))
    actual_method = ActionParser(data={
        "method": method.value
    },
                                 parser=JSONParser()).parse_method()
    assert actual_method == method, "Wrong method"
Exemplo n.º 14
0
def test_action_creation_error():
    """Test that ValueError is raised if an error occurs during action creation.

    1. Create an action parser.
    2. Replace parse_method method so that it returns invalid method.
    3. Try to call parse method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    action_data = {
        "name": "name",
        "classes": [],
        "href": "/target",
    }
    parser = ActionParser(data=action_data, parser=JSONParser())
    parser.parse_method = lambda: "invalid method"

    with pytest.raises(ValueError) as error_info:
        parser.parse()

    assert error_info.value.args[
        0] == "Failed to create an action with provided data", ("Wrong error")
Exemplo n.º 15
0
def test_missing_name():
    """Test that ValueError is raised if action data don't contain 'name' key.

    1. Create an action parser for a dictionary without name.
    2. Try to call parse_name method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    with pytest.raises(ValueError) as error_info:
        ActionParser(data={}, parser=JSONParser()).parse_name()

    assert error_info.value.args[
        0] == "Action data do not have required 'name' key", "Wrong error"
Exemplo n.º 16
0
def test_non_iterable_classes():
    """Test that ValueError is raised if action data has a non-iterable object for classes.

    1. Create an action parser for a dictionary with non-iterable classes.
    2. Try to call parse_classes method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    with pytest.raises(ValueError) as error_info:
        ActionParser(data={"class": None}, parser=JSONParser()).parse_classes()

    assert error_info.value.args[
        0] == "Failed to iterate over classes from action data", (
            "Wrong error")
Exemplo n.º 17
0
def test_not_supported_method():
    """Test that ValueError is raised if method is not supported.

    1. Create an action parser for a dictionary with not supported value for method.
    2. Try to call parse_method method.
    3. Check that ValueError is raised.
    4. Check the error message.
    """
    with pytest.raises(ValueError) as error_info:
        ActionParser(data={
            "method": "invalid method"
        }, parser=JSONParser()).parse_method()

    assert error_info.value.args[
        0] == "Action data contain not supported method", "Wrong error"
Exemplo n.º 18
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"
Exemplo n.º 19
0
def test_non_parsable_fields():
    """Test that ValueError is raised if one of fields can't be parsed.

    1. Create json parser that raises exception when parse_field method is called.
    2. Create an action parser with the json parser.
    3. Try to call parse_fields method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    class _FieldErrorParser(JSONParser):
        def parse_field(self, data):
            raise Exception()

    with pytest.raises(ValueError) as error_info:
        ActionParser(data={
            "fields": [{}]
        }, parser=_FieldErrorParser()).parse_fields()

    assert error_info.value.args[
        0] == "Failed to parse action's fields", "Wrong error"