Skip to content

Instantly share code, notes, and snippets.

@briggleman
Last active January 9, 2024 19:03
Show Gist options
  • Save briggleman/4a046f9377c7ce9aee72b7aa776732dc to your computer and use it in GitHub Desktop.
Save briggleman/4a046f9377c7ce9aee72b7aa776732dc to your computer and use it in GitHub Desktop.
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