2002-12-08 14:36:24 -04:00
|
|
|
import os
|
|
|
|
import unittest
|
|
|
|
import shelve
|
|
|
|
import glob
|
|
|
|
from test import test_support
|
|
|
|
|
|
|
|
class TestCase(unittest.TestCase):
|
|
|
|
|
2003-05-10 04:36:56 -03:00
|
|
|
fn = "shelftemp" + os.extsep + "db"
|
2002-12-24 14:31:27 -04:00
|
|
|
|
2002-12-08 14:36:24 -04:00
|
|
|
def test_ascii_file_shelf(self):
|
|
|
|
try:
|
|
|
|
s = shelve.open(self.fn, binary=False)
|
|
|
|
s['key1'] = (1,2,3,4)
|
|
|
|
self.assertEqual(s['key1'], (1,2,3,4))
|
|
|
|
s.close()
|
|
|
|
finally:
|
|
|
|
for f in glob.glob(self.fn+"*"):
|
|
|
|
os.unlink(f)
|
|
|
|
|
|
|
|
def test_binary_file_shelf(self):
|
|
|
|
try:
|
|
|
|
s = shelve.open(self.fn, binary=True)
|
|
|
|
s['key1'] = (1,2,3,4)
|
|
|
|
self.assertEqual(s['key1'], (1,2,3,4))
|
|
|
|
s.close()
|
|
|
|
finally:
|
|
|
|
for f in glob.glob(self.fn+"*"):
|
|
|
|
os.unlink(f)
|
|
|
|
|
2003-04-19 17:59:03 -03:00
|
|
|
def test_proto2_file_shelf(self):
|
|
|
|
try:
|
|
|
|
s = shelve.open(self.fn, protocol=2)
|
|
|
|
s['key1'] = (1,2,3,4)
|
|
|
|
self.assertEqual(s['key1'], (1,2,3,4))
|
|
|
|
s.close()
|
|
|
|
finally:
|
|
|
|
for f in glob.glob(self.fn+"*"):
|
|
|
|
os.unlink(f)
|
|
|
|
|
2002-12-08 14:36:24 -04:00
|
|
|
def test_in_memory_shelf(self):
|
|
|
|
d1 = {}
|
|
|
|
s = shelve.Shelf(d1, binary=False)
|
|
|
|
s['key1'] = (1,2,3,4)
|
|
|
|
self.assertEqual(s['key1'], (1,2,3,4))
|
|
|
|
s.close()
|
|
|
|
d2 = {}
|
|
|
|
s = shelve.Shelf(d2, binary=True)
|
|
|
|
s['key1'] = (1,2,3,4)
|
|
|
|
self.assertEqual(s['key1'], (1,2,3,4))
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
self.assertEqual(len(d1), 1)
|
|
|
|
self.assertNotEqual(d1, d2)
|
|
|
|
|
2003-04-19 17:59:03 -03:00
|
|
|
def test_mutable_entry(self):
|
|
|
|
d1 = {}
|
|
|
|
s = shelve.Shelf(d1, protocol=2, writeback=False)
|
|
|
|
s['key1'] = [1,2,3,4]
|
|
|
|
self.assertEqual(s['key1'], [1,2,3,4])
|
|
|
|
s['key1'].append(5)
|
|
|
|
self.assertEqual(s['key1'], [1,2,3,4])
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
d2 = {}
|
|
|
|
s = shelve.Shelf(d2, protocol=2, writeback=True)
|
|
|
|
s['key1'] = [1,2,3,4]
|
|
|
|
self.assertEqual(s['key1'], [1,2,3,4])
|
|
|
|
s['key1'].append(5)
|
|
|
|
self.assertEqual(s['key1'], [1,2,3,4,5])
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
self.assertEqual(len(d1), 1)
|
|
|
|
self.assertEqual(len(d2), 1)
|
|
|
|
|
|
|
|
|
2004-05-31 13:29:04 -03:00
|
|
|
from test import mapping_tests
|
2002-12-08 14:36:24 -04:00
|
|
|
|
2004-05-31 13:29:04 -03:00
|
|
|
class TestShelveBase(mapping_tests.BasicTestMappingProtocol):
|
2003-03-09 03:05:43 -04:00
|
|
|
fn = "shelftemp.db"
|
|
|
|
counter = 0
|
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
self._db = []
|
2004-05-31 13:29:04 -03:00
|
|
|
mapping_tests.BasicTestMappingProtocol.__init__(self, *args, **kw)
|
2004-06-02 15:42:25 -03:00
|
|
|
type2test = shelve.Shelf
|
2003-03-09 03:05:43 -04:00
|
|
|
def _reference(self):
|
|
|
|
return {"key1":"value1", "key2":2, "key3":(1,2,3)}
|
|
|
|
def _empty_mapping(self):
|
|
|
|
if self._in_mem:
|
2003-04-19 17:59:03 -03:00
|
|
|
x= shelve.Shelf({}, **self._args)
|
2003-03-09 03:05:43 -04:00
|
|
|
else:
|
|
|
|
self.counter+=1
|
2003-04-19 17:59:03 -03:00
|
|
|
x= shelve.open(self.fn+str(self.counter), **self._args)
|
2003-03-09 03:05:43 -04:00
|
|
|
self._db.append(x)
|
|
|
|
return x
|
|
|
|
def tearDown(self):
|
|
|
|
for db in self._db:
|
|
|
|
db.close()
|
|
|
|
self._db = []
|
|
|
|
if not self._in_mem:
|
|
|
|
for f in glob.glob(self.fn+"*"):
|
|
|
|
os.unlink(f)
|
|
|
|
|
|
|
|
class TestAsciiFileShelve(TestShelveBase):
|
2003-04-19 17:59:03 -03:00
|
|
|
_args={'binary':False}
|
2003-03-09 03:05:43 -04:00
|
|
|
_in_mem = False
|
|
|
|
class TestBinaryFileShelve(TestShelveBase):
|
2003-04-19 17:59:03 -03:00
|
|
|
_args={'binary':True}
|
|
|
|
_in_mem = False
|
|
|
|
class TestProto2FileShelve(TestShelveBase):
|
|
|
|
_args={'protocol':2}
|
2003-03-09 03:05:43 -04:00
|
|
|
_in_mem = False
|
|
|
|
class TestAsciiMemShelve(TestShelveBase):
|
2003-04-19 17:59:03 -03:00
|
|
|
_args={'binary':False}
|
2003-03-09 03:05:43 -04:00
|
|
|
_in_mem = True
|
|
|
|
class TestBinaryMemShelve(TestShelveBase):
|
2003-04-19 17:59:03 -03:00
|
|
|
_args={'binary':True}
|
|
|
|
_in_mem = True
|
|
|
|
class TestProto2MemShelve(TestShelveBase):
|
|
|
|
_args={'protocol':2}
|
2003-03-09 03:05:43 -04:00
|
|
|
_in_mem = True
|
|
|
|
|
|
|
|
def test_main():
|
2003-05-01 14:45:56 -03:00
|
|
|
test_support.run_unittest(
|
|
|
|
TestAsciiFileShelve,
|
|
|
|
TestBinaryFileShelve,
|
|
|
|
TestProto2FileShelve,
|
|
|
|
TestAsciiMemShelve,
|
|
|
|
TestBinaryMemShelve,
|
|
|
|
TestProto2MemShelve,
|
|
|
|
TestCase
|
|
|
|
)
|
2002-12-08 14:36:24 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|