Skip to content

Commit

Permalink
add test_mongo_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Nov 13, 2024
1 parent ecf19a5 commit bad6938
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/monty/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ class MongoDict:
a nested dict interactively (e.g. documents extracted from a MongoDB
database).
>>> m = MongoDict({'a': {'b': 1}, 'x': 2})
>>> assert m.a.b == 1 and m.x == 2
>>> assert "a" in m and "b" in m.a
>>> m["a"]
{'b': 1}
>>> m_dct = MongoDict({"a": {"b": 1}, "x": 2})
>>> assert m_dct.a.b == 1 and m_dct.x == 2
>>> assert "a" in m_dct and "b" in m_dct.a
>>> m_dct["a"]
{"b": 1}
Notes:
Cannot inherit from ABC collections.Mapping because otherwise
Expand Down
11 changes: 9 additions & 2 deletions tests/test_collections.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from __future__ import annotations

import os
from collections import namedtuple

import pytest

from monty.collections import (
AttrDict,
FrozenAttrDict,
MongoDict,
Namespace,
dict2namedtuple,
frozendict,
Expand Down Expand Up @@ -54,6 +54,8 @@ def test_attr_dict():
dct = AttrDict(foo=1, bar=2)
assert dct.bar == 2
assert dct["foo"] is dct.foo

# Test accessing values
dct.bar = "hello"
assert dct["bar"] == "hello"

Expand All @@ -77,7 +79,12 @@ def test_frozen_attrdict():


def test_mongo_dict():
"""TODO: add test"""
m_dct = MongoDict({"a": {"b": 1}, "x": 2})
assert m_dct.a.b == 1
assert m_dct.x == 2
assert "a" in m_dct
assert "b" in m_dct.a
assert m_dct["a"] == {"b": 1}


def test_dict2namedtuple():
Expand Down

0 comments on commit bad6938

Please sign in to comment.