MCPcopy Create free account
hub / github.com/cdgriffith/Box / TestInternalAttrs

Class TestInternalAttrs

test/test_box.py:1867–2038  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1865
1866
1867class TestInternalAttrs:
1868 def test_not_in_data(self):
1869 c = _make_internal()
1870 assert c.source_path == "/etc/app.yml"
1871 assert c.to_dict() == {"name": "app", "db": {"host": "x"}, "normal": "data"}
1872 assert "source_path" not in c and "_cache" not in c
1873 assert list(c) == ["name", "db", "normal"]
1874 assert list(c.keys()) == ["name", "db", "normal"]
1875 assert len(c) == 3
1876 assert c == {"name": "app", "db": {"host": "x"}, "normal": "data"}
1877 assert c.__dict__["source_path"] == "/etc/app.yml"
1878
1879 def test_exports(self):
1880 c = _make_internal()
1881 assert "source_path" not in c.to_json() and "_cache" not in c.to_json()
1882 assert "source_path" not in c.to_yaml()
1883 assert "source_path" not in c.to_toml()
1884 assert "source_path" not in str(c) and "source_path" not in repr(c)
1885 assert b"source_path" not in c.to_msgpack()
1886 assert _InternalBox.from_json(c.to_json()).to_dict() == c.to_dict()
1887
1888 def test_copy_variants(self):
1889 c = _make_internal()
1890 for name, clone in [
1891 ("copy", copy.copy(c)),
1892 ("deepcopy", copy.deepcopy(c)),
1893 (".copy()", c.copy()),
1894 ("pickle", pickle.loads(pickle.dumps(c))),
1895 ]:
1896 assert type(clone) is _InternalBox, name
1897 assert clone.source_path == "/etc/app.yml", name
1898 assert clone._cache == {"hits": 1}, name
1899 assert clone.to_dict() == c.to_dict(), name
1900 assert copy.copy(c)._cache is c._cache
1901 assert c.copy()._cache is c._cache
1902 assert copy.deepcopy(c)._cache is not c._cache
1903 assert type(pickle.loads(pickle.dumps(c)).db) is _InternalBox
1904
1905 def test_deepcopy_memo(self):
1906 shared = {"x": 1}
1907 c = _InternalBox(a=1)
1908 c._cache = {"one": shared, "two": shared}
1909 d = copy.deepcopy(c)
1910 assert d._cache["one"] is d._cache["two"]
1911 assert d._cache["one"] is not shared
1912 o2 = copy.deepcopy([c, c])
1913 assert o2[0] is o2[1]
1914 assert o2[0]._cache["one"] is o2[0]._cache["two"]
1915
1916 def test_deepcopy_self_reference_in_internal(self):
1917 c = _InternalBox(a=1)
1918 c._cache = {"owner": c}
1919 d = copy.deepcopy(c)
1920 assert d._cache["owner"] is d
1921
1922 def test_copy_does_not_share_assignment(self):
1923 c = _make_internal()
1924 d = c.copy()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected