def test_formdata():
    stream = io.BytesIO(b"data")
    upload = UploadFile(filename="file", file=stream)
    form = FormData([("a", "123"), ("a", "456"), ("b", upload)])
    assert "a" in form
    assert "A" not in form
    assert "c" not in form
    assert form["a"] == "456"
    assert form.get("a") == "456"
    assert form.get("nope", default=None) is None
    assert form.getlist("a") == ["123", "456"]
    assert list(form.keys()) == ["a", "b"]
    assert list(form.values()) == ["456", upload]
    assert list(form.items()) == [("a", "456"), ("b", upload)]
    assert len(form) == 2
    assert list(form) == ["a", "b"]
    assert dict(form) == {"a": "456", "b": upload}
    assert (repr(form) == "FormData([('a', '123'), ('a', '456'), ('b', " +
            repr(upload) + ")])")
    assert FormData(form) == form
    assert FormData({
        "a": "123",
        "b": "789"
    }) == FormData([("a", "123"), ("b", "789")])
    assert FormData({"a": "123", "b": "789"}) != {"a": "123", "b": "789"}
Beispiel #2
0
 async def resolve_data(cls, request: Request, data: FormData):
     ret = {}
     m2m_ret = {}
     for field in cls.get_fields(is_display=False):
         input_ = field.input
         if input_.context.get("disabled") or isinstance(
                 input_, inputs.DisplayOnly):
             continue
         name = input_.context.get("name")
         if isinstance(input_, inputs.ManyToMany):
             v = data.getlist(name)
             value = await input_.parse_value(request, v)
             m2m_ret[name] = await input_.model.filter(pk__in=value)
         else:
             v = data.get(name)
             value = await input_.parse_value(request, v)
             ret[name] = value
     return ret, m2m_ret