"""Tests for run_controller_import.""" from unittest.mock import MagicMock from unittest.mock import patch from json_contract_file_import.constants.constants import CONTRACT_TYPES from json_contract_file_import.entity_import_result import EntityImportResult from json_contract_file_import.run_controller_import import ORCHARD_RUN_CONTROLLER_ID from json_contract_file_import.run_controller_import import \ import_run_controller from tests.unit.utils import assert_import_result @patch( 'json_contract_file_import.run_controller_import.ows_royalties.get_run_controller') @patch( 'json_contract_file_import.run_controller_import' '.ows_royalties.create_run_controller_contract' ) def test_import_run_controller_success(mock_create_run_controller_contract, mock_get_run_controller): """Test successful import run controller.""" mock_get_response = MagicMock() mock_get_response.status_code = 404 mock_get_response.text = 'Not found' mock_get_run_controller.return_value = mock_get_response mock_response = MagicMock() mock_response.status_code = 201 mock_response.text = """{ "some_info": 123 }""" mock_create_run_controller_contract.return_value = mock_response response = import_run_controller( 1, 2, ORCHARD_RUN_CONTROLLER_ID ) expected_import_result = EntityImportResult( EntityImportResult.RUN_CONTROLLER, 'account:2-contract:1-run_controller:1', True, additional_info=mock_response.text ) mock_get_run_controller.assert_called_once_with(1) mock_create_run_controller_contract.assert_called_once_with({ 'account_id': 2, 'contract_id': 1, 'run_controller_id': 1, 'contract_type': CONTRACT_TYPES.DISTRIBUTION }) assert_import_result(response[0], expected_import_result) @patch( 'json_contract_file_import.run_controller_import.ows_royalties.get_run_controller') @patch( 'json_contract_file_import.run_controller_import' '.ows_royalties.create_run_controller_contract' ) def test_import_run_controller_error(mock_create_run_controller_contract, mock_get_run_controller): """Test failed import run controller.""" mock_get_response = MagicMock() mock_get_response.status_code = 404 mock_get_response.text = 'Not found' mock_get_run_controller.return_value = mock_get_response mock_response = MagicMock() mock_response.status_code = 400 mock_response.text = """{ "code": "error", "message": "Did not find account: 1" }""" mock_create_run_controller_contract.return_value = mock_response response = import_run_controller( 2, 1, ORCHARD_RUN_CONTROLLER_ID ) expected_import_result = EntityImportResult( EntityImportResult.RUN_CONTROLLER, 'account:1-contract:2-run_controller:1', False, '400 ' + mock_response.text) mock_get_run_controller.assert_called_once_with(2) assert_import_result(response[0], expected_import_result) @patch( 'json_contract_file_import.run_controller_import.ows_royalties.get_run_controller') @patch( 'json_contract_file_import.run_controller_import' '.ows_royalties.create_run_controller_contract' ) def test_import_run_controller_exists(mock_create_run_controller_contract, mock_get_run_controller): """Test when run controller already exists.""" mock_get_response = MagicMock() mock_get_response.status_code = 200 mock_get_response.text = """{ "run_controller_id": 1, "account_id": 2, "contract_id": 1 }""" mock_get_run_controller.return_value = mock_get_response response = import_run_controller( 1, 2, ORCHARD_RUN_CONTROLLER_ID ) assert len(response) == 0 mock_get_run_controller.assert_called_once_with(1) mock_create_run_controller_contract.assert_not_called()