Skip to content

Commit

Permalink
Add __repr__ to Request and Respond
Browse files Browse the repository at this point in the history
Simply returns to_dict().__repr__() - not ideal as some information is
lost (like the response_queue) etc. Could be improved.
  • Loading branch information
c-mita committed Jun 10, 2016
1 parent 7efc47f commit 486ba15
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
4 changes: 1 addition & 3 deletions malcolm/core/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def recv_loop(self):
try:
self._handle_functions[request.type_](request)
except Exception:
rep = request.to_dict() if hasattr(request, "to_dict") \
else request
self.log_exception("Exception while handling %s", rep)
self.log_exception("Exception while handling %s", request)

def start(self):
"""Start the process going"""
Expand Down
3 changes: 3 additions & 0 deletions malcolm/core/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,6 @@ def from_dict(cls, d):
for field in [f for f in d.keys() if f not in ["id", "type"]]:
request.fields[field] = d[field]
return request

def __repr__(self):
return self.to_dict().__repr__()
3 changes: 3 additions & 0 deletions malcolm/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def to_dict(self):
def __getattr__(self, attr):
return self.fields[attr]

def __repr__(self):
return self.to_dict().__repr__()

@classmethod
def Return(cls, id_, context, value=None):
"""Create a Return Response object with the provided parameters.
Expand Down
3 changes: 1 addition & 2 deletions tests/test_core/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,10 @@ def test_error(self):
p.start()
request = MagicMock()
request.endpoint = ["anything"]
request.to_dict.return_value = "<to_dict>"
p.q.put(request)
p.stop()
p.log_exception.assert_called_once_with("Exception while handling %s",
"<to_dict>")
request)

def test_spawned_adds_to_other_spawned(self):
s = MagicMock()
Expand Down
6 changes: 6 additions & 0 deletions tests/test_core/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def test_init(self):
self.assertEqual(self.response_queue, self.request.response_queue)
self.assertEqual("Put", self.request.type_)

def test_repr(self):
r = Request(MagicMock(), MagicMock(), "mytype")
s = r.__repr__()
self.assertTrue(isinstance(s, str))
self.assertIn("mytype", s)

def test_to_dict(self):
expected_dict = OrderedDict()
expected_dict['id'] = 1
Expand Down
7 changes: 7 additions & 0 deletions tests/test_core/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,12 @@ def test_from_dict(self):
self.assertEquals({"field": "data"}, response.fields["extra_2"])
self.assertIsNone(response.context)

def test_repr(self):
r = Response(123, Mock(), "mytype")
s = r.__repr__()
self.assertTrue(isinstance(s, str))
self.assertIn("mytype", s)
self.assertIn("123", s)

if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 486ba15

Please sign in to comment.