Changeset 650


Ignore:
Timestamp:
Nov 27, 2015, 12:48:13 PM (4 years ago)
Author:
cito
Message:

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.

Location:
branches/4.x/module/tests
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • branches/4.x/module/tests/dbapi20.py

    r640 r650  
    1616__author__ = 'Stuart Bishop <zen@shangri-la.dropbear.id.au>'
    1717
    18 import unittest
     18try:
     19    import unittest2 as unittest  # for Python < 2.7
     20except ImportError:
     21    import unittest
     22
    1923import time
    2024
  • branches/4.x/module/tests/test_classic.py

    r648 r650  
    11#! /usr/bin/python
    2 
    3 from __future__ import with_statement
    42
    53try:
     
    97
    108import sys
    11 from functools import partial
    129from time import sleep
    1310from threading import Thread
    1411
    1512from pg import *
     13
     14# check whether the "with" statement is supported
     15no_with = sys.version_info[:2] < (2, 5)
    1616
    1717# We need a database to test against.  If LOCAL_PyGreSQL.py exists we will
     
    122122        self.assertEqual(d['dvar'], 999)
    123123
     124    @unittest.skipIf(no_with, 'context managers not supported')
    124125    def test_context_manager(self):
    125126        db = opendb()
    126127        t = '_test_schema'
    127128        d = dict(_test=1235)
     129        # wrap "with" statements to avoid SyntaxError in Python < 2.5
     130        exec """from __future__ import with_statement\nif True:
    128131        with db:
    129132            db.insert(t, d)
     
    141144            db.insert(t, d)
    142145            d['_test'] += 1
    143             db.insert(t, d)
     146            db.insert(t, d)\n"""
    144147        self.assertTrue(db.get(t, 1235))
    145148        self.assertTrue(db.get(t, 1236))
     
    248251        db = opendb()
    249252        # Get function under test, can be standalone or DB method.
    250         fut = db.notification_handler if run_as_method else partial(
    251             NotificationHandler, db)
     253        if run_as_method:
     254            fut = db.notification_handler
     255        else:
     256            # functools.partial is not available in Python < 2.5
     257            fut = lambda *args: NotificationHandler(db, *args)
    252258        arg_dict = dict(event=None, called=False)
    253259        self.notify_timeout = False
     
    332338            db = opendb()
    333339            # Get function under test, can be standalone or DB method.
    334             fut = db.notification_handler if run_as_method else partial(
    335                 NotificationHandler, db)
     340            if run_as_method:
     341                fut = db.notification_handler
     342            else:
     343                fut = lambda *args: NotificationHandler(db, *args)
    336344            arg_dict = dict(event=None, called=False)
    337345            self.notify_timeout = False
     
    371379    runner = unittest.TextTestRunner(verbosity=verbosity, failfast=failfast)
    372380    rc = runner.run(suite)
    373     sys.exit(1 if rc.errors or rc.failures else 0)
     381    sys.exit((rc.errors or rc.failures) and 1 or 0)
  • branches/4.x/module/tests/test_classic_connection.py

    r647 r650  
    176176        # check that client encoding gets reset
    177177        encoding = query('show client_encoding').getresult()[0][0].upper()
    178         changed_encoding = 'LATIN1' if encoding == 'UTF8' else 'UTF8'
     178        changed_encoding = encoding == 'UTF8' and 'LATIN1' or 'UTF8'
    179179        self.assertNotEqual(encoding, changed_encoding)
    180180        self.connection.query("set client_encoding=%s" % changed_encoding)
     
    492492        except Exception:
    493493            pass
    494         finally:
    495             sys.stdout = stdout
     494        sys.stdout = stdout
    496495        f.seek(0)
    497496        r = f.read()
     
    526525            pg.set_bool(use_bool)
    527526        try:
    528             v_false, v_true = (False, True) if use_bool else 'ft'
     527            v_false, v_true = use_bool and (False, True) or 'ft'
    529528            r_false, r_true = [(v_false,)], [(v_true,)]
    530529            self.assertEqual(query("select false").getresult(), r_false)
  • branches/4.x/module/tests/test_classic_dbwrapper.py

    r644 r650  
    1111
    1212"""
    13 
    14 from __future__ import with_statement
    1513
    1614try:
     
    1917    import unittest
    2018
     19import sys
     20
    2121import pg  # the module under test
    2222
    2323from decimal import Decimal
     24
     25# check whether the "with" statement is supported
     26no_with = sys.version_info[:2] < (2, 5)
    2427
    2528# We need a database to test against.  If LOCAL_PyGreSQL.py exists we will
     
    962965        query("drop table test_table")
    963966
     967    @unittest.skipIf(no_with, 'context managers not supported')
    964968    def testContextManager(self):
    965969        query = self.db.query
    966970        query("drop table if exists test_table")
    967971        query("create table test_table (n integer check(n>0))")
     972        # wrap "with" statements to avoid SyntaxError in Python < 2.5
     973        exec """from __future__ import with_statement\nif True:
    968974        with self.db:
    969975            query("insert into test_table values (1)")
     
    985991            self.assertTrue('check' in str(error))
    986992        with self.db:
    987             query("insert into test_table values (7)")
     993            query("insert into test_table values (7)")\n"""
    988994        r = [r[0] for r in query(
    989995            "select * from test_table order by 1").getresult()]
  • branches/4.x/module/tests/test_classic_largeobj.py

    r645 r650  
    399399        except Exception:
    400400            pass
    401         finally:
    402             sys.stdout = stdout
     401        sys.stdout = stdout
    403402        f.seek(0)
    404403        r = f.read()
  • branches/4.x/module/tests/test_dbapi20.py

    r640 r650  
    22# $Id$
    33
    4 from __future__ import with_statement
    5 
    6 import unittest
     4try:
     5    import unittest2 as unittest  # for Python < 2.7
     6except ImportError:
     7    import unittest
     8
     9import sys
     10
     11import pgdb
     12
    713import dbapi20
    8 import pgdb
     14
     15# check whether the "with" statement is supported
     16no_with = sys.version_info[:2] < (2, 5)
    917
    1018# We need a database to test against.
     
    218226        self.assertEqual(con.NotSupportedError, pgdb.NotSupportedError)
    219227
     228    @unittest.skipIf(no_with, 'context managers not supported')
    220229    def test_connection_as_contextmanager(self):
    221230        table = self.table_prefix + 'booze'
    222231        con = self._connect()
     232        # wrap "with" statements to avoid SyntaxError in Python < 2.5
     233        exec """from __future__ import with_statement\nif True:
    223234        try:
    224235            cur = con.cursor()
     
    249260            rows = [row[0] for row in rows]
    250261        finally:
    251             con.close()
     262            con.close()\n"""
    252263        self.assertEqual(rows, [1, 2, 5, 6, 9])
    253264
     
    258269        cur.close()
    259270
     271    @unittest.skipIf(no_with, 'context managers not supported')
    260272    def test_cursor_as_contextmanager(self):
    261273        con = self._connect()
     274        # wrap "with" statements to avoid SyntaxError in Python < 2.5
     275        exec """from __future__ import with_statement\nif True:
    262276        with con.cursor() as cur:
    263             self.assertEqual(cur.connection, con)
     277            self.assertEqual(cur.connection, con)\n"""
    264278
    265279    def test_pgdb_type(self):
Note: See TracChangeset for help on using the changeset viewer.