test_object_store.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # test_object_store.py -- tests for object_store.py
  2. # Copyright (C) 2008 Jelmer Vernooij <jelmer@samba.org>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # or (at your option) any later version of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. """Tests for the object store interface."""
  19. from unittest import TestCase
  20. from dulwich.objects import (
  21. Blob,
  22. )
  23. from dulwich.object_store import (
  24. DiskObjectStore,
  25. MemoryObjectStore,
  26. )
  27. import os
  28. import shutil
  29. testobject = Blob()
  30. testobject.data = "yummy data"
  31. class SpecificDiskObjectStoreTests(TestCase):
  32. def test_pack_dir(self):
  33. o = DiskObjectStore("foo")
  34. self.assertEquals(os.path.join("foo", "pack"), o.pack_dir)
  35. def test_empty_packs(self):
  36. o = DiskObjectStore("foo")
  37. self.assertEquals([], o.packs)
  38. class ObjectStoreTests(object):
  39. def test_iter(self):
  40. self.assertEquals([], list(self.store))
  41. def test_get_nonexistant(self):
  42. self.assertRaises(KeyError, self.store.__getitem__, "a" * 40)
  43. def test_contains_nonexistant(self):
  44. self.assertFalse(self.store.__contains__("a" * 40))
  45. def test_add_objects_empty(self):
  46. self.store.add_objects([])
  47. def test_add_commit(self):
  48. # TODO: Argh, no way to construct Git commit objects without
  49. # access to a serialized form.
  50. self.store.add_objects([])
  51. def test_add_object(self):
  52. self.store.add_object(testobject)
  53. self.assertEquals(set([testobject.id]), set(self.store))
  54. self.assertTrue(self.store.__contains__(testobject.id))
  55. r = self.store[testobject.id]
  56. self.assertEquals(r, testobject)
  57. def test_add_objects(self):
  58. data = [(testobject, "mypath")]
  59. self.store.add_objects(data)
  60. self.assertEquals(set([testobject.id]), set(self.store))
  61. self.assertTrue(self.store.__contains__(testobject.id))
  62. r = self.store[testobject.id]
  63. self.assertEquals(r, testobject)
  64. class MemoryObjectStoreTests(ObjectStoreTests,TestCase):
  65. def setUp(self):
  66. TestCase.setUp(self)
  67. self.store = MemoryObjectStore()
  68. class DiskObjectStoreTests(ObjectStoreTests,TestCase):
  69. def setUp(self):
  70. TestCase.setUp(self)
  71. if os.path.exists("foo"):
  72. shutil.rmtree("foo")
  73. os.makedirs(os.path.join("foo", "pack"))
  74. self.store = DiskObjectStore("foo")
  75. # TODO: MissingObjectFinderTests