From c0292b055da0dbd64dbc22230b681656d4aebcd3 Mon Sep 17 00:00:00 2001 From: elias-boulharts Date: Mon, 18 Dec 2023 16:25:39 +0100 Subject: [PATCH] tests --- .../test_models/test_operations.py | 50 ++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/tests/test_service_catalog/test_models/test_operations.py b/tests/test_service_catalog/test_models/test_operations.py index 01c8e400c..f69828012 100644 --- a/tests/test_service_catalog/test_models/test_operations.py +++ b/tests/test_service_catalog/test_models/test_operations.py @@ -1,19 +1,29 @@ -from service_catalog.models import Operation +from service_catalog.models import Operation, OperationType from tests.test_service_catalog.base import BaseTest class TestOperation(BaseTest): + def assertSurveyIsValid(self): + self.job_template_test.refresh_from_db() + position = 0 + for field in self.job_template_test.survey["spec"]: + self.assertTrue( + self.new_operation.tower_survey_fields.filter(variable=field["variable"], + is_customer_field=True, position=position).exists()) + position += 1 + self.assertEqual(len(self.job_template_test.survey["spec"]), self.new_operation.tower_survey_fields.count()) + def setUp(self): super(TestOperation, self).setUp() + self.new_operation = Operation.objects.create(name="new test", + type=OperationType.UPDATE, + service=self.service_test, + job_template=self.job_template_test, + process_timeout_second=20) def test_survey_from_job_template_is_copied_on_create(self): - new_operation = Operation.objects.create(name="new test", - service=self.service_test, - job_template=self.job_template_test, - process_timeout_second=20) - for field in self.job_template_test.survey["spec"]: - self.assertTrue(new_operation.tower_survey_fields.filter(variable=field["variable"], is_customer_field=True).exists()) + self.assertSurveyIsValid() def test_service_is_disabled_when_the_create_operation_disabled(self): self.assertTrue(self.create_operation_test.service.enabled) @@ -22,3 +32,29 @@ def test_service_is_disabled_when_the_create_operation_disabled(self): self.create_operation_test.save() self.assertFalse(self.create_operation_test.service.enabled) self.assertFalse(self.create_operation_test.enabled) + + def test_update_survey_when_position_is_default(self): + self.new_operation.tower_survey_fields.update(position=0) + self.assertFalse(self.new_operation.tower_survey_fields.exclude(position=0).exists()) + self.new_operation.update_survey() + self.assertSurveyIsValid() + + def test_update_survey_when_field_is_removed_from_awx(self): + previous_count = len(self.job_template_test.survey["spec"]) + self.job_template_test.survey["spec"].pop(1) + self.job_template_test.save() + current_count = len(self.job_template_test.survey["spec"]) + self.assertEqual(previous_count - 1, current_count) + self.new_operation.update_survey() + self.assertSurveyIsValid() + + def test_update_survey_when_field_is_added_from_awx(self): + previous_count = len(self.job_template_test.survey["spec"]) + tmp = self.job_template_test.survey["spec"][1] + self.job_template_test.survey["spec"][1]["variable"] = "test_swapped" + self.job_template_test.survey["spec"].append(tmp) + self.job_template_test.save() + current_count = len(self.job_template_test.survey["spec"]) + self.assertEqual(previous_count + 1, current_count) + self.new_operation.update_survey() + self.assertSurveyIsValid()