]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr: add test cases for ObjectFormatAdapter
authorJohn Mulligan <jmulligan@redhat.com>
Sat, 9 Apr 2022 18:29:50 +0000 (14:29 -0400)
committerAdam King <adking@redhat.com>
Sat, 21 May 2022 23:20:50 +0000 (19:20 -0400)
Signed-off-by: John Mulligan <jmulligan@redhat.com>
(cherry picked from commit f2e10b7f504a6aa63a72422320d38d2b6334295b)

src/pybind/mgr/tests/test_object_format.py [new file with mode: 0644]

diff --git a/src/pybind/mgr/tests/test_object_format.py b/src/pybind/mgr/tests/test_object_format.py
new file mode 100644 (file)
index 0000000..1d551d4
--- /dev/null
@@ -0,0 +1,105 @@
+from typing import Any, Dict, Type, TypeVar
+
+import pytest
+
+import object_format
+
+
+T = TypeVar("T", bound="Parent")
+
+
+class Simpler:
+    def __init__(self, name, val=None):
+        self.name = name
+        self.val = val or {}
+        self.version = 1
+
+    def to_simplified(self) -> Dict[str, Any]:
+        return {
+            "version": self.version,
+            "name": self.name,
+            "value": self.val,
+        }
+
+
+class JSONer(Simpler):
+    def to_json(self) -> Dict[str, Any]:
+        d = self.to_simplified()
+        d["_json"] = True
+        return d
+
+    @classmethod
+    def from_json(cls: Type[T], data) -> T:
+        o = cls(data.get("name", ""), data.get("value"))
+        o.version = data.get("version", 1) + 1
+        return o
+
+
+class YAMLer(Simpler):
+    def to_yaml(self) -> Dict[str, Any]:
+        d = self.to_simplified()
+        d["_yaml"] = True
+        return d
+
+
+@pytest.mark.parametrize(
+    "obj, compatible, json_val",
+    [
+        ({}, False, "{}"),
+        ({"name": "foobar"}, False, '{"name": "foobar"}'),
+        ([1, 2, 3], False, "[1, 2, 3]"),
+        (JSONer("bob"), False, '{"name": "bob", "value": {}, "version": 1}'),
+        (
+            JSONer("betty", 77),
+            False,
+            '{"name": "betty", "value": 77, "version": 1}',
+        ),
+        ({}, True, "{}"),
+        ({"name": "foobar"}, True, '{"name": "foobar"}'),
+        (
+            JSONer("bob"),
+            True,
+            '{"_json": true, "name": "bob", "value": {}, "version": 1}',
+        ),
+    ],
+)
+def test_format_json(obj: Any, compatible: bool, json_val: str):
+    assert (
+        object_format.ObjectFormatAdapter(
+            obj, compatible=compatible, json_indent=None
+        ).format_json()
+        == json_val
+    )
+
+
+@pytest.mark.parametrize(
+    "obj, compatible, yaml_val",
+    [
+        ({}, False, "{}\n"),
+        ({"name": "foobar"}, False, "name: foobar\n"),
+        (
+            {"stuff": [1, 88, 909, 32]},
+            False,
+            "stuff:\n- 1\n- 88\n- 909\n- 32\n",
+        ),
+        (
+            JSONer("zebulon", "999"),
+            False,
+            "name: zebulon\nvalue: '999'\nversion: 1\n",
+        ),
+        ({}, True, "{}\n"),
+        ({"name": "foobar"}, True, "name: foobar\n"),
+        (
+            YAMLer("thingy", "404"),
+            True,
+            "_yaml: true\nname: thingy\nvalue: '404'\nversion: 1\n",
+        ),
+    ],
+)
+def test_format_yaml(obj: Any, compatible: bool, yaml_val: str):
+    assert (
+        object_format.ObjectFormatAdapter(
+            obj, compatible=compatible
+        ).format_yaml()
+        == yaml_val
+    )