"""Testing the CAPhysicalHistoricalProductView.""" from collections import OrderedDict from decimal import Decimal import pytest from reporting.models import persister from reporting.models.ca_physical_historical_product_view import ( CAPhysicalHistoricalProductView, ) def default_params(): """Get the default params.""" return { 'param_artist': [], 'param_inventory_data': True, 'param_product_name': [], 'param_product_status': '', 'param_product_type': [], 'param_sales_data': True, 'param_release_date': '', 'param_sub_label': [], 'param_vendor_id': [1], 'param_start_month': 5, 'param_start_year': 2017, 'param_end_month': 5, 'param_end_year': 2017, } def test_standard_query(): """Test the standard query.""" results = CAPhysicalHistoricalProductView.all(default_params()) expected = OrderedDict( [ ('local_product_cd', '709422'), ('Product Name', 'Product Name1'), ('Artist', 'Artist1'), ('Label Name', 'Label1'), ('Sub Label', 'SubLabel1'), ('Genre', 'Genre1'), ('Sub Genre', 'SubGenre1'), ('Product Code', 'ProductCode1'), ('UPC/EAN', '1'), ('Release Date', '2016-01-01'), ('Product Type', 'CD1'), ('Format', 'Album'), ('Units Per Set', 1), ('Display Configuration', '1 x CD Album'), ('Box Lot', 30), ('Exclusive', 'N'), ('Exclusive For', 'For me'), ('Orchard Price', '11.20'), ('Wholesale Price', Decimal('3.00')), ('Ship #', 21), ('Return #', -2), ('Net #', 19), ('Sales $', Decimal('186.29')), ('Return $', Decimal('-18.41')), ('Net $', Decimal('167.88')), ('Opening Inventory', 0), ('Incoming', 11), ('Shipped', 3), ('Warehouse Scrap', 0), ('Shrinkage', 0), ('Rework', 0), ('Other', 2), ('Closing Inventory', 3), ('Returns Closing Inventory', 0), ] ) content = results.message['content'] row = next(r for r in content if r['UPC/EAN'] == '1') assert row == expected assert len(content) == 2 @pytest.mark.parametrize( 'param,column_name, value, num_results', [ ('param_artist', 'Artist', 'Artist2', 1), ('param_product_type', 'Product Type', 'CD2', 1), ('param_product_name', 'Product Name', 'Product Name2', 1), ('param_sub_label', 'Sub Label', 'SubLabel2', 1), ('param_release_date', 'Release Date', '2016-01-02', 1), ], ) def test_filters(param, column_name, value, num_results): """Test filters.""" params = default_params() params[param] = [value] results = CAPhysicalHistoricalProductView.all(params) assert results.message['content'][0][column_name] == value assert len(results.message['content']) == num_results def test_change_start_and_end_date(): """Test start and end date.""" params = default_params() params['param_start_year'] = 2017 params['param_start_month'] = 1 params['param_end_year'] = 2017 params['param_end_month'] = 5 results = CAPhysicalHistoricalProductView.all(params) expected = results.message['content'][0] assert expected['Ship #'] == 108 assert expected['Return #'] == -6 assert expected['Sales $'] == Decimal('860.49') assert expected['Return $'] == Decimal('-52.32') def test_vendor_id(): """Test vendor id.""" params = default_params() params['param_vendor_id'] = ['2'] results = CAPhysicalHistoricalProductView.all(params) assert results.message['content'][0]['Artist'] == 'Artist3' assert len(results.message['content']) == 1 def test_subaccount_id(): """Test vendor id.""" params = default_params() params['param_vendor_id'] = [1] params['param_subacct_id'] = [2] results = CAPhysicalHistoricalProductView.all(params) assert results.message['content'][0]['Artist'] == 'Artist2' assert len(results.message['content']) == 1 def test_get_filters(mocker): """Test getting the filter values.""" mocker.patch.object( persister, '_get_max_reporting_date', return_value='2021-06-01' ) results = CAPhysicalHistoricalProductView.filter(1, None) assert results.message == { 'artist': ['Artist1', 'Artist2'], 'product_name': ['Product Name1', 'Product Name2'], 'product_status': ['Active Catalog1', 'Active Catalog2'], 'product_type': ['CD1', 'CD2'], 'sub_label': ['SubLabel1', 'SubLabel2'], 'first_reporting_date': ['2017-02-01'], 'product_code': ['ProductCode1', 'ProductCode2'], 'upc_ean': ['1', '2'], 'max_reporting_date': ['2021-06-01'], } def test_get_filters_with_subaccount(mocker): """Test getting the filter values with a sub-account.""" vendor_id = 1 subaccount_id = 1 mocker.patch.object( persister, '_get_max_reporting_date', return_value='2021-06-01' ) results = CAPhysicalHistoricalProductView.filter(vendor_id, subaccount_id) assert results.message == { 'artist': ['Artist1'], 'product_name': ['Product Name1'], 'product_status': ['Active Catalog1'], 'product_type': ['CD1'], 'sub_label': ['SubLabel1'], 'first_reporting_date': ['2017-02-01'], 'product_code': ['ProductCode1'], 'upc_ean': ['1'], 'max_reporting_date': ['2021-06-01'], } def test_empty_results(): """Test empty results.""" params = default_params() params['param_vendor_id'] = ['2'] params['param_start_year'] = 2018 params['param_start_month'] = 3 params['param_end_year'] = 2018 params['param_end_month'] = 3 params['param_product_name'] = 'Some non-existent product' results = CAPhysicalHistoricalProductView.all(params) assert results.message['content'] == [] assert results.status == 200