| 1 |
#-*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2015 Bastian Bowe |
| 4 |
# |
| 5 |
# This file is part of JayDeBeApi. |
| 6 |
# JayDeBeApi is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU Lesser General Public License as |
| 8 |
# published by the Free Software Foundation, either version 3 of the |
| 9 |
# License, or (at your option) any later version. |
| 10 |
# |
| 11 |
# JayDeBeApi is distributed in the hope that it will be useful, but |
| 12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
# Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public |
| 17 |
# License along with JayDeBeApi. If not, see |
| 18 |
# <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
import jaydebeapi |
| 21 |
|
| 22 |
try: |
| 23 |
import unittest2 as unittest |
| 24 |
except ImportError: |
| 25 |
import unittest |
| 26 |
|
| 27 |
class MockTest(unittest.TestCase): |
| 28 |
|
| 29 |
def setUp(self): |
| 30 |
self.conn = jaydebeapi.connect('org.jaydebeapi.mockdriver.MockDriver', |
| 31 |
'jdbc:jaydebeapi://dummyurl') |
| 32 |
|
| 33 |
def tearDown(self): |
| 34 |
self.conn.close() |
| 35 |
|
| 36 |
def test_all_db_api_type_objects_have_valid_mapping(self): |
| 37 |
extra_type_mappings = { 'DATE': 'getDate', |
| 38 |
'TIME': 'getTime', |
| 39 |
'TIMESTAMP': 'getTimestamp' } |
| 40 |
for db_api_type in jaydebeapi.__dict__.values(): |
| 41 |
if isinstance(db_api_type, jaydebeapi.DBAPITypeObject): |
| 42 |
for jsql_type_name in db_api_type.values: |
| 43 |
self.conn.jconn.mockType(jsql_type_name) |
| 44 |
with self.conn.cursor() as cursor: |
| 45 |
cursor.execute("dummy stmt") |
| 46 |
cursor.fetchone() |
| 47 |
verify = self.conn.jconn.verifyResultSet() |
| 48 |
verify_get = getattr(verify, |
| 49 |
extra_type_mappings.get(jsql_type_name, |
| 50 |
'getObject')) |
| 51 |
verify_get(1) |
| 52 |
|
| 53 |
def test_ancient_date_mapped(self): |
| 54 |
self.conn.jconn.mockDateResult(1899, 12, 31) |
| 55 |
with self.conn.cursor() as cursor: |
| 56 |
cursor.execute("dummy stmt") |
| 57 |
result = cursor.fetchone() |
| 58 |
self.assertEquals(result[0], "1899-12-31") |
| 59 |
|
| 60 |
def test_decimal_scale_zero(self): |
| 61 |
self.conn.jconn.mockBigDecimalResult(12345, 0) |
| 62 |
with self.conn.cursor() as cursor: |
| 63 |
cursor.execute("dummy stmt") |
| 64 |
result = cursor.fetchone() |
| 65 |
self.assertEquals(str(result[0]), "12345") |
| 66 |
|
| 67 |
def test_decimal_places(self): |
| 68 |
self.conn.jconn.mockBigDecimalResult(12345, 1) |
| 69 |
with self.conn.cursor() as cursor: |
| 70 |
cursor.execute("dummy stmt") |
| 71 |
result = cursor.fetchone() |
| 72 |
self.assertEquals(str(result[0]), "1234.5") |
| 73 |
|
| 74 |
def test_double_decimal(self): |
| 75 |
self.conn.jconn.mockDoubleDecimalResult(1234.5) |
| 76 |
with self.conn.cursor() as cursor: |
| 77 |
cursor.execute("dummy stmt") |
| 78 |
result = cursor.fetchone() |
| 79 |
self.assertEquals(str(result[0]), "1234.5") |
| 80 |
|
| 81 |
def test_sql_exception_on_execute(self): |
| 82 |
self.conn.jconn.mockExceptionOnExecute("java.sql.SQLException", "expected") |
| 83 |
with self.conn.cursor() as cursor: |
| 84 |
try: |
| 85 |
cursor.execute("dummy stmt") |
| 86 |
self.fail("expected exception") |
| 87 |
except jaydebeapi.DatabaseError as e: |
| 88 |
self.assertEquals(str(e), "java.sql.SQLException: expected") |
| 89 |
|
| 90 |
def test_runtime_exception_on_execute(self): |
| 91 |
self.conn.jconn.mockExceptionOnExecute("java.lang.RuntimeException", "expected") |
| 92 |
with self.conn.cursor() as cursor: |
| 93 |
try: |
| 94 |
cursor.execute("dummy stmt") |
| 95 |
self.fail("expected exception") |
| 96 |
except jaydebeapi.InterfaceError as e: |
| 97 |
self.assertEquals(str(e), "java.lang.RuntimeException: expected") |
| 98 |
|
| 99 |
def test_sql_exception_on_commit(self): |
| 100 |
self.conn.jconn.mockExceptionOnCommit("java.sql.SQLException", "expected") |
| 101 |
try: |
| 102 |
self.conn.commit() |
| 103 |
self.fail("expected exception") |
| 104 |
except jaydebeapi.DatabaseError as e: |
| 105 |
self.assertEquals(str(e), "java.sql.SQLException: expected") |
| 106 |
|
| 107 |
def test_runtime_exception_on_commit(self): |
| 108 |
self.conn.jconn.mockExceptionOnCommit("java.lang.RuntimeException", "expected") |
| 109 |
try: |
| 110 |
self.conn.commit() |
| 111 |
self.fail("expected exception") |
| 112 |
except jaydebeapi.InterfaceError as e: |
| 113 |
self.assertEquals(str(e), "java.lang.RuntimeException: expected") |
| 114 |
|
| 115 |
def test_sql_exception_on_rollback(self): |
| 116 |
self.conn.jconn.mockExceptionOnRollback("java.sql.SQLException", "expected") |
| 117 |
try: |
| 118 |
self.conn.rollback() |
| 119 |
self.fail("expected exception") |
| 120 |
except jaydebeapi.DatabaseError as e: |
| 121 |
self.assertEquals(str(e), "java.sql.SQLException: expected") |
| 122 |
|
| 123 |
def test_runtime_exception_on_rollback(self): |
| 124 |
self.conn.jconn.mockExceptionOnRollback("java.lang.RuntimeException", "expected") |
| 125 |
try: |
| 126 |
self.conn.rollback() |
| 127 |
self.fail("expected exception") |
| 128 |
except jaydebeapi.InterfaceError as e: |
| 129 |
self.assertEquals(str(e), "java.lang.RuntimeException: expected") |
| 130 |
|
| 131 |
def test_cursor_with_statement(self): |
| 132 |
self.conn.jconn.mockType("INTEGER") |
| 133 |
with self.conn.cursor() as cursor: |
| 134 |
cursor.execute("dummy stmt") |
| 135 |
self.assertIsNotNone(cursor._connection) |
| 136 |
self.assertIsNone(cursor._connection) |
| 137 |
|
| 138 |
def test_connection_with_statement(self): |
| 139 |
with jaydebeapi.connect('org.jaydebeapi.mockdriver.MockDriver', |
| 140 |
'jdbc:jaydebeapi://dummyurl') as conn: |
| 141 |
self.assertEqual(conn._closed, False) |
| 142 |
self.assertEqual(conn._closed, True) |