import os import subprocess from pathlib import Path import dotenv import pytest import dbt2sf_upload THIS_DIR = Path(__file__).parent @pytest.mark.parametrize( 'command_line, code_expected', [ ( 'echo hello world', 1 ), ( 'dbt run -m somemodel --full-refresh', 2 ), ]) def test_dbt2sf_cli(command_line, code_expected): os.chdir(THIS_DIR) completed_process = subprocess.run(['./dbt2sf', command_line]) assert completed_process.returncode == code_expected def test_get_ddl(): ddl = dbt2sf_upload.get_ddl() assert 'CREATE TABLE IF NOT EXISTS ' in ddl ddl = dbt2sf_upload.get_ddl(overwrite=True) assert 'CREATE OR REPLACE TABLE ' in ddl @pytest.mark.parametrize( 'project_dir, command, exit_status, expected_result, expected_jsons_status', [ [ 'project_freshness', 'dbt source freshness', 1, { 'project_name': 'bi_analytics_dbt', 'project_id': 'faebc42304447d4427374f806679ecb5', "invocation_id": "95a2848b-a387-45de-a83c-a52c501b363a", "generated_at": "2022-08-09T17:47:56.129568Z", 'command_line': 'dbt source freshness', 'exit_status': 1, }, { 'manifest_json': True, 'sources_json': True, 'run_results_json': False, } ], # this run contains sources.json from other run. sources_json should be None [ 'project_test', 'dbt test -s source:*', 0, { 'project_name': 'bi_analytics_dbt', 'project_id': 'faebc42304447d4427374f806679ecb5', "invocation_id": "ff59f071-9740-4f40-9ee5-9d078dc94cdd", "generated_at": "2022-08-09T17:58:55.716071Z", 'command_line': 'dbt test -s source:*', 'exit_status': 0, }, { 'manifest_json': True, 'sources_json': False, 'run_results_json': True, } ], ]) def test_target2snowflake(project_dir: str, command: str, exit_status: int, expected_result: dict, expected_jsons_status: dict): result = dbt2sf_upload.target2snowflake( dbt_dir=THIS_DIR / 'test-data' / project_dir, dbt_cli=command, dbt_exit_status=exit_status, ) for key in expected_result: assert result[key] == expected_result[key] for json, available in expected_jsons_status.items(): if available: assert isinstance(result[json], dict) else: assert result[json] is None @pytest.mark.skip(reason='Need to mock snowflake') def test_insert_into_executions(): dotenv.load_dotenv(THIS_DIR.parent / '.env') connection = dbt2sf_upload.snowflake_connection() row = { 'project_name': 'bi_analytics_dbt', 'project_id': 'faebc42304447d4427374f806679ecb5', "invocation_id": "95a2848b-a387-45de-a83c-a52c501b363a", "generated_at": "2022-08-09T17:47:56.129568Z", 'command_line': 'dbt source freshness', 'manifest_json': {'metadata': {}}, 'sources_json': {'tables': []}, 'run_results_json': {'results': []}, } dbt2sf_upload.insert_into_executions(connection, row)