OLD | NEW |
(Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import mock |
| 6 |
| 7 from dashboard.common import namespaced_stored_object |
| 8 from dashboard.common import testing_common |
| 9 from dashboard.pinpoint.models.change import commit |
| 10 |
| 11 |
| 12 _CHROMIUM_URL = 'https://chromium.googlesource.com/chromium/src' |
| 13 |
| 14 |
| 15 class _CommitTest(testing_common.TestCase): |
| 16 |
| 17 def setUp(self): |
| 18 super(_CommitTest, self).setUp() |
| 19 |
| 20 self.SetCurrentUser('internal@chromium.org', is_admin=True) |
| 21 |
| 22 namespaced_stored_object.Set('repositories', { |
| 23 'chromium': {'repository_url': _CHROMIUM_URL}, |
| 24 }) |
| 25 |
| 26 |
| 27 class CommitTest(_CommitTest): |
| 28 |
| 29 def testCommit(self): |
| 30 c = commit.Commit('chromium', 'aaa7336c821888839f759c6c0a36') |
| 31 |
| 32 other_commit = commit.Commit(u'chromium', u'aaa7336c821888839f759c6c0a36') |
| 33 self.assertEqual(c, other_commit) |
| 34 self.assertEqual(str(c), 'chromium@aaa7336') |
| 35 self.assertEqual(c.id_string, 'chromium@aaa7336c821888839f759c6c0a36') |
| 36 self.assertEqual(c.repository, 'chromium') |
| 37 self.assertEqual(c.git_hash, 'aaa7336c821888839f759c6c0a36') |
| 38 self.assertEqual(c.repository_url, |
| 39 'https://chromium.googlesource.com/chromium/src') |
| 40 |
| 41 @mock.patch('dashboard.services.gitiles_service.FileContents') |
| 42 def testDeps(self, file_contents): |
| 43 file_contents.return_value = """ |
| 44 vars = { |
| 45 'chromium_git': 'https://chromium.googlesource.com', |
| 46 } |
| 47 deps = { |
| 48 'src/v8': Var('chromium_git') + '/v8/v8.git' + '@' + 'c092edb', |
| 49 } |
| 50 deps_os = { |
| 51 'win': { |
| 52 'src/third_party/cygwin': |
| 53 Var('chromium_git') + '/chromium/deps/cygwin.git' + '@' + 'c89e446', |
| 54 } |
| 55 } |
| 56 """ |
| 57 |
| 58 c = commit.Commit('chromium', 'aaa7336') |
| 59 expected = frozenset(( |
| 60 commit.Commit('cygwin', 'c89e446'), |
| 61 commit.Commit('v8', 'c092edb'), |
| 62 )) |
| 63 self.assertEqual(c.Deps(), expected) |
| 64 |
| 65 def testAsDict(self): |
| 66 c = commit.Commit('chromium', 'aaa7336') |
| 67 expected = { |
| 68 'repository': 'chromium', |
| 69 'git_hash': 'aaa7336', |
| 70 'url': _CHROMIUM_URL + '/+/aaa7336', |
| 71 } |
| 72 self.assertEqual(c.AsDict(), expected) |
| 73 |
| 74 @mock.patch('dashboard.services.gitiles_service.CommitInfo') |
| 75 def testFromDict(self, _): |
| 76 c = commit.Commit.FromDict({ |
| 77 'repository': 'chromium', |
| 78 'git_hash': 'aaa7336', |
| 79 }) |
| 80 |
| 81 expected = commit.Commit('chromium', 'aaa7336') |
| 82 self.assertEqual(c, expected) |
| 83 |
| 84 @mock.patch('dashboard.services.gitiles_service.CommitInfo') |
| 85 def testFromDictWithRepositoryUrl(self, _): |
| 86 c = commit.Commit.FromDict({ |
| 87 'repository': 'https://chromium.googlesource.com/chromium/src', |
| 88 'git_hash': 'aaa7336', |
| 89 }) |
| 90 |
| 91 expected = commit.Commit('chromium', 'aaa7336') |
| 92 self.assertEqual(c, expected) |
| 93 |
| 94 def testFromDictFailureFromUnknownRepo(self): |
| 95 with self.assertRaises(KeyError): |
| 96 commit.Commit.FromDict({ |
| 97 'repository': 'unknown repo', |
| 98 'git_hash': 'git hash', |
| 99 }) |
| 100 |
| 101 @mock.patch('dashboard.services.gitiles_service.CommitInfo') |
| 102 def testFromDictFailureFromUnknownCommit(self, commit_info): |
| 103 commit_info.side_effect = KeyError() |
| 104 |
| 105 with self.assertRaises(KeyError): |
| 106 commit.Commit.FromDict({ |
| 107 'repository': 'chromium', |
| 108 'git_hash': 'unknown git hash', |
| 109 }) |
| 110 |
| 111 |
| 112 class MidpointTest(_CommitTest): |
| 113 |
| 114 @mock.patch('dashboard.services.gitiles_service.CommitRange') |
| 115 def testSuccess(self, commit_range): |
| 116 commit_range.return_value = [ |
| 117 {'commit': 'babe852'}, |
| 118 {'commit': 'b57345e'}, |
| 119 {'commit': '949b36d'}, |
| 120 {'commit': '1ef4789'}, |
| 121 ] |
| 122 |
| 123 commit_a = commit.Commit('chromium', '0e57e2b') |
| 124 commit_b = commit.Commit('chromium', 'babe852') |
| 125 self.assertEqual(commit.Commit.Midpoint(commit_a, commit_b), |
| 126 commit.Commit('chromium', '949b36d')) |
| 127 |
| 128 def testRaisesWithSameCommit(self): |
| 129 commit_a = commit.Commit('chromium', '0e57e2b') |
| 130 commit_b = commit.Commit('chromium', '0e57e2b') |
| 131 with self.assertRaises(commit.SameCommitWarning): |
| 132 commit.Commit.Midpoint(commit_a, commit_b) |
| 133 |
| 134 def testRaisesWithDifferingRepositories(self): |
| 135 commit_a = commit.Commit('chromium', '0e57e2b') |
| 136 commit_b = commit.Commit('not_chromium', 'babe852') |
| 137 with self.assertRaises(commit.NonLinearError): |
| 138 commit.Commit.Midpoint(commit_a, commit_b) |
| 139 |
| 140 @mock.patch('dashboard.services.gitiles_service.CommitRange') |
| 141 def testRaisesWithAdjacentCommits(self, commit_range): |
| 142 commit_range.return_value = [{'commit': 'b57345e'}] |
| 143 |
| 144 commit_a = commit.Commit('chromium', '949b36d') |
| 145 commit_b = commit.Commit('chromium', 'b57345e') |
| 146 with self.assertRaises(commit.AdjacentCommitsWarning): |
| 147 commit.Commit.Midpoint(commit_a, commit_b) |
| 148 |
| 149 @mock.patch('dashboard.services.gitiles_service.CommitRange') |
| 150 def testRaisesWithEmptyRange(self, commit_range): |
| 151 commit_range.return_value = [] |
| 152 |
| 153 commit_b = commit.Commit('chromium', 'b57345e') |
| 154 commit_a = commit.Commit('chromium', '949b36d') |
| 155 with self.assertRaises(commit.NonLinearError): |
| 156 commit.Commit.Midpoint(commit_a, commit_b) |
OLD | NEW |