~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/testtransport.py

  • Committer: Robert Collins
  • Date: 2005-10-16 14:19:24 UTC
  • mto: This revision was merged to the branch mainline in revision 1459.
  • Revision ID: robertc@lifelesslap.robertcollins.net-20051016141924-8dcbb20f1c03756e
Many transport related tweaks:

- Remove get_partial as it was not used at all.
- Move store._iter_relpaths into transport as iter_files_recursive.
- Nuke ImmutableMemoryStore, replaced with MemoryTransport.
- Remove all direct use of the stat module from store logic.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
import os
19
19
from cStringIO import StringIO
20
 
from bzrlib.selftest import TestCaseInTempDir
 
20
 
 
21
from bzrlib.errors import NoSuchFile, FileExists, TransportNotPossible
 
22
from bzrlib.selftest import TestCase, TestCaseInTempDir
21
23
from bzrlib.selftest.HTTPTestUtil import TestCaseWithWebserver
22
 
from bzrlib.errors import NoSuchFile, FileExists, TransportNotPossible
 
24
from bzrlib.transport import memory
 
25
 
23
26
 
24
27
def _append(fn, txt):
25
28
    """Append the given text (file-like object) to the supplied filename."""
431
434
                'some text for the\nthird file created\n'
432
435
                'some garbage\nto put in three\n')
433
436
 
434
 
    def test_get_partial(self):
435
 
        t = self.get_transport()
436
 
 
437
 
        contents = [
438
 
            ('f1', 
439
 
                'here is some text\nand a bit more\n'
440
 
                'adding more\ntext to two\n'),
441
 
            ('f2',
442
 
                'this is a string\nand some more stuff\n'
443
 
                'appending to\none\n'),
444
 
            ('f3',
445
 
                'some text for the\nthird file created\n'
446
 
                'some garbage\nto put in three\n')
447
 
        ]
448
 
        if self.readonly:
449
 
            for f, val in contents:
450
 
                open(f, 'wb').write(val)
451
 
        else:
452
 
            t.put_multi(contents)
453
 
 
454
 
        self.assertRaises(NoSuchFile,
455
 
                t.get_partial, 'a-missing-file', 20)
456
 
        self.assertRaises(NoSuchFile,
457
 
                t.get_partial, 'another-missing-file', 20, 30)
458
 
        f = t.get_partial('f1', 33)
459
 
        self.assertEqual(f.read(), 
460
 
                'adding more\ntext to two\n')
461
 
        f = t.get_partial('f1', 33, 10)
462
 
        self.assertEqual(f.read(10), 
463
 
                'adding mor')
464
 
 
465
 
        del f
466
 
 
467
 
        offsets = [('f2', 37), ('f3', 20, 10), ('f1', 10, 20)]
468
 
        values = ['appending to\none\n',
469
 
                  'ird file c',
470
 
                  'me text\nand a bit mo'
471
 
                 ]
472
 
        contents_f = t.get_partial_multi(offsets)
473
 
        count = 0
474
 
        for f, val in zip(contents_f, values):
475
 
            count += 1
476
 
            self.assertEqual(val, f.read(len(val)))
477
 
        # Make sure we saw all values, and no extra
478
 
        self.assertEqual(len(values), count)
479
 
        self.assertEqual(list(contents_f), [])
480
 
 
481
 
        # Do the same thing with an iterator
482
 
        offsets = iter([('f2', 34), ('f3', 18, 10), ('f1', 15, 15)])
483
 
        values = ['ff\nappending to\none\n',
484
 
                  'third file',
485
 
                  'xt\nand a bit mo'
486
 
                 ]
487
 
        contents_f = t.get_partial_multi(offsets)
488
 
        count = 0
489
 
        for f, val in zip(contents_f, values):
490
 
            count += 1
491
 
            self.assertEqual(val, f.read(len(val)))
492
 
        self.assertEqual(len(values), count)
493
 
        self.assertEqual(list(contents_f), [])
494
 
 
495
 
 
496
437
    def test_delete(self):
497
438
        # TODO: Test Transport.delete
498
439
        pass
501
442
        # TODO: Test Transport.move
502
443
        pass
503
444
 
 
445
 
504
446
class LocalTransportTest(TestCaseInTempDir, TestTransportMixIn):
505
447
    def get_transport(self):
506
448
        from bzrlib.transport.local import LocalTransport
507
449
        return LocalTransport('.')
508
450
 
 
451
 
509
452
class HttpTransportTest(TestCaseWithWebserver, TestTransportMixIn):
 
