Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Unit tests for rietveld.py.""" | 6 """Unit tests for rietveld.py.""" |
| 7 | 7 |
| 8 import logging | 8 import logging |
| 9 import os | 9 import os |
| 10 import sys | 10 import sys |
| 11 import traceback | |
| 11 import unittest | 12 import unittest |
| 12 | 13 |
| 13 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | 14 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 14 | 15 |
| 15 from testing_support.patches_data import GIT, RAW | 16 from testing_support.patches_data import GIT, RAW |
| 16 | 17 |
| 18 from testing_support import auto_stub | |
|
Paweł Hajdan Jr.
2014/02/21 00:49:30
nit: Please put it in one "block" with "from testi
Sergey Berezin
2014/02/21 01:30:56
Done.
| |
| 17 import patch | 19 import patch |
| 18 import rietveld | 20 import rietveld |
| 19 | 21 |
| 20 | 22 |
| 21 def _api(files): | 23 def _api(files): |
| 22 """Mock a rietveld api request.""" | 24 """Mock a rietveld api request.""" |
| 23 return rietveld.json.dumps({'files': files}) | 25 return rietveld.json.dumps({'files': files}) |
| 24 | 26 |
| 25 | 27 |
| 26 def _file( | 28 def _file( |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 414 | 416 |
| 415 def test_get_patchset_properties(self): | 417 def test_get_patchset_properties(self): |
| 416 self.requests = [ | 418 self.requests = [ |
| 417 ('/api/1/2', '{}'), | 419 ('/api/1/2', '{}'), |
| 418 ] | 420 ] |
| 419 expected = {} | 421 expected = {} |
| 420 self.assertEqual(expected, self.rietveld.get_patchset_properties(1, 2)) | 422 self.assertEqual(expected, self.rietveld.get_patchset_properties(1, 2)) |
| 421 self.assertEqual(expected, self.rietveld.get_patchset_properties(1, 2)) | 423 self.assertEqual(expected, self.rietveld.get_patchset_properties(1, 2)) |
| 422 | 424 |
| 423 | 425 |
| 426 class ProbeException(Exception): | |
| 427 """Deep-probe a value.""" | |
| 428 value = None | |
| 429 | |
| 430 def __init__(self, value): | |
| 431 super(ProbeException, self).__init__() | |
| 432 self.value = value | |
| 433 | |
| 434 | |
| 435 def MockSend(request_path, payload=None, | |
| 436 content_type="application/octet-stream", | |
| 437 timeout=None, | |
| 438 extra_headers=None, | |
| 439 **kwargs): | |
| 440 """Mock upload.py's Send() to probe the timeout value""" | |
| 441 raise ProbeException(timeout) | |
| 442 | |
| 443 | |
| 444 class DefaultTimeoutTest(auto_stub.TestCase): | |
| 445 TESTED_CLASS = rietveld.Rietveld | |
| 446 | |
| 447 def setUp(self): | |
| 448 super(DefaultTimeoutTest, self).setUp() | |
| 449 self.rietveld = self.TESTED_CLASS('url', 'email', 'password') | |
| 450 self.mock(self.rietveld.rpc_server, 'Send', MockSend) | |
| 451 | |
| 452 def test_timeout_get(self): | |
| 453 try: | |
| 454 self.rietveld.get('/api/1234') | |
| 455 except ProbeException as e: | |
|
Paweł Hajdan Jr.
2014/02/21 00:49:30
Use assertRaises instead. This also allows you to
Sergey Berezin
2014/02/21 01:30:56
Done.
| |
| 456 self.assertIsNotNone(e.value, 'Rietveld timeout was not set: %s' | |
| 457 % traceback.format_exc()) | |
| 458 return | |
| 459 self.assertTrue(False, 'This unittest has a bug, we should not get here.') | |
| 460 | |
| 461 def test_timeout_post(self): | |
| 462 try: | |
| 463 self.rietveld.post('/api/1234', [('key', 'data')]) | |
| 464 except ProbeException as e: | |
| 465 self.assertIsNotNone(e.value, 'Rietveld timeout was not set: %s' | |
| 466 % traceback.format_exc()) | |
| 467 return | |
| 468 self.assertTrue(False, 'This unittest has a bug, we should not get here.') | |
| 469 | |
| 424 | 470 |
| 425 if __name__ == '__main__': | 471 if __name__ == '__main__': |
| 426 logging.basicConfig(level=[ | 472 logging.basicConfig(level=[ |
| 427 logging.ERROR, logging.INFO, logging.DEBUG][min(2, sys.argv.count('-v'))]) | 473 logging.ERROR, logging.INFO, logging.DEBUG][min(2, sys.argv.count('-v'))]) |
| 428 unittest.main() | 474 unittest.main() |
| OLD | NEW |