Author: cito
Date: Fri Nov 27 12:48:13 2015
New Revision: 650

Log:
Make tests compatible with Python 2.4

We want to be able to test the 4.x branch with older Python versions
so that we can specify eligible minimum requirements.

Modified:
   branches/4.x/module/tests/dbapi20.py
   branches/4.x/module/tests/test_classic.py
   branches/4.x/module/tests/test_classic_connection.py
   branches/4.x/module/tests/test_classic_dbwrapper.py
   branches/4.x/module/tests/test_classic_largeobj.py
   branches/4.x/module/tests/test_dbapi20.py

Modified: branches/4.x/module/tests/dbapi20.py
==============================================================================
--- branches/4.x/module/tests/dbapi20.py        Fri Nov 27 12:46:15 2015        
(r649)
+++ branches/4.x/module/tests/dbapi20.py        Fri Nov 27 12:48:13 2015        
(r650)
@@ -15,7 +15,11 @@
 __version__ = '$Revision: 1.5 $'[11:-2]
 __author__ = 'Stuart Bishop <[email protected]>'
 
-import unittest
+try:
+    import unittest2 as unittest  # for Python < 2.7
+except ImportError:
+    import unittest
+
 import time
 
 # $Log: not supported by cvs2svn $

Modified: branches/4.x/module/tests/test_classic.py
==============================================================================
--- branches/4.x/module/tests/test_classic.py   Fri Nov 27 12:46:15 2015        
(r649)
+++ branches/4.x/module/tests/test_classic.py   Fri Nov 27 12:48:13 2015        
(r650)
@@ -1,19 +1,19 @@
 #! /usr/bin/python
 
-from __future__ import with_statement
-
 try:
     import unittest2 as unittest  # for Python < 2.7
 except ImportError:
     import unittest
 
 import sys
-from functools import partial
 from time import sleep
 from threading import Thread
 
 from pg import *
 
+# check whether the "with" statement is supported
+no_with = sys.version_info[:2] < (2, 5)
+
 # We need a database to test against.  If LOCAL_PyGreSQL.py exists we will
 # get our information from that.  Otherwise we use the defaults.
 dbname = 'unittest'
@@ -121,10 +121,13 @@
         db.insert('_test_schema', _test=1235)
         self.assertEqual(d['dvar'], 999)
 
+    @unittest.skipIf(no_with, 'context managers not supported')
     def test_context_manager(self):
         db = opendb()
         t = '_test_schema'
         d = dict(_test=1235)
+        # wrap "with" statements to avoid SyntaxError in Python < 2.5
+        exec """from __future__ import with_statement\nif True:
         with db:
             db.insert(t, d)
             d['_test'] += 1
@@ -140,7 +143,7 @@
             d['_test'] += 1
             db.insert(t, d)
             d['_test'] += 1
-            db.insert(t, d)
+            db.insert(t, d)\n"""
         self.assertTrue(db.get(t, 1235))
         self.assertTrue(db.get(t, 1236))
         self.assertRaises(DatabaseError, db.get, t, 1237)
@@ -247,8 +250,11 @@
         two_payloads = options.get('two_payloads')
         db = opendb()
         # Get function under test, can be standalone or DB method.
-        fut = db.notification_handler if run_as_method else partial(
-            NotificationHandler, db)
+        if run_as_method:
+            fut = db.notification_handler
+        else:
+            # functools.partial is not available in Python < 2.5
+            fut = lambda *args: NotificationHandler(db, *args)
         arg_dict = dict(event=None, called=False)
         self.notify_timeout = False
         # Listen for 'event_1'.
@@ -331,8 +337,10 @@
         for run_as_method in False, True:
             db = opendb()
             # Get function under test, can be standalone or DB method.
-            fut = db.notification_handler if run_as_method else partial(
-                NotificationHandler, db)
+            if run_as_method:
+                fut = db.notification_handler
+            else:
+                fut = lambda *args: NotificationHandler(db, *args)
             arg_dict = dict(event=None, called=False)
             self.notify_timeout = False
             # Listen for 'event_1' with timeout of 10ms.
@@ -370,4 +378,4 @@
     failfast = '-l' in sys.argv[1:]
     runner = unittest.TextTestRunner(verbosity=verbosity, failfast=failfast)
     rc = runner.run(suite)
-    sys.exit(1 if rc.errors or rc.failures else 0)
+    sys.exit((rc.errors or rc.failures) and 1 or 0)

Modified: branches/4.x/module/tests/test_classic_connection.py
==============================================================================
--- branches/4.x/module/tests/test_classic_connection.py        Fri Nov 27 
12:46:15 2015        (r649)
+++ branches/4.x/module/tests/test_classic_connection.py        Fri Nov 27 
12:48:13 2015        (r650)
@@ -175,7 +175,7 @@
         query = self.connection.query
         # check that client encoding gets reset
         encoding = query('show client_encoding').getresult()[0][0].upper()
-        changed_encoding = 'LATIN1' if encoding == 'UTF8' else 'UTF8'
+        changed_encoding = encoding == 'UTF8' and 'LATIN1' or 'UTF8'
         self.assertNotEqual(encoding, changed_encoding)
         self.connection.query("set client_encoding=%s" % changed_encoding)
         new_encoding = query('show client_encoding').getresult()[0][0].upper()
@@ -491,8 +491,7 @@
             print r
         except Exception:
             pass
