main
 1from typing import Any, cast
 2from functools import partial
 3from urllib.parse import unquote
 4
 5import pytest
 6
 7from openai._qs import Querystring, stringify
 8
 9
10def test_empty() -> None:
11    assert stringify({}) == ""
12    assert stringify({"a": {}}) == ""
13    assert stringify({"a": {"b": {"c": {}}}}) == ""
14
15
16def test_basic() -> None:
17    assert stringify({"a": 1}) == "a=1"
18    assert stringify({"a": "b"}) == "a=b"
19    assert stringify({"a": True}) == "a=true"
20    assert stringify({"a": False}) == "a=false"
21    assert stringify({"a": 1.23456}) == "a=1.23456"
22    assert stringify({"a": None}) == ""
23
24
25@pytest.mark.parametrize("method", ["class", "function"])
26def test_nested_dotted(method: str) -> None:
27    if method == "class":
28        serialise = Querystring(nested_format="dots").stringify
29    else:
30        serialise = partial(stringify, nested_format="dots")
31
32    assert unquote(serialise({"a": {"b": "c"}})) == "a.b=c"
33    assert unquote(serialise({"a": {"b": "c", "d": "e", "f": "g"}})) == "a.b=c&a.d=e&a.f=g"
34    assert unquote(serialise({"a": {"b": {"c": {"d": "e"}}}})) == "a.b.c.d=e"
35    assert unquote(serialise({"a": {"b": True}})) == "a.b=true"
36
37
38def test_nested_brackets() -> None:
39    assert unquote(stringify({"a": {"b": "c"}})) == "a[b]=c"
40    assert unquote(stringify({"a": {"b": "c", "d": "e", "f": "g"}})) == "a[b]=c&a[d]=e&a[f]=g"
41    assert unquote(stringify({"a": {"b": {"c": {"d": "e"}}}})) == "a[b][c][d]=e"
42    assert unquote(stringify({"a": {"b": True}})) == "a[b]=true"
43
44
45@pytest.mark.parametrize("method", ["class", "function"])
46def test_array_comma(method: str) -> None:
47    if method == "class":
48        serialise = Querystring(array_format="comma").stringify
49    else:
50        serialise = partial(stringify, array_format="comma")
51
52    assert unquote(serialise({"in": ["foo", "bar"]})) == "in=foo,bar"
53    assert unquote(serialise({"a": {"b": [True, False]}})) == "a[b]=true,false"
54    assert unquote(serialise({"a": {"b": [True, False, None, True]}})) == "a[b]=true,false,true"
55
56
57def test_array_repeat() -> None:
58    assert unquote(stringify({"in": ["foo", "bar"]})) == "in=foo&in=bar"
59    assert unquote(stringify({"a": {"b": [True, False]}})) == "a[b]=true&a[b]=false"
60    assert unquote(stringify({"a": {"b": [True, False, None, True]}})) == "a[b]=true&a[b]=false&a[b]=true"
61    assert unquote(stringify({"in": ["foo", {"b": {"c": ["d", "e"]}}]})) == "in=foo&in[b][c]=d&in[b][c]=e"
62
63
64@pytest.mark.parametrize("method", ["class", "function"])
65def test_array_brackets(method: str) -> None:
66    if method == "class":
67        serialise = Querystring(array_format="brackets").stringify
68    else:
69        serialise = partial(stringify, array_format="brackets")
70
71    assert unquote(serialise({"in": ["foo", "bar"]})) == "in[]=foo&in[]=bar"
72    assert unquote(serialise({"a": {"b": [True, False]}})) == "a[b][]=true&a[b][]=false"
73    assert unquote(serialise({"a": {"b": [True, False, None, True]}})) == "a[b][]=true&a[b][]=false&a[b][]=true"
74
75
76def test_unknown_array_format() -> None:
77    with pytest.raises(NotImplementedError, match="Unknown array_format value: foo, choose from comma, repeat"):
78        stringify({"a": ["foo", "bar"]}, array_format=cast(Any, "foo"))