-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
64 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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# -*- encoding: utf-8 -*- | ||
|
||
from . import test_create_dbsource | ||
from . import test_base_external_dbsource |
110 changes: 110 additions & 0 deletions
110
base_external_dbsource/tests/test_base_external_dbsource.py
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,110 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2016 LasLabs Inc. | ||
|
||
import mock | ||
|
||
from openerp.tests import common | ||
|
||
from ..exceptions import ConnectionFailedError, ConnectionSuccessError | ||
|
||
|
||
class TestBaseExternalDbsource(common.TransactionCase): | ||
|
||
def setUp(self): | ||
super(TestBaseExternalDbsource, self).setUp() | ||
self.dbsource = self.env.ref('base_external_dbsource.demo_postgre') | ||
|
||
def test_conn_string_full(self): | ||
""" It should add password if string interpolation not detected """ | ||
self.dbsource.conn_string = 'conn string' | ||
expect = self.dbsource.conn_string + ';PWD%s' | ||
self.assertEqual( | ||
self.dbsource.conn_string_full, expect, | ||
) | ||
|
||
# Interface | ||
|
||
def test_connection_success(self): | ||
""" It should raise for successful connection """ | ||
with self.assertRaises(ConnectionSuccessError): | ||
self.dbsource.connection_test() | ||
|
||
def test_connection_fail(self): | ||
""" It should raise for failed/invalid connection """ | ||
# Connection without connection_string | ||
self.dbsource.conn_string = "" | ||
with self.assertRaises(ConnectionFailedError): | ||
self.dbsource.connection_test() | ||
|
||
def test_connection_open_calls_close(self): | ||
""" It should close connection after context ends """ | ||
with mock.patch.object(self.dbsource, 'connection_close') as close: | ||
with self.dbsource.connection_open() as connection: | ||
pass | ||
close.assert_called_once_with(connection) | ||
|
||
def test_connection_close(self): | ||
""" It should call adapter's close method """ | ||
with mock.patch.object( | ||
self.dbsource, 'connection_close_postgresql' | ||
) as close: | ||
expect = mock.MagicMock() | ||
self.dbsource.connection_close(expect) | ||
close.assert_called_once_with(expect) | ||
|
||
def test_execute_asserts_query_arg(self): | ||
""" It should raise a TypeError if query and sqlquery not in args """ | ||
with self.assertRaises(TypeError): | ||
self.dbsource.execute() | ||
|
||
def test_execute_calls_adapter(self): | ||
""" It should call the adapter methods with proper args """ | ||
expect = ('query', 'execute', 'metadata') | ||
with mock.patch.object(self.dbsource, 'execute_postgresql') as psql: | ||
self.dbsource.execute(*expect) | ||
psql.assert_called_once_with(*expect) | ||
|
||
def test_execute_return(self): | ||
""" It should return rows if not metadata """ | ||
with mock.patch.object(self.dbsource, 'execute_postgresql') as psql: | ||
psql.return_value = 'rows', 'cols' | ||
res = self.dbsource.execute(True, True, False) | ||
self.assertEqual(res, 'rows') | ||
|
||
def test_execute_return_metadata(self): | ||
""" It should return rows and cols if metadata """ | ||
with mock.patch.object(self.dbsource, 'execute_postgresql') as psql: | ||
psql.return_value = 'rows', 'cols' | ||
res = self.dbsource.execute(True, True, False) | ||
self.assertDictEqual( | ||
res, | ||
{'rows': 'rows', 'cols': 'cols'}, | ||
) | ||
|
||
# Postgres | ||
|
||
def test_execute_postgresql(self): | ||
""" It should call generic executor with proper args """ | ||
expect = ('query', 'execute', 'metadata') | ||
with mock.patch.object(self.dbsource, '_execute_generic') as execute: | ||
self.dbsource.execute(*expect) | ||
execute.assert_called_once_with(*expect) | ||
|
||
# Old API Compat | ||
|
||
def test_execute_calls_adapter_old_api(self): | ||
""" It should call the adapter correctly if old kwargs provided """ | ||
expect = [None, None, 'metadata'] | ||
with mock.patch.object(self.dbsource, 'execute_postgresql') as psql: | ||
self.dbsource.execute(*expect, sqlparams='params', sqlquery='query') | ||
expect[0], expect[1] = 'query', 'params' | ||
psql.assert_called_once_with(*expect) | ||
|
||
def test_conn_open(self): | ||
""" It should return open connection for use """ | ||
with mock.patch.object(self.dbsource, 'connection_open') as connection: | ||
res = self.dbsource.conn_open() | ||
self.assertEqual( | ||
res, | ||
connection().__enter__(), | ||
) |
This file was deleted.
Oops, something went wrong.