diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-07-11 13:41:38 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-07-11 13:41:38 -0400 |
commit | f2b43da1a8e3fa8f2afc49e04ec16479a6e7da64 (patch) | |
tree | 90f12adf613f4a97c8ad5861560f86abd8263ccb /test/base/test_except.py | |
parent | a6b62cc3fed5f06d3428b1f6ee13756175ded61b (diff) | |
download | sqlalchemy-f2b43da1a8e3fa8f2afc49e04ec16479a6e7da64.tar.gz |
tidy test/base, test/ex, test/ext
Diffstat (limited to 'test/base/test_except.py')
-rw-r--r-- | test/base/test_except.py | 122 |
1 files changed, 77 insertions, 45 deletions
diff --git a/test/base/test_except.py b/test/base/test_except.py index fbe0a05de..09a9eb7b4 100644 --- a/test/base/test_except.py +++ b/test/base/test_except.py @@ -1,92 +1,125 @@ """Tests exceptions and DB-API exception wrapping.""" + + from sqlalchemy import exc as sa_exceptions from sqlalchemy.test import TestBase -# Py3K -#StandardError = BaseException -# Py2K +# Py3K StandardError = BaseException Py2K + from exceptions import StandardError, KeyboardInterrupt, SystemExit + # end Py2K + class Error(StandardError): - """This class will be old-style on <= 2.4 and new-style on >= 2.5.""" + """This class will be old-style on <= 2.4 and new-style on >= + 2.5.""" + + class DatabaseError(Error): pass + + class OperationalError(DatabaseError): pass + + class ProgrammingError(DatabaseError): def __str__(self): - return "<%s>" % self.bogus + return '<%s>' % self.bogus + + class OutOfSpec(DatabaseError): pass class WrapTest(TestBase): + def test_db_error_normal(self): try: - raise sa_exceptions.DBAPIError.instance( - '', [], OperationalError()) + raise sa_exceptions.DBAPIError.instance('', [], + OperationalError()) except sa_exceptions.DBAPIError: self.assert_(True) - + def test_tostring(self): try: - raise sa_exceptions.DBAPIError.instance( - 'this is a message', None, OperationalError()) + raise sa_exceptions.DBAPIError.instance('this is a message' + , None, OperationalError()) except sa_exceptions.DBAPIError, exc: - assert str(exc) == "(OperationalError) 'this is a message' None" + assert str(exc) \ + == "(OperationalError) 'this is a message' None" def test_tostring_large_dict(self): try: - raise sa_exceptions.DBAPIError.instance( - 'this is a message', {'a':1, 'b':2, 'c':3, 'd':4, 'e':5, 'f':6, 'g':7, 'h':8, 'i':9, 'j':10, 'k':11}, OperationalError()) + raise sa_exceptions.DBAPIError.instance('this is a message' + , + {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': + 8, 'i': 9, 'j': 10, 'k': 11, + }, OperationalError()) except sa_exceptions.DBAPIError, exc: - assert str(exc).startswith("(OperationalError) 'this is a message' {") + assert str(exc).startswith("(OperationalError) 'this is a " + "message' {") def test_tostring_large_list(self): try: - raise sa_exceptions.DBAPIError.instance( - 'this is a message', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], OperationalError()) + raise sa_exceptions.DBAPIError.instance('this is a message', + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,], OperationalError()) except sa_exceptions.DBAPIError, exc: - assert str(exc).startswith("(OperationalError) 'this is a message' [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]") + assert str(exc).startswith("(OperationalError) 'this is a " + "message' [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]") def test_tostring_large_executemany(self): try: - raise sa_exceptions.DBAPIError.instance( - 'this is a message', [{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},], OperationalError()) + raise sa_exceptions.DBAPIError.instance('this is a message', + [{1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, + {1: 1}, {1:1}, {1: 1}, {1: 1},], + OperationalError()) except sa_exceptions.DBAPIError, exc: - assert str(exc) == "(OperationalError) 'this is a message' [{1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}]", str(exc) - + assert str(exc) \ + == "(OperationalError) 'this is a message' [{1: 1}, "\ + "{1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: "\ + "1}, {1: 1}, {1: 1}]", str(exc) try: - raise sa_exceptions.DBAPIError.instance( - 'this is a message', [{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},{1:1},], OperationalError()) + raise sa_exceptions.DBAPIError.instance('this is a message', [ + {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, {1: 1}, + {1:1}, {1: 1}, {1: 1}, {1: 1}, + ], OperationalError()) except sa_exceptions.DBAPIError, exc: - assert str(exc) == "(OperationalError) 'this is a message' [{1: 1}, {1: 1}] ... and a total of 11 bound parameter sets" - + assert str(exc) \ + == "(OperationalError) 'this is a message' [{1: 1}, "\ + "{1: 1}] ... and a total of 11 bound parameter sets" try: - raise sa_exceptions.DBAPIError.instance( - 'this is a message', [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)], OperationalError()) + raise sa_exceptions.DBAPIError.instance('this is a message', + [ + (1, ), (1, ), (1, ), (1, ), (1, ), (1, ), (1, ), (1, ), (1, ), + (1, ), + ], OperationalError()) except sa_exceptions.DBAPIError, exc: - assert str(exc) == "(OperationalError) 'this is a message' [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)]" - + assert str(exc) \ + == "(OperationalError) 'this is a message' [(1,), "\ + "(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,)]" try: - raise sa_exceptions.DBAPIError.instance( - 'this is a message', [(1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), (1,), ], OperationalError()) + raise sa_exceptions.DBAPIError.instance('this is a message', [ + (1, ), (1, ), (1, ), (1, ), (1, ), (1, ), (1, ), (1, ), (1, ), + (1, ), (1, ), + ], OperationalError()) except sa_exceptions.DBAPIError, exc: - assert str(exc) == "(OperationalError) 'this is a message' [(1,), (1,)] ... and a total of 11 bound parameter sets" - + assert str(exc) \ + == "(OperationalError) 'this is a message' [(1,), "\ + "(1,)] ... and a total of 11 bound parameter sets" + def test_db_error_busted_dbapi(self): try: - raise sa_exceptions.DBAPIError.instance( - '', [], ProgrammingError()) + raise sa_exceptions.DBAPIError.instance('', [], + ProgrammingError()) except sa_exceptions.DBAPIError, e: self.assert_(True) self.assert_('Error in str() of DB-API' in e.args[0]) def test_db_error_noncompliant_dbapi(self): try: - raise sa_exceptions.DBAPIError.instance( - '', [], OutOfSpec()) + raise sa_exceptions.DBAPIError.instance('', [], OutOfSpec()) except sa_exceptions.DBAPIError, e: self.assert_(e.__class__ is sa_exceptions.DBAPIError) except OutOfSpec: @@ -94,9 +127,10 @@ class WrapTest(TestBase): # Make sure the DatabaseError recognition logic is limited to # subclasses of sqlalchemy.exceptions.DBAPIError + try: - raise sa_exceptions.DBAPIError.instance( - '', [], sa_exceptions.ArgumentError()) + raise sa_exceptions.DBAPIError.instance('', [], + sa_exceptions.ArgumentError()) except sa_exceptions.DBAPIError, e: self.assert_(e.__class__ is sa_exceptions.DBAPIError) except sa_exceptions.ArgumentError: @@ -104,8 +138,8 @@ class WrapTest(TestBase): def test_db_error_keyboard_interrupt(self): try: - raise sa_exceptions.DBAPIError.instance( - '', [], KeyboardInterrupt()) + raise sa_exceptions.DBAPIError.instance('', [], + KeyboardInterrupt()) except sa_exceptions.DBAPIError: self.assert_(False) except KeyboardInterrupt: @@ -113,11 +147,9 @@ class WrapTest(TestBase): def test_db_error_system_exit(self): try: - raise sa_exceptions.DBAPIError.instance( - '', [], SystemExit()) + raise sa_exceptions.DBAPIError.instance('', [], + SystemExit()) except sa_exceptions.DBAPIError: self.assert_(False) except SystemExit: self.assert_(True) - - |