Browse Source

Fix unique constraint

pull/1580/head
Hironsan 2 years ago
parent
commit
b44b35888a
4 changed files with 19 additions and 6 deletions
  1. 2
      backend/api/models.py
  2. 12
      backend/api/tests/api/test_annotation.py
  3. 4
      backend/api/tests/api/utils.py
  4. 7
      backend/api/tests/test_models.py

2
backend/api/models.py

@ -287,7 +287,7 @@ class Span(Annotation):
if allow_overlapping: if allow_overlapping:
super().validate_unique(exclude=exclude) super().validate_unique(exclude=exclude)
else: 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(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(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) models.Q(start_offset__lte=self.start_offset, end_offset__gte=self.end_offset)

12
backend/api/tests/api/test_annotation.py

@ -1,7 +1,7 @@
from rest_framework import status from rest_framework import status
from rest_framework.reverse import reverse 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, from .utils import (CRUDMixin, make_annotation, make_doc, make_label,
make_user, prepare_project) make_user, prepare_project)
@ -79,11 +79,17 @@ class TestAnnotationCreation(CRUDMixin):
class TestAnnotationDetail(CRUDMixin): class TestAnnotationDetail(CRUDMixin):
def setUp(self): def setUp(self):
self.project = prepare_project(task=DOCUMENT_CLASSIFICATION)
self.project = prepare_project(task=SEQUENCE_LABELING)
self.non_member = make_user() self.non_member = make_user()
doc = make_doc(self.project.item) doc = make_doc(self.project.item)
label = make_label(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.data = {'label': label.id}
self.url = reverse(viewname='annotation_detail', args=[self.project.item.id, doc.id, annotation.id]) self.url = reverse(viewname='annotation_detail', args=[self.project.item.id, doc.id, annotation.id])

4
backend/api/tests/api/utils.py

@ -109,14 +109,14 @@ def make_auto_labeling_config(project):
return mommy.make('AutoLabelingConfig', project=project) return mommy.make('AutoLabelingConfig', project=project)
def make_annotation(task, doc, user):
def make_annotation(task, doc, user, **kwargs):
annotation_model = { annotation_model = {
DOCUMENT_CLASSIFICATION: 'Category', DOCUMENT_CLASSIFICATION: 'Category',
SEQUENCE_LABELING: 'Span', SEQUENCE_LABELING: 'Span',
SEQ2SEQ: 'TextLabel', SEQ2SEQ: 'TextLabel',
SPEECH2TEXT: 'TextLabel' SPEECH2TEXT: 'TextLabel'
}.get(task) }.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): def prepare_project(task: str = 'Any', collaborative_annotation=False):

7
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=0, end_offset=5)
mommy.make('Span', example=example, start_offset=10, end_offset=15) 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): class TestSeq2seqAnnotation(TestCase):

Loading…
Cancel
Save