OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import os | 5 import os |
6 import sys | 6 import sys |
7 import unittest | 7 import unittest |
8 import urllib2 | 8 import urllib2 |
9 | 9 |
10 from query_crbug import CheckIssueClosed | 10 from query_crbug import CheckIssueClosed |
11 | 11 |
12 SRC = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir) | 12 SRC = os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir) |
13 sys.path.append(os.path.join(SRC, 'third_party', 'pymock')) | 13 sys.path.append(os.path.join(SRC, 'third_party', 'pymock')) |
14 | 14 |
15 import mock | 15 import mock |
16 | 16 |
17 _current_directory = os.path.dirname(__file__) | 17 _current_directory = os.path.dirname(__file__) |
18 _test_data_directory = os.path.join(_current_directory, 'test_data') | 18 _test_data_directory = os.path.join(_current_directory, 'test_data') |
19 | 19 |
20 # These strings are simulated responses to various conditions when querying | 20 # These strings are simulated responses to various conditions when querying |
21 # the chromium issue tracker. | 21 # the chromium issue tracker. |
22 CLOSED_ISSUE_DATA = open(os.path.join(_test_data_directory, | 22 CLOSED_ISSUE_DATA = open(os.path.join(_test_data_directory, |
23 'closed.json')).read() | 23 'closed.json')).read() |
24 OPEN_ISSUE_DATA = open(os.path.join(_test_data_directory, | 24 OPEN_ISSUE_DATA = open(os.path.join(_test_data_directory, |
25 'open.json')).read() | 25 'open.json')).read() |
26 UNEXPECTED_FORMAT_DATA = CLOSED_ISSUE_DATA.replace('issues$state', 'gibberish') | 26 UNEXPECTED_FORMAT_DATA = CLOSED_ISSUE_DATA.replace('issues$state', 'gibberish') |
27 BROKEN_ISSUE_DATA = "\n<HTML><HEAD><TITLE>Not a JSON Doc</TITLE></HEAD></HTML>" | 27 BROKEN_ISSUE_DATA = "\n<HTML><HEAD><TITLE>Not a JSON Doc</TITLE></HEAD></HTML>" |
28 | 28 |
29 | 29 |
30 class mockResponse(object): | 30 class MockResponse(object): |
31 def __init__(self, result): | 31 def __init__(self, result): |
32 self._result = result | 32 self._result = result |
33 | 33 |
34 def read(self): | 34 def read(self): |
35 return self._result | 35 return self._result |
36 | 36 |
37 | 37 |
38 def mockUrlOpen(url): | 38 def MockUrlOpen(url): |
39 # Note that these strings DO NOT represent http responses. They are just | 39 # Note that these strings DO NOT represent http responses. They are just |
40 # memorable numeric bug ids to use. | 40 # memorable numeric bug ids to use. |
41 if '200' in url: | 41 if '200' in url: |
42 return mockResponse(CLOSED_ISSUE_DATA) | 42 return MockResponse(CLOSED_ISSUE_DATA) |
43 elif '201' in url: | 43 elif '201' in url: |
44 return mockResponse(OPEN_ISSUE_DATA) | 44 return MockResponse(OPEN_ISSUE_DATA) |
45 elif '300' in url: | 45 elif '300' in url: |
46 return mockResponse(UNEXPECTED_FORMAT_DATA) | 46 return MockResponse(UNEXPECTED_FORMAT_DATA) |
47 elif '403' in url: | 47 elif '403' in url: |
48 raise urllib2.URLError('') | 48 raise urllib2.URLError('') |
49 elif '404' in url: | 49 elif '404' in url: |
50 return mockResponse('') | 50 return MockResponse('') |
51 elif '500' in url: | 51 elif '500' in url: |
52 return mockResponse(BROKEN_ISSUE_DATA) | 52 return MockResponse(BROKEN_ISSUE_DATA) |
53 | 53 |
54 | 54 |
55 class crbugQueryTest(unittest.TestCase): | 55 class crbugQueryTest(unittest.TestCase): |
56 @mock.patch('urllib2.urlopen',mockUrlOpen) | 56 @mock.patch('urllib2.urlopen', MockUrlOpen) |
57 def testClosedIssueIsClosed(self): | 57 def testClosedIssueIsClosed(self): |
58 self.assertTrue(CheckIssueClosed(200)) | 58 self.assertTrue(CheckIssueClosed(200)) |
59 | 59 |
60 @mock.patch('urllib2.urlopen',mockUrlOpen) | 60 @mock.patch('urllib2.urlopen', MockUrlOpen) |
61 def testOpenIssueIsNotClosed(self): | 61 def testOpenIssueIsNotClosed(self): |
62 self.assertFalse(CheckIssueClosed(201)) | 62 self.assertFalse(CheckIssueClosed(201)) |
63 | 63 |
64 @mock.patch('urllib2.urlopen',mockUrlOpen) | 64 @mock.patch('urllib2.urlopen', MockUrlOpen) |
65 def testUnexpectedFormat(self): | 65 def testUnexpectedFormat(self): |
66 self.assertFalse(CheckIssueClosed(300)) | 66 self.assertFalse(CheckIssueClosed(300)) |
67 | 67 |
68 @mock.patch('urllib2.urlopen',mockUrlOpen) | 68 @mock.patch('urllib2.urlopen', MockUrlOpen) |
69 def testUrlError(self): | 69 def testUrlError(self): |
70 self.assertFalse(CheckIssueClosed(403)) | 70 self.assertFalse(CheckIssueClosed(403)) |
71 | 71 |
72 @mock.patch('urllib2.urlopen',mockUrlOpen) | 72 @mock.patch('urllib2.urlopen', MockUrlOpen) |
73 def testEmptyResponse(self): | 73 def testEmptyResponse(self): |
74 self.assertFalse(CheckIssueClosed(404)) | 74 self.assertFalse(CheckIssueClosed(404)) |
75 | 75 |
76 @mock.patch('urllib2.urlopen',mockUrlOpen) | 76 @mock.patch('urllib2.urlopen', MockUrlOpen) |
77 def testBrokenResponse(self): | 77 def testBrokenResponse(self): |
78 self.assertFalse(CheckIssueClosed(500)) | 78 self.assertFalse(CheckIssueClosed(500)) |
79 | 79 |
80 | 80 |
81 if __name__ == '__main__': | 81 if __name__ == '__main__': |
82 unittest.main() | 82 unittest.main() |
OLD | NEW |