Beispiel #1
0
def test_fix_filled(mocker):
    ctor = mocker.patch(f"{PKG}.UrlencodedRequestBody",
                        return_value=sentinel.fixed)

    compiled = UrlencodedRequestBodyCompiled(data=sentinel.data)
    fixed = compiled.fix()
    assert fixed is sentinel.fixed

    ctor.assert_called_once_with(params=sentinel.data)
Beispiel #2
0
def test_compile_and_replace_given_valid_data(mocker):
    compile_params = mocker.patch(f"{PKG}.compile_url_params")
    compile_params.return_value = sentinel.params

    default = UrlencodedRequestBodyCompiled(data=sentinel.original_data)
    compiled = default.compile_and_replace({"data": sentinel.data})
    assert isinstance(compiled, UrlencodedRequestBodyCompiled)
    assert compiled.data is sentinel.params

    compile_params.assert_called_once_with(sentinel.data)
Beispiel #3
0
def test_compile_and_replace_given_invalid_data(mocker):
    compile_params = mocker.patch(f"{PKG}.compile_url_params")
    compile_params.side_effect = CompilationError("m", node=NamedNode("x"))

    default = UrlencodedRequestBodyCompiled(data=sentinel.original_data)
    with raises(CompilationError) as error_info:
        default.compile_and_replace({"data": sentinel.data})
    assert error_info.value.path == [NamedNode("data"), NamedNode("x")]

    compile_params.assert_called_once_with(sentinel.data)
Beispiel #4
0
def test_replace_given_the_same_type():
    original = UrlencodedRequestBodyCompiled(data=sentinel.original_data)
    other = UrlencodedRequestBodyCompiled()
    replaced = original.replace(other)
    assert isinstance(replaced, UrlencodedRequestBodyCompiled)
    assert replaced.data is sentinel.original_data

    other = UrlencodedRequestBodyCompiled(data=sentinel.new_data)
    replaced = original.replace(other)
    assert isinstance(replaced, UrlencodedRequestBodyCompiled)
    assert replaced.data is sentinel.new_data
Beispiel #5
0
def test_compile_and_replace_empty():
    default = UrlencodedRequestBodyCompiled(data=sentinel.original_data)
    compiled = default.compile_and_replace({})
    assert isinstance(compiled, UrlencodedRequestBodyCompiled)
    assert compiled.data is sentinel.original_data
Beispiel #6
0
def test_replace_given_another_type():
    original = UrlencodedRequestBodyCompiled()
    other = NonCallableMock(RequestBodyCompiled)
    replaced = original.replace(other)
    assert replaced is other
)
def test_compile_given_invalid_obj(compiler, obj, expected_path):
    with raises(CompilationError) as error_info:
        compiler.compile(obj)
    assert error_info.value.path == expected_path


def test_compile_empty(compiler, default_body):
    compiled = compiler.compile({})
    assert compiled is sentinel.default_result


@mark.parametrize(
    ("type_key", "expected"),
    (
        ("urlencoded", UrlencodedRequestBodyCompiled()),
        ("json", JsonRequestBodyCompiled()),
    ),
)
def test_given_type(compiler: RequestBodyCompiler, default_body, type_key,
                    expected):
    replaced_body = NonCallableMock(RequestBodyCompiled)
    replaced_body.compile_and_replace = Mock(return_value=sentinel.new_result)
    default_body.replace = Mock(return_value=replaced_body)

    obj = {"type": Argument("type"), "foo": Argument("foo")}
    compiled = compiler.compile(obj, {"type": type_key, "foo": "bar"})
    assert compiled is sentinel.new_result

    default_body.replace.assert_called_once_with(expected)
    replaced_body.compile_and_replace.assert_called_once_with({