~abentley/bzr/bzr-1.14

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_repository_chk/test_supported.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-07 22:23:23 UTC
  • mfrom: (4246.1.3 bzr.1.14)
  • Revision ID: pqm@pqm.ubuntu.com-20090407222323-wj7suiz9p413eflm
(tanner) land brisbane-core in safest possible way (Vincent Ladeuil,
        Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Tests for repositories that support CHK indices."""
 
18
 
 
19
from bzrlib import osutils
 
20
from bzrlib.versionedfile import VersionedFiles
 
21
from bzrlib.tests.per_repository_chk import TestCaseWithRepositoryCHK
 
22
 
 
23
 
 
24
class TestCHKSupport(TestCaseWithRepositoryCHK):
 
25
 
 
26
    def test_chk_bytes_attribute_is_VersionedFiles(self):
 
27
        repo = self.make_repository('.')
 
28
        self.assertIsInstance(repo.chk_bytes, VersionedFiles)
 
29
 
 
30
    def test_add_bytes_to_chk_bytes_store(self):
 
31
        repo = self.make_repository('.')
 
32
        repo.lock_write()
 
33
        try:
 
34
            repo.start_write_group()
 
35
            try:
 
36
                sha1, len, _ = repo.chk_bytes.add_lines((None,),
 
37
                    None, ["foo\n", "bar\n"], random_id=True)
 
38
                self.assertEqual('4e48e2c9a3d2ca8a708cb0cc545700544efb5021',
 
39
                    sha1)
 
40
                self.assertEqual(
 
41
                    set([('sha1:4e48e2c9a3d2ca8a708cb0cc545700544efb5021',)]),
 
42
                    repo.chk_bytes.keys())
 
43
            except:
 
44
                repo.abort_write_group()
 
45
                raise
 
46
            else:
 
47
                repo.commit_write_group()
 
48
        finally:
 
49
            repo.unlock()
 
50
        # And after an unlock/lock pair
 
51
        repo.lock_read()
 
52
        try:
 
53
            self.assertEqual(
 
54
                set([('sha1:4e48e2c9a3d2ca8a708cb0cc545700544efb5021',)]),
 
55
                repo.chk_bytes.keys())
 
56
        finally:
 
57
            repo.unlock()
 
58
        # and reopening
 
59
        repo = repo.bzrdir.open_repository()
 
60
        repo.lock_read()
 
61
        try:
 
62
            self.assertEqual(
 
63
                set([('sha1:4e48e2c9a3d2ca8a708cb0cc545700544efb5021',)]),
 
64
                repo.chk_bytes.keys())
 
65
        finally:
 
66
            repo.unlock()
 
67
 
 
68
    def test_pack_preserves_chk_bytes_store(self):
 
69
        leaf_lines = ["chkleaf:\n", "0\n", "1\n", "0\n", "\n"]
 
70
        leaf_sha1 = osutils.sha_strings(leaf_lines)
 
71
        node_lines = ["chknode:\n", "0\n", "1\n", "1\n", "foo\n",
 
72
                      "\x00sha1:%s\n" % (leaf_sha1,)]
 
73
        node_sha1 = osutils.sha_strings(node_lines)
 
74
        expected_set = set([('sha1:' + leaf_sha1,), ('sha1:' + node_sha1,)])
 
75
        repo = self.make_repository('.')
 
76
        repo.lock_write()
 
77
        try:
 
78
            repo.start_write_group()
 
79
            try:
 
80
                # Internal node pointing at a leaf.
 
81
                repo.chk_bytes.add_lines((None,), None, node_lines, random_id=True)
 
82
            except:
 
83
                repo.abort_write_group()
 
84
                raise
 
85
            else:
 
86
                repo.commit_write_group()
 
87
            repo.start_write_group()
 
88
            try:
 
89
                # Leaf in a separate pack.
 
90
                repo.chk_bytes.add_lines((None,), None, leaf_lines, random_id=True)
 
91
            except:
 
92
                repo.abort_write_group()
 
93
                raise
 
94
            else:
 
95
                repo.commit_write_group()
 
96
            repo.pack()
 
97
            self.assertEqual(expected_set, repo.chk_bytes.keys())
 
98
        finally:
 
99
            repo.unlock()
 
100
        # and reopening
 
101
        repo = repo.bzrdir.open_repository()
 
102
        repo.lock_read()
 
103
        try:
 
104
            self.assertEqual(expected_set, repo.chk_bytes.keys())
 
105
        finally:
 
106
            repo.unlock()