/Lib/test/test_shelve.py

http://unladen-swallow.googlecode.com/ · Python · 150 lines · 133 code · 17 blank · 0 comment · 18 complexity · ec2007787403ab06f603da30b368daaa MD5 · raw file

  1. import os
  2. import unittest
  3. import shelve
  4. import glob
  5. from test import test_support
  6. class TestCase(unittest.TestCase):
  7. fn = "shelftemp" + os.extsep + "db"
  8. def test_close(self):
  9. d1 = {}
  10. s = shelve.Shelf(d1, protocol=2, writeback=False)
  11. s['key1'] = [1,2,3,4]
  12. self.assertEqual(s['key1'], [1,2,3,4])
  13. self.assertEqual(len(s), 1)
  14. s.close()
  15. self.assertRaises(ValueError, len, s)
  16. try:
  17. s['key1']
  18. except ValueError:
  19. pass
  20. else:
  21. self.fail('Closed shelf should not find a key')
  22. def test_ascii_file_shelf(self):
  23. try:
  24. s = shelve.open(self.fn, protocol=0)
  25. s['key1'] = (1,2,3,4)
  26. self.assertEqual(s['key1'], (1,2,3,4))
  27. s.close()
  28. finally:
  29. for f in glob.glob(self.fn+"*"):
  30. os.unlink(f)
  31. def test_binary_file_shelf(self):
  32. try:
  33. s = shelve.open(self.fn, protocol=1)
  34. s['key1'] = (1,2,3,4)
  35. self.assertEqual(s['key1'], (1,2,3,4))
  36. s.close()
  37. finally:
  38. for f in glob.glob(self.fn+"*"):
  39. os.unlink(f)
  40. def test_proto2_file_shelf(self):
  41. try:
  42. s = shelve.open(self.fn, protocol=2)
  43. s['key1'] = (1,2,3,4)
  44. self.assertEqual(s['key1'], (1,2,3,4))
  45. s.close()
  46. finally:
  47. for f in glob.glob(self.fn+"*"):
  48. os.unlink(f)
  49. def test_in_memory_shelf(self):
  50. d1 = {}
  51. s = shelve.Shelf(d1, protocol=0)
  52. s['key1'] = (1,2,3,4)
  53. self.assertEqual(s['key1'], (1,2,3,4))
  54. s.close()
  55. d2 = {}
  56. s = shelve.Shelf(d2, protocol=1)
  57. s['key1'] = (1,2,3,4)
  58. self.assertEqual(s['key1'], (1,2,3,4))
  59. s.close()
  60. self.assertEqual(len(d1), 1)
  61. self.assertNotEqual(d1, d2)
  62. def test_mutable_entry(self):
  63. d1 = {}
  64. s = shelve.Shelf(d1, protocol=2, writeback=False)
  65. s['key1'] = [1,2,3,4]
  66. self.assertEqual(s['key1'], [1,2,3,4])
  67. s['key1'].append(5)
  68. self.assertEqual(s['key1'], [1,2,3,4])
  69. s.close()
  70. d2 = {}
  71. s = shelve.Shelf(d2, protocol=2, writeback=True)
  72. s['key1'] = [1,2,3,4]
  73. self.assertEqual(s['key1'], [1,2,3,4])
  74. s['key1'].append(5)
  75. self.assertEqual(s['key1'], [1,2,3,4,5])
  76. s.close()
  77. self.assertEqual(len(d1), 1)
  78. self.assertEqual(len(d2), 1)
  79. from test import mapping_tests
  80. class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
  81. fn = "shelftemp.db"
  82. counter = 0
  83. def __init__(self, *args, **kw):
  84. self._db = []
  85. mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
  86. type2test = shelve.Shelf
  87. def _reference(self):
  88. return {"key1":"value1", "key2":2, "key3":(1,2,3)}
  89. def _empty_mapping(self):
  90. if self._in_mem:
  91. x= shelve.Shelf({}, **self._args)
  92. else:
  93. self.counter+=1
  94. x= shelve.open(self.fn+str(self.counter), **self._args)
  95. self._db.append(x)
  96. return x
  97. def tearDown(self):
  98. for db in self._db:
  99. db.close()
  100. self._db = []
  101. if not self._in_mem:
  102. for f in glob.glob(self.fn+"*"):
  103. test_support.unlink(f)
  104. class TestAsciiFileShelve(TestShelveBase):
  105. _args={'protocol':0}
  106. _in_mem = False
  107. class TestBinaryFileShelve(TestShelveBase):
  108. _args={'protocol':1}
  109. _in_mem = False
  110. class TestProto2FileShelve(TestShelveBase):
  111. _args={'protocol':2}
  112. _in_mem = False
  113. class TestAsciiMemShelve(TestShelveBase):
  114. _args={'protocol':0}
  115. _in_mem = True
  116. class TestBinaryMemShelve(TestShelveBase):
  117. _args={'protocol':1}
  118. _in_mem = True
  119. class TestProto2MemShelve(TestShelveBase):
  120. _args={'protocol':2}
  121. _in_mem = True
  122. def test_main():
  123. test_support.run_unittest(
  124. TestAsciiFileShelve,
  125. TestBinaryFileShelve,
  126. TestProto2FileShelve,
  127. TestAsciiMemShelve,
  128. TestBinaryMemShelve,
  129. TestProto2MemShelve,
  130. TestCase
  131. )
  132. if __name__ == "__main__":
  133. test_main()