-        finally:
-            sys.stdout = stdout
+        sys.stdout = stdout
         f.seek(0)
         r = f.read()
         f.close()
@@ -525,7 +524,7 @@
             use_bool_default = pg.get_bool()
             pg.set_bool(use_bool)
         try:
-            v_false, v_true = (False, True) if use_bool else 'ft'
+            v_false, v_true = use_bool and (False, True) or 'ft'
             r_false, r_true = [(v_false,)], [(v_true,)]
             self.assertEqual(query("select false").getresult(), r_false)
             self.assertEqual(query("select true").getresult(), r_true)

Modified: branches/4.x/module/tests/test_classic_dbwrapper.py
==============================================================================
--- branches/4.x/module/tests/test_classic_dbwrapper.py Fri Nov 27 12:46:15 
2015        (r649)
+++ branches/4.x/module/tests/test_classic_dbwrapper.py Fri Nov 27 12:48:13 
2015        (r650)
@@ -11,17 +11,20 @@
 
 """
 
-from __future__ import with_statement
-
 try:
     import unittest2 as unittest  # for Python < 2.7
 except ImportError:
     import unittest
 
+import sys
+
 import pg  # the module under test
 
 from decimal import Decimal
 
+# check whether the "with" statement is supported
+no_with = sys.version_info[:2] < (2, 5)
+
 # We need a database to test against.  If LOCAL_PyGreSQL.py exists we will
 # get our information from that.  Otherwise we use the defaults.
 # The current user must have create schema privilege on the database.
@@ -961,10 +964,13 @@
         self.assertEqual(r, [1, 2, 5, 7, 9])
         query("drop table test_table")
 
+    @unittest.skipIf(no_with, 'context managers not supported')
     def testContextManager(self):
         query = self.db.query
         query("drop table if exists test_table")
         query("create table test_table (n integer check(n>0))")
+        # wrap "with" statements to avoid SyntaxError in Python < 2.5
+        exec """from __future__ import with_statement\nif True:
         with self.db:
             query("insert into test_table values (1)")
             query("insert into test_table values (2)")
@@ -984,7 +990,7 @@
         except pg.ProgrammingError, error:
             self.assertTrue('check' in str(error))
         with self.db:
-            query("insert into test_table values (7)")
+            query("insert into test_table values (7)")\n"""
         r = [r[0] for r in query(
             "select * from test_table order by 1").getresult()]
         self.assertEqual(r, [1, 2, 5, 7])

Modified: branches/4.x/module/tests/test_classic_largeobj.py
==============================================================================
--- branches/4.x/module/tests/test_classic_largeobj.py  Fri Nov 27 12:46:15 
2015        (r649)
+++ branches/4.x/module/tests/test_classic_largeobj.py  Fri Nov 27 12:48:13 
2015        (r650)
@@ -398,8 +398,7 @@
             print self.obj
         except Exception:
             pass
-        finally:
-            sys.stdout = stdout
+        sys.stdout = stdout
         f.seek(0)
         r = f.read()
         f.close()

Modified: branches/4.x/module/tests/test_dbapi20.py
==============================================================================
--- branches/4.x/module/tests/test_dbapi20.py   Fri Nov 27 12:46:15 2015        
(r649)
+++ branches/4.x/module/tests/test_dbapi20.py   Fri Nov 27 12:48:13 2015        
(r650)
@@ -1,12 +1,20 @@
 #! /usr/bin/python
 # $Id$
 
-from __future__ import with_statement
+try:
+    import unittest2 as unittest  # for Python < 2.7
+except ImportError:
+    import unittest
+
+import sys
 
-import unittest
-import dbapi20
 import pgdb
 
+import dbapi20
+
+# check whether the "with" statement is supported
+no_with = sys.version_info[:2] < (2, 5)
+
 # We need a database to test against.
 # If LOCAL_PyGreSQL.py exists we will get our information from that.
 # Otherwise we use the defaults.
@@ -217,9 +225,12 @@
         self.assertEqual(con.DataError, pgdb.DataError)
         self.assertEqual(con.NotSupportedError, pgdb.NotSupportedError)
 
+    @unittest.skipIf(no_with, 'context managers not supported')
     def test_connection_as_contextmanager(self):
         table = self.table_prefix + 'booze'
         con = self._connect()
+        # wrap "with" statements to avoid SyntaxError in Python < 2.5
+        exec """from __future__ import with_statement\nif True:
         try:
             cur = con.cursor()
             cur.execute("create table %s (n smallint check(n!=4))" % table)
@@ -248,7 +259,7 @@
             rows = cur.fetchall()
             rows = [row[0] for row in rows]
         finally:
-            con.close()
+            con.close()\n"""
         self.assertEqual(rows, [1, 2, 5, 6, 9])
 
     def test_cursor_connection(self):
@@ -257,10 +268,13 @@
         self.assertEqual(cur.connection, con)
         cur.close()
 
+    @unittest.skipIf(no_with, 'context managers not supported')
     def test_cursor_as_contextmanager(self):
         con = self._connect()
+        # wrap "with" statements to avoid SyntaxError in Python < 2.5
+        exec """from __future__ import with_statement\nif True:
         with con.cursor() as cur:
-            self.assertEqual(cur.connection, con)
+            self.assertEqual(cur.connection, con)\n"""
 
     def test_pgdb_type(self):
         self.assertEqual(pgdb.STRING, pgdb.STRING)
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql

Reply via email to