"""Unit tests for adjustment file.""" from unittest.mock import patch, MagicMock import pytest from adjustment.adjustment_file import AdjustmentFile @patch('adjustment.adjustment_file.Validation') @patch('adjustment.adjustment_file.get_and_set_contract_label_terms') @patch('adjustment.adjustment_file.get_and_set_contract_product_terms') @patch('adjustment.adjustment_file.get_and_set_upcs') @patch('adjustment.adjustment_file.get_and_set_statement_periods') @patch('adjustment.adjustment_file.get_and_set_valid_contract_ids') @patch('adjustment.adjustment_file.get_and_set_valid_account_ids') @patch('adjustment.adjustment_file.openpyxl') @patch('adjustment.adjustment_file.pd') def test_read_and_validate_file( mock_pandas, mock_openpyxl, mock_get_and_set_valid_account_ids, mock_get_and_set_valid_contract_ids, mock_get_and_set_statement_periods, mock_get_and_set_upcs, mock_get_and_set_contract_product_terms, mock_get_and_set_contract_label_terms, mock_validation, mock_adjustment ): """Test read_and_validate_file method.""" file = 'file/test.xlsx' mock_pandas.read_excel.return_value.iterrows = MagicMock(return_value = enumerate([mock_adjustment])) mock_get_and_set_valid_account_ids.return_value = [] mock_get_and_set_valid_contract_ids.return_value = [] mock_get_and_set_statement_periods.return_value = [] mock_get_and_set_upcs.return_value = [] mock_get_and_set_contract_product_terms.return_value = [] mock_get_and_set_contract_label_terms.return_value = [] mock_validation.return_value.validate_adjustment.return_value = [] wb = mock_openpyxl.workbook() wb.return_value.active = 1 mock_openpyxl.load_workbook = MagicMock(return_value=wb) wb.save.return_value = MagicMock() wb.close.return_value = MagicMock() adjustment_file = AdjustmentFile(file) result = adjustment_file.read_and_validate_file() assert result is True wb.save.assert_not_called() wb.close.assert_called_once() @patch('adjustment.adjustment_file.Validation') @patch('adjustment.adjustment_file.get_and_set_contract_label_terms') @patch('adjustment.adjustment_file.get_and_set_contract_product_terms') @patch('adjustment.adjustment_file.get_and_set_upcs') @patch('adjustment.adjustment_file.get_and_set_statement_periods') @patch('adjustment.adjustment_file.get_and_set_valid_contract_ids') @patch('adjustment.adjustment_file.get_and_set_valid_account_ids') @patch('adjustment.adjustment_file.openpyxl') @patch('adjustment.adjustment_file.pd') def test_read_and_validate_file_validation_errors( mock_pandas, mock_openpyxl, mock_get_and_set_valid_account_ids, mock_get_and_set_valid_contract_ids, mock_get_and_set_statement_periods, mock_get_and_set_upcs, mock_get_and_set_contract_product_terms, mock_get_and_set_contract_label_terms, mock_validation, mock_adjustment ): """Test read_and_validate_file method when file has validation errors.""" file = 'file/test.xlsx' mock_pandas.read_excel.return_value.iterrows = MagicMock(return_value = enumerate([mock_adjustment])) mock_get_and_set_valid_account_ids.return_value = [] mock_get_and_set_valid_contract_ids.return_value = [] mock_get_and_set_statement_periods.return_value = [] mock_get_and_set_upcs.return_value = [] mock_get_and_set_contract_product_terms.return_value = [] mock_get_and_set_contract_label_terms.return_value = [] mock_validation.return_value.validate_adjustment.return_value = ['Account Id doesn\'t exist in abacus'] wb = mock_openpyxl.workbook() wb.return_value.active = 1 mock_openpyxl.load_workbook = MagicMock(return_value=wb) wb.save.return_value = MagicMock() wb.close.return_value = MagicMock() with pytest.raises(Exception) as excinfo: adjustment_file = AdjustmentFile(file) adjustment_file.read_and_validate_file() assert 'Validation errors has been added to the file.' == str(excinfo.value) wb.save.assert_called_once() wb.close.assert_called_once() @patch('adjustment.adjustment_file.Validation') @patch('adjustment.adjustment_file.get_and_set_contract_label_terms') @patch('adjustment.adjustment_file.get_and_set_contract_product_terms') @patch('adjustment.adjustment_file.get_and_set_upcs') @patch('adjustment.adjustment_file.get_and_set_statement_periods') @patch('adjustment.adjustment_file.get_and_set_valid_contract_ids') @patch('adjustment.adjustment_file.get_and_set_valid_account_ids') def test_validate_adjustment( mock_get_and_set_valid_account_ids, mock_get_and_set_valid_contract_ids, mock_get_and_set_statement_periods, mock_get_and_set_upcs, mock_get_and_set_contract_product_terms, mock_get_and_set_contract_label_terms, mock_validation ): """Test _validate_adjustment method when no validation errors in file.""" file = 'file/test.xlsx' mock_get_and_set_valid_account_ids.return_value = [] mock_get_and_set_valid_contract_ids.return_value = [] mock_get_and_set_statement_periods.return_value = [] mock_get_and_set_upcs.return_value = [] mock_get_and_set_contract_product_terms.return_value = [] mock_get_and_set_contract_label_terms.return_value = [] mock_validation.return_value.validate_adjustment.return_value = [] adjustment_file = AdjustmentFile(file) result = adjustment_file._validate_adjustment() assert not result