"""Test for run_sql script functions.""" import os import sys from flexmock import flexmock import pytest from datalytics import config from datalytics.bin import run_sql @pytest.mark.parametrize('file_exists', [True, False]) def test_main(file_exists): """Test that main function executes correct steps.""" sql_file = 'some/sql/file_name.sql' database = 'db name' schema = 'schema name' facts_db = 'facts db name' facts_schema = 'facts schema name' sql = 'Some SQL with {DATABASE} and {SCHEMA}' formatted_sql = sql.format(DATABASE=database, SCHEMA=schema) file_path = os.path.join(config.SQL_DIR, sql_file) builtins_mock = flexmock(sys.modules['builtins']) (flexmock(run_sql.os.path) .should_receive('join') .and_return(file_path)) (flexmock(run_sql.os.path) .should_receive('isfile') .and_return(file_exists)) if file_exists: (builtins_mock.should_receive('open') .with_args(file_path) .and_return(flexmock(read=lambda: sql))) (flexmock(run_sql.Snowflake) .should_receive('sfq') .with_args(formatted_sql)) else: (builtins_mock .should_receive('open') .never()) (flexmock(run_sql.Snowflake) .should_receive('sfq') .never()) run_sql.main(sql_file, database, schema, facts_db, facts_schema)