"""Tests for database module.""" from unittest.mock import Mock from unittest.mock import patch from pytest import raises from flows import database def test_context(): """Test context function.""" cursor = Mock() connection = Mock() connection.cursor.return_value = cursor with database.context(connection) as (tcursor, tconnection): # these asserts also guarantee rollbacks and explicit commits are # called assert tcursor == cursor assert tconnection == connection tcursor.execute('foo') assert not tconnection.commit.called cursor.execute.assert_called_once_with('foo') assert cursor.close.called assert connection.commit.called assert connection.close.called # rollback on exception cursor.reset_mock() connection.reset_mock() with raises(BaseException): with database.context(connection) as (tcursor, tconnection): tcursor.execute('foo bar baz') raise BaseException() assert not connection.commit.called cursor.execute.assert_called_once_with('foo bar baz') assert connection.rollback.called def test_context_with_args(): """Test context function with args.""" connection = Mock() arg = 'string' with database.context(connection, arg, foo='bar'): pass connection.cursor.assert_called_with(arg, foo='bar') @patch('flows.database.context') def test_execute(context, database_context, monkeypatch): """Test execute function.""" monkeypatch.setattr(database, 'context', database_context) database.execute(Mock(), 'foo', {}) database_context._cursor.execute.assert_called_once_with('foo', {}) @patch('flows.database.context') def test_execute_with_args(context, database_context, monkeypatch): """Test execute function with args.""" monkeypatch.setattr(database, 'context', database_context) conn = Mock() arg = 'hello world' database.execute(conn, 'foo', {}, arg, foo='bar') database_context._cursor.execute.assert_called_once_with('foo', {}) database_context.assert_called_once_with(conn, arg, foo='bar') @patch('flows.database.context') def test_executemany(context, database_context, monkeypatch): """Test executemany function.""" monkeypatch.setattr(database, 'context', database_context) sequence = (('qwe', 'asd', 'zxc'), (123, 234, 345)) database.executemany(Mock(), 'foo', sequence) database_context._cursor.executemany.assert_called_once_with( 'foo', sequence) @patch('flows.database.context') def test_executemany_with_args(context, database_context, monkeypatch): """Test executemany function with args.""" monkeypatch.setattr(database, 'context', database_context) sequence = (('qwe', 'asd', 'zxc'), (123, 234, 345)) conn = Mock() arg = 'hello world' database.executemany(conn, 'foo', sequence, arg, foo='bar') database_context._cursor.executemany.assert_called_once_with( 'foo', sequence) database_context.assert_called_once_with(conn, arg, foo='bar') def test_query(): """Test query function.""" cursor = Mock() conn = Mock() conn.cursor.return_value = cursor arg = 'hello world' result = database.query(conn, 'select 1', {'foo': 'bar'}, arg, foo='bar') assert result == cursor cursor.execute.assert_called_once_with( 'select 1', {'foo': 'bar'}, arg, foo='bar')