"""Custom export report utility functions test cases.""" import collections import copy import pytest from processing_accounting.flows.custom_export import setting from processing_accounting.flows.custom_export.util import report ALL_TRANSACTIONS = 'all' CUSTOM_REPORT_TRANSACTION_TYPES = 'S,VR' PHYSICAL_REPORT_TRANSACTION_TYPES = 'physical' FIELD_A = ('fieldA', 'Field A') FIELD_B = ('fieldB', 'Field B') FIELD_C = ('fieldC', 'Field C') PHYSICAL_FIELD = ('physical', 'Physical Field') DEFAULT_FIELD = ('default', 'Default') NEW_FIELD = ('new_field', 'New Field') HEADER_MAP_V1 = collections.OrderedDict([FIELD_A, FIELD_B, FIELD_C]) HEADER_MAP_V2 = collections.OrderedDict([FIELD_A, FIELD_B, FIELD_C, NEW_FIELD]) DEFAULT_MAP = collections.OrderedDict([DEFAULT_FIELD, FIELD_A]) DEFAULT_MAP_PHYSICAL = collections.OrderedDict( [DEFAULT_FIELD, FIELD_A, PHYSICAL_FIELD]) HEADER_MAPPING = { 1: HEADER_MAP_V1, 2: HEADER_MAP_V2 } def test_is_physical_report(): """Test is_physical_report utility function.""" assert report.is_physical_report(PHYSICAL_REPORT_TRANSACTION_TYPES) assert not report.is_physical_report(CUSTOM_REPORT_TRANSACTION_TYPES) assert not report.is_physical_report(ALL_TRANSACTIONS) @pytest.mark.parametrize('transaction_types', [ ALL_TRANSACTIONS, CUSTOM_REPORT_TRANSACTION_TYPES, PHYSICAL_REPORT_TRANSACTION_TYPES, ]) def test_get_report_schema_does_not_chanes_settings(transaction_types): excluded_cloums = ['period', 'orchard_upc'] # Non existing report version to get a default mapping report_version = 9999 if transaction_types == PHYSICAL_REPORT_TRANSACTION_TYPES: default_map = setting.DEFAULT_PHYSICAL_REPORT_SCHEMA else: default_map = setting.DEFAULT_HEADER_MAP original_header_mapping = copy.deepcopy(default_map) report.get_report_schema( excluded_cloums, report_version, transaction_types) comparison_list = zip( original_header_mapping.items(), default_map.items()) for original_tuple, settings_tuple in comparison_list: assert original_tuple == settings_tuple @pytest.mark.parametrize('transaction_types', [ ALL_TRANSACTIONS, CUSTOM_REPORT_TRANSACTION_TYPES]) @pytest.mark.parametrize('excluded_columns, report_version, expected', [ (['fieldA'], 1, collections.OrderedDict([FIELD_B, FIELD_C])), (['fieldB'], 2, collections.OrderedDict([FIELD_A, FIELD_C, NEW_FIELD])), ([], 999, collections.OrderedDict([DEFAULT_FIELD, FIELD_A])), ]) def test_get_report_schema( monkeypatch, excluded_columns, report_version, expected, transaction_types): """Test that get_report_schema uses correct mappings""" monkeypatch.setattr(setting, 'header_mapping', HEADER_MAPPING) monkeypatch.setattr(setting, 'DEFAULT_HEADER_MAP', DEFAULT_MAP) actual = report.get_report_schema( excluded_columns, report_version, transaction_types) assert list(actual.items()) == list(expected.items()) @pytest.mark.parametrize('excluded_columns, report_version, expected', [ (['fieldA'], 1, collections.OrderedDict( [DEFAULT_FIELD, PHYSICAL_FIELD])), (['fieldB'], 2, collections.OrderedDict( [DEFAULT_FIELD, FIELD_A, PHYSICAL_FIELD])), ([], 999, collections.OrderedDict( [DEFAULT_FIELD, FIELD_A, PHYSICAL_FIELD])), ]) def test_get_report_schema_physical_report( monkeypatch, excluded_columns, report_version, expected): """Test that get_report_schema uses correct mappings""" monkeypatch.setattr(setting, 'header_mapping', HEADER_MAPPING) monkeypatch.setattr( setting, 'DEFAULT_PHYSICAL_REPORT_SCHEMA', DEFAULT_MAP_PHYSICAL) actual = report.get_report_schema( excluded_columns, report_version, PHYSICAL_REPORT_TRANSACTION_TYPES) assert list(actual.items()) == list(expected.items())