|
| 1 | +"""Test for object db""" |
| 2 | +from test.testlib import * |
| 3 | +from lib import ZippedStoreShaWriter |
| 4 | + |
| 5 | +from git.odb import * |
| 6 | +from git.odb.stream import Sha1Writer |
| 7 | +from git import Blob |
| 8 | +from git.errors import BadObject |
| 9 | + |
| 10 | + |
| 11 | +from cStringIO import StringIO |
| 12 | +import os |
| 13 | + |
| 14 | +class TestDB(TestBase): |
| 15 | + """Test the different db class implementations""" |
| 16 | + |
| 17 | + # data |
| 18 | + two_lines = "1234\nhello world" |
| 19 | + |
| 20 | + all_data = (two_lines, ) |
| 21 | + |
| 22 | + def _assert_object_writing(self, db): |
| 23 | + """General tests to verify object writing, compatible to ObjectDBW |
| 24 | + :note: requires write access to the database""" |
| 25 | + # start in 'dry-run' mode, using a simple sha1 writer |
| 26 | + ostreams = (ZippedStoreShaWriter, None) |
| 27 | + for ostreamcls in ostreams: |
| 28 | + for data in self.all_data: |
| 29 | + dry_run = ostreamcls is not None |
| 30 | + ostream = None |
| 31 | + if ostreamcls is not None: |
| 32 | + ostream = ostreamcls() |
| 33 | + assert isinstance(ostream, Sha1Writer) |
| 34 | + # END create ostream |
| 35 | + |
| 36 | + prev_ostream = db.set_ostream(ostream) |
| 37 | + assert type(prev_ostream) in ostreams or prev_ostream in ostreams |
| 38 | + |
| 39 | + istream = IStream(Blob.type, len(data), StringIO(data)) |
| 40 | + |
| 41 | + # store returns same istream instance, with new sha set |
| 42 | + my_istream = db.store(istream) |
| 43 | + sha = istream.sha |
| 44 | + assert my_istream is istream |
| 45 | + assert db.has_object(sha) != dry_run |
| 46 | + assert len(sha) == 40 # for now we require 40 byte shas as default |
| 47 | + |
| 48 | + # verify data - the slow way, we want to run code |
| 49 | + if not dry_run: |
| 50 | + info = db.info(sha) |
| 51 | + assert Blob.type == info.type |
| 52 | + assert info.size == len(data) |
| 53 | + |
| 54 | + ostream = db.stream(sha) |
| 55 | + assert ostream.read() == data |
| 56 | + assert ostream.type == Blob.type |
| 57 | + assert ostream.size == len(data) |
| 58 | + else: |
| 59 | + self.failUnlessRaises(BadObject, db.info, sha) |
| 60 | + self.failUnlessRaises(BadObject, db.stream, sha) |
| 61 | + |
| 62 | + # DIRECT STREAM COPY |
| 63 | + # our data hase been written in object format to the StringIO |
| 64 | + # we pasesd as output stream. No physical database representation |
| 65 | + # was created. |
| 66 | + # Test direct stream copy of object streams, the result must be |
| 67 | + # identical to what we fed in |
| 68 | + ostream.seek(0) |
| 69 | + istream.stream = ostream |
| 70 | + assert istream.sha is not None |
| 71 | + prev_sha = istream.sha |
| 72 | + |
| 73 | + db.set_ostream(ZippedStoreShaWriter()) |
| 74 | + db.store(istream) |
| 75 | + assert istream.sha == prev_sha |
| 76 | + new_ostream = db.ostream() |
| 77 | + |
| 78 | + # note: only works as long our store write uses the same compression |
| 79 | + # level, which is zip |
| 80 | + assert ostream.getvalue() == new_ostream.getvalue() |
| 81 | + # END for each data set |
| 82 | + # END for each dry_run mode |
| 83 | + |
| 84 | + @with_bare_rw_repo |
| 85 | + def test_writing(self, rwrepo): |
| 86 | + ldb = LooseObjectDB(os.path.join(rwrepo.git_dir, 'objects')) |
| 87 | + |
| 88 | + # write data |
| 89 | + self._assert_object_writing(ldb) |
| 90 | + |
0 commit comments