diff --git a/rorapi/common/csv_update.py b/rorapi/common/csv_update.py index 3e41716..df12c45 100644 --- a/rorapi/common/csv_update.py +++ b/rorapi/common/csv_update.py @@ -127,7 +127,7 @@ def update_record_from_csv(csv_data, version): temp_ext_ids = [i for i in temp_ext_ids if i['type'] != t] else: - if not temp_preferred in temp_all: + if temp_preferred is not None and temp_preferred not in temp_all: errors.append("Changes to external ID object with type {} result in preferred value '{}' not in all values '{}'".format(t, temp_preferred, ", ".join(temp_all))) # remove all of type and replace with new obj new_ext_id_obj = { diff --git a/rorapi/management/commands/generaterorid.py b/rorapi/management/commands/generaterorid.py index af07be0..a743981 100644 --- a/rorapi/management/commands/generaterorid.py +++ b/rorapi/management/commands/generaterorid.py @@ -23,7 +23,7 @@ def check_ror_id(version): ror_id = get_ror_id(generate_ror_id()) errors, organization = retrieve_organization(ror_id, version) if errors is None: - check_ror_id(version) + return check_ror_id(version) return ror_id diff --git a/rorapi/tests/tests_unit/tests_generaterorid_v1.py b/rorapi/tests/tests_unit/tests_generaterorid_v1.py new file mode 100644 index 0000000..d9fe5d8 --- /dev/null +++ b/rorapi/tests/tests_unit/tests_generaterorid_v1.py @@ -0,0 +1,35 @@ +from django.test import SimpleTestCase +from unittest.mock import patch +from rorapi.management.commands import generaterorid +from rorapi.common.models import Errors +from rorapi.settings import ROR_API + +DUPLICATE_ID_RAW = "duplicateid" +UNIQUE_ID_RAW = "uniqueid" +DUPLICATE_ROR_ID = f"{ROR_API['ID_PREFIX']}{DUPLICATE_ID_RAW}" +UNIQUE_ROR_ID = f"{ROR_API['ID_PREFIX']}{UNIQUE_ID_RAW}" +TEST_VERSION = 'v1' + +class GenerateRorIdCommandTestCase(SimpleTestCase): + + @patch('rorapi.management.commands.generaterorid.get_ror_id') + @patch('rorapi.management.commands.generaterorid.retrieve_organization') + @patch('rorapi.management.commands.generaterorid.generate_ror_id') + def test_check_ror_id_handles_collision_and_returns_unique( + self, mock_generate_ror_id, mock_retrieve_organization, mock_get_ror_id + ): + mock_generate_ror_id.side_effect = [ + DUPLICATE_ROR_ID, + UNIQUE_ROR_ID + ] + + mock_get_ror_id.side_effect = lambda x: x + + mock_retrieve_organization.side_effect = [ + (None, {'id': DUPLICATE_ROR_ID, 'name': 'Mock Duplicate Org'}), + (Errors(f"ROR ID '{UNIQUE_ROR_ID}' does not exist"), None) + ] + + result_ror_id = generaterorid.check_ror_id(TEST_VERSION) + + self.assertEqual(result_ror_id, UNIQUE_ROR_ID) \ No newline at end of file diff --git a/rorapi/tests/tests_unit/tests_generaterorid_v2.py b/rorapi/tests/tests_unit/tests_generaterorid_v2.py new file mode 100644 index 0000000..95f20b9 --- /dev/null +++ b/rorapi/tests/tests_unit/tests_generaterorid_v2.py @@ -0,0 +1,35 @@ +from django.test import SimpleTestCase +from unittest.mock import patch +from rorapi.management.commands import generaterorid +from rorapi.common.models import Errors +from rorapi.settings import ROR_API + +DUPLICATE_ID_RAW = "duplicateid" +UNIQUE_ID_RAW = "uniqueid" +DUPLICATE_ROR_ID = f"{ROR_API['ID_PREFIX']}{DUPLICATE_ID_RAW}" +UNIQUE_ROR_ID = f"{ROR_API['ID_PREFIX']}{UNIQUE_ID_RAW}" +TEST_VERSION = 'v2' + +class GenerateRorIdCommandTestCase(SimpleTestCase): + + @patch('rorapi.management.commands.generaterorid.get_ror_id') + @patch('rorapi.management.commands.generaterorid.retrieve_organization') + @patch('rorapi.management.commands.generaterorid.generate_ror_id') + def test_check_ror_id_handles_collision_and_returns_unique( + self, mock_generate_ror_id, mock_retrieve_organization, mock_get_ror_id + ): + mock_generate_ror_id.side_effect = [ + DUPLICATE_ROR_ID, + UNIQUE_ROR_ID + ] + + mock_get_ror_id.side_effect = lambda x: x + + mock_retrieve_organization.side_effect = [ + (None, {'id': DUPLICATE_ROR_ID, 'name': 'Mock Duplicate Org'}), + (Errors(f"ROR ID '{UNIQUE_ROR_ID}' does not exist"), None) + ] + + result_ror_id = generaterorid.check_ror_id(TEST_VERSION) + + self.assertEqual(result_ror_id, UNIQUE_ROR_ID) \ No newline at end of file