Browse Source

Add clean method to Relation

pull/1703/head
Hironsan 2 years ago
parent
commit
bb33db3e71
3 changed files with 43 additions and 1 deletions
  1. 2
      backend/label_types/urls.py
  2. 10
      backend/labels/models.py
  3. 32
      backend/labels/tests/test_relation.py

2
backend/label_types/urls.py

@ -19,7 +19,7 @@ urlpatterns = [
path(route="span-types/<int:label_id>", view=SpanTypeDetail.as_view(), name="span_type"),
path(route="category-type-upload", view=CategoryTypeUploadAPI.as_view(), name="category_type_upload"),
path(route="span-type-upload", view=SpanTypeUploadAPI.as_view(), name="span_type_upload"),
path(route="relation-type-upload", view=RelationTypeUploadAPI.as_view(), name="relation_type-upload"),
path(route="relation-type-upload", view=RelationTypeUploadAPI.as_view(), name="relation_type-upload"),
path(route="relation-types", view=RelationTypeList.as_view(), name="relation_types_list"),
path(route="relation-types/<int:label_id>", view=RelationTypeDetail.as_view(), name="relation_type_detail"),
]

10
backend/labels/models.py

@ -103,3 +103,13 @@ class Relation(Label):
def __str__(self):
return self.__dict__.__str__()
def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
self.full_clean()
super().save(force_insert, force_update, using, update_fields)
def clean(self):
same_example = self.from_id.example == self.to_id.example == self.example
if not same_example:
raise ValidationError("You need to label the same example.")
return super().clean()

32
backend/labels/tests/test_relation.py

@ -0,0 +1,32 @@
from django.core.exceptions import ValidationError
from django.test import TestCase
from model_mommy import mommy
from projects.models import SEQUENCE_LABELING
from projects.tests.utils import prepare_project
class TestRelationLabeling(TestCase):
@classmethod
def setUpTestData(cls):
cls.project = prepare_project(SEQUENCE_LABELING)
cls.example = mommy.make("Example", project=cls.project.item)
cls.label_type = mommy.make("RelationType", project=cls.project.item)
cls.user = cls.project.admin
def test_can_annotate_relation(self):
from_id = mommy.make("Span", example=self.example, start_offset=0, end_offset=1)
to_id = mommy.make("Span", example=self.example, start_offset=1, end_offset=2)
mommy.make("Relation", example=self.example, from_id=from_id, to_id=to_id)
def test_cannot_annotate_relation_if_span_example_is_different(self):
from_id = mommy.make("Span", example=self.example, start_offset=0, end_offset=1)
to_id = mommy.make("Span", start_offset=1, end_offset=2)
with self.assertRaises(ValidationError):
mommy.make("Relation", example=self.example, from_id=from_id, to_id=to_id)
def test_cannot_annotate_relation_if_relation_example_is_different_from_span_example(self):
from_id = mommy.make("Span", example=self.example, start_offset=0, end_offset=1)
to_id = mommy.make("Span", example=self.example, start_offset=1, end_offset=2)
with self.assertRaises(ValidationError):
mommy.make("Relation", from_id=from_id, to_id=to_id)
Loading…
Cancel
Save