Author: cito
Date: Sun Nov 22 11:12:29 2015
New Revision: 602
Log:
Account for statefulness of some module functions
Some functions on the module level used to escape strings memorize
the state of the last opened connection, so tests could give different
results when other tests had been running in the same interpreter
before. This can happen when you run multiple test modules in one
go using the unittest command line runner. Therefore, rigid testing
of these functions has been moved to the test module which uses
database connections, where we can add an appropriate fixture.
Modified:
trunk/module/TEST_PyGreSQL_classic.py
trunk/module/TEST_PyGreSQL_classic_connection.py
trunk/module/TEST_PyGreSQL_classic_dbwrapper.py
trunk/module/TEST_PyGreSQL_classic_functions.py
Modified: trunk/module/TEST_PyGreSQL_classic.py
==============================================================================
--- trunk/module/TEST_PyGreSQL_classic.py Sun Nov 22 09:16:50 2015
(r601)
+++ trunk/module/TEST_PyGreSQL_classic.py Sun Nov 22 11:12:29 2015
(r602)
@@ -73,7 +73,7 @@
def test_invalidname(self):
"""Make sure that invalid table names are caught"""
db = opendb()
- self.failUnlessRaises(ProgrammingError, db.get_attnames, 'x.y.z')
+ self.assertRaises(ProgrammingError, db.get_attnames, 'x.y.z')
def test_schema(self):
"""Does it differentiate the same table name in different schemas"""
@@ -113,7 +113,7 @@
db.query("INSERT INTO _test_schema VALUES (1234)")
db.get('_test_schema', 1234)
db.get('_test_schema', 1234, keyname='_test')
- self.failUnlessRaises(ProgrammingError, db.get, '_test_vschema', 1234)
+ self.assertRaises(ProgrammingError, db.get, '_test_vschema', 1234)
db.get('_test_vschema', 1234, keyname='_test')
def test_params(self):
@@ -163,7 +163,7 @@
db.query("INSERT INTO _test_schema VALUES (1234)")
except DatabaseError as error:
# currently PyGreSQL does not support IntegrityError
- self.assert_(isinstance(error, ProgrammingError))
+ self.assertTrue(isinstance(error, ProgrammingError))
# the SQLSTATE error code for unique violation is 23505
self.assertEqual(error.sqlstate, '23505')
Modified: trunk/module/TEST_PyGreSQL_classic_connection.py
==============================================================================
--- trunk/module/TEST_PyGreSQL_classic_connection.py Sun Nov 22 09:16:50
2015 (r601)
+++ trunk/module/TEST_PyGreSQL_classic_connection.py Sun Nov 22 11:12:29
2015 (r602)
@@ -1072,5 +1072,77 @@
pg.set_namedresult(_namedresult)
+class TestStandaloneEscapeFunctions(unittest.TestCase):
+ """"Test pg escape and unescape functions.
+
+ The libpq interface memorizes some parameters of the last opened
+ connection that influence the result of these functions. Therefore
+ we need to open a connection with fixed parameters prior to testing
+ in order to ensure that the tests always run under the same conditions.
+ That's why these tests are included in this test module.
+
+ """
+
+ @classmethod
+ def setUpClass(cls):
+ query = connect().query
+ query('set client_encoding=sql_ascii')
+ query('set standard_conforming_strings=off')
+ query('set bytea_output=escape')
+
+ def testEscapeString(self):
+ f = pg.escape_string
+ r = f(b'plain')
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, 'plain')
+ r = f(u'plain')
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, 'plain')
+ r = f(u"das is' käse".encode('utf-8'))
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, "das is'' käse")
+ if unicode_strings:
+ r = f("das is' käse")
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, "das is'' käse")
+ r = f(r"It's bad to have a \ inside.")
+ self.assertEqual(r, r"It''s bad to have a \\ inside.")
+
+ def testEscapeBytea(self):
+ f = pg.escape_bytea
+ r = f(b'plain')
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, 'plain')
+ r = f(u'plain')
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, 'plain')
+ r = f(u"das is' käse".encode('utf-8'))
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, r"das is'' k\\303\\244se")
+ if unicode_strings:
+ r = f("das is' käse")
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, r"das is'' k\\303\\244se")
+ r = f(b'O\x00ps\xff!')
+ self.assertEqual(r, r'O\\000ps\\377!')
+
+ def testUnescapeBytea(self):
+ f = pg.unescape_bytea
+ r = f(b'plain')
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, 'plain')
+ r = f(u'plain')
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, 'plain')
+ r = f(b"das is' k\\303\\244se")
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, "das is' käse")
+ r = f(u"das is' k\\303\\244se")
+ self.assertIsInstance(r, str)
+ self.assertEqual(r, "das is' käse")
+ r = f(r'O\\000ps\\377!')
+ self.assertEqual(r, r'O\000ps\377!')
+
+
if __name__ == '__main__':
unittest.main()
Modified: trunk/module/TEST_PyGreSQL_classic_dbwrapper.py
==============================================================================
--- trunk/module/TEST_PyGreSQL_classic_dbwrapper.py Sun Nov 22 09:16:50
2015 (r601)
+++ trunk/module/TEST_PyGreSQL_classic_dbwrapper.py Sun Nov 22 11:12:29
2015 (r602)
@@ -286,9 +286,11 @@
def setUp(self):
self.db = DB()
- self.db.query("set lc_monetary='C'")
- self.db.query('set bytea_output=hex')
- self.db.query('set standard_conforming_strings=on')
+ query = self.db.query
+ query('set client_encoding=utf8')
+ query('set standard_conforming_strings=on')
+ query('set bytea_output=hex')
+ query("set lc_monetary='C'")
def tearDown(self):
self.db.close()
Modified: trunk/module/TEST_PyGreSQL_classic_functions.py
==============================================================================
--- trunk/module/TEST_PyGreSQL_classic_functions.py Sun Nov 22 09:16:50
2015 (r601)
+++ trunk/module/TEST_PyGreSQL_classic_functions.py Sun Nov 22 11:12:29
2015 (r602)
@@ -262,7 +262,14 @@
class TestEscapeFunctions(unittest.TestCase):
- """"Test pg escape and unescape functions."""
+ """"Test pg escape and unescape functions.
+
+ The libpq interface memorizes some parameters of the last opened
+ connection that influence the result of these functions.
+ Therefore we cannot do rigid tests of these functions here.
+ We leave this for the test module that runs with a database.
+
+ """
def testEscapeString(self):
f = pg.escape_string
@@ -272,15 +279,9 @@
r = f(u'plain')
self.assertIsInstance(r, str)
self.assertEqual(r, 'plain')
- r = f(u"das is' käse".encode('utf-8'))
+ r = f("that's cheese")
self.assertIsInstance(r, str)
- self.assertEqual(r, "das is'' käse")
- if unicode_strings:
- r = f("das is' käse")
- self.assertIsInstance(r, str)
- self.assertEqual(r, "das is'' käse")
- r = f(r"It's fine to have a \ inside.")
- self.assertEqual(r, r"It''s fine to have a \\ inside.")
+ self.assertEqual(r, "that''s cheese")
def testEscapeBytea(self):
f = pg.escape_bytea
@@ -290,15 +291,9 @@
r = f(u'plain')
self.assertIsInstance(r, str)
self.assertEqual(r, 'plain')
- r = f(u"das is' käse".encode('utf-8'))
+ r = f("that's cheese")
self.assertIsInstance(r, str)
- self.assertEqual(r, r"das is'' k\\303\\244se")
- if unicode_strings:
- r = f("das is' käse")
- self.assertIsInstance(r, str)
- self.assertEqual(r, r"das is'' k\\303\\244se")
- r = f(b'O\x00ps\xff!')
- self.assertEqual(r, r'O\\000ps\\377!')
+ self.assertEqual(r, "that''s cheese")
def testUnescapeBytea(self):
f = pg.unescape_bytea
@@ -308,14 +303,9 @@
r = f(u'plain')
self.assertIsInstance(r, str)
self.assertEqual(r, 'plain')
- r = f(b"das is' k\\303\\244se")
- self.assertIsInstance(r, str)
- self.assertEqual(r, "das is' käse")
- r = f(u"das is' k\\303\\244se")
+ r = f("that's cheese")
self.assertIsInstance(r, str)
- self.assertEqual(r, "das is' käse")
- r = f(r'O\\000ps\\377!')
- self.assertEqual(r, r'O\000ps\377!')
+ self.assertEqual(r, "that's cheese")
class TestConfigFunctions(unittest.TestCase):
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql