예제 #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)
예제 #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"
예제 #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"