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