Skip to content
Snippets Groups Projects
Commit 10bae7d9 authored by Erwan Rouchet's avatar Erwan Rouchet Committed by Valentin Rigal
Browse files

Save Process.template_id when applying a template

parent 62fb3d63
No related branches found
No related tags found
1 merge request!2412Save Process.template_id when applying a template
......@@ -507,6 +507,7 @@ class ApplyProcessTemplateSerializer(ProcessACLMixin, serializers.Serializer):
# 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
target_process.save(update_fields=["template_id"])
return target_process
def validate_process_id(self, process):
......
......@@ -397,7 +397,7 @@ class TestTemplates(FixtureAPITestCase):
def test_apply(self):
self.assertIsNotNone(self.version_2.docker_image_iid)
self.client.force_login(self.user)
with self.assertNumQueries(10):
with self.assertNumQueries(11):
response = self.client.post(
reverse("api:apply-process-template", kwargs={"pk": str(self.template.id)}),
data={"process_id": str(self.process.id)},
......@@ -406,7 +406,10 @@ class TestTemplates(FixtureAPITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["template_id"], str(self.template.id))
child_run, parent_run = WorkerRun.objects.select_related("version__worker").filter(process__id=response.json()["id"]).order_by("version__worker__slug").all()
self.process.refresh_from_db()
self.assertEqual(self.process.template, self.template)
child_run, parent_run = self.process.worker_runs.select_related("version__worker").order_by("version__worker__slug")
self.assertEqual(parent_run.process_id, self.process.id)
self.assertEqual(parent_run.version_id, self.version_1.id)
......@@ -426,22 +429,26 @@ class TestTemplates(FixtureAPITestCase):
self.template_run_1.save()
self.client.force_login(self.user)
with self.assertNumQueries(9):
with self.assertNumQueries(10):
response = self.client.post(
reverse("api:apply-process-template", kwargs={"pk": str(self.template.id)}),
data={"process_id": str(self.process.id)},
format="json",
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["template_id"], str(self.template.id))
created_process_id = response.json()["id"]
data = response.json()
self.assertEqual(data["id"], str(self.process.id))
self.assertEqual(data["template_id"], str(self.template.id))
self.process.refresh_from_db()
self.assertEqual(self.process.template, self.template)
# Only two workers runs in the created process
# Only two workers runs in the process
self.assertEqual(self.template.worker_runs.count(), 3)
self.assertEqual(Process.objects.get(id=created_process_id).worker_runs.count(), 2)
self.assertEqual(self.process.worker_runs.count(), 2)
child_run, parent_run = WorkerRun.objects.select_related("version__worker").filter(process__id=created_process_id).order_by("version__worker__slug").all()
child_run, parent_run = self.process.worker_runs.select_related("version__worker").order_by("version__worker__slug")
self.assertEqual(parent_run.process_id, self.process.id)
self.assertEqual(parent_run.version_id, self.version_1.id)
......@@ -456,7 +463,7 @@ class TestTemplates(FixtureAPITestCase):
self.assertListEqual(child_run.parents, [parent_run.id])
# No elements initialisation run in the created process
self.assertFalse(WorkerRun.objects.filter(process_id=created_process_id, version_id=WorkerVersion.objects.init_elements_version))
self.assertFalse(self.process.worker_runs.filter(version_id=WorkerVersion.objects.init_elements_version).exists())
def test_apply_delete_previous_worker_runs(self):
self.client.force_login(self.user)
......@@ -469,7 +476,7 @@ class TestTemplates(FixtureAPITestCase):
parents=[],
)
# Apply a template that has two other worker runs
with self.assertNumQueries(12):
with self.assertNumQueries(13):
response = self.client.post(
reverse("api:apply-process-template", kwargs={"pk": str(self.template.id)}),
data={"process_id": str(process.id)},
......@@ -478,8 +485,11 @@ class TestTemplates(FixtureAPITestCase):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json()["template_id"], str(self.template.id))
process.refresh_from_db()
self.assertEqual(process.template, self.template)
# 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(process__id=response.json()["id"]).order_by("version__worker__slug").all()
child_run, parent_run = process.worker_runs.select_related("version__worker").order_by("version__worker__slug")
self.assertEqual(parent_run.process_id, process.id)
self.assertEqual(parent_run.version_id, self.version_1.id)
......@@ -518,7 +528,7 @@ class TestTemplates(FixtureAPITestCase):
(FeatureUsage.Supported, True),
])
with self.assertNumQueries(10):
with self.assertNumQueries(11):
response = self.client.post(
reverse("api:apply-process-template", kwargs={"pk": str(self.template.id)}),
{"process_id": str(self.process.id)},
......
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