Skip to content
Snippets Groups Projects
Commit 452aa2d6 authored by Erwan Rouchet's avatar Erwan Rouchet
Browse files

Merge branch 'delete-worker-runs-when-applying-templates' into 'master'

delete previous worker runs before applying the template

Closes #924

See merge request !1580
parents 90b9df7b d238fdb7
No related branches found
No related tags found
1 merge request!1580delete previous worker runs before applying the template
......@@ -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
......
......@@ -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())
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment