/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/inventory_implementations/basics.py

  • Committer: Martin Pool
  • Date: 2009-06-05 23:21:51 UTC
  • mfrom: (4415 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4416.
  • Revision ID: mbp@sourcefrog.net-20090605232151-luwmyyl95siraqyz
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for different inventory implementations"""
18
18
 
37
37
        )
38
38
 
39
39
 
40
 
class TestInventoryBasics(TestCase):
41
 
    # Most of these were moved the rather old bzrlib.tests.test_inv module
 
40
class TestInventory(TestCase):
42
41
 
43
42
    def make_inventory(self, root_id):
44
43
        return self.inventory_class(root_id=root_id)
45
44
 
 
45
    def prepare_inv_with_nested_dirs(self):
 
46
        inv = self.make_inventory('tree-root')
 
47
        for args in [('src', 'directory', 'src-id'),
 
48
                     ('doc', 'directory', 'doc-id'),
 
49
                     ('src/hello.c', 'file', 'hello-id'),
 
50
                     ('src/bye.c', 'file', 'bye-id'),
 
51
                     ('zz', 'file', 'zz-id'),
 
52
                     ('src/sub/', 'directory', 'sub-id'),
 
53
                     ('src/zz.c', 'file', 'zzc-id'),
 
54
                     ('src/sub/a', 'file', 'a-id'),
 
55
                     ('Makefile', 'file', 'makefile-id')]:
 
56
            inv.add_path(*args)
 
57
        return inv
 
58
 
 
59
 
 
60
class TestInventoryUpdates(TestInventory):
 
61
 
46
62
    def test_creation_from_root_id(self):
47
63
        # iff a root id is passed to the constructor, a root directory is made
48
64
        inv = self.make_inventory(root_id='tree-root')
88
104
        self.assertEquals('someroot', inv2.root.file_id)
89
105
        self.assertEquals('therev', inv2.root.revision)
90
106
 
91
 
    def test_is_root(self):
92
 
        """Ensure our root-checking code is accurate."""
93
 
        inv = self.make_inventory('TREE_ROOT')
94
 
        self.assertTrue(inv.is_root('TREE_ROOT'))
95
 
        self.assertFalse(inv.is_root('booga'))
96
 
        inv.root.file_id = 'booga'
97
 
        self.assertFalse(inv.is_root('TREE_ROOT'))
98
 
        self.assertTrue(inv.is_root('booga'))
99
 
        # works properly even if no root is set
100
 
        inv.root = None
101
 
        self.assertFalse(inv.is_root('TREE_ROOT'))
102
 
        self.assertFalse(inv.is_root('booga'))
103
 
 
104
107
    def test_create_tree_reference(self):
105
108
        inv = self.make_inventory('tree-root-123')
106
109
        inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
116
119
        else:
117
120
            self.fail('BzrError not raised')
118
121
 
 
122
    def test_add_recursive(self):
 
123
        parent = InventoryDirectory('src-id', 'src', 'tree-root')
 
124
        child = InventoryFile('hello-id', 'hello.c', 'src-id')
 
125
        parent.children[child.file_id] = child
 
126
        inv = self.make_inventory('tree-root')
 
127
        inv.add(parent)
 
128
        self.assertEqual('src/hello.c', inv.id2path('hello-id'))
 
129
 
 
130
 
 
131
class TestInventoryApplyDelta(TestInventory):
 
132
 
 
133
    def test_apply_delta_add(self):
 
134
        inv = self.make_inventory('tree-root')
 
135
        inv.apply_delta([
 
136
            (None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
 
137
            ])
 
138
        self.assertEqual('a', inv.id2path('a-id'))
 
139
 
 
140
    def test_apply_delta_delete(self):
 
141
        inv = self.make_inventory('tree-root')
 
142
        inv.apply_delta([
 
143
            (None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
 
144
            ])
 
145
        self.assertEqual('a', inv.id2path('a-id'))
 
146
        a_ie = inv['a-id']
 
147
        inv.apply_delta([("a", None, "a-id", a_ie)])
 
148
        self.assertRaises(errors.NoSuchId, inv.id2path, 'a-id')
 
149
 
 
150
    def test_apply_delta_rename(self):
 
151
        inv = self.make_inventory('tree-root')
 
152
        inv.apply_delta([
 
153
            (None, "a", "a-id", InventoryFile('a-id', 'a', 'tree-root')),
 
154
            ])
 
155
        self.assertEqual('a', inv.id2path('a-id'))
 
156
        a_ie = inv['a-id']
 
157
        b_ie = InventoryFile(a_ie.file_id, "b", a_ie.parent_id)
 
158
        inv.apply_delta([("a", "b", "a-id", b_ie)])
 
159
        self.assertEqual("b", inv.id2path('a-id'))
 
160
 
 
161
    def test_apply_delta_illegal(self):
 
162
        # A file-id cannot appear in a delta more than once
 
163
        inv = self.make_inventory('tree-root')
 
164
        self.assertRaises(AssertionError, inv.apply_delta, [
 
165
            ("a", "a", "id-1", InventoryFile('id-1', 'a', 'tree-root')),
 
166
            ("a", "b", "id-1", InventoryFile('id-1', 'b', 'tree-root')),
 
167
            ])
 
168
 
 
169
 
 
170
class TestInventoryReads(TestInventory):
 
171
 
 
172
    def test_is_root(self):
 
173
        """Ensure our root-checking code is accurate."""
 
174
        inv = self.make_inventory('TREE_ROOT')
 
175
        self.assertTrue(inv.is_root('TREE_ROOT'))
 
176
        self.assertFalse(inv.is_root('booga'))
 
177
        inv.root.file_id = 'booga'
 
178
        self.assertFalse(inv.is_root('TREE_ROOT'))
 
179
        self.assertTrue(inv.is_root('booga'))
 
180
        # works properly even if no root is set
 
181
        inv.root = None
 
182
        self.assertFalse(inv.is_root('TREE_ROOT'))
 
183
        self.assertFalse(inv.is_root('booga'))
 
184
 
119
185
    def test_ids(self):
120
186
        """Test detection of files within selected directories."""
121
187
        inv = self.make_inventory(ROOT_ID)
145
211
                     ('doc', 'directory', 'doc-id'),
146
212
                     ('src/hello.c', 'file', 'hello-id'),
147
213
                     ('src/bye.c', 'file', 'bye-id'),
 
214
                     ('src/sub', 'directory', 'sub-id'),
 
215
                     ('src/sub/a', 'file', 'a-id'),
148
216
                     ('Makefile', 'file', 'makefile-id')]:
149
217
            inv.add_path(*args)
 
218
 
 
219
        # Test all entries
150
220
        self.assertEqual([
151
221
            ('', 'tree-root'),
152
222
            ('Makefile', 'makefile-id'),
154
224
            ('src', 'src-id'),
155
225
            ('src/bye.c', 'bye-id'),
156
226
            ('src/hello.c', 'hello-id'),
 
227
            ('src/sub', 'sub-id'),
 
228
            ('src/sub/a', 'a-id'),
157
229
            ], [(path, ie.file_id) for path, ie in inv.iter_entries()])
158
230
 
 
231
        # Test a subdirectory
 
232
        self.assertEqual([
 
233
            ('bye.c', 'bye-id'),
 
234
            ('hello.c', 'hello-id'),
 
235
            ('sub', 'sub-id'),
 
236
            ('sub/a', 'a-id'),
 
237
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
 
238
            from_dir='src-id')])
 
239
 
 
240
        # Test not recursing at the root level
 
241
        self.assertEqual([
 
242
            ('', 'tree-root'),
 
243
            ('Makefile', 'makefile-id'),
 
244
            ('doc', 'doc-id'),
 
245
            ('src', 'src-id'),
 
246
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
 
247
            recursive=False)])
 
248
 
 
249
        # Test not recursing at a subdirectory level
 
250
        self.assertEqual([
 
251
            ('bye.c', 'bye-id'),
 
252
            ('hello.c', 'hello-id'),
 
253
            ('sub', 'sub-id'),
 
254
            ], [(path, ie.file_id) for path, ie in inv.iter_entries(
 
255
            from_dir='src-id', recursive=False)])
 
256
 
 
257
    def test_iter_just_entries(self):
 
258
        inv = self.make_inventory('tree-root')
 
259
        for args in [('src', 'directory', 'src-id'),
 
260
                     ('doc', 'directory', 'doc-id'),
 
261
                     ('src/hello.c', 'file', 'hello-id'),
 
262
                     ('src/bye.c', 'file', 'bye-id'),
 
263
                     ('Makefile', 'file', 'makefile-id')]:
 
264
            inv.add_path(*args)
 
265
        self.assertEqual([
 
266
            'bye-id',
 
267
            'doc-id',
 
268
            'hello-id',
 
269
            'makefile-id',
 
270
            'src-id',
 
271
            'tree-root',
 
272
            ], sorted([ie.file_id for ie in inv.iter_just_entries()]))
 
273
 
159
274
    def test_iter_entries_by_dir(self):
160
 
        inv = self.make_inventory('tree-root')
161
 
        for args in [('src', 'directory', 'src-id'),
162
 
                     ('doc', 'directory', 'doc-id'),
163
 
                     ('src/hello.c', 'file', 'hello-id'),
164
 
                     ('src/bye.c', 'file', 'bye-id'),
165
 
                     ('zz', 'file', 'zz-id'),
166
 
                     ('src/sub/', 'directory', 'sub-id'),
167
 
                     ('src/zz.c', 'file', 'zzc-id'),
168
 
                     ('src/sub/a', 'file', 'a-id'),
169
 
                     ('Makefile', 'file', 'makefile-id')]:
170
 
            inv.add_path(*args)
 
275
        inv = self. prepare_inv_with_nested_dirs()
171
276
        self.assertEqual([
172
277
            ('', 'tree-root'),
173
278
            ('Makefile', 'makefile-id'),
231
336
            ('src/bye.c', 'bye-id'),
232
337
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
233
338
                specific_file_ids=('bye-id',), yield_parents=True)])
234
 
 
235
 
    def test_add_recursive(self):
236
 
        parent = InventoryDirectory('src-id', 'src', 'tree-root')
237
 
        child = InventoryFile('hello-id', 'hello.c', 'src-id')
238
 
        parent.children[child.file_id] = child
239
 
        inv = self.make_inventory('tree-root')
240
 
        inv.add(parent)
241
 
        self.assertEqual('src/hello.c', inv.id2path('hello-id'))
 
339
 
 
340
 
 
341
class TestInventoryFiltering(TestInventory):
 
342
 
 
343
    def test_inv_filter_empty(self):
 
344
        inv = self.prepare_inv_with_nested_dirs()
 
345
        new_inv = inv.filter([])
 
346
        self.assertEqual([
 
347
            ('', 'tree-root'),
 
348
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
 
349
    
 
350
    def test_inv_filter_files(self):
 
351
        inv = self.prepare_inv_with_nested_dirs()
 
352
        new_inv = inv.filter(['zz-id', 'hello-id', 'a-id'])
 
353
        self.assertEqual([
 
354
            ('', 'tree-root'),
 
355
            ('src', 'src-id'),
 
356
            ('src/hello.c', 'hello-id'),
 
357
            ('src/sub', 'sub-id'),
 
358
            ('src/sub/a', 'a-id'),
 
359
            ('zz', 'zz-id'),
 
360
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
 
361
    
 
362
    def test_inv_filter_dirs(self):
 
363
        inv = self.prepare_inv_with_nested_dirs()
 
364
        new_inv = inv.filter(['doc-id', 'sub-id'])
 
365
        self.assertEqual([
 
366
            ('', 'tree-root'),
 
367
            ('doc', 'doc-id'),
 
368
            ('src', 'src-id'),
 
369
            ('src/sub', 'sub-id'),
 
370
            ('src/sub/a', 'a-id'),
 
371
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
 
372
 
 
373
    def test_inv_filter_files_and_dirs(self):
 
374
        inv = self.prepare_inv_with_nested_dirs()
 
375
        new_inv = inv.filter(['makefile-id', 'src-id'])
 
376
        self.assertEqual([
 
377
            ('', 'tree-root'),
 
378
            ('Makefile', 'makefile-id'),
 
379
            ('src', 'src-id'),
 
380
            ('src/bye.c', 'bye-id'),
 
381
            ('src/hello.c', 'hello-id'),
 
382
            ('src/sub', 'sub-id'),
 
383
            ('src/sub/a', 'a-id'),
 
384
            ('src/zz.c', 'zzc-id'),
 
385
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])