From 04012d53493661d6bf53a29ed0acf07fb17db21c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gregor=20Jer=C5=A1e?= Date: Fri, 22 Nov 2024 11:14:18 +0100 Subject: [PATCH] Fix crash in get_annotation when delete marker is set --- docs/CHANGELOG.rst | 9 +++++++++ resolwe/flow/models/entity.py | 10 +++++++--- resolwe/flow/tests/test_annotations.py | 23 ++++++++++++++++++++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/docs/CHANGELOG.rst b/docs/CHANGELOG.rst index 341349836..92ee9d4db 100644 --- a/docs/CHANGELOG.rst +++ b/docs/CHANGELOG.rst @@ -6,6 +6,15 @@ All notable changes to this project are documented in this file. This project adheres to `Semantic Versioning `_. +========== +Unreleased +========== + +Fixed +----- +- Fix crash in ``get_annotation`` when delete marker is set + + =================== 42.0.0 - 2024-11-21 =================== diff --git a/resolwe/flow/models/entity.py b/resolwe/flow/models/entity.py index febcef652..7b9bedf12 100644 --- a/resolwe/flow/models/entity.py +++ b/resolwe/flow/models/entity.py @@ -210,9 +210,13 @@ def get_annotation(self, path: str, default: Any = None) -> Any: :return: value of the annotation or default if not found. """ group_name, field_name = path.split(".", maxsplit=1) - annotation: AnnotationValue = self.annotations.filter( - field__group__name=group_name, field__name=field_name - ).first() + annotation: AnnotationValue = ( + self.annotations.filter( + field__group__name=group_name, field__name=field_name + ) + .remove_delete_markers() + .first() + ) return annotation.value if annotation else default def set_annotation(self, path: str, value: Any, contributor=None): diff --git a/resolwe/flow/tests/test_annotations.py b/resolwe/flow/tests/test_annotations.py index 9a3aaf9ee..4c6bc7147 100644 --- a/resolwe/flow/tests/test_annotations.py +++ b/resolwe/flow/tests/test_annotations.py @@ -40,21 +40,38 @@ class AnnotationValueTest(TestCase): def setUp(self): """Prepare the test entity and annotation values.""" super().setUp() - entity: Entity = Entity.objects.create( + self.entity: Entity = Entity.objects.create( name="Entity", contributor=self.contributor ) annotation_group: AnnotationGroup = AnnotationGroup.objects.create( name="group", label="Annotation group", sort_order=1 ) self.field = AnnotationField.objects.create( - name="Field 1", + name="field_1", label="Field 1 label", type=AnnotationType.STRING.value, sort_order=1, group=annotation_group, ) self.value = AnnotationValue.objects.create( - entity=entity, field=self.field, value="Test", contributor=self.contributor + entity=self.entity, + field=self.field, + value="Test", + contributor=self.contributor, + ) + + def test_get_annotation(self): + self.assertEqual(self.entity.get_annotation("group.field_1"), "Test") + # Create delete marker and test again. + AnnotationValue.objects.create( + entity=self.entity, + field=self.field, + _value=None, + contributor=self.contributor, + ) + self.assertIsNone(self.entity.get_annotation("group.field_1")) + self.assertEqual( + self.entity.get_annotation("group.field_1", "default"), "default" ) def test_protected(self):