Skip to content

Commit

Permalink
added __str__ for GroupView
Browse files Browse the repository at this point in the history
  • Loading branch information
mdorier committed Jun 10, 2024
1 parent 7d7e95c commit d577b92
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
28 changes: 28 additions & 0 deletions python/mochi/flock/test_view.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import json
from mochi.flock.view import GroupView


Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions python/src/py-flock-common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>(gv);
})
;
}

0 comments on commit d577b92

Please sign in to comment.