From: John Mulligan Date: Sat, 9 Apr 2022 18:29:50 +0000 (-0400) Subject: pybind/mgr: add test cases for ObjectFormatAdapter X-Git-Tag: v17.2.1~48^2~14 X-Git-Url: http://git.apps.os.sepia.ceph.com/?a=commitdiff_plain;h=59962229226e222c320a0543243d95bcb9eb4037;p=ceph.git pybind/mgr: add test cases for ObjectFormatAdapter Signed-off-by: John Mulligan (cherry picked from commit f2e10b7f504a6aa63a72422320d38d2b6334295b) --- diff --git a/src/pybind/mgr/tests/test_object_format.py b/src/pybind/mgr/tests/test_object_format.py new file mode 100644 index 0000000000000..1d551d4f78652 --- /dev/null +++ b/src/pybind/mgr/tests/test_object_format.py @@ -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 + )