OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2015 Google Inc. |
| 3 # |
| 4 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 # you may not use this file except in compliance with the License. |
| 6 # You may obtain a copy of the License at |
| 7 # |
| 8 # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 # |
| 10 # Unless required by applicable law or agreed to in writing, software |
| 11 # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 # See the License for the specific language governing permissions and |
| 14 # limitations under the License. |
| 15 |
| 16 """Tests for codereview/dependency_utils.py.""" |
| 17 |
| 18 import unittest |
| 19 |
| 20 import setup |
| 21 setup.process_args() |
| 22 |
| 23 from codereview import models |
| 24 from codereview import dependency_utils |
| 25 |
| 26 |
| 27 class MockKey(object): |
| 28 def __init__(self, key_id): |
| 29 self.key_id = key_id |
| 30 def id(self): |
| 31 return self.key_id |
| 32 |
| 33 |
| 34 class MockIssue(object): |
| 35 def __init__(self, key_id): |
| 36 self.key_id = key_id |
| 37 |
| 38 @property |
| 39 def key(self): |
| 40 return MockKey(self.key_id) |
| 41 |
| 42 |
| 43 class MockPatchSet(object): |
| 44 def __init__(self, key_id, issue_key_id, dependent_patchsets, |
| 45 depends_on_patchset): |
| 46 self.key_id = key_id |
| 47 self.issue_key_id = issue_key_id |
| 48 self.dependent_patchsets = dependent_patchsets |
| 49 self.depends_on_patchset = depends_on_patchset |
| 50 self.put_called = False |
| 51 |
| 52 def put(self): |
| 53 self.put_called = True |
| 54 @property |
| 55 def key(self): |
| 56 return MockKey(self.key_id) |
| 57 @property |
| 58 def issue_key(self): |
| 59 return MockKey(self.issue_key_id) |
| 60 |
| 61 |
| 62 class TestPatchSetDependencyUtils(unittest.TestCase): |
| 63 """Test the dependency_utils module.""" |
| 64 |
| 65 def setUp(self): |
| 66 # Allow models.Issue.get_by_id to be monkeypatched by the tests. |
| 67 self.original_issue_get_by_id = models.Issue.get_by_id |
| 68 # Allow models.PatchSet.get_by_id to be monkeypatched by the tests. |
| 69 self.original_patchset_get_by_id = models.PatchSet.get_by_id |
| 70 |
| 71 def tearDown(self): |
| 72 # Undo any monkeypatching done by the tests. |
| 73 models.Issue.get_by_id = self.original_issue_get_by_id |
| 74 models.PatchSet.get_by_id = self.original_patchset_get_by_id |
| 75 |
| 76 def test_remove_as_dependent(self): |
| 77 # Create the patchset we will be removing as a dependent. |
| 78 patchset = MockPatchSet('40', '4', [], '3:30') |
| 79 |
| 80 # Make get_by_id methods return what we expect. |
| 81 def mock_issue_get_by_id(): |
| 82 def _w(*args, **_kwargs): |
| 83 return MockIssue(args[1]) |
| 84 return classmethod(_w) |
| 85 models.Issue.get_by_id = mock_issue_get_by_id() |
| 86 |
| 87 mockpatchset = MockPatchSet('30', '3', ['4:40', '1:10'], '') |
| 88 def mock_patchset_get_by_id(): |
| 89 def _w(*_args, **_kwargs): |
| 90 return mockpatchset |
| 91 return classmethod(_w) |
| 92 models.PatchSet.get_by_id = mock_patchset_get_by_id() |
| 93 |
| 94 # Assert that dependent_patchsets of the MockpatchSet is as expected and |
| 95 # that put was called on it. |
| 96 dependency_utils.remove_as_dependent(patchset) |
| 97 self.assertEquals(['1:10'], mockpatchset.dependent_patchsets) |
| 98 self.assertTrue(mockpatchset.put_called) |
| 99 |
| 100 |
| 101 def test_remove_dependencies(self): |
| 102 # Create the patchset we will be removing dependencies of. |
| 103 dependent_patchsets = ['1:10', '2:20', '3:30'] |
| 104 patchset = MockPatchSet('40', '4', dependent_patchsets, '') |
| 105 |
| 106 # Make get_by_id methods return what we expect. |
| 107 def mock_issue_get_by_id(): |
| 108 def _w(*args, **_kwargs): |
| 109 return MockIssue(args[1]) |
| 110 return classmethod(_w) |
| 111 models.Issue.get_by_id = mock_issue_get_by_id() |
| 112 |
| 113 mockpatchsets = [] |
| 114 def mock_patchset_get_by_id(): |
| 115 def _w(*args, **kwargs): |
| 116 mockpatchset = MockPatchSet(args[1], kwargs['parent'].id(), [], '4:40') |
| 117 mockpatchsets.append(mockpatchset) |
| 118 return mockpatchset |
| 119 return classmethod(_w) |
| 120 models.PatchSet.get_by_id = mock_patchset_get_by_id() |
| 121 |
| 122 # Assert that depends_on_patchset of the MockpatchSets are empty and that |
| 123 # put was called on them. |
| 124 dependency_utils.remove_dependencies(patchset) |
| 125 for mockpatchset in mockpatchsets: |
| 126 self.assertEquals('', mockpatchset.depends_on_patchset) |
| 127 self.assertTrue(mockpatchset.put_called) |
| 128 |
| 129 # Now change the depends_on_str for the dependents. Their dependency should |
| 130 # not be changed and put should not be called on them. |
| 131 mockpatchsets = [] |
| 132 def mock_patchset_get_by_id(): |
| 133 def _w(*args, **kwargs): |
| 134 mockpatchset = MockPatchSet(args[1], kwargs['parent'].id(), [], '4:41') |
| 135 mockpatchsets.append(mockpatchset) |
| 136 return mockpatchset |
| 137 return classmethod(_w) |
| 138 models.PatchSet.get_by_id = mock_patchset_get_by_id() |
| 139 dependency_utils.remove_dependencies(patchset) |
| 140 for mockpatchset in mockpatchsets: |
| 141 self.assertEquals('4:41', mockpatchset.depends_on_patchset) |
| 142 self.assertFalse(mockpatchset.put_called) |
| 143 |
| 144 |
| 145 def test_mark_as_dependent_and_get_dependency_str(self): |
| 146 # Make get_by_id methods return what we expect. |
| 147 def mock_issue_get_by_id(): |
| 148 def _w(*args, **_kwargs): |
| 149 return MockIssue(args[1]) |
| 150 return classmethod(_w) |
| 151 models.Issue.get_by_id = mock_issue_get_by_id() |
| 152 |
| 153 mockpatchset = MockPatchSet('40', '4', ['1:10', '2:20'], '') |
| 154 def mock_patchset_get_by_id(): |
| 155 def _w(*_args, **_kwargs): |
| 156 return mockpatchset |
| 157 return classmethod(_w) |
| 158 models.PatchSet.get_by_id = mock_patchset_get_by_id() |
| 159 |
| 160 dependency_str = ( |
| 161 dependency_utils.mark_as_dependent_and_get_dependency_str( |
| 162 '4:40', '3', '30')) |
| 163 # Since the depends on Issue and PatchSet were found the dependency str |
| 164 # should be returned. |
| 165 self.assertEquals('4:40', dependency_str) |
| 166 # Assert that the dependent_patchsets was updated and that put was called. |
| 167 self.assertEquals(['1:10', '2:20', '3:30'], |
| 168 mockpatchset.dependent_patchsets) |
| 169 self.assertTrue(mockpatchset.put_called) |
| 170 |
| 171 # Make the referenced Issue be invalid and assert that a dependency str is |
| 172 # not returned and dependent_patchsets is not updated and that put is not |
| 173 # called. |
| 174 def mock_issue_get_by_id(): |
| 175 def _w(*_args, **_kwargs): |
| 176 return None |
| 177 return classmethod(_w) |
| 178 models.Issue.get_by_id = mock_issue_get_by_id() |
| 179 mockpatchset = MockPatchSet('40', '4', ['1:10', '2:20'], '') |
| 180 dependency_str = ( |
| 181 dependency_utils.mark_as_dependent_and_get_dependency_str( |
| 182 '4:40', '3', '30')) |
| 183 self.assertEquals(None, dependency_str) |
| 184 self.assertEquals(['1:10', '2:20'], mockpatchset.dependent_patchsets) |
| 185 self.assertFalse(mockpatchset.put_called) |
| 186 |
| 187 # Make the referenced Patchset be invalid and assert that a dependency str |
| 188 # is not returned. |
| 189 def mock_issue_get_by_id(): |
| 190 def _w(*args, **_kwargs): |
| 191 return MockIssue(args[1]) |
| 192 return classmethod(_w) |
| 193 models.Issue.get_by_id = mock_issue_get_by_id() |
| 194 def mock_patchset_get_by_id(): |
| 195 def _w(*_args, **_kwargs): |
| 196 return None |
| 197 return classmethod(_w) |
| 198 models.PatchSet.get_by_id = mock_patchset_get_by_id() |
| 199 dependency_str = ( |
| 200 dependency_utils.mark_as_dependent_and_get_dependency_str( |
| 201 '4:40', '3', '30')) |
| 202 self.assertEquals(None, dependency_str) |
| 203 |
| 204 |
| 205 if __name__ == '__main__': |
| 206 unittest.main() |
| 207 |
OLD | NEW |