diff --git a/arkindex/dataimport/serializers/imports.py b/arkindex/dataimport/serializers/imports.py index e94a8df67c25ab3c97010d0fc4a2e544cc9260b3..3b6d47fd4dbcb5f9e6f666508367160d1be7ff5b 100644 --- a/arkindex/dataimport/serializers/imports.py +++ b/arkindex/dataimport/serializers/imports.py @@ -286,6 +286,9 @@ class ApplyProcessTemplateSerializer(ProcessACLMixin, serializers.Serializer): # (from url id) onto the newly created one. template_process = self.context["template"] target_process = validated_data["process"] + # If the target process, already has worker runs, these will be deleted before applying the template + WorkerRun.objects.filter(dataimport_id=target_process.id).delete() + # Apply the template by copying all the worker runs on to the new process template_process.copy_runs(target_process) target_process.template_id = template_process.id return target_process diff --git a/arkindex/dataimport/tests/test_templates.py b/arkindex/dataimport/tests/test_templates.py index 8c41c59b7ef2d560b4a8fdf00516b461695098cd..d926cf68cfd03656796b6a890f67baf63e037aee 100644 --- a/arkindex/dataimport/tests/test_templates.py +++ b/arkindex/dataimport/tests/test_templates.py @@ -254,7 +254,7 @@ class TestTemplates(FixtureAPITestCase): def test_apply_process_template(self): self.client.force_login(self.user) - with self.assertNumQueries(15): + with self.assertNumQueries(16): response = self.client.post( reverse('api:apply-process-template', kwargs={'pk': str(self.template.id)}), data=json.dumps({"process_id": str(self.dataimport.id)}), @@ -263,7 +263,39 @@ class TestTemplates(FixtureAPITestCase): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.json()['template_id'], str(self.template.id)) - new_process_workers = WorkerRun.objects.select_related('version__worker').filter(dataimport__id=response.json()["id"]) + child_run, parent_run = WorkerRun.objects.select_related('version__worker').filter(dataimport__id=response.json()["id"]).order_by('version__worker__slug').all() + # Check dependency + self.assertListEqual(child_run.parents, [parent_run.id]) + + # Check that every new worker_run is the same as one of the template's + self.assertTrue(self.dataimport_template.worker_runs.filter(version=parent_run.version).exists()) + self.assertTrue(self.dataimport_template.worker_runs.filter(version=child_run.version).exists()) - for template_worker in self.template.worker_runs.all(): - self.assertTrue(new_process_workers.filter(version=template_worker.version).exists()) + def test_apply_process_template_delete_previous_worker_runs(self): + self.client.force_login(self.user) + # Create a dataimport with one worker run already + dataimport = self.corpus.imports.create( + creator=self.user, mode=DataImportMode.Workers + ) + dataimport.worker_runs.create( + version=self.version_2, + parents=[], + ) + # Apply a template that has two other worker runs + with self.assertNumQueries(17): + response = self.client.post( + reverse('api:apply-process-template', kwargs={'pk': str(self.template.id)}), + data=json.dumps({"process_id": str(dataimport.id)}), + content_type='application/json', + ) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(response.json()['template_id'], str(self.template.id)) + + # Assert that the previous worker runs was deleted and the template was correctly applied + child_run, parent_run = WorkerRun.objects.select_related('version__worker').filter(dataimport__id=response.json()["id"]).order_by('version__worker__slug').all() + # Check dependency + self.assertListEqual(child_run.parents, [parent_run.id]) + + # Check that every new worker_run is the same as one of the template's + self.assertTrue(self.template.worker_runs.filter(version=parent_run.version).exists()) + self.assertTrue(self.template.worker_runs.filter(version=child_run.version).exists())