PgManager: Fix logging, log queries before executing, possible exceptions are logged after. Add tests for RowDict. Add tests.py - runs all tests.
--- a/pgtoolkit/pgmanager.py Wed Sep 26 23:32:02 2012 +0200
+++ b/pgtoolkit/pgmanager.py Sat Sep 29 12:08:47 2012 +0200
@@ -139,32 +139,29 @@
def __contains__(self, key):
return key in self._dict
+ def keys(self):
+ return self._dict.keys()
+
class Cursor(psycopg2.extensions.cursor):
def execute(self, query, args=None):
+ # log query before executing
+ self._log_query(query, args)
try:
return super(Cursor, self).execute(query, args)
except DatabaseError:
- # log query and exception
- self._log_query(query, args)
self._log_exception()
raise
- else:
- # log query from self.query
- self._log_query()
def callproc(self, procname, args=None):
+ # log query before executing (not query actually executed but should correspond)
+ self._log_query(self._build_callproc_query(procname, len(args)), args)
try:
return super(Cursor, self).callproc(procname, args)
except DatabaseError:
- # log query and exception
- self._log_query(self._build_callproc_query(procname, len(args)), args)
self._log_exception()
raise
- else:
- # log query from self.query
- self._log_query()
def row_dict(self, row, lstrip=None):
adjustname = lambda a: a
@@ -222,7 +219,7 @@
log_sql.exception('[%s] exception:' % (name,))
def _build_callproc_query(self, procname, num_args):
- return 'SELECT * FROM %s(%s)' %(procname, ', '.join('%s'))
+ return 'SELECT * FROM %s(%s)' % (procname, ', '.join(['%s'] * num_args))
class Connection(psycopg2.extensions.connection):
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests.py Sat Sep 29 12:08:47 2012 +0200
@@ -0,0 +1,16 @@
+#!/usr/bin/env python3
+
+import unittest
+
+from tests import test_rowdict
+from tests import testpgmanager
+
+loader = unittest.TestLoader()
+
+suite = unittest.TestSuite()
+suite.addTests(loader.loadTestsFromModule(test_rowdict))
+suite.addTests(loader.loadTestsFromModule(testpgmanager))
+
+runner = unittest.TextTestRunner(verbosity=2)
+result = runner.run(suite)
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_rowdict.py Sat Sep 29 12:08:47 2012 +0200
@@ -0,0 +1,31 @@
+#!/usr/bin/env python3
+
+from pgtoolkit.pgmanager import RowDict
+
+import unittest
+
+
+class TestRowDict(unittest.TestCase):
+ def setUp(self):
+ self.rowdict = RowDict([('id', 123), ('name', 'hello')])
+
+ def test_rowdict_as_kwargs(self):
+ rowdict_id = (lambda **kwargs: kwargs['id']) (**self.rowdict)
+ self.assertEqual(rowdict_id, 123)
+
+ def test_rowdict_key_access(self):
+ self.assertEqual(self.rowdict['id'], 123)
+
+ def test_rowdict_attr_access(self):
+ self.assertEqual(self.rowdict.name, 'hello')
+
+ def test_rowdict_index_access(self):
+ self.assertEqual(self.rowdict[0], 123)
+
+ def test_rowdict_unpack(self):
+ _id, name = self.rowdict
+ self.assertEqual(name, 'hello')
+
+
+if __name__ == '__main__':
+ unittest.main()
--- a/tests/testpgmanager.py Wed Sep 26 23:32:02 2012 +0200
+++ b/tests/testpgmanager.py Sat Sep 29 12:08:47 2012 +0200
@@ -1,10 +1,10 @@
#!/usr/bin/env python3.2
+from tests.config import Config
+from pgtoolkit import pgmanager
+
import unittest
-from config import Config
-from pgtoolkit import pgmanager
-
class TestSuite(unittest.TestCase):