diff --git a/python/mochi/flock/test_view.py b/python/mochi/flock/test_view.py index c9e0c16..07af987 100644 --- a/python/mochi/flock/test_view.py +++ b/python/mochi/flock/test_view.py @@ -1,4 +1,5 @@ import unittest +import json from mochi.flock.view import GroupView @@ -61,6 +62,33 @@ def test_metadata(self): self.assertEqual(view.metadata.count, 3) self.assertIsNone(view.metadata["key2"]) + def test_str(self): + view = GroupView() + # add 5 metadata + for i in range(0, 5): + view.metadata.add( + key=f"key{i}", + value=f"value{i}") + view = GroupView() + # add 5 members + for i in range(0, 5): + view.members.add( + address=f"address{i}", + provider_id=i) + v = json.loads(str(view)) + self.assertIn("members", v) + self.assertIn("metadata", v) + self.assertIsInstance(v["members"], list) + self.assertIsInstance(v["metadata"], dict) + for i, member in enumerate(v["members"]): + self.assertEqual(member["address"], f"address{i}") + self.assertEqual(member["provider_id"], i) + i = 0 + for key, val in v["metadata"]: + self.assertEqual(key, f"key{i}") + self.assertEqual(val, f"value{i}") + i = i + 1 + def test_digest(self): view = GroupView() self.assertEqual(view.digest, 0) diff --git a/python/src/py-flock-common.cpp b/python/src/py-flock-common.cpp index 89d09ab..8d1b93d 100644 --- a/python/src/py-flock-common.cpp +++ b/python/src/py-flock-common.cpp @@ -75,5 +75,8 @@ PYBIND11_MODULE(pyflock_common, m) { .def("unlock", &flock::GroupView::unlock) .def_property_readonly("members", &flock::GroupView::members, py11::keep_alive<0, 1>()) .def_property_readonly("metadata", &flock::GroupView::metadata, py11::keep_alive<0, 1>()) + .def("__str__", [](const flock::GroupView& gv) { + return static_cast(gv); + }) ; }