"""Projections ETL utils tests.""" from unittest.mock import patch from flows.projections import config from flows.projections import util as etl_util @patch('flows.projections.util.datastore') @patch('flows.projections.util.queries') def test_get_upc_projection_anchor_date( queries, datastore, database_context, projection_type='regular'): """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.PROJECTIONS_ANCHOR_DATE = query etl_util.get_upc_projection_anchor_date(upc, projection_type) datastore.query.assert_called_once_with(query, {'upc': upc}) @patch('flows.projections.util.art_relations') @patch('flows.projections.util.queries') def test_get_upc_projection_anchor_date_original( queries, art_relations, database_context, projection_type='original'): """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. """ art_relations.context = database_context query = 'Get me the anchor date, please!' upc = '123456' queries.ORIGINAL_PROJECTIONS_ANCHOR_DATE = query etl_util.get_upc_projection_anchor_date(upc, projection_type) art_relations.query.assert_called_once_with(query, {'upc': upc}) def test_transform(): """Test of data transformation method used in download_to_db task.""" rows = (('EST', '3000', '0'), ('Ad Subscription Video', '0', '2500'), ('SV', '100', '2100')) upc = '123456789012' anchor_date = '2014-01-06' result = ( (upc, '2014-01-06', config.TRANSACTION_TYPE['EST'], 3000), (upc, '2014-01-13', config.TRANSACTION_TYPE['EST'], 0), (upc, '2014-01-06', config.TRANSACTION_TYPE[ 'AD SUBSCRIPTION VIDEO'], 0), (upc, '2014-01-13', config.TRANSACTION_TYPE[ 'AD SUBSCRIPTION VIDEO'], 2500), (upc, '2014-01-06', config.TRANSACTION_TYPE['SV'], 100), (upc, '2014-01-13', config.TRANSACTION_TYPE['SV'], 2100), ) for expected, actual in zip( result, etl_util.transform( rows, anchor_date, upc, config.TRANSACTION_TYPE)): assert expected == actual def test_transform_special_chars(): """Test of data transformation.""" rows = (('EST', '$30.20', '$19'), ('VOD', '$4,500.80', '2,500')) upc = '123456789012' anchor_date = '2014-01-06' result = ( (upc, '2014-01-06', config.TRANSACTION_TYPE['EST'], 30), (upc, '2014-01-13', config.TRANSACTION_TYPE['EST'], 20), (upc, '2014-01-06', config.TRANSACTION_TYPE['VOD'], 4500), (upc, '2014-01-13', config.TRANSACTION_TYPE['VOD'], 2500), ) for expected, actual in zip( result, etl_util.transform( rows, anchor_date, upc, config.TRANSACTION_TYPE)): assert expected == actual def test_transform_invalid_type(): """Test transform skip invalid transaction_type.""" rows = (('dummy', '3000', '0'), ('VOD', '0', '2500')) upc = '123456789012' anchor_date = '2014-01-06' result = ( (upc, '2014-01-06', config.TRANSACTION_TYPE['VOD'], 0), (upc, '2014-01-13', config.TRANSACTION_TYPE['VOD'], 2500), ) for expected, actual in zip( result, etl_util.transform( rows, anchor_date, upc, config.TRANSACTION_TYPE)): assert expected == actual def test_transform_rounding(): """Test of transform to round amount to the nearest tenth place.""" rows = (('EST', '77.034314', '1234'), ('VOD', '4.593352', '-105.55'), ('THEATRICAL', '53.93', None)) upc = '123456789012' anchor_date = '2014-01-06' result = ( (upc, '2014-01-06', config.TRANSACTION_TYPE['EST'], 80.0), (upc, '2014-01-13', config.TRANSACTION_TYPE['EST'], 1230.0), (upc, '2014-01-06', config.TRANSACTION_TYPE['VOD'], 0.0), (upc, '2014-01-13', config.TRANSACTION_TYPE['VOD'], -110.0), (upc, '2014-01-06', config.TRANSACTION_TYPE['THEATRICAL'], 50.0), (upc, '2014-01-13', config.TRANSACTION_TYPE['THEATRICAL'], None)) for expected, actual in zip( result, etl_util.transform( rows, anchor_date, upc, config.TRANSACTION_TYPE)): assert expected == actual def test_parse_file_name(): """Test parse_file_name function.""" upc = '889845077732' file_date = '20161130064705' s3_key = 'test/path/{upc}-{file_date}.csv'.format( upc=upc, file_date=file_date) response = etl_util.parse_file_name(s3_key) assert response == (upc, file_date)