示例#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_marshal():
    """Test that field data is properly marshaled.

    1. Create a field.
    2. Create a field marshaler for the field.
    3. Replace marshaler methods so that they return predefined data.
    4. Marshal the field.
    5. Check the marshaled data.
    """
    marshaler = FieldMarshaler(Field(name="field"))
    marshaler.marshal_name = lambda: "marshal_name"
    marshaler.marshal_classes = lambda: "marshal_classes"
    marshaler.marshal_input_type = lambda: "marshal_input_type"
    marshaler.marshal_value = lambda: "marshal_value"
    marshaler.marshal_title = lambda: "marshal_title"

    actual_data = marshaler.marshal()
    expected_data = {
        "name": "marshal_name",
        "class": "marshal_classes",
        "type": "marshal_input_type",
        "value": "marshal_value",
        "title": "marshal_title",
    }
    assert actual_data == expected_data, "Field is not properly marshaled"
示例#3
0
def test_parse(component_validator):
    """Test that field data is properly parsed.

    1. Create a field.
    2. Create a field parser.
    3. Replace parser methods so that they return predefined data.
    4. Parse the field.
    5. Check the parsed data.
    """
    field = Field(
        name="parsed name",
        classes=("parsed class 1", "parsed class 2"),
        input_type=InputType.HIDDEN,
        value="parsed value",
        title="parsed title",
        )

    parser = FieldParser(data={})
    parser.parse_name = lambda: field.name
    parser.parse_classes = lambda: field.classes
    parser.parse_input_type = lambda: field.input_type
    parser.parse_value = lambda: field.value
    parser.parse_title = lambda: field.title

    actual_field = parser.parse()
    component_validator.validate_field(actual_field, field)
示例#4
0
def test_default_value():
    """Check default value of a field.

    1. Create a field without specifying a value.
    2. Check the value of the field.
    """
    field = Field(name="test default title")
    assert field.value is None, "Wrong value"
示例#5
0
def test_default_classes():
    """Check default classes of the field.

    1. Create a field without specifying classes.
    2. Check classes of the field.
    """
    field = Field(name="test default classes")
    assert field.classes == (), "Wrong classes"
示例#6
0
def test_default_title():
    """Check default title of a field.

    1. Create a field without specifying a title.
    2. Check the title of the field.
    """
    field = Field(name="test default title")
    assert field.title is None, "Wrong title"
示例#7
0
def test_invalid_classes():
    """Check that ValueError is raised if invalid classes are passed.

    1. Try to create a field with non-iterable classes.
    2. Check that ValueError is raised.
    """
    with pytest.raises(ValueError):
        Field(name="test invalid classes", classes=1)
示例#8
0
def test_default_input_type():
    """Check default input type of the field.

    1. Create a field without specifying input type.
    2. Check input field of the field.
    """
    field = Field(name="test default input type")
    assert field.input_type == InputType.TEXT, "Wrong default input type"
示例#9
0
def test_classes(classes, expected_classes):
    """Check classes of a field.

    1. Create a field with different classes.
    2. Get classes of the field.
    3. Check the classes.
    """
    field = Field(name="test classes", classes=classes)
    assert field.classes == expected_classes, "Wrong classes"
示例#10
0
def test_input_type(input_type):
    """Check input type of the field.

    1. Create a field with explicit input type.
    2. Get input type of the field.
    3. Check the type.
    """
    field = Field(name="test input type", input_type=input_type)
    assert field.input_type == InputType(input_type), "Wrong input type"
示例#11
0
def test_name(name, expected_name):
    """Check name of a field.

    1. Create a field with different names.
    2. Get name of the field.
    3. Check the name.
    """
    field = Field(name=name)
    assert field.name == expected_name, "Wrong name"
示例#12
0
def test_title(title, expected_title):
    """Check title of a field.

    1. Create a field with different titles.
    2. Get title of the field.
    3. Check the title.
    """
    field = Field(name="test title", title=title)
    assert field.title == expected_title, "Wrong title"
示例#13
0
def test_value(value, expected_value):
    """Check value of a field.

    1. Create a field with different values.
    2. Get value of the field.
    3. Check the value.
    """
    field = Field(name="test title", value=value)
    assert field.value == expected_value, "Wrong value"
示例#14
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"
示例#15
0
def test_marshal_field():
    """Check that NotImplementedError is raised if marshal_field is called.

    1. Create an instance of Marshaler class.
    2. Try to marshal a field.
    3. Check that NotImplementedError is raised.
    4. Check the error message.
    """
    with pytest.raises(NotImplementedError) as error_info:
        Marshaler().marshal_field(field=Field(name="field"))

    assert error_info.value.args[0] == "Marshaler does not support siren fields"
示例#16
0
def test_duplicated_field_names():
    """Check that ValueError is raised if some of the fields have the same name.

    1. Create 2 fields with the same name.
    1. Try to create an action with the created fields.
    2. Check that ValueError is raised.
    3. Check the error message.
    """
    fields = [
        Field(name="duplicate", title="First field with non-unique name"),
        Field(name="unique", title="Field with unique name"),
        Field(name="duplicate", title="Second field with non-unique name"),
    ]

    with pytest.raises(ValueError) as error_info:
        Action(name="test duplicate fields",
               target="/test/duplicate/fields",
               fields=fields)

    assert error_info.value.args[
        0] == "Some of the fields have the same name", "Wrong error"
