|
9 | 9 | import pytest |
10 | 10 |
|
11 | 11 | from openeo.util import first_not_none, get_temporal_extent, TimingLogger, ensure_list, ensure_dir, dict_no_none, \ |
12 | | - deep_get, DeepKeyError, get_user_config_dir, get_user_data_dir, Rfc3339, rfc3339 |
| 12 | + deep_get, DeepKeyError, get_user_config_dir, get_user_data_dir, Rfc3339, rfc3339, deep_set |
13 | 13 |
|
14 | 14 |
|
15 | 15 | def test_rfc3339_date(): |
@@ -356,6 +356,24 @@ def test_deep_get_mixed(): |
356 | 356 | deep_get(d, "bar", 2, 22, 222) |
357 | 357 |
|
358 | 358 |
|
| 359 | +@pytest.mark.parametrize(["init", "keys", "expected"], [ |
| 360 | + ({}, ("foo",), {"foo": 42}), |
| 361 | + ({}, ("foo", "bar", "baz"), {"foo": {"bar": {"baz": 42}}}), |
| 362 | + ({"foo": {"x": "y"}}, ("foo", "bar"), {"foo": {"x": "y", "bar": 42}}), |
| 363 | + ({"foo": {"x": "y"}}, ("foo", "bar", "baz"), {"foo": {"x": "y", "bar": {"baz": 42}}}), |
| 364 | + ({"foo": [1, 2, 3]}, ("foo", 1), {"foo": [1, 42, 3]}), |
| 365 | + ({"foo": {1: "a", 2: "b"}}, ("foo", 1), {"foo": {1: 42, 2: "b"}}), |
| 366 | + ({"foo": [{"x": 1}, {"x": 2}, {"x": 3}]}, ("foo", 1, "x"), {"foo": [{"x": 1}, {"x": 42}, {"x": 3}]}), |
| 367 | + ({"foo": ({"x": 1}, {"x": 2}, {"x": 3})}, ("foo", 1, "x"), {"foo": ({"x": 1}, {"x": 42}, {"x": 3})}), |
| 368 | + ({"foo": [{"x": {}}, {"x": {}}]}, ("foo", 1, "x", "bar"), {"foo": [{"x": {}}, {"x": {"bar": 42}}]}), |
| 369 | + ({"foo": [{"x": {}}, {"x": {}}]}, ("foo", 1, "x", "y", "z"), {"foo": [{"x": {}}, {"x": {"y": {"z": 42}}}]}), |
| 370 | +]) |
| 371 | +def test_deep_set_dict(init, keys, expected): |
| 372 | + d = init |
| 373 | + deep_set(d, *keys, value=42) |
| 374 | + assert d == expected |
| 375 | + |
| 376 | + |
359 | 377 | def test_get_user_config_dir(): |
360 | 378 | assert get_user_config_dir() == pathlib.Path(__file__).parent / "data/user_dirs/config/openeo-python-client" |
361 | 379 |
|
|
0 commit comments