From b44b35888af377226a5cc0a2e1631711add4a253 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Wed, 1 Dec 2021 10:38:30 +0900 Subject: [PATCH] Fix unique constraint --- backend/api/models.py | 2 +- backend/api/tests/api/test_annotation.py | 12 +++++++++--- backend/api/tests/api/utils.py | 4 ++-- backend/api/tests/test_models.py | 7 +++++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/backend/api/models.py b/backend/api/models.py index a6ea556e..f945231c 100644 --- a/backend/api/models.py +++ b/backend/api/models.py @@ -287,7 +287,7 @@ class Span(Annotation): if allow_overlapping: super().validate_unique(exclude=exclude) else: - if Span.objects.filter(example=self.example).filter( + if Span.objects.exclude(id=self.id).filter(example=self.example).filter( models.Q(start_offset__gte=self.start_offset, start_offset__lt=self.end_offset) | models.Q(end_offset__gt=self.start_offset, end_offset__lte=self.end_offset) | models.Q(start_offset__lte=self.start_offset, end_offset__gte=self.end_offset) diff --git a/backend/api/tests/api/test_annotation.py b/backend/api/tests/api/test_annotation.py index 27d2f485..242f88c5 100644 --- a/backend/api/tests/api/test_annotation.py +++ b/backend/api/tests/api/test_annotation.py @@ -1,7 +1,7 @@ from rest_framework import status from rest_framework.reverse import reverse -from ...models import DOCUMENT_CLASSIFICATION, Category +from ...models import DOCUMENT_CLASSIFICATION, SEQUENCE_LABELING, Category from .utils import (CRUDMixin, make_annotation, make_doc, make_label, make_user, prepare_project) @@ -79,11 +79,17 @@ class TestAnnotationCreation(CRUDMixin): class TestAnnotationDetail(CRUDMixin): def setUp(self): - self.project = prepare_project(task=DOCUMENT_CLASSIFICATION) + self.project = prepare_project(task=SEQUENCE_LABELING) self.non_member = make_user() doc = make_doc(self.project.item) label = make_label(self.project.item) - annotation = make_annotation(task=DOCUMENT_CLASSIFICATION, doc=doc, user=self.project.users[0]) + annotation = make_annotation( + task=SEQUENCE_LABELING, + doc=doc, + user=self.project.users[0], + start_offset=0, + end_offset=1 + ) self.data = {'label': label.id} self.url = reverse(viewname='annotation_detail', args=[self.project.item.id, doc.id, annotation.id]) diff --git a/backend/api/tests/api/utils.py b/backend/api/tests/api/utils.py index ce8019b4..a66b3292 100644 --- a/backend/api/tests/api/utils.py +++ b/backend/api/tests/api/utils.py @@ -109,14 +109,14 @@ def make_auto_labeling_config(project): return mommy.make('AutoLabelingConfig', project=project) -def make_annotation(task, doc, user): +def make_annotation(task, doc, user, **kwargs): annotation_model = { DOCUMENT_CLASSIFICATION: 'Category', SEQUENCE_LABELING: 'Span', SEQ2SEQ: 'TextLabel', SPEECH2TEXT: 'TextLabel' }.get(task) - return mommy.make(annotation_model, example=doc, user=user) + return mommy.make(annotation_model, example=doc, user=user, **kwargs) def prepare_project(task: str = 'Any', collaborative_annotation=False): diff --git a/backend/api/tests/test_models.py b/backend/api/tests/test_models.py index be29e4eb..5c264929 100644 --- a/backend/api/tests/test_models.py +++ b/backend/api/tests/test_models.py @@ -150,6 +150,13 @@ class TestSequenceAnnotation(TestCase): mommy.make('Span', example=example, start_offset=0, end_offset=5) mommy.make('Span', example=example, start_offset=10, end_offset=15) + def test_update(self): + project = mommy.make('SequenceLabelingProject', allow_overlapping=False) + example = mommy.make('Example', project=project) + span = mommy.make('Span', example=example, start_offset=0, end_offset=5) + span.end_offset = 6 + span.save() + class TestSeq2seqAnnotation(TestCase):