Changeset 602


Ignore:
Timestamp:
Nov 22, 2015, 11:12:29 AM (4 years ago)
Author:
cito
Message:

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.

Location:
trunk/module
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/module/TEST_PyGreSQL_classic.py

    r599 r602  
    7474        """Make sure that invalid table names are caught"""
    7575        db = opendb()
    76         self.failUnlessRaises(ProgrammingError, db.get_attnames, 'x.y.z')
     76        self.assertRaises(ProgrammingError, db.get_attnames, 'x.y.z')
    7777
    7878    def test_schema(self):
     
    114114        db.get('_test_schema', 1234)
    115115        db.get('_test_schema', 1234, keyname='_test')
    116         self.failUnlessRaises(ProgrammingError, db.get, '_test_vschema', 1234)
     116        self.assertRaises(ProgrammingError, db.get, '_test_vschema', 1234)
    117117        db.get('_test_vschema', 1234, keyname='_test')
    118118
     
    164164        except DatabaseError as error:
    165165            # currently PyGreSQL does not support IntegrityError
    166             self.assert_(isinstance(error, ProgrammingError))
     166            self.assertTrue(isinstance(error, ProgrammingError))
    167167            # the SQLSTATE error code for unique violation is 23505
    168168            self.assertEqual(error.sqlstate, '23505')
  • trunk/module/TEST_PyGreSQL_classic_connection.py

    r597 r602  
    10731073
    10741074
     1075class TestStandaloneEscapeFunctions(unittest.TestCase):
     1076    """"Test pg escape and unescape functions.
     1077
     1078    The libpq interface memorizes some parameters of the last opened
     1079    connection that influence the result of these functions.  Therefore
     1080    we need to open a connection with fixed parameters prior to testing
     1081    in order to ensure that the tests always run under the same conditions.
     1082    That's why these tests are included in this test module.
     1083
     1084    """
     1085
     1086    @classmethod
     1087    def setUpClass(cls):
     1088        query = connect().query
     1089        query('set client_encoding=sql_ascii')
     1090        query('set standard_conforming_strings=off')
     1091        query('set bytea_output=escape')
     1092
     1093    def testEscapeString(self):
     1094        f = pg.escape_string
     1095        r = f(b'plain')
     1096        self.assertIsInstance(r, str)
     1097        self.assertEqual(r, 'plain')
     1098        r = f(u'plain')
     1099        self.assertIsInstance(r, str)
     1100        self.assertEqual(r, 'plain')
     1101        r = f(u"das is' kÀse".encode('utf-8'))
     1102        self.assertIsInstance(r, str)
     1103        self.assertEqual(r, "das is'' kÀse")
     1104        if unicode_strings:
     1105            r = f("das is' kÀse")
     1106            self.assertIsInstance(r, str)
     1107            self.assertEqual(r, "das is'' kÀse")
     1108        r = f(r"It's bad to have a \ inside.")
     1109        self.assertEqual(r, r"It''s bad to have a \\ inside.")
     1110
     1111    def testEscapeBytea(self):
     1112        f = pg.escape_bytea
     1113        r = f(b'plain')
     1114        self.assertIsInstance(r, str)
     1115        self.assertEqual(r, 'plain')
     1116        r = f(u'plain')
     1117        self.assertIsInstance(r, str)
     1118        self.assertEqual(r, 'plain')
     1119        r = f(u"das is' kÀse".encode('utf-8'))
     1120        self.assertIsInstance(r, str)
     1121        self.assertEqual(r, r"das is'' k\\303\\244se")
     1122        if unicode_strings:
     1123            r = f("das is' kÀse")
     1124            self.assertIsInstance(r, str)
     1125            self.assertEqual(r, r"das is'' k\\303\\244se")
     1126        r = f(b'O\x00ps\xff!')
     1127        self.assertEqual(r, r'O\\000ps\\377!')
     1128
     1129    def testUnescapeBytea(self):
     1130        f = pg.unescape_bytea
     1131        r = f(b'plain')
     1132        self.assertIsInstance(r, str)
     1133        self.assertEqual(r, 'plain')
     1134        r = f(u'plain')
     1135        self.assertIsInstance(r, str)
     1136        self.assertEqual(r, 'plain')
     1137        r = f(b"das is' k\\303\\244se")
     1138        self.assertIsInstance(r, str)
     1139        self.assertEqual(r, "das is' kÀse")
     1140        r = f(u"das is' k\\303\\244se")
     1141        self.assertIsInstance(r, str)
     1142        self.assertEqual(r, "das is' kÀse")
     1143        r = f(r'O\\000ps\\377!')
     1144        self.assertEqual(r, r'O\000ps\377!')
     1145
     1146
    10751147if __name__ == '__main__':
    10761148    unittest.main()
  • trunk/module/TEST_PyGreSQL_classic_dbwrapper.py

    r598 r602  
    287287    def setUp(self):
    288288        self.db = DB()
    289         self.db.query("set lc_monetary='C'")
    290         self.db.query('set bytea_output=hex')
    291         self.db.query('set standard_conforming_strings=on')
     289        query = self.db.query
     290        query('set client_encoding=utf8')
     291        query('set standard_conforming_strings=on')
     292        query('set bytea_output=hex')
     293        query("set lc_monetary='C'")
    292294
    293295    def tearDown(self):
  • trunk/module/TEST_PyGreSQL_classic_functions.py

    r573 r602  
    263263
    264264class TestEscapeFunctions(unittest.TestCase):
    265     """"Test pg escape and unescape functions."""
     265    """"Test pg escape and unescape functions.
     266
     267    The libpq interface memorizes some parameters of the last opened
     268    connection that influence the result of these functions.
     269    Therefore we cannot do rigid tests of these functions here.
     270    We leave this for the test module that runs with a database.
     271
     272    """
    266273
    267274    def testEscapeString(self):
     
    273280        self.assertIsInstance(r, str)
    274281        self.assertEqual(r, 'plain')
    275         r = f(u"das is' kÀse".encode('utf-8'))
    276         self.assertIsInstance(r, str)
    277         self.assertEqual(r, "das is'' kÀse")
    278         if unicode_strings:
    279             r = f("das is' kÀse")
    280             self.assertIsInstance(r, str)
    281             self.assertEqual(r, "das is'' kÀse")
    282         r = f(r"It's fine to have a \ inside.")
    283         self.assertEqual(r, r"It''s fine to have a \\ inside.")
     282        r = f("that's cheese")
     283        self.assertIsInstance(r, str)
     284        self.assertEqual(r, "that''s cheese")
    284285
    285286    def testEscapeBytea(self):
     
    291292        self.assertIsInstance(r, str)
    292293        self.assertEqual(r, 'plain')
    293         r = f(u"das is' kÀse".encode('utf-8'))
    294         self.assertIsInstance(r, str)
    295         self.assertEqual(r, r"das is'' k\\303\\244se")
    296         if unicode_strings:
    297             r = f("das is' kÀse")
    298             self.assertIsInstance(r, str)
    299             self.assertEqual(r, r"das is'' k\\303\\244se")
    300         r = f(b'O\x00ps\xff!')
    301         self.assertEqual(r, r'O\\000ps\\377!')
     294        r = f("that's cheese")
     295        self.assertIsInstance(r, str)
     296        self.assertEqual(r, "that''s cheese")
    302297
    303298    def testUnescapeBytea(self):
     
    309304        self.assertIsInstance(r, str)
    310305        self.assertEqual(r, 'plain')
    311         r = f(b"das is' k\\303\\244se")
    312         self.assertIsInstance(r, str)
    313         self.assertEqual(r, "das is' kÀse")
    314         r = f(u"das is' k\\303\\244se")
    315         self.assertIsInstance(r, str)
    316         self.assertEqual(r, "das is' kÀse")
    317         r = f(r'O\\000ps\\377!')
    318         self.assertEqual(r, r'O\000ps\377!')
     306        r = f("that's cheese")
     307        self.assertIsInstance(r, str)
     308        self.assertEqual(r, "that's cheese")
    319309
    320310
Note: See TracChangeset for help on using the changeset viewer.