diff --git a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py index 363cbf3f627bbc4a3f510787bfe54dc150430b21..c28a983c5e7fd0d1d25bbce9da9ed18645805aae 100644 --- a/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py +++ b/SAS/TMSS/backend/src/tmss/tmssapp/models/specification.py @@ -301,6 +301,24 @@ class BaseStrategyTemplate(NamedVersionedCommon): class Meta(NamedVersionedCommon.Meta): abstract = True + @property + def _need_to_auto_increment_version_number(self) -> bool: + '''For any StrategyTemplate instance, we only update the version number if the template document itself changed''' + if self.pk is None: + # this is a new instance, so we need a new version number + return True + + # check current contents of template document in the database... + prev_template_value = self.__class__.objects.values('template').get(pk=self.pk)['template'] + + # ... if it's changed + if prev_template_value != self.template: + # the template value itself changed, so do auto increment version number if this template instance is used + return self.is_used + + # the template(value) did not change, so do NOT auto_increment the version number + return False + def save(self, force_insert=False, force_update=False, using=None, update_fields=None): self.auto_set_version_number() super().save(force_insert, force_update, using, update_fields) diff --git a/SAS/TMSS/backend/test/t_tmssapp_specification_django_API.py b/SAS/TMSS/backend/test/t_tmssapp_specification_django_API.py index 3dce344b8eeed7caa5671c30b4370018cc6c9606..7e37bf7bde152b8f4dd5301831a94b1c24beee15 100755 --- a/SAS/TMSS/backend/test/t_tmssapp_specification_django_API.py +++ b/SAS/TMSS/backend/test/t_tmssapp_specification_django_API.py @@ -837,6 +837,64 @@ class SchedulingUnitBlueprintTest(unittest.TestCase): self.assertIn('does not allow triggering', str(context.exception)) +class SchedulingUnitObservingStrategyTemplateTest(unittest.TestCase): + def test_SchedulingUnitObservingStrategyTemplateTest_version_auto_increment(self): + name = str(uuid.uuid4()) + description = str(uuid.uuid4()) + scheduling_unit_template = models.SchedulingUnitTemplate.objects.create(**SchedulingUnitTemplate_test_data()) + template_doc = scheduling_unit_template.get_default_json_document_for_schema() + + template = models.SchedulingUnitObservingStrategyTemplate.objects.create(name=name, + description=description, + scheduling_unit_template=scheduling_unit_template, + template=template_doc) + + # version should be 1 + self.assertEqual(1, template.version) + + # changing name should not auto-increment version + template.name = str(uuid.uuid4()) + template.save() + template.refresh_from_db() + self.assertEqual(1, template.version) + + # changing description should not auto-increment version + template.description = str(uuid.uuid4()) + template.save() + template.refresh_from_db() + self.assertEqual(1, template.version) + + # changing template should not auto-increment version yet because the template is not used (yet) + self.assertFalse(template.is_used) + template.template['extra'] = 'property' + template.save() + template.refresh_from_db() + self.assertEqual(1, template.version) + + # use the template + models.SchedulingUnitDraft.objects.create(**SchedulingUnitDraft_test_data(observation_strategy_template=template)) + self.assertTrue(template.is_used) + + # changing name should still not auto-increment version, even now that it's used + template.name = str(uuid.uuid4()) + template.save() + template.refresh_from_db() + self.assertEqual(1, template.version) + + # changing description should still not auto-increment version, even now that it's used + template.description = str(uuid.uuid4()) + template.save() + template.refresh_from_db() + self.assertEqual(1, template.version) + + # but changing template should auto-increment version now, because the template is used + self.assertTrue(template.is_used) + template.template['extra2'] = 'property2' + template.save() + template.refresh_from_db() + self.assertEqual(2, template.version) + + class TaskBlueprintTest(unittest.TestCase): @classmethod def setUpClass(cls) -> None: