Skip to content
Snippets Groups Projects
Commit 44d76208 authored by Eva Bardou's avatar Eva Bardou
Browse files

Add a MinValueValidator on TranscriptioEntity.length

parent d6fa6078
No related branches found
No related tags found
1 merge request!1327Add a MinValueValidator on TranscriptioEntity.length
# Generated by Django 3.1.7 on 2021-04-28 10:10
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('documents', '0031_add_indexable_fields'),
]
operations = [
migrations.AlterField(
model_name='transcriptionentity',
name='length',
field=models.PositiveIntegerField(validators=[django.core.validators.MinValueValidator(1)]),
),
]
......@@ -6,6 +6,7 @@ from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.postgres.fields import HStoreField
from django.contrib.postgres.indexes import GinIndex
from django.core.exceptions import ValidationError
from django.core.validators import MinValueValidator
from django.db import models, transaction
from django.db.models import Q
from django.utils.functional import cached_property
......@@ -427,7 +428,7 @@ class TranscriptionEntity(models.Model):
related_name='transcription_entities',
)
offset = models.PositiveIntegerField()
length = models.PositiveIntegerField()
length = models.PositiveIntegerField(validators=[MinValueValidator(1)])
class Meta:
unique_together = (
......
......@@ -195,7 +195,7 @@ class TranscriptionEntitySerializer(serializers.ModelSerializer):
Transcription element must be passed in serializer context
"""
offset = serializers.IntegerField(min_value=0)
length = serializers.IntegerField(min_value=0)
length = serializers.IntegerField(min_value=1)
class Meta:
model = TranscriptionEntity
......
......@@ -494,6 +494,23 @@ class TestEntitiesAPI(FixtureAPITestCase):
{'transcription': ['invalid UUID or Corpus write-access is forbidden']}
)
def test_create_transcription_entity_wrong_length(self):
self.client.force_login(self.user)
response = self.client.post(
reverse('api:transcription-entity-create', kwargs={'pk': str(self.transcription.id)}),
data={
'entity': str(self.entity.id),
'offset': 4,
'length': 0
},
format='json'
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertDictEqual(
response.json(),
{'length': ['Ensure this value is greater than or equal to 1.']}
)
def test_create_transcription_entity_different_corpus(self):
self.client.force_login(self.user)
ent = Entity.objects.create(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment