~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/code/tests/test_directbranchcommit.py

[release-critical=flacoste][r=mwhudson][ui=none][bug=401835] Fix
        commit-translations-to-branch: repeated path error.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
from canonical.testing.layers import ZopelessDatabaseLayer
14
14
 
15
15
 
16
 
class TestDirectBranchCommit(TestCaseWithFactory):
17
 
    """Test `DirectBranchCommit`."""
18
 
 
19
 
    layer = ZopelessDatabaseLayer
20
 
 
 
16
class DirectBranchCommitTestCase(TestCaseWithFactory):
 
17
    """Base class for `DirectBranchCommit` tests."""
21
18
    db_branch = None
22
19
    committer = None
23
20
 
24
21
    def setUp(self):
25
 
        super(TestDirectBranchCommit, self).setUp()
26
 
 
 
22
        super(DirectBranchCommitTestCase, self).setUp()
27
23
        self.useBzrBranches()
28
24
 
29
25
        self.series = self.factory.makeProductSeries()
33
29
        self.series.translations_branch = self.db_branch
34
30
 
35
31
        self._setUpCommitter()
 
32
        self.addCleanup(self._tearDownCommitter)
36
33
 
37
34
    def _setUpCommitter(self, update_last_scanned_id=True):
 
35
        """Clean up any existing `DirectBranchCommit`, set up a new one."""
38
36
        if self.committer:
39
37
            self.committer.unlock()
40
38
 
43
41
            self.db_branch.last_scanned_id = (
44
42
                self.committer.bzrbranch.last_revision())
45
43
 
46
 
    def tearDown(self):
47
 
        self.committer.unlock()
 
44
    def _tearDownCommitter(self):
 
45
        if self.committer:
 
46
            self.committer.unlock()
48
47
 
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())
52
51
 
 
52
 
 
53
class TestDirectBranchCommit(DirectBranchCommitTestCase):
 
54
    """Test `DirectBranchCommit`."""
 
55
 
 
56
    layer = ZopelessDatabaseLayer
 
57
 
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())
57
62
 
94
99
        }
95
100
        self.assertEqual(expected, self._getContents())
96
101
 
 
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
 
105
        # that.
 
106
        self.committer.writeFile('foo/x.txt', 'x')
 
107
        self.committer.writeFile('foo/y.txt', 'y')
 
108
        self.committer.commit('')
 
109
        expected = {
 
110
            'foo/x.txt': 'x',
 
111
            'foo/y.txt': 'y',
 
112
        }
 
113
        self.assertEqual(expected, self._getContents())
 
114
 
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, '')
124
142
 
125
143
 
 
144
class TestDirectBranchCommit_getDir(DirectBranchCommitTestCase):
 
145
    """Test `DirectBranchCommit._getDir`."""
 
146
 
 
147
    layer = ZopelessDatabaseLayer
 
148
 
 
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('')
 
157
 
 
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('')
 
167
 
 
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('')
 
176
 
 
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('')
 
185
 
 
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('')
 
196
 
 
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('')
 
202
 
 
203
 
126
204
def test_suite():
127
205
    return TestLoader().loadTestsFromName(__name__)