def test_set_other_yaml_navigator(self): a = YamlDict() b = YamlDict() a.b = b self.assertFalse(isinstance(a._raw["b"], YamlNavigator))
def test_write_with_property(self): p = YamlDict(content={"x": "original"}) p.x = "new" self.assertEqual("new", p.x) self.assertEqual("new", p["x"])
def test_items_iteration_gives_yaml(self): d = YamlDict(content={ "a": { "x": 1 }, }) for k, v in d._items(): self.assertEqual("a", k) self.assertTrue( isinstance(v, YamlDict), "The iterated property over items is not an YamlDict", ) self.assertEqual("YamlDict(.a) {'x': 1}", f"{v}") self.assertEqual(1, d.a.x) self.assertFalse(d.a.y)
def test_set_other_yaml_navigator(self): a = YamlList(property_name="a", content=["a"]) b = YamlDict() a[0] = b self.assertFalse(isinstance(a._raw[0], YamlNavigator))
def convert_type(*, property_name: str, value: Any) -> Any: if isinstance(value, dict): return YamlDict(property_name=property_name, content=value) elif isinstance(value, list): return YamlList(property_name=property_name, content=value) return value
def test_none_navigation(self): d = YamlDict(content={"x": 1, "y": 2, "z": 3}) self.assertTrue(not d.a) self.assertTrue(not d.a.b) self.assertTrue("a" not in d) self.assertTrue("b" not in d.a)
def test_nested_repr(self): p = YamlDict(property_name="a.b", content={"x": [{"y": [1, 2, 3]}]}) representation = f"{p.x[0].y}" self.assertEqual("YamlList(a.b.x.0.y) [1, 2, 3]", representation) representation = f"{p.x[0].z}" self.assertEqual("YamlMissing(a.b.x.0.z)", representation)
def test_deep_copy_really_deep_copies(self): dict = {"x": 1} p = YamlDict(content=dict) p_copy = copy.deepcopy(p) p_copy.x = 2 self.assertEqual(2, p_copy.x) self.assertEqual(1, dict["x"]) self.assertEqual(1, p.x)
def test_nested_property_read(self): p = YamlDict( content={ "x": 3, "y": {"key": 1, "list": [1, {"more": [1, {"nested": ["nested"]}]}, 3]}, } ) self.assertEqual(1, p.y.key) self.assertEqual(["nested"], p.y.list[1].more[1].nested._raw)
def test_iteration_key_value(self): p = YamlDict(content={ "x": "x", "y": "y", "z": "z", }) items = dict() for k, v in p._items(): items[k] = v self.assertDictEqual( { "x": "x", "y": "y", "z": "z", }, items, )
def test_iteration_as_iterable(self): p = YamlDict(content={ "x": "original", "y": "original", "z": "original", }) items = set() for item in p: items.add(item) self.assertSetEqual({"x", "y", "z"}, items)
def __getitem__(self, item): result = self.__content[item] if isinstance(result, dict): return YamlDict(property_name=f"{self.__property_name}.{item}", content=result) if isinstance(result, list): return YamlList(property_name=f"{self.__property_name}.{item}", content=result) return result
def __create_if_missing(self) -> "YamlDict": if self.__parent_property is None: raise Exception(f"No parent_property set on the missing property {self}") parent: YamlDict = self.__parent_property.__create_if_missing() if self.__property_name in parent: return parent[self.__property_name] container: Dict[str, Any] = dict() parent[self.__property_name] = container return YamlDict( property_name=self.__property_name, content=container, )
def test_set_nested_property_navigator(self): """ The `__content` should always be kept as objects pointing to each other, not property navigators. :return: """ p = YamlDict() x = YamlDict() p.x = x p.x.y1 = "y1" x.y2 = "y2" p.y = "y" self.assertEqual({"x": {"y1": "y1", "y2": "y2"}, "y": "y"}, p._raw)
def test_repr_missing_nested(self): d = YamlDict(property_name="a.b", content={"c": 1}) representation = f"{d.x.y}" self.assertEqual("YamlMissing(a.b.x.y)", representation)
def test_set_attribute(self): p = YamlDict() p.x.y.z = "test" self.assertEqual("test", p.x.y.z)
def test_set_item(self): p = YamlDict() p["x"]["y"]["z"] = "test" self.assertEqual("test", p["x"]["y"]["z"])
def test_len(self): d = YamlDict(content={"x": 1, "y": 2, "z": 3}) self.assertEqual(3, len(d))
def test_simple_property_read(self): p = YamlDict(content={"x": 3}) self.assertEqual(3, p.x)
def test_yaml_gets_pickle_serialized(self): a = YamlDict(content={"x": 3}) pickle.dumps(a)
def test_read_via_get(self): p = YamlDict(content={"items": [1, 2, 3]}) self.assertEqual([1, 2, 3], p["items"]._raw)
def test_yaml_gets_serialized(self): a = YamlDict(content={"x": 3}) data = yaml.safe_dump(a) print(data)
def test_removal_attribute(self): d = YamlDict(content={"x": 1, "y": 2, "z": 3}) del d.x self.assertEqual({"y": 2, "z": 3}, d._raw)
def test_nested_property_read(self): p = YamlDict(content={"x": 3, "y": {"key": 1, "list": [1, 2, 3]}}) self.assertEqual(1, p.y.key) self.assertEqual([1, 2, 3], p.y.list._raw)
def test_removal_item(self): d = YamlDict(content={"x": 1, "y": 2, "z": 3}) del d["x"] self.assertEqual({"y": 2, "z": 3}, d._raw)
def test_repr_dict(self): d = YamlDict(property_name="a.b", content={"x": 1}) representation = f"{d}" self.assertEqual("YamlDict(a.b) {'x': 1}", representation)