From 67afd678b0d5e84aeb743c5e45b04d141701f4f0 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sun, 11 Nov 2007 10:40:12 +0000 Subject: [PATCH] Added 'make check' target, running all the available tests. Most of the updates have been provided by James Henstridge. Closes ticket #195. --- Makefile | 15 +++++++++++++++ tests/__init__.py | 34 +++++++++++++++++++--------------- tests/bugX000.py | 23 +++++++++++++++++------ tests/extras_dictcursor.py | 28 ++++++++++++++-------------- tests/test_psycopg2_dbapi20.py | 30 ++++++++++++------------------ tests/test_quote.py | 1 + tests/test_transaction.py | 1 + tests/types_basic.py | 22 ++++++++++------------ 8 files changed, 89 insertions(+), 65 deletions(-) create mode 100644 Makefile mode change 100644 => 100755 tests/__init__.py mode change 100644 => 100755 tests/bugX000.py mode change 100644 => 100755 tests/extras_dictcursor.py mode change 100644 => 100755 tests/test_psycopg2_dbapi20.py mode change 100644 => 100755 tests/test_quote.py mode change 100644 => 100755 tests/test_transaction.py mode change 100644 => 100755 tests/types_basic.py diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..fb219534 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +PYTHON = python$(PYTHON_VERSION) +#PYTHON_VERSION = 2.4 + +TESTDB = psycopg2_test + +all: + @: + +check: + @echo "* Creating $(TESTDB)" + @if psql -l | grep -q " $(TESTDB) "; then \ + dropdb $(TESTDB) >/dev/null; \ + fi + createdb $(TESTDB) + PSYCOPG2_TESTDB=$(TESTDB) $(PYTHON) tests/__init__.py --verbose diff --git a/tests/__init__.py b/tests/__init__.py old mode 100644 new mode 100755 index 20553ccc..aa1562a9 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,21 +1,25 @@ +#!/usr/bin/env python import os import unittest -dbname = os.environ.get('PSYCOPG2_TESTDB', 'test') +dbname = os.environ.get('PSYCOPG2_TESTDB', 'psycopg2_test') -import test_psycopg2_dbapi20 -import test_transaction -import types_basic -import extras_dictcursor - -def test_suite(): - suite = unittest.TestSuite() - suite.addTest(test_psycopg2_dbapi20.test_suite()) - suite.addTest(test_transaction.test_suite()) - suite.addTest(types_basic.test_suite()) - suite.addTest(extras_dictcursor.test_suite()) - return suite +import bugX000 +import extras_dictcursor +import test_psycopg2_dbapi20 +import test_quote +import test_transaction +import types_basic -if __name__ == "__main__": - unittest.main() +def test_suite(): + suite = unittest.TestSuite() + suite.addTest(bugX000.test_suite()) + suite.addTest(extras_dictcursor.test_suite()) + suite.addTest(test_psycopg2_dbapi20.test_suite()) + suite.addTest(test_quote.test_suite()) + suite.addTest(test_transaction.test_suite()) + suite.addTest(types_basic.test_suite()) + return suite +if __name__ == '__main__': + unittest.main(defaultTest='test_suite') diff --git a/tests/bugX000.py b/tests/bugX000.py old mode 100644 new mode 100755 index 486d3201..f39a8268 --- a/tests/bugX000.py +++ b/tests/bugX000.py @@ -1,10 +1,21 @@ +#!/usr/bin/env python import psycopg2 import time +import unittest -d1 = psycopg2.Date(2002,12,25) -d2 = psycopg2.DateFromTicks(time.mktime((2002,12,25,0,0,0,0,0,0))) -t1 = psycopg2.Time(13,45,30) -t2 = psycopg2.TimeFromTicks(time.mktime((2001,1,1,13,45,30,0,0,0))) -t1 = psycopg2.Timestamp(2002,12,25,13,45,30) -t2 = psycopg2.TimestampFromTicks( time.mktime((2002,12,25,13,45,30,0,0,0))) +class DateTimeAllocationBugTestCase(unittest.TestCase): + def test_date_time_allocation_bug(self): + d1 = psycopg2.Date(2002,12,25) + d2 = psycopg2.DateFromTicks(time.mktime((2002,12,25,0,0,0,0,0,0))) + t1 = psycopg2.Time(13,45,30) + t2 = psycopg2.TimeFromTicks(time.mktime((2001,1,1,13,45,30,0,0,0))) + t1 = psycopg2.Timestamp(2002,12,25,13,45,30) + t2 = psycopg2.TimestampFromTicks( + time.mktime((2002,12,25,13,45,30,0,0,0))) + +def test_suite(): + return unittest.TestLoader().loadTestsFromName(__name__) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/extras_dictcursor.py b/tests/extras_dictcursor.py old mode 100644 new mode 100755 index 270d37f5..c8e2222d --- a/tests/extras_dictcursor.py +++ b/tests/extras_dictcursor.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # extras_dictcursor - test if DictCursor extension class works # # Copyright (C) 2004 Federico Di Gregorio @@ -14,17 +15,22 @@ import psycopg2 import psycopg2.extras -from unittest import TestCase, TestSuite, main +import unittest + +import tests -class ExtrasDictCursorTests(TestCase): +class ExtrasDictCursorTests(unittest.TestCase): """Test if DictCursor extension class works.""" def setUp(self): - self.conn = psycopg2.connect("dbname=test") + self.conn = psycopg2.connect("dbname=%s" % tests.dbname) curs = self.conn.cursor() - curs.execute("CREATE TABLE ExtrasDictCursorTests (foo text)") - + curs.execute("CREATE TEMPORARY TABLE ExtrasDictCursorTests (foo text)") + + def tearDown(self): + self.conn.close() + def testDictCursor(self): curs = self.conn.cursor(cursor_factory=psycopg2.extras.DictCursor) curs.execute("INSERT INTO ExtrasDictCursorTests VALUES ('bar')") @@ -33,15 +39,9 @@ class ExtrasDictCursorTests(TestCase): self.failUnless(row['foo'] == 'bar') self.failUnless(row[0] == 'bar') -class ExtrasDictCursorSuite(TestSuite): - """Build a suite of all tests.""" - - def __init__(self): - """Build a list of tests.""" - self.tests = [x for x in dir(ExtrasDictCursorTests) - if x.startswith('test')] - TestSuite.__init__(self, map(TestModule, self.tests)) +def test_suite(): + return unittest.TestLoader().loadTestsFromName(__name__) if __name__ == "__main__": - main() + unittest.main() diff --git a/tests/test_psycopg2_dbapi20.py b/tests/test_psycopg2_dbapi20.py old mode 100644 new mode 100755 index 028b718c..1d1b03e9 --- a/tests/test_psycopg2_dbapi20.py +++ b/tests/test_psycopg2_dbapi20.py @@ -4,32 +4,26 @@ import unittest import psycopg2 import popen2 -class test_Psycopg(dbapi20.DatabaseAPI20Test): +import tests + +class Psycopg2TestCase(dbapi20.DatabaseAPI20Test): driver = psycopg2 connect_args = () - connect_kw_args = {'dsn': 'dbname=dbapi20_test'} + connect_kw_args = {'dsn': 'dbname=%s' % tests.dbname} lower_func = 'lower' # For stored procedure test - def setUp(self): - # Call superclass setUp In case this does something in the - # future - dbapi20.DatabaseAPI20Test.setUp(self) + def test_setoutputsize(self): + # psycopg2's setoutputsize() is a no-op + pass - try: - con = self._connect() - con.close() - except: - cmd = "psql -c 'create database dbapi20_test' template1" - cout,cin = popen2.popen2(cmd) - cin.close() - cout.read() + def test_nextset(self): + # psycopg2 does not implement nextset() + pass - def tearDown(self): - dbapi20.DatabaseAPI20Test.tearDown(self) - def test_nextset(self): pass - def test_setoutputsize(self): pass +def test_suite(): + return unittest.TestLoader().loadTestsFromName(__name__) if __name__ == '__main__': unittest.main() diff --git a/tests/test_quote.py b/tests/test_quote.py old mode 100644 new mode 100755 index 60f1d6a8..0603032b --- a/tests/test_quote.py +++ b/tests/test_quote.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python import psycopg2 import psycopg2.extensions import unittest diff --git a/tests/test_transaction.py b/tests/test_transaction.py old mode 100644 new mode 100755 index b6f6880f..285d5e44 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python import psycopg2 import unittest import tests diff --git a/tests/types_basic.py b/tests/types_basic.py old mode 100644 new mode 100755 index 7fb7756b..ee79f44e --- a/tests/types_basic.py +++ b/tests/types_basic.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # types_basic.py - tests for basic types conversions # # Copyright (C) 2004 Federico Di Gregorio @@ -18,14 +19,16 @@ try: except: pass import psycopg2 -from unittest import TestCase, TestSuite, main +import unittest + +import tests -class TypesBasicTests(TestCase): +class TypesBasicTests(unittest.TestCase): """Test presence of mandatory attributes and methods.""" def setUp(self): - self.conn = psycopg2.connect("dbname=test") + self.conn = psycopg2.connect("dbname=%s" % tests.dbname) def execute(self, *args): curs = self.conn.cursor() @@ -74,14 +77,9 @@ class TypesBasicTests(TestCase): "wrong array quoting " + str(s)) -class TypesBasicSuite(TestSuite): - """Build a suite of all tests.""" - - def __init__(self): - """Build a list of tests.""" - self.tests = [x for x in dir(TypesBasicTests) if x.startswith('test')] - TestSuite.__init__(self, map(TestModule, self.tests)) - +def test_suite(): + return unittest.TestLoader().loadTestsFromName(__name__) if __name__ == "__main__": - main() + unittest.main() +