OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 unittest | 5 import unittest |
6 | 6 |
| 7 import mock |
| 8 |
7 from dashboard.pinpoint.models.change import patch | 9 from dashboard.pinpoint.models.change import patch |
8 | 10 |
9 | 11 |
10 class PatchTest(unittest.TestCase): | 12 _GERRIT_CHANGE_INFO = { |
| 13 'id': 'repo~branch~id', |
| 14 'project': 'chromium/src', |
| 15 '_number': 658277, |
| 16 'current_revision': 'current revision', |
| 17 'revisions': { |
| 18 'current revision': { |
| 19 '_number': 5, |
| 20 'fetch': { |
| 21 'http': { |
| 22 'url': 'https://googlesource.com/chromium/src', |
| 23 'ref': 'refs/changes/77/658277/5', |
| 24 }, |
| 25 }, |
| 26 }, |
| 27 'other revision': { |
| 28 '_number': 4, |
| 29 'fetch': { |
| 30 'http': { |
| 31 'url': 'https://googlesource.com/chromium/src', |
| 32 'ref': 'refs/changes/77/658277/4', |
| 33 }, |
| 34 }, |
| 35 }, |
| 36 }, |
| 37 } |
| 38 |
| 39 |
| 40 class FromDictTest(unittest.TestCase): |
| 41 |
| 42 @mock.patch('dashboard.services.gerrit_service.GetChange') |
| 43 def testFromDictGerrit(self, get_change): |
| 44 get_change.return_value = _GERRIT_CHANGE_INFO |
| 45 |
| 46 p = patch.FromDict('https://example.com/c/repo/+/658277') |
| 47 |
| 48 expected = patch.GerritPatch( |
| 49 'https://example.com', 'repo~branch~id', 'current revision') |
| 50 self.assertEqual(p, expected) |
| 51 |
| 52 |
| 53 class GerritPatchTest(unittest.TestCase): |
11 | 54 |
12 def testPatch(self): | 55 def testPatch(self): |
13 p = patch.Patch('https://codereview.chromium.org', 2851943002, 40001) | 56 p = patch.GerritPatch('https://example.com', 672011, '2f0d5c7') |
14 | 57 |
15 other_patch = patch.Patch(u'https://codereview.chromium.org', | 58 other_patch = patch.GerritPatch(u'https://example.com', 672011, '2f0d5c7') |
16 2851943002, 40001) | |
17 self.assertEqual(p, other_patch) | 59 self.assertEqual(p, other_patch) |
18 string = 'https://codereview.chromium.org/2851943002/40001' | 60 string = 'https://example.com/672011/2f0d5c7' |
19 self.assertEqual(str(p), string) | 61 self.assertEqual(str(p), string) |
20 self.assertEqual(p.id_string, string) | 62 self.assertEqual(p.id_string, string) |
21 | 63 |
| 64 @mock.patch('dashboard.services.gerrit_service.GetChange') |
| 65 def testBuildParameters(self, get_change): |
| 66 get_change.return_value = _GERRIT_CHANGE_INFO |
| 67 |
| 68 p = patch.GerritPatch('https://example.com', 658277, 'current revision') |
| 69 expected = { |
| 70 'patch_gerrit_url': 'https://example.com', |
| 71 'patch_issue': 658277, |
| 72 'patch_project': 'chromium/src', |
| 73 'patch_ref': 'refs/changes/77/658277/5', |
| 74 'patch_repository_url': 'https://googlesource.com/chromium/src', |
| 75 'patch_set': 5, |
| 76 'patch_storage': 'gerrit', |
| 77 } |
| 78 self.assertEqual(p.BuildParameters(), expected) |
| 79 |
22 def testAsDict(self): | 80 def testAsDict(self): |
23 p = patch.Patch('https://codereview.chromium.org', 2851943002, 40001) | 81 p = patch.GerritPatch('https://example.com', 672011, '2f0d5c7') |
24 expected = { | 82 expected = { |
25 'server': 'https://codereview.chromium.org', | 83 'server': 'https://example.com', |
26 'issue': 2851943002, | 84 'change': 672011, |
27 'patchset': 40001, | 85 'revision': '2f0d5c7', |
28 } | 86 } |
29 self.assertEqual(p.AsDict(), expected) | 87 self.assertEqual(p.AsDict(), expected) |
30 | 88 |
31 def testFromDict(self): | 89 @mock.patch('dashboard.services.gerrit_service.GetChange') |
32 p = patch.Patch.FromDict({ | 90 def testFromDict(self, get_change): |
33 'server': 'https://codereview.chromium.org', | 91 get_change.return_value = _GERRIT_CHANGE_INFO |
34 'issue': 2851943002, | 92 |
35 'patchset': 40001, | 93 p = patch.GerritPatch.FromDict({ |
| 94 'server': 'https://example.com', |
| 95 'change': 658277, |
| 96 'revision': 4, |
36 }) | 97 }) |
37 | 98 |
38 expected = patch.Patch('https://codereview.chromium.org', 2851943002, 40001) | 99 expected = patch.GerritPatch( |
| 100 'https://example.com', 'repo~branch~id', 'other revision') |
39 self.assertEqual(p, expected) | 101 self.assertEqual(p, expected) |
| 102 |
| 103 @mock.patch('dashboard.services.gerrit_service.GetChange') |
| 104 def testFromDictString(self, get_change): |
| 105 get_change.return_value = _GERRIT_CHANGE_INFO |
| 106 |
| 107 p = patch.GerritPatch.FromDict('https://example.com/c/repo/+/658277') |
| 108 |
| 109 expected = patch.GerritPatch( |
| 110 'https://example.com', 'repo~branch~id', 'current revision') |
| 111 self.assertEqual(p, expected) |
OLD | NEW |