258
249
return repo.get_inventory('result')
261
class TestInventoryUpdates(TestCase):
263
def test_creation_from_root_id(self):
264
# iff a root id is passed to the constructor, a root directory is made
265
inv = inventory.Inventory(root_id='tree-root')
266
self.assertNotEqual(None, inv.root)
267
self.assertEqual('tree-root', inv.root.file_id)
269
def test_add_path_of_root(self):
270
# if no root id is given at creation time, there is no root directory
271
inv = inventory.Inventory(root_id=None)
272
self.assertIs(None, inv.root)
273
# add a root entry by adding its path
274
ie = inv.add_path("", "directory", "my-root")
275
ie.revision = 'test-rev'
276
self.assertEqual("my-root", ie.file_id)
277
self.assertIs(ie, inv.root)
279
def test_add_path(self):
280
inv = inventory.Inventory(root_id='tree_root')
281
ie = inv.add_path('hello', 'file', 'hello-id')
282
self.assertEqual('hello-id', ie.file_id)
283
self.assertEqual('file', ie.kind)
286
"""Make sure copy() works and creates a deep copy."""
287
inv = inventory.Inventory(root_id='some-tree-root')
288
ie = inv.add_path('hello', 'file', 'hello-id')
290
inv.root.file_id = 'some-new-root'
292
self.assertEqual('some-tree-root', inv2.root.file_id)
293
self.assertEqual('hello', inv2['hello-id'].name)
295
def test_copy_empty(self):
296
"""Make sure an empty inventory can be copied."""
297
inv = inventory.Inventory(root_id=None)
299
self.assertIs(None, inv2.root)
301
def test_copy_copies_root_revision(self):
302
"""Make sure the revision of the root gets copied."""
303
inv = inventory.Inventory(root_id='someroot')
304
inv.root.revision = 'therev'
306
self.assertEquals('someroot', inv2.root.file_id)
307
self.assertEquals('therev', inv2.root.revision)
309
def test_create_tree_reference(self):
310
inv = inventory.Inventory('tree-root-123')
311
inv.add(TreeReference('nested-id', 'nested', parent_id='tree-root-123',
312
revision='rev', reference_revision='rev2'))
314
def test_error_encoding(self):
315
inv = inventory.Inventory('tree-root')
316
inv.add(InventoryFile('a-id', u'\u1234', 'tree-root'))
317
e = self.assertRaises(errors.InconsistentDelta, inv.add,
318
InventoryFile('b-id', u'\u1234', 'tree-root'))
319
self.assertContainsRe(str(e), r'\\u1234')
321
def test_add_recursive(self):
322
parent = InventoryDirectory('src-id', 'src', 'tree-root')
323
child = InventoryFile('hello-id', 'hello.c', 'src-id')
324
parent.children[child.file_id] = child
325
inv = inventory.Inventory('tree-root')
327
self.assertEqual('src/hello.c', inv.id2path('hello-id'))
331
252
class TestDeltaApplication(TestCaseWithTransport):
333
254
def get_empty_inventory(self, reference_inv=None):
1210
1123
self.assertIsInstance(ie2.name, unicode)
1211
1124
self.assertEqual(('tree\xce\xa9name', 'tree-root-id', 'tree-rev-id'),
1212
1125
inv._bytes_to_utf8name_key(bytes))
1215
class TestCHKInventoryExpand(tests.TestCaseWithMemoryTransport):
1217
def get_chk_bytes(self):
1218
factory = groupcompress.make_pack_factory(True, True, 1)
1219
trans = self.get_transport('')
1220
return factory(trans)
1222
def make_dir(self, inv, name, parent_id):
1223
inv.add(inv.make_entry('directory', name, parent_id, name + '-id'))
1225
def make_file(self, inv, name, parent_id, content='content\n'):
1226
ie = inv.make_entry('file', name, parent_id, name + '-id')
1227
ie.text_sha1 = osutils.sha_string(content)
1228
ie.text_size = len(content)
1231
def make_simple_inventory(self):
1232
inv = Inventory('TREE_ROOT')
1233
inv.revision_id = "revid"
1234
inv.root.revision = "rootrev"
1237
# sub-file1 sub-file1-id
1238
# sub-file2 sub-file2-id
1239
# sub-dir1/ sub-dir1-id
1240
# subsub-file1 subsub-file1-id
1242
# sub2-file1 sub2-file1-id
1244
self.make_dir(inv, 'dir1', 'TREE_ROOT')
1245
self.make_dir(inv, 'dir2', 'TREE_ROOT')
1246
self.make_dir(inv, 'sub-dir1', 'dir1-id')
1247
self.make_file(inv, 'top', 'TREE_ROOT')
1248
self.make_file(inv, 'sub-file1', 'dir1-id')
1249
self.make_file(inv, 'sub-file2', 'dir1-id')
1250
self.make_file(inv, 'subsub-file1', 'sub-dir1-id')
1251
self.make_file(inv, 'sub2-file1', 'dir2-id')
1252
chk_bytes = self.get_chk_bytes()
1253
# use a small maximum_size to force internal paging structures
1254
chk_inv = CHKInventory.from_inventory(chk_bytes, inv,
1256
search_key_name='hash-255-way')
1257
bytes = ''.join(chk_inv.to_lines())
1258
return CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
1260
def assert_Getitems(self, expected_fileids, inv, file_ids):
1261
self.assertEqual(sorted(expected_fileids),
1262
sorted([ie.file_id for ie in inv._getitems(file_ids)]))
1264
def assertExpand(self, all_ids, inv, file_ids):
1266
val_children) = inv._expand_fileids_to_parents_and_children(file_ids)
1267
self.assertEqual(set(all_ids), val_all_ids)
1268
entries = inv._getitems(val_all_ids)
1269
expected_children = {}
1270
for entry in entries:
1271
s = expected_children.setdefault(entry.parent_id, [])
1272
s.append(entry.file_id)
1273
val_children = dict((k, sorted(v)) for k, v
1274
in val_children.iteritems())
1275
expected_children = dict((k, sorted(v)) for k, v
1276
in expected_children.iteritems())
1277
self.assertEqual(expected_children, val_children)
1279
def test_make_simple_inventory(self):
1280
inv = self.make_simple_inventory()
1282
for path, entry in inv.iter_entries_by_dir():
1283
layout.append((path, entry.file_id))
1286
('dir1', 'dir1-id'),
1287
('dir2', 'dir2-id'),
1289
('dir1/sub-dir1', 'sub-dir1-id'),
1290
('dir1/sub-file1', 'sub-file1-id'),
1291
('dir1/sub-file2', 'sub-file2-id'),
1292
('dir1/sub-dir1/subsub-file1', 'subsub-file1-id'),
1293
('dir2/sub2-file1', 'sub2-file1-id'),
1296
def test__getitems(self):
1297
inv = self.make_simple_inventory()
1299
self.assert_Getitems(['dir1-id'], inv, ['dir1-id'])
1300
self.assertTrue('dir1-id' in inv._fileid_to_entry_cache)
1301
self.assertFalse('sub-file2-id' in inv._fileid_to_entry_cache)
1303
self.assert_Getitems(['dir1-id'], inv, ['dir1-id'])
1305
self.assert_Getitems(['dir1-id', 'sub-file2-id'], inv,
1306
['dir1-id', 'sub-file2-id'])
1307
self.assertTrue('dir1-id' in inv._fileid_to_entry_cache)
1308
self.assertTrue('sub-file2-id' in inv._fileid_to_entry_cache)
1310
def test_single_file(self):
1311
inv = self.make_simple_inventory()
1312
self.assertExpand(['TREE_ROOT', 'top-id'], inv, ['top-id'])
1314
def test_get_all_parents(self):
1315
inv = self.make_simple_inventory()
1316
self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id',
1318
], inv, ['subsub-file1-id'])
1320
def test_get_children(self):
1321
inv = self.make_simple_inventory()
1322
self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id',
1323
'sub-file1-id', 'sub-file2-id', 'subsub-file1-id',
1324
], inv, ['dir1-id'])
1326
def test_from_root(self):
1327
inv = self.make_simple_inventory()
1328
self.assertExpand(['TREE_ROOT', 'dir1-id', 'dir2-id', 'sub-dir1-id',
1329
'sub-file1-id', 'sub-file2-id', 'sub2-file1-id',
1330
'subsub-file1-id', 'top-id'], inv, ['TREE_ROOT'])
1332
def test_top_level_file(self):
1333
inv = self.make_simple_inventory()
1334
self.assertExpand(['TREE_ROOT', 'top-id'], inv, ['top-id'])
1336
def test_subsub_file(self):
1337
inv = self.make_simple_inventory()
1338
self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id',
1339
'subsub-file1-id'], inv, ['subsub-file1-id'])
1341
def test_sub_and_root(self):
1342
inv = self.make_simple_inventory()
1343
self.assertExpand(['TREE_ROOT', 'dir1-id', 'sub-dir1-id', 'top-id',
1344
'subsub-file1-id'], inv, ['top-id', 'subsub-file1-id'])