453
 
510
454
    readonly = True
 
455
 
511
456
    def get_transport(self):
512
457
        from bzrlib.transport.http import HttpTransport
513
458
        url = self.get_remote_url('.')
514
459
        return HttpTransport(url)
515
460
 
 
461
 
 
462
class TestMemoryTransport(TestCase):
 
463
 
 
464
    def test_get_transport(self):
 
465
        memory.MemoryTransport()
 
466
 
 
467
    def test_clone(self):
 
468
        transport = memory.MemoryTransport()
 
469
        self.failUnless(transport.clone() is transport)
 
470
 
 
471
    def test_abspath(self):
 
472
        transport = memory.MemoryTransport()
 
473
        self.assertEqual("in-memory:relpath", transport.abspath('relpath'))
 
474
 
 
475
    def test_relpath(self):
 
476
        transport = memory.MemoryTransport()
 
477
 
 
478
    def test_append_and_get(self):
 
479
        transport = memory.MemoryTransport()
 
480
        transport.append('path', StringIO('content'))
 
481
        self.assertEqual(transport.get('path').read(), 'content')
 
482
        transport.append('path', StringIO('content'))
 
483
        self.assertEqual(transport.get('path').read(), 'contentcontent')
 
484
 
 
485
    def test_put_and_get(self):
 
486
        transport = memory.MemoryTransport()
 
487
        transport.put('path', StringIO('content'))
 
488
        self.assertEqual(transport.get('path').read(), 'content')
 
489
        transport.put('path', StringIO('content'))
 
490
        self.assertEqual(transport.get('path').read(), 'content')
 
491
 
 
492
    def test_append_without_dir_fails(self):
 
493
        transport = memory.MemoryTransport()
 
494
        self.assertRaises(NoSuchFile,
 
495
                          transport.append, 'dir/path', StringIO('content'))
 
496
 
 
497
    def test_put_without_dir_fails(self):
 
498
        transport = memory.MemoryTransport()
 
499
        self.assertRaises(NoSuchFile,
 
500
                          transport.put, 'dir/path', StringIO('content'))
 
501
 
 
502
    def test_get_missing(self):
 
503
        transport = memory.MemoryTransport()
 
504
        self.assertRaises(NoSuchFile, transport.get, 'foo')
 
505
 
 
506
    def test_has_missing(self):
 
507
        transport = memory.MemoryTransport()
 
508
        self.assertEquals(False, transport.has('foo'))
 
509
 
 
510
    def test_has_present(self):
 
511
        transport = memory.MemoryTransport()
 
512
        transport.append('foo', StringIO('content'))
 
513
        self.assertEquals(True, transport.has('foo'))
 
514
 
 
515
    def test_mkdir(self):
 
516
        transport = memory.MemoryTransport()
 
517
        transport.mkdir('dir')
 
518
        transport.append('dir/path', StringIO('content'))
 
519
        self.assertEqual(transport.get('dir/path').read(), 'content')
 
520
 
 
521
    def test_mkdir_missing_parent(self):
 
522
        transport = memory.MemoryTransport()
 
523
        self.assertRaises(NoSuchFile,
 
524
                          transport.mkdir, 'dir/dir')
 
525
 
 
526
    def test_mkdir_twice(self):
 
527
        transport = memory.MemoryTransport()
 
528
        transport.mkdir('dir')
 
529
        self.assertRaises(FileExists, transport.mkdir, 'dir')
 
530
 
 
531
    def test_parameters(self):
 
532
        transport = memory.MemoryTransport()
 
533
        self.assertEqual(True, transport.listable())
 
534
        self.assertEqual(False, transport.should_cache())
 
535
 
 
536
    def test_iter_files_recursive(self):
 
537
        transport = memory.MemoryTransport()
 
538
        transport.mkdir('dir')
 
539
        transport.put('dir/foo', StringIO('content'))
 
540
        transport.put('dir/bar', StringIO('content'))
 
541
        transport.put('bar', StringIO('content'))
 
542
        paths = set(transport.iter_files_recursive())
 
543
        self.assertEqual(set(['dir/foo', 'dir/bar', 'bar']), paths)
 
544
 
 
545
    def test_stat(self):
 
546
        transport = memory.MemoryTransport()
 
547
        transport.put('foo', StringIO('content'))
 
548
        transport.put('bar', StringIO('phowar'))
 
549
        self.assertEqual(7, transport.stat('foo').st_size)
 
550
        self.assertEqual(6, transport.stat('bar').st_size)
 
551