"""Unit tests for helpers and util functions of the sql2sf workflow.""" from unittest.mock import call from unittest.mock import MagicMock import pymysql import pytest from snowflake_etl.flows.sql2sf import helpers def test_execute_with_mysql(monkeypatch): """Test execute_with_mysql helper.""" connect_mock = MagicMock() monkeypatch.setattr(pymysql, 'connect', connect_mock) helpers.execute_with_mysql( 'SELECT *', 'fetchone', 'somehost', 0, 'someuser', 'somepass') connect_mock.assert_has_calls([ call( charset='utf8mb4', port=0, password='somepass', host='somehost', user='someuser'), call().cursor(), call().cursor().execute('SELECT *'), call().cursor().fetchone(), call().cursor().close(), call().close()]) # check if DictCursor is working helpers.execute_with_mysql( 'SELECT *', 'fetchone', 'somehost', 0, 'someuser', 'somepass', 'DictCursor') connect_mock.assert_has_calls([ call().cursor(pymysql.cursors.DictCursor)], any_order=True) # check if fetchall command is working helpers.execute_with_mysql( 'SELECT *', 'fetchall', 'somehost', 0, 'someuser', 'somepass', 'DictCursor') connect_mock.assert_has_calls([ call().cursor().fetchall()], any_order=True) # check if wrong command raises ValueError with pytest.raises(ValueError): helpers.execute_with_mysql( 'SELECT *', 'failfetch', 'somehost', 0, 'someuser', 'somepass', 'DictCursor')