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 issue_get_by_id_wrapper(wrapped_func): |
| 82 def _w(*args, **kwargs): |
| 83 return MockIssue(args[1]) |
| 84 return classmethod(_w) |
| 85 models.Issue.get_by_id = issue_get_by_id_wrapper(models.Issue.get_by_id) |
| 86 |
| 87 mockpatchset = MockPatchSet('30', '3', ['4:40', '1:10'], '') |
| 88 def patchset_get_by_id_wrapper(wrapped_func): |
| 89 def _w(*args, **kwargs): |
| 90 return mockpatchset |
| 91 return classmethod(_w) |
| 92 models.PatchSet.get_by_id = patchset_get_by_id_wrapper( |
| 93 models.PatchSet.get_by_id) |
| 94 |
| 95 # Assert that dependent_patchsets of the MockpatchSet is as expected and |
| 96 # that put was called on it. |
| 97 dependency_utils.remove_as_dependent(patchset) |
| 98 self.assertEquals(['1:10'], mockpatchset.dependent_patchsets) |
| 99 self.assertTrue(mockpatchset.put_called) |
| 100 |
| 101 |
| 102 def test_remove_dependencies(self): |
| 103 # Create the patchset we will be removing dependencies of. |
| 104 dependent_patchsets = ['1:10', '2:20', '3:30'] |
| 105 patchset = MockPatchSet('40', '4', dependent_patchsets, '') |
| 106 |
| 107 # Make get_by_id methods return what we expect. |
| 108 def issue_get_by_id_wrapper(wrapped_func): |
| 109 def _w(*args, **kwargs): |
| 110 return MockIssue(args[1]) |
| 111 return classmethod(_w) |
| 112 models.Issue.get_by_id = issue_get_by_id_wrapper(models.Issue.get_by_id) |
| 113 |
| 114 mockpatchsets = [] |
| 115 def patchset_get_by_id_wrapper(wrapped_func): |
| 116 def _w(*args, **kwargs): |
| 117 mockpatchset = MockPatchSet(args[1], kwargs['parent'].id(), [], '4:40') |
| 118 mockpatchsets.append(mockpatchset) |
| 119 return mockpatchset |
| 120 return classmethod(_w) |
| 121 models.PatchSet.get_by_id = patchset_get_by_id_wrapper( |
| 122 models.PatchSet.get_by_id) |
| 123 |
| 124 # Assert that depends_on_patchset of the MockpatchSets are empty and that |
| 125 # put was called on them. |
| 126 dependency_utils.remove_dependencies(patchset) |
| 127 for mockpatchset in mockpatchsets: |
| 128 self.assertEquals('', mockpatchset.depends_on_patchset) |
| 129 self.assertTrue(mockpatchset.put_called) |
| 130 |
| 131 # Now change the depends_on_str for the dependents. Their dependency should |
| 132 # not be changed and put should not be called on them. |
| 133 mockpatchsets = [] |
| 134 def patchset_get_by_id_wrapper(wrapped_func): |
| 135 def _w(*args, **kwargs): |
| 136 mockpatchset = MockPatchSet(args[1], kwargs['parent'].id(), [], '4:41') |
| 137 mockpatchsets.append(mockpatchset) |
| 138 return mockpatchset |
| 139 return classmethod(_w) |
| 140 models.PatchSet.get_by_id = patchset_get_by_id_wrapper( |
| 141 models.PatchSet.get_by_id) |
| 142 dependency_utils.remove_dependencies(patchset) |
| 143 for mockpatchset in mockpatchsets: |
| 144 self.assertEquals('4:41', mockpatchset.depends_on_patchset) |
| 145 self.assertFalse(mockpatchset.put_called) |
| 146 |
| 147 |
| 148 def test_mark_as_dependent_and_get_dependency_str(self): |
| 149 # Make get_by_id methods return what we expect. |
| 150 def issue_get_by_id_wrapper(wrapped_func): |
| 151 def _w(*args, **kwargs): |
| 152 return MockIssue(args[1]) |
| 153 return classmethod(_w) |
| 154 models.Issue.get_by_id = issue_get_by_id_wrapper(models.Issue.get_by_id) |
| 155 |
| 156 mockpatchset = MockPatchSet('40', '4', ['1:10', '2:20'], '') |
| 157 def patchset_get_by_id_wrapper(wrapped_func): |
| 158 def _w(*args, **kwargs): |
| 159 return mockpatchset |
| 160 return classmethod(_w) |
| 161 models.PatchSet.get_by_id = patchset_get_by_id_wrapper( |
| 162 models.PatchSet.get_by_id) |
| 163 |
| 164 dependency_str = ( |
| 165 dependency_utils.mark_as_dependent_and_get_dependency_str( |
| 166 '4:40', '3', '30')) |
| 167 # Since the depends on Issue and PatchSet were found the dependency str |
| 168 # should be returned. |
| 169 self.assertEquals('4:40', dependency_str) |
| 170 # Assert that the dependent_patchsets was updated and that put was called. |
| 171 self.assertEquals(['1:10', '2:20', '3:30'], |
| 172 mockpatchset.dependent_patchsets) |
| 173 self.assertTrue(mockpatchset.put_called) |
| 174 |
| 175 # Make the referenced Issue be invalid and assert that a dependency str is |
| 176 # not returned and dependent_patchsets is not updated and that put is not |
| 177 # called. |
| 178 def issue_get_by_id_wrapper(wrapped_func): |
| 179 def _w(*args, **kwargs): |
| 180 return None |
| 181 return classmethod(_w) |
| 182 models.Issue.get_by_id = issue_get_by_id_wrapper(models.Issue.get_by_id) |
| 183 mockpatchset = MockPatchSet('40', '4', ['1:10', '2:20'], '') |
| 184 dependency_str = ( |
| 185 dependency_utils.mark_as_dependent_and_get_dependency_str( |
| 186 '4:40', '3', '30')) |
| 187 self.assertEquals(None, dependency_str) |
| 188 self.assertEquals(['1:10', '2:20'], mockpatchset.dependent_patchsets) |
| 189 self.assertFalse(mockpatchset.put_called) |
| 190 |
| 191 # Make the referenced Patchset be invalid and assert that a dependency str |
| 192 # is not returned. |
| 193 def issue_get_by_id_wrapper(wrapped_func): |
| 194 def _w(*args, **kwargs): |
| 195 return MockIssue(args[1]) |
| 196 return classmethod(_w) |
| 197 models.Issue.get_by_id = issue_get_by_id_wrapper(models.Issue.get_by_id) |
| 198 def patchset_get_by_id_wrapper(wrapped_func): |
| 199 def _w(*args, **kwargs): |
| 200 return None |
| 201 return classmethod(_w) |
| 202 models.PatchSet.get_by_id = patchset_get_by_id_wrapper( |
| 203 models.PatchSet.get_by_id) |
| 204 dependency_str = ( |
| 205 dependency_utils.mark_as_dependent_and_get_dependency_str( |
| 206 '4:40', '3', '30')) |
| 207 self.assertEquals(None, dependency_str) |
| 208 |
| 209 |
| 210 if __name__ == '__main__': |
| 211 unittest.main() |
| 212 |
OLD | NEW |