"""Tests for the Snowflake utility functions.""" from unittest.mock import patch import pytest from snowflake_etl.util.snowflake_meta import fetch_meta from snowflake_etl.util.snowflake_meta import format_meta from snowflake_etl.util.snowflake_meta import get_table_schema from snowflake_etl.util.snowflake_meta import SFTable @pytest.fixture def sf_table(): """Return SFTable object.""" return SFTable('db', 'schema', 'table') @patch('snowflake_etl.util.snowflake_meta.garcon_snowflake') def test_fetch_meta(sf_mock, sf_table): """Test fetch_meta function.""" sf_mock.execute_with_py_conn.return_value = {'results': {'check': 'me'}} result = fetch_meta(sf_table) call, *_ = sf_mock.execute_with_py_conn.call_args_list # first call (sql_arg, *_), *_ = call # first argument in positional arguments assert ('db.schema.table' in sql_arg) assert result == {'check': 'me'} def test_format_meta_incorrect_number_of_fields(sf_table): """Test that number of a column's fields matches.""" with pytest.raises(Exception): print(format_meta(sf_table, [('one', 'two', 'three')])) @pytest.mark.parametrize('test_input,expected_columns', [ ([('value',) * 10], [('value', 'value', False, 'value')]), # ===== ([tuple('Y' if i == 3 else str(i) for i in range(10))], [('0', '1', True, '4')]), # ===== ([ tuple(('Y' if i in (0, 2) else 'F') if j == 3 else str(j * i) for j in range(10)) for i in range(4) ], [('0', '0', True, '0'), ('0', '1', False, '4'), ('0', '2', True, '8'), ('0', '3', False, '12')]), # ===== ]) def test_format_meta(test_input, expected_columns, sf_table): """Test format_meta on correct inputs.""" def preformat_output(columns): """Format expected output of format_meta.""" return { 'database': sf_table.db, 'schema': sf_table.schema, 'table': sf_table.name, 'columns': [ { 'column_name': cn, 'data_type': dt, 'is_nullable': inu, 'column_default': df } for cn, dt, inu, df in columns ] } assert (format_meta(sf_table, test_input) == preformat_output(expected_columns)) @patch('snowflake_etl.util.snowflake_meta.format_meta') @patch('snowflake_etl.util.snowflake_meta.fetch_meta') def test_get_table_schema(fetch_meta_mock, format_meta_mock, sf_table): """Test get_table_schema function.""" fetch_meta_mock.return_value = {'data': 'for_format'} format_meta_mock.return_value = {'what': 'is_this'} result = get_table_schema(*sf_table) call, *_ = fetch_meta_mock.call_args_list (sf_table_arg, *_), *_ = call assert sf_table_arg == sf_table call, *_ = format_meta_mock.call_args_list (sf_table_arg, data, *_), *_ = call assert sf_table_arg == sf_table assert data == {'data': 'for_format'} assert result == {'what': 'is_this'}