|
| 1 | +#!/usr/bin/python |
| 2 | +# -- Content-Encoding: UTF-8 -- |
| 3 | +""" |
| 4 | +Tests the History class |
| 5 | +
|
| 6 | +:license: Apache License 2.0 |
| 7 | +""" |
| 8 | + |
| 9 | +# JSON-RPC library |
| 10 | +from jsonrpclib.history import History |
| 11 | + |
| 12 | +# Standard library |
| 13 | +try: |
| 14 | + import unittest2 as unittest |
| 15 | +except ImportError: |
| 16 | + import unittest |
| 17 | + |
| 18 | + |
| 19 | +# ------------------------------------------------------------------------------ |
| 20 | + |
| 21 | + |
| 22 | +class HistoryTests(unittest.TestCase): |
| 23 | + """ |
| 24 | + Tests the methods of the History class |
| 25 | + """ |
| 26 | + |
| 27 | + def test_basic(self): |
| 28 | + """ |
| 29 | + Tests basic access to history |
| 30 | + """ |
| 31 | + history = History() |
| 32 | + |
| 33 | + # Empty history |
| 34 | + self.assertIsNone(history.request) |
| 35 | + self.assertIsNone(history.response) |
| 36 | + self.assertListEqual(history.requests, []) |
| 37 | + self.assertListEqual(history.responses, []) |
| 38 | + |
| 39 | + # First value |
| 40 | + req1 = object() |
| 41 | + res1 = object() |
| 42 | + history.add_request(req1) |
| 43 | + history.add_response(res1) |
| 44 | + |
| 45 | + self.assertListEqual(history.requests, [req1]) |
| 46 | + self.assertListEqual(history.responses, [res1]) |
| 47 | + self.assertIs(history.request, req1) |
| 48 | + self.assertIs(history.response, res1) |
| 49 | + |
| 50 | + # Second value |
| 51 | + req2 = object() |
| 52 | + res2 = object() |
| 53 | + history.add_request(req2) |
| 54 | + history.add_response(res2) |
| 55 | + |
| 56 | + self.assertListEqual(history.requests, [req1, req2]) |
| 57 | + self.assertListEqual(history.responses, [res1, res2]) |
| 58 | + self.assertIs(history.request, req2) |
| 59 | + self.assertIs(history.response, res2) |
| 60 | + |
| 61 | + def test_clear(self): |
| 62 | + """ |
| 63 | + Ensures that the clear() method doesn't create new history lists |
| 64 | + """ |
| 65 | + # Fill some history |
| 66 | + history = History() |
| 67 | + history.add_request(1) |
| 68 | + history.add_response(1) |
| 69 | + |
| 70 | + # Keep track of lists |
| 71 | + original_requests = history.requests |
| 72 | + original_responses = history.responses |
| 73 | + |
| 74 | + # Clear |
| 75 | + history.clear() |
| 76 | + |
| 77 | + # Check aftermath |
| 78 | + self.assertListEqual(history.requests, []) |
| 79 | + self.assertListEqual(history.responses, []) |
| 80 | + self.assertIs(history.requests, original_requests) |
| 81 | + self.assertIs(history.responses, original_responses) |
| 82 | + self.assertIsNone(history.request) |
| 83 | + self.assertIsNone(history.response) |
0 commit comments