~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Matt Nordhoff
  • Date: 2009-04-04 02:50:01 UTC
  • mfrom: (4253 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4256.
  • Revision ID: mnordhoff@mattnordhoff.com-20090404025001-z1403k0tatmc8l91
Merge bzr.dev, fixing conflicts.

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
 
34
34
 
35
35
from bzrlib.tests import (
36
36
        TestCase,
37
 
        multiply_tests_from_modules,
38
37
        )
39
38
 
40
39
 
41
 
class TestInventoryBasics(TestCase):
42
 
    # Most of these were moved the rather old bzrlib.tests.test_inv module
43
 
    
 
40
class TestInventory(TestCase):
 
41
 
44
42
    def make_inventory(self, root_id):
45
43
        return self.inventory_class(root_id=root_id)
46
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
 
47
62
    def test_creation_from_root_id(self):
48
63
        # iff a root id is passed to the constructor, a root directory is made
49
64
        inv = self.make_inventory(root_id='tree-root')
89
104
        self.assertEquals('someroot', inv2.root.file_id)
90
105
        self.assertEquals('therev', inv2.root.revision)
91
106
 
92
 
    def test_is_root(self):
93
 
        """Ensure our root-checking code is accurate."""
94
 
        inv = self.make_inventory('TREE_ROOT')
95
 
        self.assertTrue(inv.is_root('TREE_ROOT'))
96
 
        self.assertFalse(inv.is_root('booga'))
97
 
        inv.root.file_id = 'booga'
98
 
        self.assertFalse(inv.is_root('TREE_ROOT'))
99
 
        self.assertTrue(inv.is_root('booga'))
100
 
        # works properly even if no root is set
101
 
        inv.root = None
102
 
        self.assertFalse(inv.is_root('TREE_ROOT'))
103
 
        self.assertFalse(inv.is_root('booga'))
104
 
 
105
107
    def test_create_tree_reference(self):
106
108
        inv = self.make_inventory('tree-root-123')
107
109
        inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
117
119
        else:
118
120
            self.fail('BzrError not raised')
119
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
 
120
185
    def test_ids(self):
121
186
        """Test detection of files within selected directories."""
122
187
        inv = self.make_inventory(ROOT_ID)
158
223
            ], [(path, ie.file_id) for path, ie in inv.iter_entries()])
159
224
 
160
225
    def test_iter_entries_by_dir(self):
161
 
        inv = self.make_inventory('tree-root')
162
 
        for args in [('src', 'directory', 'src-id'),
163
 
                     ('doc', 'directory', 'doc-id'),
164
 
                     ('src/hello.c', 'file', 'hello-id'),
165
 
                     ('src/bye.c', 'file', 'bye-id'),
166
 
                     ('zz', 'file', 'zz-id'),
167
 
                     ('src/sub/', 'directory', 'sub-id'),
168
 
                     ('src/zz.c', 'file', 'zzc-id'),
169
 
                     ('src/sub/a', 'file', 'a-id'),
170
 
                     ('Makefile', 'file', 'makefile-id')]:
171
 
            inv.add_path(*args)
 
226
        inv = self. prepare_inv_with_nested_dirs()
172
227
        self.assertEqual([
173
228
            ('', 'tree-root'),
174
229
            ('Makefile', 'makefile-id'),
232
287
            ('src/bye.c', 'bye-id'),
233
288
            ], [(path, ie.file_id) for path, ie in inv.iter_entries_by_dir(
234
289
                specific_file_ids=('bye-id',), yield_parents=True)])
235
 
 
236
 
    def test_add_recursive(self):
237
 
        parent = InventoryDirectory('src-id', 'src', 'tree-root')
238
 
        child = InventoryFile('hello-id', 'hello.c', 'src-id')
239
 
        parent.children[child.file_id] = child
240
 
        inv = self.make_inventory('tree-root')
241
 
        inv.add(parent)
242
 
        self.assertEqual('src/hello.c', inv.id2path('hello-id'))
 
290
 
 
291
 
 
292
class TestInventoryFiltering(TestInventory):
 
293
 
 
294
    def test_inv_filter_empty(self):
 
295
        inv = self.prepare_inv_with_nested_dirs()
 
296
        new_inv = inv.filter([])
 
297
        self.assertEqual([
 
298
            ('', 'tree-root'),
 
299
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
 
300
    
 
301
    def test_inv_filter_files(self):
 
302
        inv = self.prepare_inv_with_nested_dirs()
 
303
        new_inv = inv.filter(['zz-id', 'hello-id', 'a-id'])
 
304
        self.assertEqual([
 
305
            ('', 'tree-root'),
 
306
            ('src', 'src-id'),
 
307
            ('src/hello.c', 'hello-id'),
 
308
            ('src/sub', 'sub-id'),
 
309
            ('src/sub/a', 'a-id'),
 
310
            ('zz', 'zz-id'),
 
311
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
 
312
    
 
313
    def test_inv_filter_dirs(self):
 
314
        inv = self.prepare_inv_with_nested_dirs()
 
315
        new_inv = inv.filter(['doc-id', 'sub-id'])
 
316
        self.assertEqual([
 
317
            ('', 'tree-root'),
 
318
            ('doc', 'doc-id'),
 
319
            ('src', 'src-id'),
 
320
            ('src/sub', 'sub-id'),
 
321
            ('src/sub/a', 'a-id'),
 
322
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])
 
323
 
 
324
    def test_inv_filter_files_and_dirs(self):
 
325
        inv = self.prepare_inv_with_nested_dirs()
 
326
        new_inv = inv.filter(['makefile-id', 'src-id'])
 
327
        self.assertEqual([
 
328
            ('', 'tree-root'),
 
329
            ('Makefile', 'makefile-id'),
 
330
            ('src', 'src-id'),
 
331
            ('src/bye.c', 'bye-id'),
 
332
            ('src/hello.c', 'hello-id'),
 
333
            ('src/sub', 'sub-id'),
 
334
            ('src/sub/a', 'a-id'),
 
335
            ('src/zz.c', 'zzc-id'),
 
336
            ], [(path, ie.file_id) for path, ie in new_inv.iter_entries()])