/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_pack.py

  • Committer: Jelmer Vernooij
  • Date: 2017-08-27 13:57:26 UTC
  • mto: This revision was merged to the branch mainline in revision 6773.
  • Revision ID: jelmer@jelmer.uk-20170827135726-o6k0a4j205zdh8k0
Fix some tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
 
1
# Copyright (C) 2007, 2009, 2011, 2012, 2016 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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
 
"""Tests for bzrlib.pack."""
18
 
 
19
 
 
20
 
from cStringIO import StringIO
21
 
 
22
 
from bzrlib import pack, errors, tests
 
17
"""Tests for breezy.pack."""
 
18
 
 
19
from .. import errors, tests
 
20
from ..bzr import (
 
21
    pack,
 
22
    )
 
23
from ..sixish import (
 
24
    BytesIO,
 
25
    )
23
26
 
24
27
 
25
28
class TestContainerSerialiser(tests.TestCase):
64
67
            errors.InvalidRecordError,
65
68
            serialiser.bytes_record, 'bytes', [('bad name',)])
66
69
 
 
70
    def test_bytes_record_header(self):
 
71
        serialiser = pack.ContainerSerialiser()
 
72
        record = serialiser.bytes_header(32, [('name1',), ('name2',)])
 
73
        self.assertEqual('B32\nname1\nname2\n\n', record)
 
74
 
67
75
 
68
76
class TestContainerWriter(tests.TestCase):
69
77
 
70
78
    def setUp(self):
71
 
        tests.TestCase.setUp(self)
72
 
        self.output = StringIO()
 
79
        super(TestContainerWriter, self).setUp()
 
80
        self.output = BytesIO()
73
81
        self.writer = pack.ContainerWriter(self.output.write)
74
82
 
75
83
    def assertOutput(self, expected_output):
126
134
    def test_add_bytes_record_one_name(self):
127
135
        """Add a bytes record with one name."""
128
136
        self.writer.begin()
 
137
 
129
138
        offset, length = self.writer.add_bytes_record(
130
139
            'abc', names=[('name1', )])
131
140
        self.assertEqual((42, 13), (offset, length))
133
142
            'Bazaar pack format 1 (introduced in 0.18)\n'
134
143
            'B3\nname1\n\nabc')
135
144
 
 
145
    def test_add_bytes_record_split_writes(self):
 
146
        """Write a large record which does multiple IOs"""
 
147
 
 
148
        writes = []
 
149
        real_write = self.writer.write_func
 
150
 
 
151
        def record_writes(bytes):
 
152
            writes.append(bytes)
 
153
            return real_write(bytes)
 
154
 
 
155
        self.writer.write_func = record_writes
 
156
        self.writer._JOIN_WRITES_THRESHOLD = 2
 
157
 
 
158
        self.writer.begin()
 
159
        offset, length = self.writer.add_bytes_record(
 
160
            'abcabc', names=[('name1', )])
 
161
        self.assertEqual((42, 16), (offset, length))
 
162
        self.assertOutput(
 
163
            'Bazaar pack format 1 (introduced in 0.18)\n'
 
164
            'B6\nname1\n\nabcabc')
 
165
 
 
166
        self.assertEqual([
 
167
            'Bazaar pack format 1 (introduced in 0.18)\n',
 
168
            'B6\nname1\n\n',
 
169
            'abcabc'],
 
170
            writes)
 
171
 
136
172
    def test_add_bytes_record_two_names(self):
137
173
        """Add a bytes record with two names."""
138
174
        self.writer.begin()
200
236
    """
201
237
 
202
238
    def get_reader_for(self, bytes):
203
 
        stream = StringIO(bytes)
 
239
        stream = BytesIO(bytes)
204
240
        reader = pack.ContainerReader(stream)
205
241
        return reader
206
242
 
207
243
    def test_construct(self):
208
244
        """Test constructing a ContainerReader.
209
245
 
210
 
        This uses None as the output stream to show that the constructor doesn't
211
 
        try to use the input stream.
 
246
        This uses None as the output stream to show that the constructor
 
247
        doesn't try to use the input stream.
212
248
        """
213
249
        reader = pack.ContainerReader(None)
214
250
 
232
268
            "Bazaar pack format 1 (introduced in 0.18)\n")
233
269
        iterator = reader.iter_records()
234
270
        self.assertRaises(
235
 
            errors.UnexpectedEndOfContainerError, iterator.next)
 
271
            errors.UnexpectedEndOfContainerError, next, iterator)
236
272
 
237
273
    def test_unknown_record_type(self):
238
274
        """Unknown record types cause UnknownRecordTypeError to be raised."""
240
276
            "Bazaar pack format 1 (introduced in 0.18)\nX")
241
277
        iterator = reader.iter_records()
242
278
        self.assertRaises(
243
 
            errors.UnknownRecordTypeError, iterator.next)
 
279
            errors.UnknownRecordTypeError, next, iterator)
244
280
 
245
281
    def test_container_with_one_unnamed_record(self):
246
282
        """Read a container with one Bytes record.
259
295
 
260
296
    def test_validate_empty_container(self):
261
297
        """validate does not raise an error for a container with no records."""
262
 
        reader = self.get_reader_for("Bazaar pack format 1 (introduced in 0.18)\nE")
 
298
        reader = self.get_reader_for(
 
299
            "Bazaar pack format 1 (introduced in 0.18)\nE")
263
300
        # No exception raised
264
301
        reader.validate()
265
302
 
339
376
    """
340
377
 
341
378
    def get_reader_for(self, bytes):
342
 
        stream = StringIO(bytes)
 
379
        stream = BytesIO(bytes)
343
380
        reader = pack.BytesRecordReader(stream)
344
381
        return reader
345
382
 
496
533
class TestMakeReadvReader(tests.TestCaseWithTransport):
497
534
 
498
535
    def test_read_skipping_records(self):
499
 
        pack_data = StringIO()
 
536
        pack_data = BytesIO()
500
537
        writer = pack.ContainerWriter(pack_data.write)
501
538
        writer.begin()
502
539
        memos = []
705
742
        parser.accept_bytes("6\n\nabcdef")
706
743
        self.assertEqual([([], 'abcdef')], parser.read_pending_records())
707
744
        self.assertEqual([], parser.read_pending_records())
708
 
 
709