示例#17
0
def test_custom_field_marshaler():
    """Test that json marshaler use custom marshaler to marshal a field.

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

    marshaled_field = _CustomFieldMarshaler().marshal_field(Field(name="field"))
    assert marshaled_field == "Custom marshaled field", "Wrong field data"
示例#18
0
def test_media_type_with_fields(media_type, expected_media_type):
    """Check media type of an action with fields.

    1. Create an action with different media type with 1 field.
    2. Get media type of the action.
    3. Check the media type.
    """
    action = Action(
        name="test media type. No fields",
        target="/test-media-types",
        fields=[Field(name="single field")],
        media_type=media_type,
    )
    assert action.media_type == expected_media_type, "Wrong media type"
示例#19
0
def test_invalid_input_type():
    """Check that ValueError is raised if invalid input type is specified.

    1. Try to create a field with name of the input field instead of constant from enumeration.
    2. Check that ValueError is raised.
    3. Check the error message.
    """
    invalid_input_type = InputType.TEXT.name

    with pytest.raises(ValueError) as error_info:
        Field(name="test invalid input type", input_type=invalid_input_type)

    assert error_info.value.args[0] == "Unsupported input type '{0}'".format(
        invalid_input_type), ("Wrong error message")
示例#20
0
def test_action(component_validator):
    """Test that action can be marshaled and parsed back.

    1. Create an action.
    2. Create JSON parser and marshaler.
    3. Marshal the field.
    4. Parse the data.
    5. Check that parsed action has the same data as the original one.
    """
    action = Action(
        name="action name",
        classes=("action class 1", "action class 2"),
        method=random.choice(list(Method)),
        target="/action/target",
        title="action title",
        media_type="application/json",
        fields=(Field(name="first"), Field(name="second")),
    )

    action_data = JSONMarshaler().marshal_action(action)
    actual_action = JSONParser().parse_action(action_data)

    component_validator.validate_action(actual_action, action)
示例#21
0
def test_field(component_validator):
    """Test that field can be marshaled and parsed back.

    1. Create a field.
    2. Create JSON parser and marshaler.
    3. Marshal the field.
    4. Parse the data.
    5. Check that parsed field has the same data as the original one.
    """
    field = Field(
        name="field name",
        classes=("field class 1", "field class 2"),
        input_type=random.choice(list(InputType)),
        value="field value",
        title="field title",
    )

    field_data = JSONMarshaler().marshal_field(field)
    actual_field = JSONParser().parse_field(field_data)

    component_validator.validate_field(actual_field, field)
示例#22
0
def test_non_marshalable_fields():
    """Test that ValueError is raised if one of action fields is not marshallable.

    1. Create json marshaler that raises exception when marshal_field method is called.
    2. Create an action marshaler with the json marshaler.
    3. Try to call marshal_fields method.
    4. Check that ValueError is raised.
    5. Check the error message.
    """
    class _FieldErrorMarshaler(JSONMarshaler):
        def marshal_field(self, field):
            raise Exception()

    FieldsAction = namedtuple("FieldsAction", "fields")
    marshaler = ActionMarshaler(
        marshaler=_FieldErrorMarshaler(),
        action=FieldsAction(fields=[Field(name="first")]),
    )

    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_fields()

    assert error_info.value.args[
        0] == "Failed to marshal action's fields", "Wrong error"
示例#23
0
    2. Try to call parse_media_type 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_media_type()

    assert error_info.value.args[
        0] == "Failed to get media type from action data", "Wrong error"


@pytest.mark.parametrize(
    argnames="parsed_fields,expected_media_type",
    argvalues=[
        [(), None],
        [(Field(name="name"), ), "application/x-www-form-urlencoded"],
    ],
    ids=[
        "Without fields",
        "With fields",
    ],
)
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())
示例#24
0
        marshaler=_FieldErrorMarshaler(),
        action=FieldsAction(fields=[Field(name="first")]),
    )

    with pytest.raises(ValueError) as error_info:
        marshaler.marshal_fields()

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


@pytest.mark.parametrize(
    argnames="fields",
    argvalues=[
        [],
        [Field(name="single field")],
        [Field(name="first"), Field(name="second")],
    ],
    ids=[
        "Empty list",
        "Single field",
        "Multiple fields",
    ],
)
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.
示例#25
0
def test_default_title():
    """Check default title of an action.

    1. Create an action without specifying a title.
    2. Check the title of the action.
    """
    action = Action(name="test default title", target="/test/default/title")
    assert action.title is None, "Wrong title"


@pytest.mark.parametrize(
    argnames="fields",
    argvalues=[
        [],
        [Field(name="single")],
        [Field(name="first"), Field(name="second")],
    ],
    ids=[
        "Without fields",
        "Single",
        "Several",
    ],
)
def test_fields(fields):
    """Check fields of an action.

    1. Create an action with different number of fields.
    2. Get fields of the action.
    3. Check the fields.
    """