Skip to content

Commit

Permalink
Fix crash in get_annotation when delete marker is set
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorjerse committed Nov 22, 2024
1 parent f37ad36 commit 04012d5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
9 changes: 9 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ All notable changes to this project are documented in this file.
This project adheres to `Semantic Versioning <http://semver.org/>`_.


==========
Unreleased
==========

Fixed
-----
- Fix crash in ``get_annotation`` when delete marker is set


===================
42.0.0 - 2024-11-21
===================
Expand Down
10 changes: 7 additions & 3 deletions resolwe/flow/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 20 additions & 3 deletions resolwe/flow/tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 04012d5

Please sign in to comment.