Beispiel #1
0
    def test_set_other_yaml_navigator(self):
        a = YamlDict()
        b = YamlDict()

        a.b = b

        self.assertFalse(isinstance(a._raw["b"], YamlNavigator))
Beispiel #2
0
    def test_write_with_property(self):
        p = YamlDict(content={"x": "original"})

        p.x = "new"

        self.assertEqual("new", p.x)
        self.assertEqual("new", p["x"])
Beispiel #3
0
    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)
Beispiel #4
0
    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))
Beispiel #5
0
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
Beispiel #6
0
    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)
Beispiel #7
0
    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)
Beispiel #8
0
    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)
Beispiel #9
0
    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)
Beispiel #10
0
    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,
        )
Beispiel #11
0
    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)
Beispiel #12
0
    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
Beispiel #13
0
    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,
        )
Beispiel #14
0
    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)
Beispiel #15
0
    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)
Beispiel #16
0
    def test_set_attribute(self):
        p = YamlDict()

        p.x.y.z = "test"
        self.assertEqual("test", p.x.y.z)
Beispiel #17
0
    def test_set_item(self):
        p = YamlDict()

        p["x"]["y"]["z"] = "test"
        self.assertEqual("test", p["x"]["y"]["z"])
Beispiel #18
0
 def test_len(self):
     d = YamlDict(content={"x": 1, "y": 2, "z": 3})
     self.assertEqual(3, len(d))
Beispiel #19
0
    def test_simple_property_read(self):
        p = YamlDict(content={"x": 3})

        self.assertEqual(3, p.x)
Beispiel #20
0
    def test_yaml_gets_pickle_serialized(self):
        a = YamlDict(content={"x": 3})

        pickle.dumps(a)
Beispiel #21
0
    def test_read_via_get(self):
        p = YamlDict(content={"items": [1, 2, 3]})

        self.assertEqual([1, 2, 3], p["items"]._raw)
Beispiel #22
0
    def test_yaml_gets_serialized(self):
        a = YamlDict(content={"x": 3})

        data = yaml.safe_dump(a)
        print(data)
Beispiel #23
0
    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)
Beispiel #24
0
    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)
Beispiel #25
0
    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)
Beispiel #26
0
    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)