"""Tests for query-composers related to theatrical ETL.""" import pytest from flows.theatrical import queries def test_insert_raw_data(): """Test of queries.insert_raw_data function.""" header = ['a', 'b'] expected_query = """ INSERT INTO theatrical_revenue_raw (a, b) VALUES (%s, %s); """ query = queries.insert_raw_data(header) assert query.split() == expected_query.split() @pytest.fixture(params=[ ['123456789012', '123456789013'], [] ]) def upcs(request): """Set upcs fixture.""" return request.param def test_clear_existing_data(upcs): """Test of queries.clear_existing_data function.""" table_name = 'table' if upcs: upc_condition = 'upc IN ({}) AND '.format( ', '.join("'%s'" % upc for upc in upcs)) else: upc_condition = '' expected_query = """ DELETE FROM table WHERE {} date BETWEEN %(date_start)s AND %(date_end)s; """.format(upc_condition) query = list(queries.clear_existing_data(table_name, upcs)) if upcs: assert query[0].split() == expected_query.split() else: assert not query def test_insert_to_temp_from_raw(upcs): """Test insert_to_temp_from_raw function.""" table_name = 'test_table' if upcs: upcs_condition = 'upc IN ({}) AND '.format( ', '.join("'{}'".format(upc) for upc in upcs)) else: return '' expected_query = """ INSERT INTO {table_name} ( upc, display_upc, amount, date, transaction_type_id, country_id, units, orchard_amount) SELECT upc, RIGHT(CONCAT('0000000000', CAST(upc AS CHAR(12))), 12), SUM(gross), date, 41, 1, 1, SUM(gross) * 0.4 FROM theatrical_revenue_raw WHERE {upc_condition} date BETWEEN %(date_start)s AND %(date_end)s GROUP BY upc, date; """.format(table_name=table_name, upc_condition=upcs_condition) sql = list(queries.insert_to_temp_from_raw(table_name, upcs))[0] assert sql.split() == expected_query.split()