diff --git a/CHANGELOG.md b/CHANGELOG.md index c9ba24912..deb8101a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed +- Fix delete facility endpoint [#1472](https://github.com/open-apparel-registry/open-apparel-registry/pull/1472) + ### Security ## [47] 2021-09-14 diff --git a/src/django/api/tests.py b/src/django/api/tests.py index aefa9a65b..5215bc858 100644 --- a/src/django/api/tests.py +++ b/src/django/api/tests.py @@ -2744,6 +2744,60 @@ def test_other_match_is_promoted(self): alias.refresh_from_db() self.assertEqual(match_3.facility, alias.facility) + def test_matches_without_locations_are_ignored(self): + list_2 = FacilityList \ + .objects \ + .create(header='header', + file_name='two', + name='Second List') + + source_2 = Source \ + .objects \ + .create(facility_list=list_2, + source_type=Source.LIST, + is_active=True, + is_public=True, + contributor=self.contributor) + + list_item_2 = FacilityListItem \ + .objects \ + .create(name='Item', + address='Address', + country_code='US', + geocoded_point=None, + row_index=1, + status=FacilityListItem.MATCHED, + facility=self.facility, + source=source_2) + + FacilityMatch \ + .objects \ + .create(status=FacilityMatch.AUTOMATIC, + facility=self.facility, + facility_list_item=list_item_2, + confidence=0.65, + results='') + + self.client.login(email=self.superuser_email, + password=self.superuser_password) + response = self.client.delete(self.facility_url) + self.assertEqual(204, response.status_code) + + self.assertEqual( + 0, Facility.objects.filter(id=self.facility.id).count()) + self.assertEqual( + 0, FacilityMatch.objects.filter(facility=self.facility).count()) + + self.list_item.refresh_from_db() + self.assertEqual( + FacilityListItem.DELETED, self.list_item.status) + self.assertEqual( + ProcessingAction.DELETE_FACILITY, + self.list_item.processing_results[-1]['action']) + self.assertEqual( + self.facility.id, + self.list_item.processing_results[-1]['deleted_oar_id']) + def test_other_inactive_match_is_promoted(self): initial_facility_count = Facility.objects.all().count() list_2 = FacilityList \ diff --git a/src/django/api/views.py b/src/django/api/views.py index d6c6bbdd0..2c5bb533a 100644 --- a/src/django/api/views.py +++ b/src/django/api/views.py @@ -1395,7 +1395,8 @@ def destroy(self, request, pk=None): best_match = max( other_matches.filter( status__in=(FacilityMatch.AUTOMATIC, - FacilityMatch.CONFIRMED)), + FacilityMatch.CONFIRMED)).exclude( + facility_list_item__geocoded_point__isnull=True), key=lambda m: m.confidence) except ValueError: # Raised when there are no AUTOMATIC or CONFIRMED matches