Skip to content
Snippets Groups Projects
Verified Commit 6c2b7116 authored by Erwan Rouchet's avatar Erwan Rouchet
Browse files

Add tests

parent 84d32ae6
No related branches found
No related tags found
No related merge requests found
......@@ -395,14 +395,13 @@ class TranscriptionBulkSerializer(serializers.Serializer):
if not missing_ids:
return data
# Return an error message with list indexes just like DRF's ListField, for easier debugging
errors = {}
for i, transcription in enumerate(data['transcriptions']):
if transcription['element_id'] in missing_ids:
errors[str(i)] = {
"element_id": [f'Element {transcription["element_id"]} was not found or cannot be written to.'],
}
raise ValidationError({'transcriptions': errors})
# Return an error message as a list just like DRF's ListField, for easier debugging
raise ValidationError({'transcriptions': [
{"element_id": [f'Element {transcription["element_id"]} was not found or cannot be written to.']}
if transcription['element_id'] in missing_ids
else {}
for i, transcription in enumerate(data['transcriptions'])
]})
def create(self, validated_data):
transcriptions = [
......
from django.urls import reverse
from rest_framework import status
from arkindex_common.enums import TranscriptionType
from arkindex.project.tests import FixtureAPITestCase
class TestBulkTranscriptions(FixtureAPITestCase):
def test_bulk_transcriptions_requires_login(self):
with self.assertNumQueries(0):
response = self.client.post(reverse('api:transcription-bulk'))
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_bulk_transcriptions_not_found(self):
self.client.force_login(self.user)
self.user.corpus_right.all().delete()
forbidden_element = self.corpus.elements.get(name='Volume 1, page 1r')
with self.assertNumQueries(3):
response = self.client.post(reverse('api:transcription-bulk'), {
"transcriptions": [
{
"element_id": str(forbidden_element.id),
"type": TranscriptionType.Word.value,
"text": "lol",
"score": 0.4
},
{
"element_id": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
"type": TranscriptionType.Word.value,
"text": "lol",
"score": 0.4
}
],
}, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.json(), {
'transcriptions': [
{'element_id': [f'Element {forbidden_element.id} was not found or cannot be written to.']},
{'element_id': ['Element aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa was not found or cannot be written to.']},
]
})
def test_bulk_transcriptions(self):
self.client.force_login(self.user)
element1 = self.corpus.elements.get(name='Volume 2')
element2 = self.corpus.elements.get(name='Volume 2, page 1r')
self.assertFalse(element1.transcriptions.exists())
self.assertFalse(element2.transcriptions.exists())
with self.assertNumQueries(4):
response = self.client.post(reverse('api:transcription-bulk'), {
"transcriptions": [
{
"element_id": str(element1.id),
"type": TranscriptionType.Word.value,
"text": "Sneasel",
"score": 0.54
},
{
"element_id": str(element2.id),
"type": TranscriptionType.Line.value,
"text": "Charizard",
"score": 0.85
},
{
"element_id": str(element1.id),
"type": TranscriptionType.Word.value,
"text": "Raticate",
"score": 0.12
},
],
}, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertCountEqual(
list(element1.transcriptions.values('type', 'text', 'score')),
[
{
"type": TranscriptionType.Word,
"text": "Sneasel",
"score": 0.54
},
{
"type": TranscriptionType.Word,
"text": "Raticate",
"score": 0.12
},
]
)
self.assertCountEqual(
list(element2.transcriptions.values('type', 'text', 'score')),
[{
"type": TranscriptionType.Line,
"text": "Charizard",
"score": 0.85
}]
)
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