def test_execute_insert(mocked_connection, mocked_cursor, mocker): mocked_cursor.description = '' mocked_cursor.rowcount = 1 mocked_cursor.fetchone.side_effect = [[0]] dummy = PostgreSQLJDBC(mocked_connection) assert dummy.execute( "insert into users values (1, 'abc')" ) == ExecuteStatementResponse(numberOfRecordsUpdated=1, generatedFields=[]) mocked_cursor.execute.assert_has_calls( [mocker.call("insert into users values (1, 'abc')")] ) mocked_cursor.close.assert_called_once_with() mocked_cursor = mocker.Mock() mocked_connection.cursor.side_effect = [mocked_cursor] mocked_cursor.description = '' mocked_cursor.rowcount = 1 mocked_cursor.fetchone.side_effect = [[0]] assert dummy.execute( "insert into users values (1, 'abc')" ) == ExecuteStatementResponse(numberOfRecordsUpdated=1, generatedFields=[]) mocked_cursor.execute.assert_has_calls( [mocker.call("insert into users values (1, 'abc')")] ) mocked_cursor.close.assert_called_once_with()
def test_execute_exception_1(mocked_connection, mocked_cursor, mocker): error = jaydebeapi.DatabaseError('error_message') error.args = ['error_message'] mocked_cursor.execute.side_effect = [error] mocked_connection.cursor.side_effect = [mocked_cursor] dummy = PostgreSQLJDBC(mocked_connection, transaction_id='123') with pytest.raises(BadRequestException) as e: dummy.execute("select * from users") assert e.value.message == 'error_message' mocked_cursor.close.assert_called_once_with()
def test_execute_select(mocked_connection, mocked_cursor, mocker): mocked_cursor.description = 1, 1, 1, 1, 1, 1, 1 mocked_cursor.fetchall.side_effect = [((1, 'abc'), )] dummy = PostgreSQLJDBC(mocked_connection, transaction_id='123') assert dummy.execute("select * from users", ) == ExecuteStatementResponse( numberOfRecordsUpdated=0, records=[[Field.from_value(1), Field.from_value('abc')]], ) mocked_cursor.execute.assert_has_calls( [mocker.call('select * from users')]) mocked_cursor.close.assert_called_once_with()
def test_execute_select(mocked_connection, mocked_cursor, mocker): mocked_cursor.description = 1, 1, 1, 1, 1, 1, 1 mocked_cursor.fetchall.side_effect = [((1, 'abc'), )] dummy = PostgreSQLJDBC(mocked_connection, transaction_id='123') dummy.create_column_metadata_set = create_column_metadata_set_mock = mocker.Mock( ) create_column_metadata_set_mock.side_effect = [[ ColumnMetadata( arrayBaseColumnType=0, isAutoIncrement=False, isCaseSensitive=False, isCurrency=False, isSigned=False, label=1, name=1, precision=5, scale=6, tableName=None, type=None, typeName=None, ), ColumnMetadata( arrayBaseColumnType=0, isAutoIncrement=False, isCaseSensitive=False, isCurrency=False, isSigned=False, label=8, name=8, precision=12, scale=13, tableName=None, type=None, typeName=None, ), ]] assert dummy.execute("select * from users", ) == ExecuteStatementResponse( numberOfRecordsUpdated=0, records=[[ dummy.get_field_from_value(1), dummy.get_field_from_value('abc') ]], ) mocked_cursor.execute.assert_has_calls( [mocker.call('select * from users')]) mocked_cursor.close.assert_called_once_with()
def test_execute_exception_3(mocked_connection, mocked_cursor, mocker): mocked_connection.cursor.side_effect = [jaydebeapi.DatabaseError()] dummy = PostgreSQLJDBC(mocked_connection, transaction_id='123') with pytest.raises(BadRequestException): dummy.execute("select * from users") mocked_cursor.close.assert_not_called()
def test_execute_select_with_include_metadata(mocked_connection, mocked_cursor, mocker): meta_mock = mocker.Mock() mocked_cursor._meta = meta_mock mocked_cursor.description = (1, 2, 3, 4, 5, 6, 7), (8, 9, 10, 11, 12, 13, 14) mocked_cursor.fetchall.side_effect = [((1, 'abc'),)] dummy = PostgreSQLJDBC(mocked_connection, transaction_id='123') dummy.use_database = mocker.Mock() create_column_metadata_set_mock = mocker.patch( 'local_data_api.resources.jdbc.create_column_metadata_set' ) create_column_metadata_set_mock.side_effect = [ [ ColumnMetadata( arrayBaseColumnType=0, isAutoIncrement=False, isCaseSensitive=False, isCurrency=False, isSigned=False, label=1, name=1, precision=5, scale=6, tableName=None, type=None, typeName=None, ), ColumnMetadata( arrayBaseColumnType=0, isAutoIncrement=False, isCaseSensitive=False, isCurrency=False, isSigned=False, label=8, name=8, precision=12, scale=13, tableName=None, type=None, typeName=None, ), ] ] assert dummy.execute( "select * from users", database_name='test', include_result_metadata=True ) == ExecuteStatementResponse( numberOfRecordsUpdated=0, records=[[Field.from_value(1), Field.from_value('abc')]], columnMetadata=[ ColumnMetadata( arrayBaseColumnType=0, isAutoIncrement=False, isCaseSensitive=False, isCurrency=False, isSigned=False, label=1, name=1, precision=5, scale=6, tableName=None, type=None, typeName=None, ), ColumnMetadata( arrayBaseColumnType=0, isAutoIncrement=False, isCaseSensitive=False, isCurrency=False, isSigned=False, label=8, name=8, precision=12, scale=13, tableName=None, type=None, typeName=None, ), ], ) create_column_metadata_set_mock.assert_called_once_with(meta_mock) mocked_cursor.execute.assert_has_calls([mocker.call('select * from users')]) mocked_cursor.close.assert_called_once_with()