summaryrefslogtreecommitdiff
path: root/sys/lib/python/test/test_anydbm.py
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@localhost>2011-05-03 11:25:13 +0000
committercinap_lenrek <cinap_lenrek@localhost>2011-05-03 11:25:13 +0000
commit458120dd40db6b4df55a4e96b650e16798ef06a0 (patch)
tree8f82685be24fef97e715c6f5ca4c68d34d5074ee /sys/lib/python/test/test_anydbm.py
parent3a742c699f6806c1145aea5149bf15de15a0afd7 (diff)
add hg and python
Diffstat (limited to 'sys/lib/python/test/test_anydbm.py')
-rw-r--r--sys/lib/python/test/test_anydbm.py95
1 files changed, 95 insertions, 0 deletions
diff --git a/sys/lib/python/test/test_anydbm.py b/sys/lib/python/test/test_anydbm.py
new file mode 100644
index 000000000..0cdc2c321
--- /dev/null
+++ b/sys/lib/python/test/test_anydbm.py
@@ -0,0 +1,95 @@
+#! /usr/bin/env python
+"""Test script for the anydbm module
+ based on testdumbdbm.py
+"""
+
+import os
+import unittest
+import anydbm
+import glob
+from test import test_support
+
+_fname = test_support.TESTFN
+
+def _delete_files():
+ # we don't know the precise name the underlying database uses
+ # so we use glob to locate all names
+ for f in glob.glob(_fname + "*"):
+ try:
+ os.unlink(f)
+ except OSError:
+ pass
+
+class AnyDBMTestCase(unittest.TestCase):
+ _dict = {'0': '',
+ 'a': 'Python:',
+ 'b': 'Programming',
+ 'c': 'the',
+ 'd': 'way',
+ 'f': 'Guido',
+ 'g': 'intended'
+ }
+
+ def __init__(self, *args):
+ unittest.TestCase.__init__(self, *args)
+
+ def test_anydbm_creation(self):
+ f = anydbm.open(_fname, 'c')
+ self.assertEqual(f.keys(), [])
+ for key in self._dict:
+ f[key] = self._dict[key]
+ self.read_helper(f)
+ f.close()
+
+ def test_anydbm_modification(self):
+ self.init_db()
+ f = anydbm.open(_fname, 'c')
+ self._dict['g'] = f['g'] = "indented"
+ self.read_helper(f)
+ f.close()
+
+ def test_anydbm_read(self):
+ self.init_db()
+ f = anydbm.open(_fname, 'r')
+ self.read_helper(f)
+ f.close()
+
+ def test_anydbm_keys(self):
+ self.init_db()
+ f = anydbm.open(_fname, 'r')
+ keys = self.keys_helper(f)
+ f.close()
+
+ def read_helper(self, f):
+ keys = self.keys_helper(f)
+ for key in self._dict:
+ self.assertEqual(self._dict[key], f[key])
+
+ def init_db(self):
+ f = anydbm.open(_fname, 'n')
+ for k in self._dict:
+ f[k] = self._dict[k]
+ f.close()
+
+ def keys_helper(self, f):
+ keys = f.keys()
+ keys.sort()
+ dkeys = self._dict.keys()
+ dkeys.sort()
+ self.assertEqual(keys, dkeys)
+ return keys
+
+ def tearDown(self):
+ _delete_files()
+
+ def setUp(self):
+ _delete_files()
+
+def test_main():
+ try:
+ test_support.run_unittest(AnyDBMTestCase)
+ finally:
+ _delete_files()
+
+if __name__ == "__main__":
+ test_main()