"""Single Product Overview utils tests.""" from datetime import date, datetime import pytest import reporting.utils.single_product_overview as spo @pytest.mark.parametrize( 'val_one, val_two, expected', [ (2.0000, 2.333333, 4.33), (2, 2, 4), (3.3333, 3.3333, 6.67), (None, 0, 0), (None, None, None), (0, None, 0), ], ) def test__add(val_one, val_two, expected): """Test add and round helper.""" result = spo._add(val_one, val_two) assert result == expected @pytest.mark.parametrize( 'val_one, val_two, expected', [ (2.0000, 2.333333, -0.33), (12, 10, 2), (3.3333, 3.3333, 0), (None, 0, 0), (None, None, 0), (0, None, 0), ], ) def test__subtract(val_one, val_two, expected): """Test subtract and round helper.""" result = spo._subtract(val_one, val_two) assert result == expected @pytest.mark.parametrize( 'part, whole, expected', [ (2, 10, 20.0), (-2, 10, 20.0), (2, 2, 100), (3, 0, 0), (None, 1, None), (1, None, None), ], ) def test__pecentage(part, whole, expected): """Test percentage helper.""" result = spo._percentage(part, whole) assert result == expected @pytest.mark.parametrize( 'args, expected', [(0.00, '$0.00'), (-12.99, '-$12.99'), (5, '$5.00'), (None, '-')], ) def test__dollar_format(args, expected): """Test dollar fomatting.""" result = spo._dollar_format(args) assert result == expected @pytest.mark.parametrize( 'args, expected', [(date(2002, 3, 11), 'March 2002'), (date(2018, 2, 11), 'February 2018')], ) def test__month_year_format(args, expected): """Test month-year formatter.""" result = spo._month_year_format(args) assert result == expected @pytest.mark.parametrize( 'args, expected', [ ([date(2002, 2, 11), date(2002, 3, 11)], 'FEB-MAR'), ( [date(2002, 10, 11), date(2002, 11, 11), date(2002, 12, 11)], 'OCT-NOV-DEC', ), ], ) def test__month_abbrev_format(args, expected): """Test month abbreviations.""" result = spo._month_abbrev_format(args) assert result == expected @pytest.mark.parametrize( 'args, expected', [ (0.00, '-'), (0, '-'), (-1, '-'), (12.9, '12.90%'), (5, '5.00%'), (None, '-'), ], ) def test__percentage_format(args, expected): """Test percentage formatting.""" result = spo._percentage_format(args) assert result == expected def test__extract_rtd_row(): """Test extracting a row data as a dict.""" response = { 'day1_ship_qt': 10, 'day1_return_qt': 0, 'day1_ship_am': 10.00, 'day1_return_am': 0.00, } expected_result = { 'ship_num': 10, 'return_num': 0, 'net_num': 10, 'ship_amount': 10.0, 'return_amount': 0.0, 'net_amount': 10.0, 'return_percentage': 0.0, 'row_name': 'Day 1', } result = spo._extract_rtd_row(response, 'day1', 'Day 1') assert result == expected_result def test__extract_rtd_row_no_data(): """Test extracting a row data when its empty.""" response = { 'day1_ship_qt': None, 'day1_return_qt': None, 'day1_ship_am': None, 'day1_return_am': None, } expected_result = { 'ship_num': None, 'return_num': None, 'net_num': None, 'ship_amount': None, 'return_amount': None, 'net_amount': None, 'return_percentage': None, 'row_name': 'Day 1', } result = spo._extract_rtd_row(response, 'day1', 'Day 1') assert result == expected_result @pytest.mark.parametrize( 'dict_one, dict_two, expected', [ ({'x': 1, 'y': 2}, {'x': 3, 'y': 4}, {'x': 4, 'y': 6}), ({'x': -1, 'y': 0}, {'x': 3, 'y': -1}, {'x': 2, 'y': -1}), ], ) def test__sum_dict(dict_one, dict_two, expected): """Test sum dictionaries.""" result = spo._sum_dict(dict_one, dict_two) assert result == expected def test__extract_summary_row(): """Test extract summary row.""" row_collection = [ {'ship_num': 200, 'return_num': 1}, {'ship_num': 300, 'return_num': 20}, {'ship_num': 505, 'return_num': 100}, ] expected = { 'ship_num': 1005, 'return_num': 121, 'row_name': 'Test Summary!', 'return_percentage': 12.04, } result = spo._extract_summary_row(row_collection, 'Test Summary!') assert result == expected def test__extract_summary_row_no_data(): """Test extract summary row with some no data.""" row_collection = [ {'ship_num': None, 'return_num': None}, {'ship_num': 300, 'return_num': 20}, {'ship_num': 505, 'return_num': 100}, ] expected = { 'ship_num': 805, 'return_num': 120, 'row_name': 'Test Summary!', 'return_percentage': 14.91, } result = spo._extract_summary_row(row_collection, 'Test Summary!') assert result == expected @pytest.mark.parametrize( 'args, expected, size', [ ([1, 2, 3, 4, 5, 6], [[1, 2, 3], [4, 5, 6]], 3), (['a', 'b', 'c', 'd'], [['a', 'b'], ['c', 'd']], 2), ], ) def test__group_list(args, expected, size): """Test splitting a list into another list of n chunks.""" result = list(spo._group_list(args, size)) assert result == expected def test__extract_month_row(): """Test extracting a row data as a dict.""" response = { 'ship_qt': 10, 'return_qt': 0, 'ship_am': 10.00, 'return_am': 0.00, } expected_result = { 'ship_num': 10, 'return_num': 0, 'net_num': 10, 'ship_amount': 10.0, 'return_amount': 0.0, 'net_amount': 10.0, 'return_percentage': 0.0, } result = spo._extract_month_row(response) assert result == expected_result def test__extract_month_row_with_date(): """Test extracting a row data as a dict.""" response = { 'ship_qt': 10, 'return_qt': 0, 'ship_am': 10.00, 'return_am': 0.00, 'reporting_dt': date(2010, 2, 3), } expected_result = { 'ship_num': 10, 'return_num': 0, 'net_num': 10, 'ship_amount': 10.0, 'return_amount': 0.0, 'net_amount': 10.0, 'return_percentage': 0.0, 'row_name': 'February 2010', } result = spo._extract_month_row_with_date(response) assert result == expected_result def test__summarize_monthly_rows(single_product_monthly): """Test summarizing an array of dicts.""" three_month_intervals = list(spo._group_list(single_product_monthly)) single_interval = three_month_intervals[0] result = spo._summarize_monthly_rows(single_interval) assert result == { 'net_amount': 533.61, 'net_num': 71, 'return_amount': 0.0, 'return_num': 0, 'return_percentage': 0.0, 'ship_amount': 533.61, 'ship_num': 71, } def test__get_date_keys(single_product_monthly): """Get a list from the `reporting_dt` field.""" three_month_intervals = list(spo._group_list(single_product_monthly)) single_interval = three_month_intervals[0] result = spo._get_date_keys(single_interval) assert result == [ datetime(2018, 1, 1, 0, 0), datetime(2017, 12, 1, 0, 0), datetime(2017, 11, 1, 0, 0), ] def test__extract_summary_for_interval(single_product_monthly): """Generate a summary row for an interval of months.""" three_month_intervals = list(spo._group_list(single_product_monthly)) single_interval = three_month_intervals[0] result = spo._extract_summary_for_interval(single_interval) assert result == { 'row_name': 'NOV-DEC-JAN', 'net_amount': 533.61, 'net_num': 71, 'return_amount': 0.0, 'return_num': 0, 'return_percentage': 0.0, 'ship_amount': 533.61, 'ship_num': 71, } def test__format_values(): """Test formatting an array of dictionary values.""" input_rows = [ { 'ship_num': 0, 'return_num': -1, 'net_num': -1, 'ship_amount': 0.0, 'return_amount': -8.15, 'net_amount': -8.15, 'return_percentage': 0, }, { 'ship_num': 0, 'return_num': 0, 'net_num': 0, 'ship_amount': 0.0, 'return_amount': 0.0, 'net_amount': 0.0, 'return_percentage': 0, }, { 'ship_num': 0, 'return_num': 0, 'net_num': 0, 'ship_amount': 0.0, 'return_amount': 0.0, 'net_amount': 0.0, 'return_percentage': 0, }, ] expected = [ { 'ship_num': 0, 'return_num': -1, 'net_num': -1, 'ship_amount': '$0.00', 'return_amount': '-$8.15', 'net_amount': '-$8.15', 'return_percentage': '-', }, { 'ship_num': 0, 'return_num': 0, 'net_num': 0, 'ship_amount': '$0.00', 'return_amount': '$0.00', 'net_amount': '$0.00', 'return_percentage': '-', }, { 'ship_num': 0, 'return_num': 0, 'net_num': 0, 'ship_amount': '$0.00', 'return_amount': '$0.00', 'net_amount': '$0.00', 'return_percentage': '-', }, ] result = spo._format_values(input_rows) assert result == expected def test__extract_metadata(single_product_metadata): """Test formatting metadata.""" result = spo._extract_metadata(single_product_metadata) assert result == { 'Artist Name': 'good riddance', 'Product Name': 'peace in our time', 'Label Name': 'fat wreck chords', 'Product Code': '709422', 'Display Configuration': '1 x cd album', 'Genre': 'punk', 'Sub Genre': 'punk', 'UPC': 751097094228, 'Orchard Price': 9, 'Wholesale Price': '$9.00', 'Release Date': '2015-04-21', 'Product Status': 'active catalog', } def test__extract_account(single_product_metadata): """Test extracting vendor/subaccount ids.""" result = spo._extract_account(single_product_metadata) assert result == { 'vendor_id': 21945, 'subaccount_id': 7483, } def test__extract_account_no_subaccount(single_product_metadata): """Test extracting account ids when the product has no subaccount.""" single_product_metadata['subacct_id'] = None result = spo._extract_account(single_product_metadata) assert result == { 'vendor_id': 21945, 'subaccount_id': None, } @pytest.mark.parametrize( 'disp_cd, override_cd, expected', [ ('N', 'S', 'Scrap'), ('N', 'K', 'Keep'), (None, 'N', ''), ('K', None, ''), ], ) def test__get_return_disposition(disp_cd, override_cd, expected): """Test get return disposition helper.""" result = spo._get_return_disposition(disp_cd, override_cd) assert result == expected def test__extract_inventory(single_product_overview): """Test extract inventory data.""" result = spo._extract_inventory(single_product_overview) assert result == [ {'row_name': 'On Hand', 'total': 248}, {'row_name': 'Open Orders', 'total': 3}, {'row_name': 'Future Orders', 'total': 0}, {'row_name': 'Available', 'total': 248}, {'row_name': 'Backorders', 'total': 0}, {'row_name': 'Purchase Orders', 'total': 0}, {'row_name': 'Potential', 'total': 248}, {'row_name': 'Returns in Process', 'total': 38}, {'row_name': 'On Hold', 'total': 0}, {'row_name': 'Returnability', 'total': 'Yes'}, {'row_name': 'Return Disposition', 'total': 'Keep'}, {'row_name': 'Open Scrap', 'total': 1}, {'row_name': '12M Overstock', 'total': 20}, {'row_name': '24M Overstock', 'total': 11}, ]