コード例 #1
0
def test_invalid_field():
    """Test that error is propagated from field marshaler to json marshaler.

    1. Create json marshaler.
    2. Try to marshal invalid field.
    3. Check that error is raised.
    4. Check that error is the same as one that field marshaler raises.
    """
    invalid_field = None
    marshaler = JSONMarshaler()
    with pytest.raises(ValueError) as actual_error_info:
        marshaler.marshal_field(invalid_field)

    with pytest.raises(ValueError) as expected_error_info:
        marshaler.create_field_marshaler(invalid_field).marshal()

    assert actual_error_info.value.args[0] == expected_error_info.value.args[0], (
        "Wrong error is raised"
        )
コード例 #2
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"