From 461642a529be32573abcf191c8428ddb0363adbf Mon Sep 17 00:00:00 2001 From: profitroll Date: Sun, 6 Aug 2023 12:51:32 +0200 Subject: [PATCH] Added nested_set tests --- tests/test_nested_set.py | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/test_nested_set.py diff --git a/tests/test_nested_set.py b/tests/test_nested_set.py new file mode 100644 index 0000000..3cffffa --- /dev/null +++ b/tests/test_nested_set.py @@ -0,0 +1,63 @@ +from typing import Any, List + +import pytest + +from libbot import sync + + +@pytest.mark.parametrize( + "target, value, path, create_missing, expected", + [ + ({"foo": "bar"}, "rab", ["foo"], True, {"foo": "rab"}), + ({"foo": "bar"}, {"123": 456}, ["foo"], True, {"foo": {"123": 456}}), + ( + {"foo": {"bar": {}}}, + True, + ["foo", "bar", "test"], + True, + {"foo": {"bar": {"test": True}}}, + ), + ( + {"foo": {"bar": {}}}, + True, + ["foo", "bar", "test"], + False, + {"foo": {"bar": {}}}, + ), + ], +) +def test_nested_set_valid( + target: dict[str, Any], + value: Any, + path: List[str], + create_missing: bool, + expected: Any, +): + assert ( + sync.nested_set(target, value, *path, create_missing=create_missing) + ) == expected + + +@pytest.mark.parametrize( + "target, value, path, create_missing, expected", + [ + ( + {"foo": {"bar": {}}}, + True, + ["foo", "bar", "test1", "test2"], + False, + KeyError, + ), + ], +) +def test_nested_set_invalid( + target: dict[str, Any], + value: Any, + path: List[str], + create_missing: bool, + expected: Any, +): + with pytest.raises(expected): + assert ( + sync.nested_set(target, value, *path, create_missing=create_missing) + ) == expected