~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_pack.py

Merge from bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
        output = StringIO()
55
55
        writer = pack.ContainerWriter(output.write)
56
56
        writer.begin()
57
 
        writer.add_bytes_record('abc', names=[])
 
57
        offset, length = writer.add_bytes_record('abc', names=[])
 
58
        self.assertEqual((42, 7), (offset, length))
58
59
        self.assertEqual('Bazaar pack format 1 (introduced in 0.18)\nB3\n\nabc',
59
60
                         output.getvalue())
60
61
 
63
64
        output = StringIO()
64
65
        writer = pack.ContainerWriter(output.write)
65
66
        writer.begin()
66
 
        writer.add_bytes_record('abc', names=['name1'])
 
67
        offset, length = writer.add_bytes_record('abc', names=['name1'])
 
68
        self.assertEqual((42, 13), (offset, length))
67
69
        self.assertEqual(
68
70
            'Bazaar pack format 1 (introduced in 0.18)\n'
69
71
            'B3\nname1\n\nabc',
74
76
        output = StringIO()
75
77
        writer = pack.ContainerWriter(output.write)
76
78
        writer.begin()
77
 
        writer.add_bytes_record('abc', names=['name1', 'name2'])
 
79
        offset, length = writer.add_bytes_record('abc', names=['name1', 'name2'])
 
80
        self.assertEqual((42, 19), (offset, length))
78
81
        self.assertEqual(
79
82
            'Bazaar pack format 1 (introduced in 0.18)\n'
80
83
            'B3\nname1\nname2\n\nabc',
81
84
            output.getvalue())
82
85
 
 
86
    def test_add_second_bytes_record_gets_higher_offset(self):
 
87
        output = StringIO()
 
88
        writer = pack.ContainerWriter(output.write)
 
89
        writer.begin()
 
90
        writer.add_bytes_record('abc', names=[])
 
91
        offset, length = writer.add_bytes_record('abc', names=[])
 
92
        self.assertEqual((49, 7), (offset, length))
 
93
        self.assertEqual(
 
94
            'Bazaar pack format 1 (introduced in 0.18)\n'
 
95
            'B3\n\nabc'
 
96
            'B3\n\nabc',
 
97
            output.getvalue())
 
98
 
83
99
    def test_add_bytes_record_invalid_name(self):
84
100
        """Adding a Bytes record with a name with whitespace in it raises
85
101
        InvalidRecordError.
375
391
        self.assertEqual('', get_bytes(99))
376
392
 
377
393
 
 
394
class TestMakeReadvReader(tests.TestCaseWithTransport):
 
395
 
 
396
    def test_read_skipping_records(self):
 
397
        pack_data = StringIO()
 
398
        writer = pack.ContainerWriter(pack_data.write)
 
399
        writer.begin()
 
400
        memos = []
 
401
        memos.append(writer.add_bytes_record('abc', names=[]))
 
402
        memos.append(writer.add_bytes_record('def', names=['name1']))
 
403
        memos.append(writer.add_bytes_record('ghi', names=['name2']))
 
404
        memos.append(writer.add_bytes_record('jkl', names=[]))
 
405
        writer.end()
 
406
        transport = self.get_transport()
 
407
        transport.put_bytes('mypack', pack_data.getvalue())
 
408
        requested_records = [memos[0], memos[2]]
 
409
        reader = pack.make_readv_reader(transport, 'mypack', requested_records)
 
410
        result = []
 
411
        for names, reader_func in reader.iter_records():
 
412
            result.append((names, reader_func(None)))
 
413
        self.assertEqual([([], 'abc'), (['name2'], 'ghi')], result)
 
414
 
 
415
 
 
416
class TestReadvFile(tests.TestCaseWithTransport):
 
417
    """Tests of the ReadVFile class.
 
418
 
 
419
    Error cases are deliberately undefined: this code adapts the underlying
 
420
    transport interface to a single 'streaming read' interface as 
 
421
    ContainerReader needs.
 
422
    """
 
423
 
 
424
    def test_read_bytes(self):
 
425
        """Test reading of both single bytes and all bytes in a hunk."""
 
426
        transport = self.get_transport()
 
427
        transport.put_bytes('sample', '0123456789')
 
428
        f = pack.ReadVFile(transport.readv('sample', [(0,1), (1,2), (4,1), (6,2)]))
 
429
        results = []
 
430
        results.append(f.read(1))
 
431
        results.append(f.read(2))
 
432
        results.append(f.read(1))
 
433
        results.append(f.read(1))
 
434
        results.append(f.read(1))
 
435
        self.assertEqual(['0', '12', '4', '6', '7'], results)
 
436
 
 
437
    def test_readline(self):
 
438
        """Test using readline() as ContainerReader does.
 
439
 
 
440
        This is always within a readv hunk, never across it.
 
441
        """
 
442
        transport = self.get_transport()
 
443
        transport.put_bytes('sample', '0\n2\n4\n')
 
444
        f = pack.ReadVFile(transport.readv('sample', [(0,2), (2,4)]))
 
445
        results = []
 
446
        results.append(f.readline())
 
447
        results.append(f.readline())
 
448
        results.append(f.readline())
 
449
        self.assertEqual(['0\n', '2\n', '4\n'], results)
 
450
 
 
451
    def test_readline_and_read(self):
 
452
        """Test exercising one byte reads, readline, and then read again."""
 
453
        transport = self.get_transport()
 
454
        transport.put_bytes('sample', '0\n2\n4\n')
 
455
        f = pack.ReadVFile(transport.readv('sample', [(0,6)]))
 
456
        results = []
 
457
        results.append(f.read(1))
 
458
        results.append(f.readline())
 
459
        results.append(f.read(4))
 
460
        self.assertEqual(['0', '\n', '2\n4\n'], results)