~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_pack.py

  • Committer: Robert Collins
  • Date: 2007-08-02 03:17:46 UTC
  • mto: (2592.3.65 repository)
  • mto: This revision was merged to the branch mainline in revision 2667.
  • Revision ID: robertc@robertcollins.net-20070802031746-mpnoaxym829719w6
* ``bzrlib.pack.make_readv_reader`` allows readv based access to pack
  files that are stored on a transport. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
391
391
        self.assertEqual('', get_bytes(99))
392
392
 
393
393
 
 
394
class TestReadvReader(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
        pack_data.seek(0)
 
408
        transport.put_file('mypack', pack_data)
 
409
        requested_records = [memos[0], memos[2]]
 
410
        reader = pack.make_readv_reader(transport, 'mypack', requested_records)
 
411
        result = []
 
412
        for names, reader_func in reader.iter_records():
 
413
            result.append((names, reader_func(None)))
 
414
        self.assertEqual([([], 'abc'), (['name2'], 'ghi')], result)
 
415
 
 
416
 
 
417
class TestReadvFile(tests.TestCaseWithTransport):
 
418
 
 
419
    def test_read_bytes(self):
 
420
        # this tests byte reading - solo, all in a hunk at once
 
421
        transport = self.get_transport()
 
422
        transport.put_bytes('sample', '0123456789')
 
423
        f = pack.ReadVFile(transport.readv('sample', [(0,1), (1,2), (4,1), (6,2)]))
 
424
        results = []
 
425
        results.append(f.read(1))
 
426
        results.append(f.read(2))
 
427
        results.append(f.read(1))
 
428
        results.append(f.read(1))
 
429
        results.append(f.read(1))
 
430
        self.assertEqual(['0', '12', '4', '6', '7'], results)
 
431
 
 
432
    def test_readline(self):
 
433
        # this tests readline as the ContainerReader needs it - 
 
434
        # just within a record, never across.
 
435
        transport = self.get_transport()
 
436
        transport.put_bytes('sample', '0\n2\n4\n')
 
437
        f = pack.ReadVFile(transport.readv('sample', [(0,2), (2,4)]))
 
438
        results = []
 
439
        results.append(f.readline())
 
440
        results.append(f.readline())
 
441
        results.append(f.readline())
 
442
        self.assertEqual(['0\n', '2\n', '4\n'], results)
 
443
 
 
444
    def test_readline_and_read(self):
 
445
        # this tests read, then readline, then read again 
 
446
        transport = self.get_transport()
 
447
        transport.put_bytes('sample', '0\n2\n4\n')
 
448
        f = pack.ReadVFile(transport.readv('sample', [(0,6)]))
 
449
        results = []
 
450
        results.append(f.read(1))
 
451
        results.append(f.readline())
 
452
        results.append(f.read(4))
 
453
        self.assertEqual(['0', '\n', '2\n4\n'], results)