test_object_store.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. import os
  20. import shutil
  21. import tempfile
  22. from dulwich.index import (
  23. commit_tree,
  24. )
  25. from dulwich.objects import (
  26. Blob,
  27. )
  28. from dulwich.object_store import (
  29. DiskObjectStore,
  30. MemoryObjectStore,
  31. )
  32. from dulwich.tests import (
  33. TestCase,
  34. )
  35. from utils import (
  36. make_object,
  37. )
  38. testobject = make_object(Blob, data="yummy data")
  39. class ObjectStoreTests(object):
  40. def test_iter(self):
  41. self.assertEquals([], list(self.store))
  42. def test_get_nonexistant(self):
  43. self.assertRaises(KeyError, lambda: self.store["a" * 40])
  44. def test_contains_nonexistant(self):
  45. self.assertFalse(("a" * 40) in self.store)
  46. def test_add_objects_empty(self):
  47. self.store.add_objects([])
  48. def test_add_commit(self):
  49. # TODO: Argh, no way to construct Git commit objects without
  50. # access to a serialized form.
  51. self.store.add_objects([])
  52. def test_add_object(self):
  53. self.store.add_object(testobject)
  54. self.assertEquals(set([testobject.id]), set(self.store))
  55. self.assertTrue(testobject.id in self.store)
  56. r = self.store[testobject.id]
  57. self.assertEquals(r, testobject)
  58. def test_add_objects(self):
  59. data = [(testobject, "mypath")]
  60. self.store.add_objects(data)
  61. self.assertEquals(set([testobject.id]), set(self.store))
  62. self.assertTrue(testobject.id in self.store)
  63. r = self.store[testobject.id]
  64. self.assertEquals(r, testobject)
  65. def test_iter_tree_contents(self):
  66. blob_a = make_object(Blob, data='a')
  67. blob_b = make_object(Blob, data='b')
  68. blob_c = make_object(Blob, data='c')
  69. for blob in [blob_a, blob_b, blob_c]:
  70. self.store.add_object(blob)
  71. blobs = [
  72. ('a', blob_a.id, 0100644),
  73. ('ad/b', blob_b.id, 0100644),
  74. ('ad/bd/c', blob_c.id, 0100755),
  75. ('ad/c', blob_c.id, 0100644),
  76. ('c', blob_c.id, 0100644),
  77. ]
  78. tree_id = commit_tree(self.store, blobs)
  79. self.assertEquals([(p, m, h) for (p, h, m) in blobs],
  80. list(self.store.iter_tree_contents(tree_id)))
  81. class MemoryObjectStoreTests(ObjectStoreTests, TestCase):
  82. def setUp(self):
  83. TestCase.setUp(self)
  84. self.store = MemoryObjectStore()
  85. class PackBasedObjectStoreTests(ObjectStoreTests):
  86. def test_empty_packs(self):
  87. self.assertEquals([], self.store.packs)
  88. def test_pack_loose_objects(self):
  89. b1 = make_object(Blob, data="yummy data")
  90. self.store.add_object(b1)
  91. b2 = make_object(Blob, data="more yummy data")
  92. self.store.add_object(b2)
  93. self.assertEquals([], self.store.packs)
  94. self.assertEquals(2, self.store.pack_loose_objects())
  95. self.assertNotEquals([], self.store.packs)
  96. self.assertEquals(0, self.store.pack_loose_objects())
  97. class DiskObjectStoreTests(PackBasedObjectStoreTests, TestCase):
  98. def setUp(self):
  99. TestCase.setUp(self)
  100. self.store_dir = tempfile.mkdtemp()
  101. self.store = DiskObjectStore.init(self.store_dir)
  102. def tearDown(self):
  103. TestCase.tearDown(self)
  104. shutil.rmtree(self.store_dir)
  105. def test_pack_dir(self):
  106. o = DiskObjectStore(self.store_dir)
  107. self.assertEquals(os.path.join(self.store_dir, "pack"), o.pack_dir)
  108. # TODO: MissingObjectFinderTests