~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_inv.py

merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
24
24
    repository,
25
25
    revision,
26
26
    tests,
27
 
    )
28
 
from bzrlib.inventory import (CHKInventory, Inventory, ROOT_ID, InventoryFile,
29
 
    InventoryDirectory, InventoryEntry, TreeReference)
 
27
    workingtree,
 
28
    )
 
29
from bzrlib.inventory import (
 
30
    CHKInventory,
 
31
    Inventory,
 
32
    ROOT_ID,
 
33
    InventoryFile,
 
34
    InventoryDirectory,
 
35
    InventoryEntry,
 
36
    TreeReference,
 
37
    )
30
38
from bzrlib.tests import (
31
39
    TestCase,
32
40
    TestCaseWithTransport,
33
 
    condition_isinstance,
34
 
    multiply_tests,
35
 
    split_suite_by_condition,
36
41
    )
37
 
from bzrlib.tests.per_workingtree import workingtree_formats
38
 
 
39
 
 
40
 
def load_tests(standard_tests, module, loader):
41
 
    """Parameterise some inventory tests."""
42
 
    to_adapt, result = split_suite_by_condition(standard_tests,
43
 
        condition_isinstance(TestDeltaApplication))
 
42
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
43
 
 
44
 
 
45
load_tests = load_tests_apply_scenarios
 
46
 
 
47
 
 
48
def delta_application_scenarios():
44
49
    scenarios = [
45
50
        ('Inventory', {'apply_delta':apply_inventory_Inventory}),
46
51
        ]
51
56
    # just creating trees.
52
57
    formats = set()
53
58
    for _, format in repository.format_registry.iteritems():
54
 
        scenarios.append((str(format.__name__), {
55
 
            'apply_delta':apply_inventory_Repository_add_inventory_by_delta,
56
 
            'format':format}))
57
 
    for format in workingtree_formats():
 
59
        if format.supports_full_versioned_files:
 
60
            scenarios.append((str(format.__name__), {
 
61
                'apply_delta':apply_inventory_Repository_add_inventory_by_delta,
 
62
                'format':format}))
 
63
    for format in workingtree.format_registry._get_all():
 
64
        repo_fmt = format._matchingbzrdir.repository_format
 
65
        if not repo_fmt.supports_full_versioned_files:
 
66
            continue
