From e043c6e1f723adfa9e09fd5ee5ef09cb96f26b35 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Tue, 23 Apr 2024 17:11:16 +0100 Subject: [PATCH] Implement keys() and items() for AttrDict --- elasticsearch_dsl/utils.py | 6 ++++++ tests/test_integration/_async/test_document.py | 2 +- tests/test_integration/_sync/test_document.py | 2 +- tests/test_utils.py | 6 ++++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/elasticsearch_dsl/utils.py b/elasticsearch_dsl/utils.py index 1a8e8973..51dbe27d 100644 --- a/elasticsearch_dsl/utils.py +++ b/elasticsearch_dsl/utils.py @@ -187,6 +187,12 @@ def __iter__(self): def to_dict(self): return self._d_ + def keys(self): + return self._d_.keys() + + def items(self): + return self._d_.items() + class DslMeta(type): """ diff --git a/tests/test_integration/_async/test_document.py b/tests/test_integration/_async/test_document.py index 008f71dc..e18b9467 100644 --- a/tests/test_integration/_async/test_document.py +++ b/tests/test_integration/_async/test_document.py @@ -219,7 +219,7 @@ async def test_update_object_field(async_write_client): ) await w.save() - assert "updated" == await w.update(owner=[{"name": "Honza"}, {"name": "Nick"}]) + assert "updated" == await w.update(owner=[{"name": "Honza"}, User(name="Nick")]) assert w.owner[0].name == "Honza" assert w.owner[1].name == "Nick" diff --git a/tests/test_integration/_sync/test_document.py b/tests/test_integration/_sync/test_document.py index ea508b95..16f39ad9 100644 --- a/tests/test_integration/_sync/test_document.py +++ b/tests/test_integration/_sync/test_document.py @@ -219,7 +219,7 @@ def test_update_object_field(write_client): ) w.save() - assert "updated" == w.update(owner=[{"name": "Honza"}, {"name": "Nick"}]) + assert "updated" == w.update(owner=[{"name": "Honza"}, User(name="Nick")]) assert w.owner[0].name == "Honza" assert w.owner[1].name == "Nick" diff --git a/tests/test_utils.py b/tests/test_utils.py index 615b4c24..c35c75f0 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -44,6 +44,12 @@ class MyAttrDict(utils.AttrDict): assert isinstance(l[:][0], MyAttrDict) +def test_attrdict_keys_items(): + a = utils.AttrDict({"a": {"b": 42, "c": 47}, "d": "e"}) + assert list(a.keys()) == ["a", "d"] + assert list(a.items()) == [("a", {"b": 42, "c": 47}), ("d", "e")] + + def test_merge(): a = utils.AttrDict({"a": {"b": 42, "c": 47}}) b = {"a": {"b": 123, "d": -12}, "e": [1, 2, 3]}