コード例 #1
0
def test_json(request_builder):
    json = decorators.json()

    # Verify that we add the hook
    json.modify_request(request_builder)
    request_builder.add_transaction_hook.assert_called_with(json._hook)

    # Verify
    request_builder.info["data"] = {"field_name": "field_value"}
    json.set_json_body(request_builder)
    assert request_builder.info["json"] == {"field_name": "field_value"}
    assert len(request_builder.info["json"]) == 1

    # Check delete
    del request_builder.info["json"]["field_name"]
    assert len(request_builder.info["json"]) == 0
    assert "data" not in request_builder.info

    # Verify nested attribute
    request_builder.info["data"] = {("outer", "inner"): "inner_value"}
    json.set_json_body(request_builder)
    assert request_builder.info["json"] == {"outer": {"inner": "inner_value"}}
    assert "data" not in request_builder.info

    # Verify that error is raised when path is empty
    request_builder.info["data"] = {(): "value"}
    with pytest.raises(ValueError):
        json.set_json_body(request_builder)

    # Verify that error is raised when paths conflict
    request_builder.info["data"] = body = collections.OrderedDict()
    body["key"] = "outer"
    body["key", "inner"] = "inner value"
    with pytest.raises(ValueError):
        json.set_json_body(request_builder)
コード例 #2
0
def test_json_list(request_builder):
    json = decorators.json()

    # Verify nested attribute
    request_builder.info["data"] = ["one", "two", "three"]
    json.set_json_body(request_builder)
    assert request_builder.info["json"] == ["one", "two", "three"]
    assert "data" not in request_builder.info
コード例 #3
0
ファイル: test_decorators.py プロジェクト: ppslim/uplink
def test_json(request_builder):
    json = decorators.json()

    # Verify without
    json.modify_request(request_builder)
    assert "json" not in request_builder.info

    # Verify with
    request_builder.info["data"] = {"field_name": "field_value"}
    json.modify_request(request_builder)
    assert request_builder.info["json"] == {"field_name": "field_value"}
    assert "data" not in request_builder.info
コード例 #4
0
ファイル: test_decorators.py プロジェクト: vault-the/uplink
def test_json(request_builder):
    multipart = decorators.json()
    request_builder.info["data"] = {"field_name": "field_value"}
    multipart.modify_request(request_builder)
    assert request_builder.info["json"] == {"field_name": "field_value"}
    assert "data" not in request_builder.info
コード例 #5
0
    request_definition.argument_annotations = ()
    request_definition.method_annotations = ()
    value = getattr(factory, method)(None, request_definition)
    assert value is None

    # Verify: relevant
    request_definition.argument_annotations = (object(), )
    value = getattr(factory, method)(object, request_definition)
    assert callable(value)


@pytest.mark.parametrize(
    "cls, method, decorator",
    [
        (loads.from_json, "create_response_body_converter", returns.json()),
        (dumps.to_json, "create_request_body_converter", json()),
    ],
)
def test_json_builders(mocker, cls, method, decorator, request_definition):
    # Setup
    func = mocker.stub()
    func.return_value = 1
    obj = cls(object)
    factory = obj.using(func)

    # Verify: not relevant
    request_definition.argument_annotations = ()
    request_definition.method_annotations = ()
    value = getattr(factory, method)(object, request_definition)
    assert value is None