13
13
from canonical.testing.layers import ZopelessDatabaseLayer
16
class TestDirectBranchCommit(TestCaseWithFactory):
17
"""Test `DirectBranchCommit`."""
19
layer = ZopelessDatabaseLayer
16
class DirectBranchCommitTestCase(TestCaseWithFactory):
17
"""Base class for `DirectBranchCommit` tests."""
25
super(TestDirectBranchCommit, self).setUp()
22
super(DirectBranchCommitTestCase, self).setUp()
27
23
self.useBzrBranches()
29
25
self.series = self.factory.makeProductSeries()
43
41
self.db_branch.last_scanned_id = (
44
42
self.committer.bzrbranch.last_revision())
47
self.committer.unlock()
44
def _tearDownCommitter(self):
46
self.committer.unlock()
49
48
def _getContents(self):
50
49
"""Return branch contents as dict mapping filenames to contents."""
51
50
return map_branch_contents(self.committer.db_branch.getPullURL())
53
class TestDirectBranchCommit(DirectBranchCommitTestCase):
54
"""Test `DirectBranchCommit`."""
56
layer = ZopelessDatabaseLayer
53
58
def test_DirectBranchCommit_commits_no_changes(self):
54
# Committing to an empty branch leaves the branch empty.
59
# Committing to an empty branch leaves an empty branch empty.
55
60
self.committer.commit('')
56
61
self.assertEqual({}, self._getContents())
95
100
self.assertEqual(expected, self._getContents())
102
def test_DirectBranchCommit_reuses_new_directories(self):
103
# If a directory doesn't exist in the committed branch, creating
104
# it twice would be an error. DirectBranchCommit doesn't do
106
self.committer.writeFile('foo/x.txt', 'x')
107
self.committer.writeFile('foo/y.txt', 'y')
108
self.committer.commit('')
113
self.assertEqual(expected, self._getContents())
97
115
def test_DirectBranchCommit_writes_new_file_twice(self):
98
116
# If you write the same new file multiple times before
99
117
# committing, the original wins.
123
141
self.assertRaises(ConcurrentUpdateError, self.committer.commit, '')
144
class TestDirectBranchCommit_getDir(DirectBranchCommitTestCase):
145
"""Test `DirectBranchCommit._getDir`."""
147
layer = ZopelessDatabaseLayer
149
def test_getDir_creates_root(self):
150
# An id is created even for the branch root directory.
151
self.assertFalse('' in self.committer.path_ids)
152
root_id = self.committer._getDir('')
153
self.assertNotEqual(None, root_id)
154
self.assertTrue('' in self.committer.path_ids)
155
self.assertEqual(self.committer.path_ids[''], root_id)
156
self.committer.commit('')
158
def test_getDir_creates_dir(self):
159
# _getDir will create a new directory, under the root.
160
self.assertFalse('dir' in self.committer.path_ids)
161
dir_id = self.committer._getDir('dir')
162
self.assertTrue('' in self.committer.path_ids)
163
self.assertTrue('dir' in self.committer.path_ids)
164
self.assertEqual(self.committer.path_ids['dir'], dir_id)
165
self.assertNotEqual(self.committer.path_ids[''], dir_id)
166
self.committer.commit('')
168
def test_getDir_creates_subdir(self):
169
# _getDir will create nested directories.
170
subdir_id = self.committer._getDir('dir/subdir')
171
self.assertTrue('' in self.committer.path_ids)
172
self.assertTrue('dir' in self.committer.path_ids)
173
self.assertTrue('dir/subdir' in self.committer.path_ids)
174
self.assertEqual(self.committer.path_ids['dir/subdir'], subdir_id)
175
self.committer.commit('')
177
def test_getDir_finds_existing_dir(self):
178
# _getDir finds directories that already existed in a previously
179
# committed version of the branch.
180
existing_id = self.committer._getDir('po')
181
self._setUpCommitter()
182
dir_id = self.committer._getDir('po')
183
self.assertEqual(existing_id, dir_id)
184
self.committer.commit('')
186
def test_getDir_creates_dir_in_existing_dir(self):
187
# _getDir creates directories inside ones that already existed
188
# in a previously committed version of the branch.
189
existing_id = self.committer._getDir('po')
190
self._setUpCommitter()
191
new_dir_id = self.committer._getDir('po/main/files')
192
self.assertTrue('po/main' in self.committer.path_ids)
193
self.assertTrue('po/main/files' in self.committer.path_ids)
194
self.assertEqual(self.committer.path_ids['po/main/files'], new_dir_id)
195
self.committer.commit('')
197
def test_getDir_reuses_new_id(self):
198
# If a directory was newly created, _getDir will reuse its id.
199
dir_id = self.committer._getDir('foo/bar')
200
self.assertEqual(dir_id, self.committer._getDir('foo/bar'))
201
self.committer.commit('')
126
204
def test_suite():
127
205
return TestLoader().loadTestsFromName(__name__)