| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 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 import json | 6 import json |
| 7 import os | 7 import os |
| 8 import sys | 8 import sys |
| 9 import unittest | 9 import unittest |
| 10 | 10 |
| 11 sys.path.insert(0, os.path.join( | 11 sys.path.insert(0, os.path.join( |
| 12 os.path.dirname(__file__), '..', '..', '..', '..')) | 12 os.path.dirname(__file__), '..', '..', '..', '..')) |
| 13 import common.env | 13 import common.env |
| 14 common.env.Install() | 14 common.env.Install() |
| 15 | 15 |
| 16 import mock | 16 import mock |
| 17 import requests | 17 import requests |
| 18 | 18 |
| 19 import crrev_client # pylint: disable=relative-import | 19 import crrev_client # pylint: disable=relative-import |
| 20 | 20 |
| 21 | 21 |
| 22 @mock.patch('requests.get') | 22 @mock.patch('requests.get') |
| 23 class CrrevClientTest(unittest.TestCase): | 23 class CrrevClientTest(unittest.TestCase): |
| 24 | 24 |
| 25 def test_main(self, mock_get): |
| 26 mock_response = mock.MagicMock() |
| 27 response_data = { |
| 28 'git_sha': 'e91f8875e590ddf00af267062fc1a9ec48658373', |
| 29 'numbering_type': 'COMMIT_POSITION' |
| 30 } |
| 31 mock_response.text = json.dumps(response_data) |
| 32 mock_get.return_value = mock_response |
| 33 output = crrev_client.main([ |
| 34 'get_numbering', |
| 35 '--params-file=test_params_file.json', |
| 36 ]) |
| 37 self.assertEqual(json.dumps(response_data, indent=2), output) |
| 38 self.assertEqual(mock_get.call_count, 1) |
| 39 |
| 25 def test_simple_get(self, mock_get): | 40 def test_simple_get(self, mock_get): |
| 26 mock_response = mock.MagicMock() | 41 mock_response = mock.MagicMock() |
| 27 response_data = { | 42 response_data = { |
| 28 'git_sha': 'e91f8875e590ddf00af267062fc1a9ec48658373', | 43 'git_sha': 'e91f8875e590ddf00af267062fc1a9ec48658373', |
| 29 'numbering_type': 'COMMIT_POSITION' | 44 'numbering_type': 'COMMIT_POSITION' |
| 30 } | 45 } |
| 31 mock_response.text = json.dumps(response_data) | 46 mock_response.text = json.dumps(response_data) |
| 32 mock_get.return_value = mock_response | 47 mock_get.return_value = mock_response |
| 33 params = [ | 48 params = [ |
| 34 ('number', '375953'), | 49 ('number', '375953'), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 63 def test_retry(self, mock_get): | 78 def test_retry(self, mock_get): |
| 64 mock_get.side_effect = requests.RequestException | 79 mock_get.side_effect = requests.RequestException |
| 65 with self.assertRaises(requests.RequestException): | 80 with self.assertRaises(requests.RequestException): |
| 66 crrev_client.crrev_get('redirect/123456', params={}, attempts=3) | 81 crrev_client.crrev_get('redirect/123456', params={}, attempts=3) |
| 67 self.assertEqual(3, mock_get.call_count) | 82 self.assertEqual(3, mock_get.call_count) |
| 68 | 83 |
| 69 | 84 |
| 70 if __name__ == '__main__': | 85 if __name__ == '__main__': |
| 71 unittest.main() | 86 unittest.main() |
| 72 | 87 |
| OLD | NEW |