def test_invalid_operation(self, deactivate_users_mock, update_usercase_mock): transition = Transition(domain=self.domain, location_type_code='city', operation=MoveOperation.type) transition.add(old_site_code='missing_old_location', new_site_code='new_boston', new_location_details={ 'name': 'New Boston', 'parent_site_code': self.locations['Suffolk'].site_code, 'lgd_code': 'New Boston LGD Code', 'sub_district_name': None }, old_username="******", new_username="******") self.assertEqual( SQLLocation.objects.filter(domain=self.domain, site_code='new_boston').count(), 0) with self.assertRaisesMessage( InvalidTransitionError, "Could not load location with following site codes: missing_old_location" ): transition.perform() self.assertEqual( SQLLocation.objects.filter(domain=self.domain, site_code='new_boston').count(), 0, "Unexpected new location created") deactivate_users_mock.assert_not_called() update_usercase_mock.assert_not_called()
def test_can_perform_deprecation(self, *mocks): transition = Transition(domain=self.domain, location_type_code='city', operation=MoveOperation.type) transition.add(old_site_code=self.locations['Boston'].site_code, new_site_code='new_boston', new_location_details={ 'name': 'New Boston', 'parent_site_code': self.locations['Suffolk'].site_code, 'lgd_code': 'New Boston LGD Code', 'sub_district_name': 'New Boston Sub District' }, old_username="******", new_username="******") location = self.locations['Boston'] location.metadata[DEPRECATED_VIA] = MergeOperation.type location.save() with self.assertRaisesMessage( InvalidTransitionError, "Move operation: location Boston with site code boston cannot be deprecated again." ): transition.perform() location = self.locations['Boston'] location.metadata[DEPRECATED_VIA] = ExtractOperation.type location.save() location = self.locations['Boston'] location.metadata[DEPRECATED_VIA] = ExtractOperation.type location.save() transition.perform() self._validate_operation(transition.operation_obj, archived=True)
def test_perform(self, deactivate_users_mock, update_usercase_mock): transition = Transition(domain=self.domain, location_type_code='city', operation=MoveOperation.type) transition.add(old_site_code=self.locations['Boston'].site_code, new_site_code='new_boston', new_location_details={ 'name': 'New Boston', 'parent_site_code': self.locations['Suffolk'].site_code, 'lgd_code': 'New Boston LGD Code', 'sub_district_name': 'New Boston Sub District' }, old_username="******", new_username="******") self.assertEqual( SQLLocation.objects.filter(domain=self.domain, site_code='new_boston').count(), 0, "New location already present") transition.perform() self._validate_operation(transition.operation_obj, archived=True) new_location = SQLLocation.active_objects.get_or_None( domain=self.domain, site_code='new_boston') self.assertIsNotNone(new_location, "New location not created successfully") self.assertEqual(new_location.metadata[LGD_CODE], 'New Boston LGD Code') self.assertEqual(new_location.metadata[MAP_LOCATION_NAME], 'New Boston Sub District') deactivate_users_mock.assert_called_with( self.locations['Boston'].location_id) update_usercase_mock.assert_called_with(self.domain, "ethan", "aquaman")
def deprecate_locations(domain, old_locations, new_locations, operation): """ add metadata on locations on old locations, add 1. DEPRECATED_TO: [list] would be a single location except in case of a split 2. DEPRECATION_OPERATION: this location was deprecated by a merge/split/extract/move operation 3. DEPRECATED_AT: [dict] new location id mapped to the timestamp of deprecation performed for new locations location 1. DEPRECATES: [list] append this location id on it. This would be more than one location in case of merge :param domain: domain name :param old_locations: the locations to deprecate :param new_locations: the location that deprecates these location :param operation: the operation being performed, split/merge/move/extract :return: errors in case of failure """ transition = Transition(domain, operation, old_locations, new_locations) if transition.valid(): transition.perform() for old_location in old_locations: deactivate_users_at_location(old_location.location_id) return transition.errors