From 024adf37acddd6a5d8293b6b5d15795c59a142c0 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 30 May 2011 13:06:37 +0200 Subject: Fixed tests far enough to allow basic repository tests to be applied to any of the new database types. This reduces code duplication to the mere minimum, but allows custom tests to be added on top easily and flexibly --- git/test/db/py/__init__.py | 4 ++++ git/test/db/py/test_base.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 git/test/db/py/__init__.py create mode 100644 git/test/db/py/test_base.py (limited to 'git/test/db/py') diff --git a/git/test/db/py/__init__.py b/git/test/db/py/__init__.py new file mode 100644 index 00000000..8a681e42 --- /dev/null +++ b/git/test/db/py/__init__.py @@ -0,0 +1,4 @@ +# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors +# +# This module is part of GitDB and is released under +# the New BSD License: http://www.opensource.org/licenses/bsd-license.php diff --git a/git/test/db/py/test_base.py b/git/test/db/py/test_base.py new file mode 100644 index 00000000..84899651 --- /dev/null +++ b/git/test/db/py/test_base.py @@ -0,0 +1,18 @@ +# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors +# +# This module is part of GitDB and is released under +# the New BSD License: http://www.opensource.org/licenses/bsd-license.php +from git.test.lib import * +from git.test.db.base import RepoBase +from git.db.py.complex import * + +from git.db.complex import PureCmdGitDB + +class TestPyDBBase(RepoBase): + + RepoCls = PureCmdGitDB + + def test_instantiation(self): + db = PureGitDB(rorepo_dir()) + cdb = PureCompatibilityGitDB(rorepo_dir()) + -- cgit v1.2.3 From 1f71ed94578799ee1667ba54b66a369e307f415b Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 30 May 2011 16:32:56 +0200 Subject: git cmd implementation of repository appears to work, at least this is what the test suggests. Pure python implementation still has some trouble, but this should be very fixable --- git/test/db/py/test_base.py | 21 +++++++++---- git/test/db/py/test_git.py | 47 +++++++++++++++++++++++++++++ git/test/db/py/test_loose.py | 34 +++++++++++++++++++++ git/test/db/py/test_mem.py | 30 ++++++++++++++++++ git/test/db/py/test_pack.py | 72 ++++++++++++++++++++++++++++++++++++++++++++ git/test/db/py/test_ref.py | 60 ++++++++++++++++++++++++++++++++++++ 6 files changed, 258 insertions(+), 6 deletions(-) create mode 100644 git/test/db/py/test_git.py create mode 100644 git/test/db/py/test_loose.py create mode 100644 git/test/db/py/test_mem.py create mode 100644 git/test/db/py/test_pack.py create mode 100644 git/test/db/py/test_ref.py (limited to 'git/test/db/py') diff --git a/git/test/db/py/test_base.py b/git/test/db/py/test_base.py index 84899651..ade05c8d 100644 --- a/git/test/db/py/test_base.py +++ b/git/test/db/py/test_base.py @@ -2,17 +2,26 @@ # # This module is part of GitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php -from git.test.lib import * +from git.test.lib import rorepo_dir from git.test.db.base import RepoBase + +# import test +from git.db.py.base import * +from git.db.py.loose import * +from git.db.py.mem import * +from git.db.py.pack import * +from git.db.py.ref import * +from git.db.py.resolve import * +from git.db.py.submodule import * +from git.db.py.transport import * from git.db.py.complex import * -from git.db.complex import PureCmdGitDB +from git.db.complex import PureCompatibilityGitDB class TestPyDBBase(RepoBase): - RepoCls = PureCmdGitDB + RepoCls = PureCompatibilityGitDB - def test_instantiation(self): - db = PureGitDB(rorepo_dir()) - cdb = PureCompatibilityGitDB(rorepo_dir()) + def test_basics(self): + pass diff --git a/git/test/db/py/test_git.py b/git/test/db/py/test_git.py new file mode 100644 index 00000000..46a2d24f --- /dev/null +++ b/git/test/db/py/test_git.py @@ -0,0 +1,47 @@ +# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors +# +# This module is part of GitDB and is released under +# the New BSD License: http://www.opensource.org/licenses/bsd-license.php +from lib import * +from git.exc import BadObject +from git.db.py import PureGitODB +from git.base import OStream, OInfo +from git.util import hex_to_bin, bin_to_hex + +class TestGitDB(TestDBBase): + + def test_reading(self): + gdb = PureGitODB(fixture_path('../../../.git/objects')) + + # we have packs and loose objects, alternates doesn't necessarily exist + assert 1 < len(gdb.databases()) < 4 + + # access should be possible + git_sha = hex_to_bin("5690fd0d3304f378754b23b098bd7cb5f4aa1976") + assert isinstance(gdb.info(git_sha), OInfo) + assert isinstance(gdb.stream(git_sha), OStream) + assert gdb.size() > 200 + sha_list = list(gdb.sha_iter()) + assert len(sha_list) == gdb.size() + + + # This is actually a test for compound functionality, but it doesn't + # have a separate test module + # test partial shas + # this one as uneven and quite short + assert gdb.partial_to_complete_sha_hex('155b6') == hex_to_bin("155b62a9af0aa7677078331e111d0f7aa6eb4afc") + + # mix even/uneven hexshas + for i, binsha in enumerate(sha_list): + assert gdb.partial_to_complete_sha_hex(bin_to_hex(binsha)[:8-(i%2)]) == binsha + # END for each sha + + self.failUnlessRaises(BadObject, gdb.partial_to_complete_sha_hex, "0000") + + @with_rw_directory + def test_writing(self, path): + gdb = PureGitODB(path) + + # its possible to write objects + self._assert_object_writing(gdb) + self._assert_object_writing_async(gdb) diff --git a/git/test/db/py/test_loose.py b/git/test/db/py/test_loose.py new file mode 100644 index 00000000..16c12d8e --- /dev/null +++ b/git/test/db/py/test_loose.py @@ -0,0 +1,34 @@ +# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors +# +# This module is part of GitDB and is released under +# the New BSD License: http://www.opensource.org/licenses/bsd-license.php +from lib import * +from git.db.py import PureLooseObjectODB +from git.exc import BadObject +from git.util import bin_to_hex + +class TestLooseDB(TestDBBase): + + @with_rw_directory + def test_basics(self, path): + ldb = PureLooseObjectODB(path) + + # write data + self._assert_object_writing(ldb) + self._assert_object_writing_async(ldb) + + # verify sha iteration and size + shas = list(ldb.sha_iter()) + assert shas and len(shas[0]) == 20 + + assert len(shas) == ldb.size() + + # verify find short object + long_sha = bin_to_hex(shas[-1]) + for short_sha in (long_sha[:20], long_sha[:5]): + assert bin_to_hex(ldb.partial_to_complete_sha_hex(short_sha)) == long_sha + # END for each sha + + self.failUnlessRaises(BadObject, ldb.partial_to_complete_sha_hex, '0000') + # raises if no object could be foudn + diff --git a/git/test/db/py/test_mem.py b/git/test/db/py/test_mem.py new file mode 100644 index 00000000..ed14cc21 --- /dev/null +++ b/git/test/db/py/test_mem.py @@ -0,0 +1,30 @@ +# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors +# +# This module is part of GitDB and is released under +# the New BSD License: http://www.opensource.org/licenses/bsd-license.php +from lib import * +from git.db.py import ( + PureMemoryDB, + PureLooseObjectODB + ) + +class TestPureMemoryDB(TestDBBase): + + @with_rw_directory + def test_writing(self, path): + mdb = PureMemoryDB() + + # write data + self._assert_object_writing_simple(mdb) + + # test stream copy + ldb = PureLooseObjectODB(path) + assert ldb.size() == 0 + num_streams_copied = mdb.stream_copy(mdb.sha_iter(), ldb) + assert num_streams_copied == mdb.size() + + assert ldb.size() == mdb.size() + for sha in mdb.sha_iter(): + assert ldb.has_object(sha) + assert ldb.stream(sha).read() == mdb.stream(sha).read() + # END verify objects where copied and are equal diff --git a/git/test/db/py/test_pack.py b/git/test/db/py/test_pack.py new file mode 100644 index 00000000..4854c4e7 --- /dev/null +++ b/git/test/db/py/test_pack.py @@ -0,0 +1,72 @@ +# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors +# +# This module is part of GitDB and is released under +# the New BSD License: http://www.opensource.org/licenses/bsd-license.php +from lib import * +from git.db.py import PurePackedODB +from git.test.lib import fixture_path + +from git.exc import BadObject, AmbiguousObjectName + +import os +import random + +class TestPackDB(TestDBBase): + + @with_packs_rw + def test_writing(self, path): + pdb = PurePackedODB(path) + + # on demand, we init our pack cache + num_packs = len(pdb.entities()) + assert pdb._st_mtime != 0 + + # test pack directory changed: + # packs removed - rename a file, should affect the glob + pack_path = pdb.entities()[0].pack().path() + new_pack_path = pack_path + "renamed" + os.rename(pack_path, new_pack_path) + + pdb.update_cache(force=True) + assert len(pdb.entities()) == num_packs - 1 + + # packs added + os.rename(new_pack_path, pack_path) + pdb.update_cache(force=True) + assert len(pdb.entities()) == num_packs + + # bang on the cache + # access the Entities directly, as there is no iteration interface + # yet ( or required for now ) + sha_list = list(pdb.sha_iter()) + assert len(sha_list) == pdb.size() + + # hit all packs in random order + random.shuffle(sha_list) + + for sha in sha_list: + info = pdb.info(sha) + stream = pdb.stream(sha) + # END for each sha to query + + + # test short finding - be a bit more brutal here + max_bytes = 19 + min_bytes = 2 + num_ambiguous = 0 + for i, sha in enumerate(sha_list): + short_sha = sha[:max((i % max_bytes), min_bytes)] + try: + assert pdb.partial_to_complete_sha(short_sha, len(short_sha)*2) == sha + except AmbiguousObjectName: + num_ambiguous += 1 + pass # valid, we can have short objects + # END exception handling + # END for each sha to find + + # we should have at least one ambiguous, considering the small sizes + # but in our pack, there is no ambigious ... + # assert num_ambiguous + + # non-existing + self.failUnlessRaises(BadObject, pdb.partial_to_complete_sha, "\0\0", 4) diff --git a/git/test/db/py/test_ref.py b/git/test/db/py/test_ref.py new file mode 100644 index 00000000..43fbb48f --- /dev/null +++ b/git/test/db/py/test_ref.py @@ -0,0 +1,60 @@ +# Copyright (C) 2010, 2011 Sebastian Thiel (byronimo@gmail.com) and contributors +# +# This module is part of GitDB and is released under +# the New BSD License: http://www.opensource.org/licenses/bsd-license.php +from lib import * +from git.db.py import PureReferenceDB + +from git.util import ( + NULL_BIN_SHA, + hex_to_bin + ) + +import os + +class TestPureReferenceDB(TestDBBase): + + def make_alt_file(self, alt_path, alt_list): + """Create an alternates file which contains the given alternates. + The list can be empty""" + alt_file = open(alt_path, "wb") + for alt in alt_list: + alt_file.write(alt + "\n") + alt_file.close() + + @with_rw_directory + def test_writing(self, path): + NULL_BIN_SHA = '\0' * 20 + + alt_path = os.path.join(path, 'alternates') + rdb = PureReferenceDB(alt_path) + assert len(rdb.databases()) == 0 + assert rdb.size() == 0 + assert len(list(rdb.sha_iter())) == 0 + + # try empty, non-existing + assert not rdb.has_object(NULL_BIN_SHA) + + + # setup alternate file + # add two, one is invalid + own_repo_path = fixture_path('../../../.git/objects') # use own repo + self.make_alt_file(alt_path, [own_repo_path, "invalid/path"]) + rdb.update_cache() + assert len(rdb.databases()) == 1 + + # we should now find a default revision of ours + git_sha = hex_to_bin("5690fd0d3304f378754b23b098bd7cb5f4aa1976") + assert rdb.has_object(git_sha) + + # remove valid + self.make_alt_file(alt_path, ["just/one/invalid/path"]) + rdb.update_cache() + assert len(rdb.databases()) == 0 + + # add valid + self.make_alt_file(alt_path, [own_repo_path]) + rdb.update_cache() + assert len(rdb.databases()) == 1 + + -- cgit v1.2.3 From 09a11c78950ce8e6376ab7a34cdaec77555c6679 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 30 May 2011 17:55:04 +0200 Subject: Fixed test_git.py --- git/test/db/py/test_git.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'git/test/db/py') diff --git a/git/test/db/py/test_git.py b/git/test/db/py/test_git.py index 46a2d24f..524d4080 100644 --- a/git/test/db/py/test_git.py +++ b/git/test/db/py/test_git.py @@ -2,22 +2,26 @@ # # This module is part of GitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php -from lib import * +from git.test.lib import rorepo_dir +from git.test.db.lib import * from git.exc import BadObject -from git.db.py import PureGitODB +from git.db.py.complex import PureGitODB from git.base import OStream, OInfo from git.util import hex_to_bin, bin_to_hex - + +import os + class TestGitDB(TestDBBase): + RepoCls = PureGitODB def test_reading(self): - gdb = PureGitODB(fixture_path('../../../.git/objects')) + gdb = self.RepoCls(os.path.join(rorepo_dir(), 'objects')) # we have packs and loose objects, alternates doesn't necessarily exist assert 1 < len(gdb.databases()) < 4 # access should be possible - git_sha = hex_to_bin("5690fd0d3304f378754b23b098bd7cb5f4aa1976") + git_sha = hex_to_bin("5aebcd5cb3340fb31776941d7e4d518a712a8655") assert isinstance(gdb.info(git_sha), OInfo) assert isinstance(gdb.stream(git_sha), OStream) assert gdb.size() > 200 @@ -29,10 +33,10 @@ class TestGitDB(TestDBBase): # have a separate test module # test partial shas # this one as uneven and quite short - assert gdb.partial_to_complete_sha_hex('155b6') == hex_to_bin("155b62a9af0aa7677078331e111d0f7aa6eb4afc") + assert gdb.partial_to_complete_sha_hex('5aebcd') == hex_to_bin("5aebcd5cb3340fb31776941d7e4d518a712a8655") # mix even/uneven hexshas - for i, binsha in enumerate(sha_list): + for i, binsha in enumerate(sha_list[:50]): assert gdb.partial_to_complete_sha_hex(bin_to_hex(binsha)[:8-(i%2)]) == binsha # END for each sha @@ -40,7 +44,7 @@ class TestGitDB(TestDBBase): @with_rw_directory def test_writing(self, path): - gdb = PureGitODB(path) + gdb = self.RepoCls(path) # its possible to write objects self._assert_object_writing(gdb) -- cgit v1.2.3 From 2bfc2e99111ef0e31f2bfda8a01c261a4f3f67cf Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 30 May 2011 18:00:44 +0200 Subject: Fixed test_loose.py --- git/test/db/py/test_loose.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'git/test/db/py') diff --git a/git/test/db/py/test_loose.py b/git/test/db/py/test_loose.py index 16c12d8e..eb18c05d 100644 --- a/git/test/db/py/test_loose.py +++ b/git/test/db/py/test_loose.py @@ -2,16 +2,18 @@ # # This module is part of GitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php -from lib import * -from git.db.py import PureLooseObjectODB +from git.test.db.lib import * +from git.db.py.loose import PureLooseObjectODB from git.exc import BadObject from git.util import bin_to_hex class TestLooseDB(TestDBBase): + RepoCls = PureLooseObjectODB + @with_rw_directory def test_basics(self, path): - ldb = PureLooseObjectODB(path) + ldb = self.RepoCls(path) # write data self._assert_object_writing(ldb) -- cgit v1.2.3 From c192638aae09c1b5c087d67cc99dd4c7ec4ed916 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 30 May 2011 18:19:44 +0200 Subject: Fixed all remaining python repository tests --- git/test/db/py/test_git.py | 8 ++++---- git/test/db/py/test_loose.py | 6 +++--- git/test/db/py/test_mem.py | 10 +++++----- git/test/db/py/test_pack.py | 8 ++++++-- git/test/db/py/test_ref.py | 8 +++++--- 5 files changed, 23 insertions(+), 17 deletions(-) (limited to 'git/test/db/py') diff --git a/git/test/db/py/test_git.py b/git/test/db/py/test_git.py index 524d4080..ecaa5c8f 100644 --- a/git/test/db/py/test_git.py +++ b/git/test/db/py/test_git.py @@ -3,7 +3,7 @@ # This module is part of GitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php from git.test.lib import rorepo_dir -from git.test.db.lib import * +from git.test.db.lib import TestDBBase, with_rw_directory from git.exc import BadObject from git.db.py.complex import PureGitODB from git.base import OStream, OInfo @@ -12,10 +12,10 @@ from git.util import hex_to_bin, bin_to_hex import os class TestGitDB(TestDBBase): - RepoCls = PureGitODB + needs_ro_repo = False def test_reading(self): - gdb = self.RepoCls(os.path.join(rorepo_dir(), 'objects')) + gdb = PureGitODB(os.path.join(rorepo_dir(), 'objects')) # we have packs and loose objects, alternates doesn't necessarily exist assert 1 < len(gdb.databases()) < 4 @@ -44,7 +44,7 @@ class TestGitDB(TestDBBase): @with_rw_directory def test_writing(self, path): - gdb = self.RepoCls(path) + gdb = PureGitODB(path) # its possible to write objects self._assert_object_writing(gdb) diff --git a/git/test/db/py/test_loose.py b/git/test/db/py/test_loose.py index eb18c05d..0c9b4831 100644 --- a/git/test/db/py/test_loose.py +++ b/git/test/db/py/test_loose.py @@ -2,18 +2,18 @@ # # This module is part of GitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php -from git.test.db.lib import * +from git.test.db.lib import TestDBBase, with_rw_directory from git.db.py.loose import PureLooseObjectODB from git.exc import BadObject from git.util import bin_to_hex class TestLooseDB(TestDBBase): - RepoCls = PureLooseObjectODB + needs_ro_repo = False @with_rw_directory def test_basics(self, path): - ldb = self.RepoCls(path) + ldb = PureLooseObjectODB(path) # write data self._assert_object_writing(ldb) diff --git a/git/test/db/py/test_mem.py b/git/test/db/py/test_mem.py index ed14cc21..bc98dc56 100644 --- a/git/test/db/py/test_mem.py +++ b/git/test/db/py/test_mem.py @@ -2,14 +2,14 @@ # # This module is part of GitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php -from lib import * -from git.db.py import ( - PureMemoryDB, - PureLooseObjectODB - ) +from git.test.db.lib import TestDBBase, with_rw_directory +from git.db.py.mem import PureMemoryDB +from git.db.py.loose import PureLooseObjectODB class TestPureMemoryDB(TestDBBase): + needs_ro_repo = False + @with_rw_directory def test_writing(self, path): mdb = PureMemoryDB() diff --git a/git/test/db/py/test_pack.py b/git/test/db/py/test_pack.py index 4854c4e7..5043f446 100644 --- a/git/test/db/py/test_pack.py +++ b/git/test/db/py/test_pack.py @@ -2,8 +2,9 @@ # # This module is part of GitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php -from lib import * -from git.db.py import PurePackedODB +from git.test.db.lib import TestDBBase, with_packs_rw + +from git.db.py.pack import PurePackedODB from git.test.lib import fixture_path from git.exc import BadObject, AmbiguousObjectName @@ -13,12 +14,15 @@ import random class TestPackDB(TestDBBase): + needs_ro_repo = False + @with_packs_rw def test_writing(self, path): pdb = PurePackedODB(path) # on demand, we init our pack cache num_packs = len(pdb.entities()) + assert num_packs assert pdb._st_mtime != 0 # test pack directory changed: diff --git a/git/test/db/py/test_ref.py b/git/test/db/py/test_ref.py index 43fbb48f..c5374dc9 100644 --- a/git/test/db/py/test_ref.py +++ b/git/test/db/py/test_ref.py @@ -2,8 +2,8 @@ # # This module is part of GitDB and is released under # the New BSD License: http://www.opensource.org/licenses/bsd-license.php -from lib import * -from git.db.py import PureReferenceDB +from git.test.db.lib import * +from git.db.py.ref import PureReferenceDB from git.util import ( NULL_BIN_SHA, @@ -14,6 +14,8 @@ import os class TestPureReferenceDB(TestDBBase): + needs_ro_repo = False + def make_alt_file(self, alt_path, alt_list): """Create an alternates file which contains the given alternates. The list can be empty""" @@ -44,7 +46,7 @@ class TestPureReferenceDB(TestDBBase): assert len(rdb.databases()) == 1 # we should now find a default revision of ours - git_sha = hex_to_bin("5690fd0d3304f378754b23b098bd7cb5f4aa1976") + git_sha = hex_to_bin("5aebcd5cb3340fb31776941d7e4d518a712a8655") assert rdb.has_object(git_sha) # remove valid -- cgit v1.2.3 From 6f960586feccff8c1f2c717765eb0a5e8b9cd6f3 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 30 May 2011 21:14:22 +0200 Subject: Fixed remaining tests as good as possible. remote/fetch/pull and submodule tests need some more work. Also, the tests need to be reorganized and move closer to their actual location within gitpython. Hence the refs tests go to git.test.refs, etc --- git/test/db/py/test_base.py | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'git/test/db/py') diff --git a/git/test/db/py/test_base.py b/git/test/db/py/test_base.py index ade05c8d..6b06bbe9 100644 --- a/git/test/db/py/test_base.py +++ b/git/test/db/py/test_base.py @@ -5,17 +5,6 @@ from git.test.lib import rorepo_dir from git.test.db.base import RepoBase -# import test -from git.db.py.base import * -from git.db.py.loose import * -from git.db.py.mem import * -from git.db.py.pack import * -from git.db.py.ref import * -from git.db.py.resolve import * -from git.db.py.submodule import * -from git.db.py.transport import * -from git.db.py.complex import * - from git.db.complex import PureCompatibilityGitDB class TestPyDBBase(RepoBase): -- cgit v1.2.3