test_object_store.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 shutil
  20. import tempfile
  21. from unittest import TestCase
  22. from dulwich.objects import (
  23. Blob,
  24. )
  25. from dulwich.object_store import (
  26. DiskObjectStore,
  27. MemoryObjectStore,
  28. )
  29. import os
  30. import shutil
  31. import tempfile
  32. testobject = Blob()
  33. testobject.data = "yummy data"
  34. class ObjectStoreTests(object):
  35. def test_iter(self):
  36. self.assertEquals([], list(self.store))
  37. def test_get_nonexistant(self):
  38. self.assertRaises(KeyError, lambda: self.store["a" * 40])
  39. def test_contains_nonexistant(self):
  40. self.assertFalse(("a" * 40) in self.store)
  41. def test_add_objects_empty(self):
  42. self.store.add_objects([])
  43. def test_add_commit(self):
  44. # TODO: Argh, no way to construct Git commit objects without
  45. # access to a serialized form.
  46. self.store.add_objects([])
  47. def test_add_object(self):
  48. self.store.add_object(testobject)
  49. self.assertEquals(set([testobject.id]), set(self.store))
  50. self.assertTrue(testobject.id in self.store)
  51. r = self.store[testobject.id]
  52. self.assertEquals(r, testobject)
  53. def test_add_objects(self):
  54. data = [(testobject, "mypath")]
  55. self.store.add_objects(data)
  56. self.assertEquals(set([testobject.id]), set(self.store))
  57. self.assertTrue(testobject.id in self.store)
  58. r = self.store[testobject.id]
  59. self.assertEquals(r, testobject)
  60. class MemoryObjectStoreTests(ObjectStoreTests, TestCase):
  61. def setUp(self):
  62. TestCase.setUp(self)
  63. self.store = MemoryObjectStore()
  64. class DiskObjectStoreTests(ObjectStoreTests, TestCase):
  65. def setUp(self):
  66. TestCase.setUp(self)
  67. self.store_dir = tempfile.mkdtemp()
  68. self.store = DiskObjectStore.init(self.store_dir)
  69. def tearDown(self):
  70. TestCase.tearDown(self)
  71. shutil.rmtree(self.store_dir)
  72. def test_pack_dir(self):
  73. o = DiskObjectStore(self.store_dir)
  74. self.assertEquals(os.path.join(self.store_dir, "pack"), o.pack_dir)
  75. def test_empty_packs(self):
  76. o = DiskObjectStore(self.store_dir)
  77. self.assertEquals([], o.packs)
  78. # TODO: MissingObjectFinderTests