Last active
January 9, 2024 19:03
-
-
Save briggleman/4a046f9377c7ce9aee72b7aa776732dc to your computer and use it in GitHub Desktop.
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
from unittest import mock | |
from sdca.models.base import PooledMySQLDatabase, MyPooledMySQLDatabase | |
from peewee import OperationalError | |
@mock.patch.object(PooledMySQLDatabase, 'commit') | |
@mock.patch.object(PooledMySQLDatabase, 'in_transaction') | |
@mock.patch.object(PooledMySQLDatabase, 'cursor') | |
@mock.patch.object(PooledMySQLDatabase, 'close') | |
@mock.patch.object(PooledMySQLDatabase, 'is_closed') | |
@mock.patch.object(PooledMySQLDatabase, 'execute_sql') | |
def test_sql_db_model(mocked_execute, mocked_is_closed, mocked_close, mocked_cursor, mocked_in_trans, mocked_commit): | |
mocked_execute.side_effect = OperationalError | |
mocked_is_closed.return_value = False | |
mocked_in_trans.return_value = False | |
db = MyPooledMySQLDatabase(host='host', database='api_dev') | |
cursor = db.execute_sql('select * from test;') | |
assert cursor == mocked_cursor.return_value | |
assert mocked_is_closed.call_count == 2 | |
assert mocked_close.call_count == 2 | |
mocked_execute.assert_called_once_with('select * from test;', None, True) | |
mocked_cursor.assert_called_once_with() | |
mocked_cursor.return_value.execute.assert_called_once_with('select * from test;', ()) | |
mocked_in_trans.assert_called_once_with() | |
mocked_commit.assert_called_once_with() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment