PageRenderTime 60ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Lib/bsddb/test/test_dbshelve.py

https://github.com/rpattabi/ironruby
Python | 405 lines | 280 code | 106 blank | 19 comment | 36 complexity | 7c95db3a1b408e30a6f4a521856d37f9 MD5 | raw file
  1. """
  2. TestCases for checking dbShelve objects.
  3. """
  4. import os, string, sys
  5. import random
  6. import unittest
  7. from test_all import db, dbshelve, test_support, verbose, \
  8. get_new_environment_path, get_new_database_path
  9. if sys.version_info < (2, 4) :
  10. from sets import Set as set
  11. #----------------------------------------------------------------------
  12. # We want the objects to be comparable so we can test dbshelve.values
  13. # later on.
  14. class DataClass:
  15. def __init__(self):
  16. self.value = random.random()
  17. def __repr__(self) : # For Python 3.0 comparison
  18. return "DataClass %f" %self.value
  19. def __cmp__(self, other): # For Python 2.x comparison
  20. return cmp(self.value, other)
  21. class DBShelveTestCase(unittest.TestCase):
  22. if sys.version_info < (2, 4):
  23. def assertTrue(self, expr, msg=None):
  24. return self.failUnless(expr,msg=msg)
  25. if (sys.version_info < (2, 7)) or ((sys.version_info >= (3, 0)) and
  26. (sys.version_info < (3, 2))) :
  27. def assertIn(self, a, b, msg=None) :
  28. return self.assertTrue(a in b, msg=msg)
  29. def setUp(self):
  30. if sys.version_info[0] >= 3 :
  31. from test_all import do_proxy_db_py3k
  32. self._flag_proxy_db_py3k = do_proxy_db_py3k(False)
  33. self.filename = get_new_database_path()
  34. self.do_open()
  35. def tearDown(self):
  36. if sys.version_info[0] >= 3 :
  37. from test_all import do_proxy_db_py3k
  38. do_proxy_db_py3k(self._flag_proxy_db_py3k)
  39. self.do_close()
  40. test_support.unlink(self.filename)
  41. def mk(self, key):
  42. """Turn key into an appropriate key type for this db"""
  43. # override in child class for RECNO
  44. if sys.version_info[0] < 3 :
  45. return key
  46. else :
  47. return bytes(key, "iso8859-1") # 8 bits
  48. def populateDB(self, d):
  49. for x in string.letters:
  50. d[self.mk('S' + x)] = 10 * x # add a string
  51. d[self.mk('I' + x)] = ord(x) # add an integer
  52. d[self.mk('L' + x)] = [x] * 10 # add a list
  53. inst = DataClass() # add an instance
  54. inst.S = 10 * x
  55. inst.I = ord(x)
  56. inst.L = [x] * 10
  57. d[self.mk('O' + x)] = inst
  58. # overridable in derived classes to affect how the shelf is created/opened
  59. def do_open(self):
  60. self.d = dbshelve.open(self.filename)
  61. # and closed...
  62. def do_close(self):
  63. self.d.close()
  64. def test01_basics(self):
  65. if verbose:
  66. print '\n', '-=' * 30
  67. print "Running %s.test01_basics..." % self.__class__.__name__
  68. self.populateDB(self.d)
  69. self.d.sync()
  70. self.do_close()
  71. self.do_open()
  72. d = self.d
  73. l = len(d)
  74. k = d.keys()
  75. s = d.stat()
  76. f = d.fd()
  77. if verbose:
  78. print "length:", l
  79. print "keys:", k
  80. print "stats:", s
  81. self.assertEqual(0, d.has_key(self.mk('bad key')))
  82. self.assertEqual(1, d.has_key(self.mk('IA')))
  83. self.assertEqual(1, d.has_key(self.mk('OA')))
  84. d.delete(self.mk('IA'))
  85. del d[self.mk('OA')]
  86. self.assertEqual(0, d.has_key(self.mk('IA')))
  87. self.assertEqual(0, d.has_key(self.mk('OA')))
  88. self.assertEqual(len(d), l-2)
  89. values = []
  90. for key in d.keys():
  91. value = d[key]
  92. values.append(value)
  93. if verbose:
  94. print "%s: %s" % (key, value)
  95. self.checkrec(key, value)
  96. dbvalues = d.values()
  97. self.assertEqual(len(dbvalues), len(d.keys()))
  98. if sys.version_info < (2, 6) :
  99. values.sort()
  100. dbvalues.sort()
  101. self.assertEqual(values, dbvalues)
  102. else : # XXX: Convert all to strings. Please, improve
  103. values.sort(key=lambda x : str(x))
  104. dbvalues.sort(key=lambda x : str(x))
  105. self.assertEqual(repr(values), repr(dbvalues))
  106. items = d.items()
  107. self.assertEqual(len(items), len(values))
  108. for key, value in items:
  109. self.checkrec(key, value)
  110. self.assertEqual(d.get(self.mk('bad key')), None)
  111. self.assertEqual(d.get(self.mk('bad key'), None), None)
  112. self.assertEqual(d.get(self.mk('bad key'), 'a string'), 'a string')
  113. self.assertEqual(d.get(self.mk('bad key'), [1, 2, 3]), [1, 2, 3])
  114. d.set_get_returns_none(0)
  115. self.assertRaises(db.DBNotFoundError, d.get, self.mk('bad key'))
  116. d.set_get_returns_none(1)
  117. d.put(self.mk('new key'), 'new data')
  118. self.assertEqual(d.get(self.mk('new key')), 'new data')
  119. self.assertEqual(d[self.mk('new key')], 'new data')
  120. def test02_cursors(self):
  121. if verbose:
  122. print '\n', '-=' * 30
  123. print "Running %s.test02_cursors..." % self.__class__.__name__
  124. self.populateDB(self.d)
  125. d = self.d
  126. count = 0
  127. c = d.cursor()
  128. rec = c.first()
  129. while rec is not None:
  130. count = count + 1
  131. if verbose:
  132. print rec
  133. key, value = rec
  134. self.checkrec(key, value)
  135. # Hack to avoid conversion by 2to3 tool
  136. rec = getattr(c, "next")()
  137. del c
  138. self.assertEqual(count, len(d))
  139. count = 0
  140. c = d.cursor()
  141. rec = c.last()
  142. while rec is not None:
  143. count = count + 1
  144. if verbose:
  145. print rec
  146. key, value = rec
  147. self.checkrec(key, value)
  148. rec = c.prev()
  149. self.assertEqual(count, len(d))
  150. c.set(self.mk('SS'))
  151. key, value = c.current()
  152. self.checkrec(key, value)
  153. del c
  154. def test03_append(self):
  155. # NOTE: this is overridden in RECNO subclass, don't change its name.
  156. if verbose:
  157. print '\n', '-=' * 30
  158. print "Running %s.test03_append..." % self.__class__.__name__
  159. self.assertRaises(dbshelve.DBShelveError,
  160. self.d.append, 'unit test was here')
  161. def test04_iterable(self) :
  162. self.populateDB(self.d)
  163. d = self.d
  164. keys = d.keys()
  165. keyset = set(keys)
  166. self.assertEqual(len(keyset), len(keys))
  167. for key in d :
  168. self.assertIn(key, keyset)
  169. keyset.remove(key)
  170. self.assertEqual(len(keyset), 0)
  171. def checkrec(self, key, value):
  172. # override this in a subclass if the key type is different
  173. if sys.version_info[0] >= 3 :
  174. if isinstance(key, bytes) :
  175. key = key.decode("iso8859-1") # 8 bits
  176. x = key[1]
  177. if key[0] == 'S':
  178. self.assertEqual(type(value), str)
  179. self.assertEqual(value, 10 * x)
  180. elif key[0] == 'I':
  181. self.assertEqual(type(value), int)
  182. self.assertEqual(value, ord(x))
  183. elif key[0] == 'L':
  184. self.assertEqual(type(value), list)
  185. self.assertEqual(value, [x] * 10)
  186. elif key[0] == 'O':
  187. if sys.version_info[0] < 3 :
  188. from types import InstanceType
  189. self.assertEqual(type(value), InstanceType)
  190. else :
  191. self.assertEqual(type(value), DataClass)
  192. self.assertEqual(value.S, 10 * x)
  193. self.assertEqual(value.I, ord(x))
  194. self.assertEqual(value.L, [x] * 10)
  195. else:
  196. self.assert_(0, 'Unknown key type, fix the test')
  197. #----------------------------------------------------------------------
  198. class BasicShelveTestCase(DBShelveTestCase):
  199. def do_open(self):
  200. self.d = dbshelve.DBShelf()
  201. self.d.open(self.filename, self.dbtype, self.dbflags)
  202. def do_close(self):
  203. self.d.close()
  204. class BTreeShelveTestCase(BasicShelveTestCase):
  205. dbtype = db.DB_BTREE
  206. dbflags = db.DB_CREATE
  207. class HashShelveTestCase(BasicShelveTestCase):
  208. dbtype = db.DB_HASH
  209. dbflags = db.DB_CREATE
  210. class ThreadBTreeShelveTestCase(BasicShelveTestCase):
  211. dbtype = db.DB_BTREE
  212. dbflags = db.DB_CREATE | db.DB_THREAD
  213. class ThreadHashShelveTestCase(BasicShelveTestCase):
  214. dbtype = db.DB_HASH
  215. dbflags = db.DB_CREATE | db.DB_THREAD
  216. #----------------------------------------------------------------------
  217. class BasicEnvShelveTestCase(DBShelveTestCase):
  218. def do_open(self):
  219. self.env = db.DBEnv()
  220. self.env.open(self.homeDir,
  221. self.envflags | db.DB_INIT_MPOOL | db.DB_CREATE)
  222. self.filename = os.path.split(self.filename)[1]
  223. self.d = dbshelve.DBShelf(self.env)
  224. self.d.open(self.filename, self.dbtype, self.dbflags)
  225. def do_close(self):
  226. self.d.close()
  227. self.env.close()
  228. def setUp(self) :
  229. self.homeDir = get_new_environment_path()
  230. DBShelveTestCase.setUp(self)
  231. def tearDown(self):
  232. if sys.version_info[0] >= 3 :
  233. from test_all import do_proxy_db_py3k
  234. do_proxy_db_py3k(self._flag_proxy_db_py3k)
  235. self.do_close()
  236. test_support.rmtree(self.homeDir)
  237. class EnvBTreeShelveTestCase(BasicEnvShelveTestCase):
  238. envflags = 0
  239. dbtype = db.DB_BTREE
  240. dbflags = db.DB_CREATE
  241. class EnvHashShelveTestCase(BasicEnvShelveTestCase):
  242. envflags = 0
  243. dbtype = db.DB_HASH
  244. dbflags = db.DB_CREATE
  245. class EnvThreadBTreeShelveTestCase(BasicEnvShelveTestCase):
  246. envflags = db.DB_THREAD
  247. dbtype = db.DB_BTREE
  248. dbflags = db.DB_CREATE | db.DB_THREAD
  249. class EnvThreadHashShelveTestCase(BasicEnvShelveTestCase):
  250. envflags = db.DB_THREAD
  251. dbtype = db.DB_HASH
  252. dbflags = db.DB_CREATE | db.DB_THREAD
  253. #----------------------------------------------------------------------
  254. # test cases for a DBShelf in a RECNO DB.
  255. class RecNoShelveTestCase(BasicShelveTestCase):
  256. dbtype = db.DB_RECNO
  257. dbflags = db.DB_CREATE
  258. def setUp(self):
  259. BasicShelveTestCase.setUp(self)
  260. # pool to assign integer key values out of
  261. self.key_pool = list(range(1, 5000))
  262. self.key_map = {} # map string keys to the number we gave them
  263. self.intkey_map = {} # reverse map of above
  264. def mk(self, key):
  265. if key not in self.key_map:
  266. self.key_map[key] = self.key_pool.pop(0)
  267. self.intkey_map[self.key_map[key]] = key
  268. return self.key_map[key]
  269. def checkrec(self, intkey, value):
  270. key = self.intkey_map[intkey]
  271. BasicShelveTestCase.checkrec(self, key, value)
  272. def test03_append(self):
  273. if verbose:
  274. print '\n', '-=' * 30
  275. print "Running %s.test03_append..." % self.__class__.__name__
  276. self.d[1] = 'spam'
  277. self.d[5] = 'eggs'
  278. self.assertEqual(6, self.d.append('spam'))
  279. self.assertEqual(7, self.d.append('baked beans'))
  280. self.assertEqual('spam', self.d.get(6))
  281. self.assertEqual('spam', self.d.get(1))
  282. self.assertEqual('baked beans', self.d.get(7))
  283. self.assertEqual('eggs', self.d.get(5))
  284. #----------------------------------------------------------------------
  285. def test_suite():
  286. suite = unittest.TestSuite()
  287. suite.addTest(unittest.makeSuite(DBShelveTestCase))
  288. suite.addTest(unittest.makeSuite(BTreeShelveTestCase))
  289. suite.addTest(unittest.makeSuite(HashShelveTestCase))
  290. suite.addTest(unittest.makeSuite(ThreadBTreeShelveTestCase))
  291. suite.addTest(unittest.makeSuite(ThreadHashShelveTestCase))
  292. suite.addTest(unittest.makeSuite(EnvBTreeShelveTestCase))
  293. suite.addTest(unittest.makeSuite(EnvHashShelveTestCase))
  294. suite.addTest(unittest.makeSuite(EnvThreadBTreeShelveTestCase))
  295. suite.addTest(unittest.makeSuite(EnvThreadHashShelveTestCase))
  296. suite.addTest(unittest.makeSuite(RecNoShelveTestCase))
  297. return suite
  298. if __name__ == '__main__':
  299. unittest.main(defaultTest='test_suite')