58
67
        scenarios.append(
59
68
            (str(format.__class__.__name__) + ".update_basis_by_delta", {
60
69
            'apply_delta':apply_inventory_WT_basis,
63
72
            (str(format.__class__.__name__) + ".apply_inventory_delta", {
64
73
            'apply_delta':apply_inventory_WT,
65
74
            'format':format}))
66
 
    return multiply_tests(to_adapt, scenarios, result)
 
75
    return scenarios
67
76
 
68
77
 
69
78
def create_texts_for_inv(repo, inv):
73
82
        else:
74
83
            lines = []
75
84
        repo.texts.add_lines((ie.file_id, ie.revision), [], lines)
76
 
    
 
85
 
 
86
 
77
87
def apply_inventory_Inventory(self, basis, delta):
78
88
    """Apply delta to basis and return the result.
79
89
    
329
339
 
330
340
 
331
341
class TestDeltaApplication(TestCaseWithTransport):
 
342
 
 
343
    scenarios = delta_application_scenarios()
332
344
 
333
345
    def get_empty_inventory(self, reference_inv=None):
334
346
        """Get an empty inventory.
589
601
        self.assertFalse(inv.is_root('TREE_ROOT'))
590
602
        self.assertFalse(inv.is_root('booga'))
591
603
 
 
604
    def test_entries_for_empty_inventory(self):
 
605
        """Test that entries() will not fail for an empty inventory"""
 
606
        inv = Inventory(root_id=None)
 
607
        self.assertEqual([], inv.entries())
 
608
 
592
609
 
593
610
class TestInventoryEntry(TestCase):
594
611
 
1206
1223
        self.assertEqual(('tree\xce\xa9name', 'tree-root-id', 'tree-rev-id'),
1207
1224
                         inv._bytes_to_utf8name_key(bytes))
1208
1225
 
 
1226
    def make_basic_utf8_inventory(self):
 
1227
        inv = Inventory()
 
1228
        inv.revision_id = "revid"
 
1229
        inv.root.revision = "rootrev"
 
1230
        root_id = inv.root.file_id
 
1231
        inv.add(InventoryFile("fileid", u'f\xefle', root_id))
 
1232
        inv["fileid"].revision = "filerev"
 
1233
        inv["fileid"].text_sha1 = "ffff"
 
1234
        inv["fileid"].text_size = 0
 
1235
        inv.add(InventoryDirectory("dirid", u'dir-\N{EURO SIGN}', root_id))
 
1236
        inv.add(InventoryFile("childid", u'ch\xefld', "dirid"))
 
1237
        inv["childid"].revision = "filerev"
 
1238
        inv["childid"].text_sha1 = "ffff"
 
1239
        inv["childid"].text_size = 0
 
1240
        chk_bytes = self.get_chk_bytes()
 
1241
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
1242
        bytes = ''.join(chk_inv.to_lines())
 
1243
        return CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
 
1244
 
 
1245
    def test__preload_handles_utf8(self):
 
1246
        new_inv = self.make_basic_utf8_inventory()
 
1247
        self.assertEqual({}, new_inv._fileid_to_entry_cache)
 
1248
        self.assertFalse(new_inv._fully_cached)
 
1249
        new_inv._preload_cache()
 
1250
        self.assertEqual(
 
1251
            sorted([new_inv.root_id, "fileid", "dirid", "childid"]),
 
1252
            sorted(new_inv._fileid_to_entry_cache.keys()))
 
1253
        ie_root = new_inv._fileid_to_entry_cache[new_inv.root_id]
 
1254
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
 
1255
                         sorted(ie_root._children.keys()))
 
1256
        ie_dir = new_inv._fileid_to_entry_cache['dirid']
 
1257
        self.assertEqual([u'ch\xefld'], sorted(ie_dir._children.keys()))
 
1258
 
 
1259
    def test__preload_populates_cache(self):
 
1260
        inv = Inventory()
 
1261
        inv.revision_id = "revid"
 
1262
        inv.root.revision = "rootrev"
 
1263
        root_id = inv.root.file_id
 
1264
        inv.add(InventoryFile("fileid", "file", root_id))
 
1265
        inv["fileid"].revision = "filerev"
 
1266
        inv["fileid"].executable = True
 
1267
        inv["fileid"].text_sha1 = "ffff"
 
1268
        inv["fileid"].text_size = 1
 
1269
        inv.add(InventoryDirectory("dirid", "dir", root_id))
 
1270
        inv.add(InventoryFile("childid", "child", "dirid"))
 
1271
        inv["childid"].revision = "filerev"
 
1272
        inv["childid"].executable = False
 
1273
        inv["childid"].text_sha1 = "dddd"
 
1274
        inv["childid"].text_size = 1
 
1275
        chk_bytes = self.get_chk_bytes()
 
1276
        chk_inv = CHKInventory.from_inventory(chk_bytes, inv)
 
1277
        bytes = ''.join(chk_inv.to_lines())
 
1278
        new_inv = CHKInventory.deserialise(chk_bytes, bytes, ("revid",))
 
1279
        self.assertEqual({}, new_inv._fileid_to_entry_cache)
 
1280
        self.assertFalse(new_inv._fully_cached)
 
1281
        new_inv._preload_cache()
 
1282
        self.assertEqual(
 
1283
            sorted([root_id, "fileid", "dirid", "childid"]),
 
1284
            sorted(new_inv._fileid_to_entry_cache.keys()))
 
1285
        self.assertTrue(new_inv._fully_cached)
 
1286
        ie_root = new_inv._fileid_to_entry_cache[root_id]
 
1287
        self.assertEqual(['dir', 'file'], sorted(ie_root._children.keys()))
 
1288
        ie_dir = new_inv._fileid_to_entry_cache['dirid']
 
1289
        self.assertEqual(['child'], sorted(ie_dir._children.keys()))
 
1290
 
 
1291
    def test__preload_handles_partially_evaluated_inventory(self):
 
1292
        new_inv = self.make_basic_utf8_inventory()
 
1293
        ie = new_inv[new_inv.root_id]
 
1294
        self.assertIs(None, ie._children)
 
1295
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
 
1296
                         sorted(ie.children.keys()))
 
1297
        # Accessing .children loads _children
 
1298
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
 
1299
                         sorted(ie._children.keys()))
 
1300
        new_inv._preload_cache()
 
1301
        # No change
 
1302
        self.assertEqual([u'dir-\N{EURO SIGN}', u'f\xefle'],
 
1303
                         sorted(ie._children.keys()))
 
1304
        ie_dir = new_inv["dirid"]
 
1305
        self.assertEqual([u'ch\xefld'],
 
1306
                         sorted(ie_dir._children.keys()))
 
1307
 
1209
1308
 
1210
1309
class TestCHKInventoryExpand(tests.TestCaseWithMemoryTransport):
1211
1310