"""Projections ETL utils tests.""" from unittest.mock import Mock from unittest.mock import patch from flows.theatrical_cuts import util @patch('flows.theatrical_cuts.util.datastore') @patch('flows.theatrical_cuts.util.queries') def test_get_upc_theatrical_cuts_anchor_date( queries, datastore, database_context): """Test determination of upc anchor date. The logic is actually in the database. This is for coverage completion and making sure the predetermined procedure call is being used. """ datastore.context = database_context query = 'Get me the anchor date, please!' upc = '123456' queries.THEATRICAL_CUTS_ANCHOR_DATE = query util.get_upc_theatrical_cuts_anchor_date(upc) datastore.query.assert_called_once_with(query, {'upc': upc}) @patch('flows.theatrical_cuts.util.s3') def test_get_cuts_upc(s3_mock): """Test get_cuts_upc function.""" expected_upc = 'test_upc' test_row = [[expected_upc]] s3_mock.download_csv.return_value = test_row # test function call upc = util.get_cuts_upc(Mock()) assert upc == expected_upc def test_transpose_weekly_percentage(): """Test of data transformation method used in download_to_db task.""" upc = '123456789012' row = [upc, '20', '30', '45.7'] anchor_date = '2014-01-06' result = ( { 'upc': upc, 'start_date': '2014-01-06', 'end_date': '2014-01-12', 'percentage': 80.0}, { 'upc': upc, 'start_date': '2014-01-13', 'end_date': '2014-01-19', 'percentage': 70.0}, { 'upc': upc, 'start_date': '2014-01-20', 'end_date': '2014-01-26', 'percentage': 54.3}) for expected, actual in zip( result, util.transpose_weekly_percentage(row, anchor_date, upc)): for key in expected.keys(): assert expected[key] == actual[key] def test_transpose_percentage_data(): """Test transpose_percentage_data util function.""" upc = '123456789012' row = [upc, '20', '30', '45.7'] anchor_date = '2014-01-06' result = ( {'upc': upc, 'date': '2014-01-06', 'percentage': 20.0}, {'upc': upc, 'date': '2014-01-13', 'percentage': 30.0}, {'upc': upc, 'date': '2014-01-20', 'percentage': 45.7}) for expected, actual in zip( result, util.transpose_percentage_data(row, anchor_date, upc)): for key in expected.keys(): assert expected[key] == actual[key]