forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AIRFLOW-1168] Add closing() to all connections and cursors
This will prevent any left-open connections whenever an exception occurs Closes apache#2269 from NielsZeilemaker/AIRFLOW-1168
- Loading branch information
1 parent
443e6b2
commit 8aeebd4
Showing
2 changed files
with
136 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import mock | ||
import unittest | ||
|
||
from airflow.hooks.dbapi_hook import DbApiHook | ||
|
||
|
||
class TestDbApiHook(unittest.TestCase): | ||
|
||
def setUp(self): | ||
super(TestDbApiHook, self).setUp() | ||
|
||
self.cur = mock.MagicMock() | ||
self.conn = conn = mock.MagicMock() | ||
self.conn.cursor.return_value = self.cur | ||
|
||
class TestDBApiHook(DbApiHook): | ||
conn_name_attr = 'test_conn_id' | ||
|
||
def get_conn(self): | ||
return conn | ||
|
||
self.db_hook = TestDBApiHook() | ||
|
||
def test_get_records(self): | ||
statement = "SQL" | ||
rows = [("hello",), | ||
("world",)] | ||
|
||
self.cur.fetchall.return_value = rows | ||
|
||
self.assertEqual(rows, self.db_hook.get_records(statement)) | ||
|
||
self.conn.close.assert_called_once() | ||
self.cur.close.assert_called_once() | ||
self.cur.execute.assert_called_once_with(statement) | ||
|
||
def test_get_records_parameters(self): | ||
statement = "SQL" | ||
parameters = ["X", "Y", "Z"] | ||
rows = [("hello",), | ||
("world",)] | ||
|
||
self.cur.fetchall.return_value = rows | ||
|
||
|
||
self.assertEqual(rows, self.db_hook.get_records(statement, parameters)) | ||
|
||
self.conn.close.assert_called_once() | ||
self.cur.close.assert_called_once() | ||
self.cur.execute.assert_called_once_with(statement, parameters) | ||
|
||
def test_get_records_exception(self): | ||
statement = "SQL" | ||
self.cur.fetchall.side_effect = RuntimeError('Great Problems') | ||
|
||
with self.assertRaises(RuntimeError): | ||
self.db_hook.get_records(statement) | ||
|
||
self.conn.close.assert_called_once() | ||
self.cur.close.assert_called_once() | ||
self.cur.execute.assert_called_once_with(statement) |