"""Tests for contract_import.""" from unittest.mock import MagicMock from unittest.mock import patch from json_contract_file_import import contract_import from json_contract_file_import.constants import constants from json_contract_file_import.constants.constants import CONTRACT_TYPES from json_contract_file_import.contract_import import _contract_already_exists from json_contract_file_import.contract_import import import_contract from json_contract_file_import.entity_import_result import EntityImportResult from json_contract_file_import.run_controller_import import ORCHARD_RUN_CONTROLLER_ID from tests.unit.utils import assert_import_result @patch('json_contract_file_import.contract_import._contract_already_exists') @patch('json_contract_file_import.contract_import._import_new_contract') def test_import_contract_contract_exists_overwrite_disabled( mock_import_new_contract, mock_contract_already_exists, mock_json_contract_file_parsed_with_reserves_and_contract_id ): """Test import contract when contract already exists and overwrite is disabled.""" mock_contract_already_exists.return_value = True mock_import_new_contract.return_value = [] import_results = import_contract( mock_json_contract_file_parsed_with_reserves_and_contract_id, False ) assert import_results == [] mock_contract_already_exists.assert_called_once_with(100) mock_import_new_contract.assert_called_once_with( 100, mock_json_contract_file_parsed_with_reserves_and_contract_id ) @patch('json_contract_file_import.contract_import._contract_already_exists') @patch('json_contract_file_import.contract_import._import_existing_contract') def test_import_contract_contract_exists( mock_import_existing_contract, mock_contract_already_exists, mock_json_contract_file_parsed_with_reserves_and_contract_id ): """Test import contract when contract already exists and overwrite is enabled.""" mock_contract_already_exists.return_value = True mock_import_existing_contract.return_value = [] import_results = import_contract( mock_json_contract_file_parsed_with_reserves_and_contract_id, True ) assert import_results == [] mock_contract_already_exists.assert_called_once_with(100) mock_import_existing_contract.assert_called_once_with( 100, mock_json_contract_file_parsed_with_reserves_and_contract_id ) @patch('json_contract_file_import.contract_import._contract_already_exists') @patch('json_contract_file_import.contract_import._import_new_contract') def test_import_contract_new_contract( mock_import_new_contract, mock_contract_already_exists, mock_json_contract_file_parsed_with_reserves_and_contract_id ): """Test import contract when new contract and overwrite is enabled.""" mock_contract_already_exists.return_value = False mock_import_new_contract.return_value = [] import_results = import_contract( mock_json_contract_file_parsed_with_reserves_and_contract_id, True) assert import_results == [] mock_contract_already_exists.assert_called_once_with(100) mock_import_new_contract.assert_called_once_with( 100, mock_json_contract_file_parsed_with_reserves_and_contract_id ) def test_import_contract_no_contract_data(): """Test import contract when no contract data.""" import_results = import_contract( { 'account_id': 791766, 'account_name': 'Plastic Head Distribution', 'account_currency': 'EUR', 'account_status': 'signed', 'paid_by': 6, 'payment_minimum': 250, 'payment_schedule': '60_days_after_month_end', 'reserves': None }, True ) assert import_results == [] def test_import_contract_empty_contract_data(): """Test import contract when contract data related fields are empty.""" import_results = import_contract( { 'account_id': 791766, 'account_name': 'Plastic Head Distribution', 'account_currency': 'EUR', 'account_status': 'signed', 'contract_id': None, 'contract_name': None, 'paid_by': 6, 'payment_minimum': 250, 'payment_schedule': '60_days_after_month_end', 'reserves': None }, True ) assert import_results == [] @patch('json_contract_file_import.contract_import.update_contract') @patch('json_contract_file_import.contract_import.create_contract') @patch('json_contract_file_import.contract_import.import_run_controller') @patch('json_contract_file_import.contract_import.import_contract_terms') @patch('json_contract_file_import.contract_import.import_contract_reserves') @patch('json_contract_file_import.contract_import._contract_already_exists') def test_import_contract_success( mock_contract_already_exists, mock_import_contract_reserves, mock_import_contract_term, mock_import_run_controller, mock_create_contract, mock_update_contract, mock_json_contract_file_parsed_with_reserves ): """Test successful import a new contract and overwrite is enabled.""" created_contract_id = 3 account_id = 2486 mock_contract_already_exists.return_value = False mock_response = MagicMock() mock_response.status_code = 201 mock_response.text = f"""{{ "contract_type": "{constants.CONTRACT_TYPES.DISTRIBUTION}", "execution_date": "2015-02-23", "reference_signing_entity_id": 11, "term_end": null, "term_start": null, "summary_note": null, "general_note": null, "contract_id": {created_contract_id}, "contract_name": "Zahara Distribution Agreement", "oa_contract_id": null, "account_id": {account_id}, "sap_created_at": null, "initial_start_date": "2015-04-23", "is_excluded_from_accounting_run": false }}""" mock_response.json.return_value.get.return_value = created_contract_id mock_create_contract.return_value = mock_response run_controller_import_result = EntityImportResult( EntityImportResult.RUN_CONTROLLER, f'account:{account_id}-contract:{created_contract_id}', True, additional_info='some_additional_info' ) mock_import_run_controller.return_value = [run_controller_import_result] contract_reserve_import_result = EntityImportResult( EntityImportResult.CONTRACT_RESERVE, '1', True, additional_info='some_additional_info' ) mock_import_contract_reserves.return_value = contract_reserve_import_result contract_term_import_result = EntityImportResult( EntityImportResult.CONTRACT_TERM, '123', True, additional_info='some_additional_info' ) mock_import_contract_term.return_value = [contract_term_import_result] response = contract_import.import_contract( mock_json_contract_file_parsed_with_reserves, True ) expected_contract_import_result = EntityImportResult( EntityImportResult.CONTRACT, -1, True, additional_info=mock_response.text ) mock_create_contract.assert_called_once_with({ 'contract_name': 'Zahara Distribution Agreement', 'contract_type': CONTRACT_TYPES.DISTRIBUTION, 'contract_id': None, 'account_id': account_id, 'execution_date': '2015-02-23', 'general_note': 'Some general note.', 'reference_signing_entity_id': 11, }) mock_update_contract.assert_called_once_with( created_contract_id, {'initial_start_date': '2015-04-23'} ) mock_import_run_controller.assert_called_once_with( created_contract_id, account_id, ORCHARD_RUN_CONTROLLER_ID ) mock_import_contract_reserves.assert_called_once_with( created_contract_id, mock_json_contract_file_parsed_with_reserves.get('reserves') ) mock_import_contract_term.assert_called_once_with( created_contract_id, mock_json_contract_file_parsed_with_reserves ) assert len(response) == 4 assert_import_result(response[0], expected_contract_import_result) assert_import_result(response[1], run_controller_import_result) assert_import_result(response[2], contract_reserve_import_result) assert_import_result(response[3], contract_term_import_result) @patch('json_contract_file_import.contract_import.update_contract') @patch('json_contract_file_import.contract_import.create_contract') @patch('json_contract_file_import.contract_import.import_run_controller') @patch('json_contract_file_import.contract_import.import_contract_terms') @patch('json_contract_file_import.contract_import.import_contract_reserves') @patch('json_contract_file_import.contract_import.import_contract_lifecycle_schedules') @patch('json_contract_file_import.contract_import' '.import_contract_mechanical_deductions') def test__import_new_contract( mock_import_contract_mechanical_deductions, mock_import_contract_lifecycle_schedules, mock_import_contract_reserves, mock_import_contract_term, mock_import_run_controller, mock_create_contract, mock_update_contract, mock_json_contract_file_parsed_with_reserves_and_contract_id ): """Test successful import a new contract with contract_id.""" contract_id = 100 account_id = 2486 run_controller_id = 30 mock_response = MagicMock() mock_response.status_code = 201 mock_response.text = f"""{{ "contract_type": "{constants.CONTRACT_TYPES.DISTRIBUTION}", "execution_date": "2015-02-23", "reference_signing_entity_id": 11, "term_end": null, "term_start": null, "summary_note": null, "general_note": null, "contract_id": {contract_id}, "contract_name": "Zahara Distribution Agreement", "oa_contract_id": null, "account_id": {account_id}, "sap_created_at": null, "initial_start_date": "2015-04-23", "is_excluded_from_accounting_run": false }}""" mock_response.json.return_value.get.return_value = contract_id mock_create_contract.return_value = mock_response run_controller_import_result = EntityImportResult( EntityImportResult.RUN_CONTROLLER, f'account:{account_id}-contract:{contract_id}', True, additional_info='some_additional_info' ) mock_import_run_controller.return_value = [run_controller_import_result] contract_reserve_import_result = EntityImportResult( EntityImportResult.CONTRACT_RESERVE, '1', True, additional_info='some_additional_info' ) mock_import_contract_reserves.return_value = contract_reserve_import_result mechanical_deductions_import_result = EntityImportResult( EntityImportResult.CONTRACT_MECHANICAL_DEDUCTIONS, '1', True, ) mock_import_contract_mechanical_deductions.return_value = [ mechanical_deductions_import_result ] contract_term_import_result = EntityImportResult( EntityImportResult.CONTRACT_TERM, '123', True, additional_info='some_additional_info' ) mock_import_contract_term.return_value = [contract_term_import_result] response = contract_import._import_new_contract( contract_id, mock_json_contract_file_parsed_with_reserves_and_contract_id, ) expected_contract_import_result = EntityImportResult( EntityImportResult.CONTRACT, contract_id, True, additional_info=mock_response.text ) mock_create_contract.assert_called_once_with({ 'contract_name': 'Zahara Distribution Agreement', 'contract_type': CONTRACT_TYPES.DISTRIBUTION, 'contract_id': contract_id, 'oa_contract_id': contract_id, 'account_id': account_id, 'execution_date': '2015-02-23', 'general_note': 'Some general note.', 'reference_signing_entity_id': 11, }) mock_update_contract.assert_called_once_with( contract_id, {'initial_start_date': '2015-04-23'} ) mock_import_contract_lifecycle_schedules.assert_called_once_with( contract_id, mock_json_contract_file_parsed_with_reserves_and_contract_id[ 'contract_lifecycle' ] ) mock_import_run_controller.assert_called_once_with( contract_id, account_id, run_controller_id ) mock_import_contract_reserves.assert_called_once_with( contract_id, mock_json_contract_file_parsed_with_reserves_and_contract_id.get('reserves') ) mock_import_contract_mechanical_deductions.assert_called_once_with( contract_id, mock_json_contract_file_parsed_with_reserves_and_contract_id.get( 'mechanical_deductions' ) ) mock_import_contract_term.assert_called_once_with( contract_id, mock_json_contract_file_parsed_with_reserves_and_contract_id ) assert len(response) == 5 assert_import_result(response[0], expected_contract_import_result) assert_import_result(response[1], run_controller_import_result) assert_import_result(response[2], contract_reserve_import_result) assert_import_result(response[3], mechanical_deductions_import_result) assert_import_result(response[4], contract_term_import_result) @patch('json_contract_file_import.contract_import.update_contract') @patch('json_contract_file_import.contract_import' '.contract_terms_delete.delete_contract_terms') @patch('json_contract_file_import.contract_import.import_contract_reserves') @patch('json_contract_file_import.contract_import.import_contract_terms') @patch('json_contract_file_import.contract_import.import_contract_lifecycle_schedules') @patch('json_contract_file_import.contract_import' '.import_contract_mechanical_deductions') @patch('json_contract_file_import.contract_import.import_run_controller') def test__import_existing_contract( mock_import_run_controller, mock_import_contract_mechanical_deductions, mock_import_contract_lifecycle_schedules, mock_import_contract_terms, mock_import_contract_reserves, mock_delete_contract_terms, mock_update_contract, mock_json_contract_file_parsed_with_reserves_and_contract_id ): """Test successful updating existing contract.""" created_contract_id = 100 account_id = 2486 mock_response = MagicMock() mock_response.status_code = 200 mock_response.text = f"""{{ "contract_type": "{constants.CONTRACT_TYPES.DISTRIBUTION}", "execution_date": "2015-02-23", "reference_signing_entity_id": 11, "term_end": null, "term_start": null "summary_note": null, "general_note": null, "contract_id": {created_contract_id}, "contract_name": "Zahara Distribution Agreement", "oa_contract_id": null, "account_id": {account_id}, "sap_created_at": null, "initial_start_date": "2015-04-23", "is_excluded_from_accounting_run": false }}""" mock_response.json.return_value.get.return_value = created_contract_id mock_update_contract.return_value = mock_response run_controller_import_result = EntityImportResult( EntityImportResult.RUN_CONTROLLER, f'account:{account_id}-contract:{created_contract_id}', True, additional_info='some_additional_info' ) mock_import_run_controller.return_value = [run_controller_import_result] contract_reserve_import_result = EntityImportResult( EntityImportResult.CONTRACT_RESERVE, '1', True, additional_info='some_additional_info' ) mock_import_contract_reserves.return_value = contract_reserve_import_result contract_mechanical_deductions_import_result = EntityImportResult( EntityImportResult.CONTRACT_MECHANICAL_DEDUCTIONS, '1', True, ) mock_import_contract_mechanical_deductions.return_value = [ contract_mechanical_deductions_import_result ] contract_term_import_result = EntityImportResult( EntityImportResult.CONTRACT_TERM, '123', True, additional_info='some_additional_info' ) mock_import_contract_terms.return_value = [contract_term_import_result] response = contract_import._import_existing_contract( created_contract_id, mock_json_contract_file_parsed_with_reserves_and_contract_id ) expected_contract_import_result = EntityImportResult( EntityImportResult.CONTRACT, 100, True, additional_info=mock_response.text ) mock_import_contract_lifecycle_schedules.assert_called_once_with( created_contract_id, mock_json_contract_file_parsed_with_reserves_and_contract_id[ 'contract_lifecycle' ] ) mock_update_contract.assert_called_once_with(100, { 'contract_name': 'Zahara Distribution Agreement', 'general_note': 'Some general note.', 'execution_date': '2015-02-23', 'initial_start_date': '2015-04-23', 'reference_signing_entity_id': 11, }) mock_import_run_controller.assert_called_once_with( created_contract_id, account_id, 30 ) mock_import_contract_reserves.assert_called_once_with( created_contract_id, mock_json_contract_file_parsed_with_reserves_and_contract_id.get( 'reserves') ) mock_delete_contract_terms.assert_called_once_with( created_contract_id ) mock_import_contract_terms.assert_called_once_with( created_contract_id, mock_json_contract_file_parsed_with_reserves_and_contract_id ) assert len(response) == 5 assert_import_result(response[0], expected_contract_import_result) assert_import_result(response[1], run_controller_import_result) assert_import_result(response[2], contract_reserve_import_result) assert_import_result(response[3], contract_mechanical_deductions_import_result) assert_import_result(response[4], contract_term_import_result) @patch('json_contract_file_import.contract_import.create_contract') @patch('json_contract_file_import.contract_import._contract_already_exists') @patch('json_contract_file_import.contract_import.logger') def test_import_contract_error( mock_logger, mock_contract_already_exists, mock_create_contract, mock_json_contract_file_parsed ): """Test failed import contract.""" mock_contract_already_exists.return_value = False mock_response = MagicMock() mock_response.status_code = 400 mock_response.text = """{ "code": "error", "message": "Count not create contract." }""" mock_create_contract.return_value = mock_response response = contract_import.import_contract(mock_json_contract_file_parsed, True) expected_import_result = EntityImportResult( EntityImportResult.CONTRACT, -1, False, f'400 {mock_response.text}' ) mock_logger.error.assert_called_once_with( 'Failed response from creating contract. ' 'Skipping creating further entities. Contract: Zahara Distribution Agreement' ) assert len(response) == 1 assert_import_result(response[0], expected_import_result) @patch('json_contract_file_import.contract_import.get_contract') @patch('json_contract_file_import.contract_import.failed_response') def test__contract_already_exists_contract_exists( mock_failed_response, mock_get_contract ): """Test contract exists when contract exists.""" mock_failed_response.return_value = False mock_get_contract.return_value = MagicMock() exists = _contract_already_exists(100) assert exists @patch('json_contract_file_import.contract_import.get_contract') @patch('json_contract_file_import.contract_import.failed_response') def test__contract_already_exists_new_id( mock_failed_response, mock_get_contract ): """Test contract exists when negative id is passed.""" mock_failed_response.return_value = True mock_get_contract.return_value = MagicMock() exists = _contract_already_exists(-1) assert not exists @patch('json_contract_file_import.contract_import.get_contract') @patch('json_contract_file_import.contract_import.failed_response') def test__contract_already_exists_when_new_contract( mock_failed_response, mock_get_contract ): """Test contract exists when get_contract returns the contract.""" mock_failed_response.return_value = True mock_get_contract.return_value = MagicMock() exists = _contract_already_exists(100) assert not exists