~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Copyright (C) 2005 by Canonical Development Ltd

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Test Store implementation
"""
from StringIO import StringIO
import os

from bzrlib.store import copy_all
from bzrlib.transport.local import LocalTransport
from bzrlib.transport import NoSuchFile
from bzrlib.store.compressed_text import CompressedTextStore
from bzrlib.selftest import TestCaseInTempDir
from bzrlib.errors import BzrError, UnlistableStore


def fill_store(store):
    store.add(StringIO('hello'), 'a')
    store.add(StringIO('other'), 'b')
    store.add(StringIO('something'), 'c')
    store.add(StringIO('goodbye'), '123123')

def check_equals(tester, store, files, values, permit_failure=False):
    files = store.get(files, permit_failure=permit_failure)
    count = 0
    for f, v in zip(files, values):
        count += 1
        if v is None:
            tester.assert_(f is None)
        else:
            tester.assertEquals(f.read(), v)
    tester.assertEquals(count, len(values))
    # We need to check to make sure there are no more
    # files to be returned, I'm using a cheezy way
    # Convert to a list, and there shouldn't be any left
    tester.assertEquals(len(list(files)), 0)

def test_multiple_add(tester, store):
    fill_store(store)
    tester.assertRaises(BzrError, store.add, StringIO('goodbye'), '123123')

def test_get(tester, store):
    fill_store(store)

    check_equals(tester, store, ['a'], ['hello'])
    check_equals(tester, store, ['b', 'c'], ['other', 'something'])

    # Make sure that requesting a non-existing file fails
    tester.assertRaises(NoSuchFile, check_equals, tester, store,
            ['d'], [None])
    tester.assertRaises(NoSuchFile, check_equals, tester, store,
            ['a', 'd'], ['hello', None])
    tester.assertRaises(NoSuchFile, check_equals, tester, store,
            ['d', 'a'], [None, 'hello'])
    tester.assertRaises(NoSuchFile, check_equals, tester, store,
            ['d', 'd', 'd'], [None, None, None])
    tester.assertRaises(NoSuchFile, check_equals, tester, store,
            ['a', 'd', 'b'], ['hello', None, 'other'])

def test_ignore_get(tester, store):
    fill_store(store)

    files = store.get(['d'], permit_failure=True)
    files = list(files)
    tester.assertEquals(len(files), 1)
    tester.assert_(files[0] is None)

    check_equals(tester, store, ['a', 'd'], ['hello', None],
            permit_failure=True)
    check_equals(tester, store, ['d', 'a'], [None, 'hello'],
            permit_failure=True)
    check_equals(tester, store, ['d', 'd'], [None, None],
            permit_failure=True)
    check_equals(tester, store, ['a', 'd', 'b'], ['hello', None, 'other'],
            permit_failure=True)
    check_equals(tester, store, ['a', 'd', 'b'], ['hello', None, 'other'],
            permit_failure=True)
    check_equals(tester, store, ['b', 'd', 'c'], ['other', None, 'something'],
            permit_failure=True)

def get_compressed_store():
    t = LocalTransport('.')
    return CompressedTextStore(t)

class TestCompressedTextStore(TestCaseInTempDir):
    def test_multiple_add(self):
        """Multiple add with same ID should raise a BzrError"""
        store = get_compressed_store()
        test_multiple_add(self, store)

    def test_get(self):
        store = get_compressed_store()
        test_get(self, store)

    def test_ignore_get(self):
        store = get_compressed_store()
        test_ignore_get(self, store)


    def test_copy_all(self):
        """Test copying"""
        os.mkdir('a')
        store_a = CompressedTextStore('a')
        store_a.add('foo', '1')
        os.mkdir('b')
        store_b = CompressedTextStore('b')
        copy_all(store_a, store_b)
        self.assertEqual(store_a['1'].read(), 'foo')
        self.assertEqual(store_b['1'].read(), 'foo')
        # TODO: Switch the exception form UnlistableStore to
        #       or make Stores throw UnlistableStore if their
        #       Transport doesn't support listing
        # store_c = RemoteStore('http://example.com/')
        # self.assertRaises(UnlistableStore, copy_all, store_c, store_b)