2Tests for `funkycache.key`.
5from dataclasses import dataclass
6from pathlib import Path
9from pydantic import BaseModel
11from _pytest.mark.structures import ParameterSet
13from funkycache.key import encode_key_as_json
16class Shape(BaseModel):
17 """A basic model that uses Pydantic."""
25 """A basic model that uses dataclasses."""
31KEY_ENCODING_TEST_CASES: list[ParameterSet] = [
32 pytest.param({"n": "one"}, '{"n":"one"}', id="str"),
33 pytest.param({"n": 1}, '{"n":1}', id="int"),
35 {"n": ["one", "uno", "eins"]}, '{"n":["one","uno","eins"]}', id="list"
38 {"n": ("one", "uno", "eins")}, '{"n":["one","uno","eins"]}', id="tuple"
40 pytest.param({"n": []}, '{"n":[]}', id="empty_list"),
41 pytest.param({"n": [{"en": "one"}]}, '{"n":[{"en":"one"}]}', id="list_1"),
45 {"lang": "en", "val": "one"},
46 {"lang": "fr", "val": "un"},
47 {"lang": "de", "val": "eins"},
50 '{"n":{"_keys":["lang","val"],"_values":[["en","one"],["fr","un"],["de","eins"]]}}',
54 {"n": Shape(sides=4, colour="red")},
55 '{"n":{"colour":"red","sides":4}}',
59 {"n": Greeting(name="alex", message="hello")},
60 '{"n":{"message":"hello","name":"alex"}}',
64 {"long": "a" * 5000, "short": "a"},
65 '{"long":"xxh64:b2b67943d4803cd6","short":"a"}',
69 {"n": {"long": "a" * 5000, "short": "a"}},
70 '{"n":{"long":"xxh64:b2b67943d4803cd6","short":"a"}}',
73 pytest.param({"n": {Path("1"): "one"}}, '{"n":{"1":"one"}}', id="nested_path_key"),
77@pytest.mark.parametrize(
79 KEY_ENCODING_TEST_CASES,
81def test_encode_key_as_json(key: dict[str, Any], json_key: str) -> None:
83 Tests for encode_key_as_json.
85 assert encode_key_as_json(key) == json_key