From 556da7f45a22a995a4769802aeb77aa992197da3 Mon Sep 17 00:00:00 2001
From: Bastien Abadie <bastien@nextcairn.com>
Date: Fri, 29 Jun 2018 11:31:43 +0200
Subject: [PATCH] Restore unit tests + flake8

---
 arkindex/documents/api.py                     |  1 +
 arkindex/documents/serializers.py             |  1 +
 .../documents/tests/test_annotation_list.py   |  4 +-
 arkindex/documents/tests/test_search_post.py  | 44 +++++++++++++++----
 ...create.py => test_transcription_create.py} | 30 ++++++++-----
 arkindex/images/importer.py                   |  2 +-
 arkindex/images/tests.py                      |  2 +-
 7 files changed, 60 insertions(+), 24 deletions(-)
 rename arkindex/documents/tests/{test_text_create.py => test_transcription_create.py} (81%)

diff --git a/arkindex/documents/api.py b/arkindex/documents/api.py
index 7c638bf723..7f6cc57b94 100644
--- a/arkindex/documents/api.py
+++ b/arkindex/documents/api.py
@@ -179,6 +179,7 @@ class TranscriptionBulk(CreateAPIView):
                 ),
             )
 
+
 class TranscriptionCreate(CreateAPIView):
     serializer_class = TranscriptionCreateSerializer
     permission_classes = (IsAuthenticated, )
diff --git a/arkindex/documents/serializers.py b/arkindex/documents/serializers.py
index e4c3424a1d..1eb57df216 100644
--- a/arkindex/documents/serializers.py
+++ b/arkindex/documents/serializers.py
@@ -195,6 +195,7 @@ class TranscriptionsSerializer(serializers.Serializer):
     parent = serializers.PrimaryKeyRelatedField(queryset=Element.objects.all())
     image = serializers.PrimaryKeyRelatedField(queryset=Image.objects.all())
 
+
 class TranscriptionSearchResultSerializer(serializers.ModelSerializer):
     """
     Link between objects & their search indexation
diff --git a/arkindex/documents/tests/test_annotation_list.py b/arkindex/documents/tests/test_annotation_list.py
index 3fb53971d2..9457bf7aa8 100644
--- a/arkindex/documents/tests/test_annotation_list.py
+++ b/arkindex/documents/tests/test_annotation_list.py
@@ -19,8 +19,8 @@ class TestPageAnnotationListSerializer(APITestCase):
             polygon=[(100, 200), (100, 300), (300, 300), (300, 200), (100, 200)], image=self.img)
         self.z2 = Zone.objects.create(
             polygon=[(50, 100), (50, 150), (150, 150), (150, 100), (50, 100)], image=self.img)
-        self.t1 = Transcription.objects.create(corpus=self.corpus, text="AAA", zone=self.z1)
-        self.t2 = Transcription.objects.create(corpus=self.corpus, text="BBB", zone=self.z2)
+        self.t1 = Transcription.objects.create(type=ElementType.Word, corpus=self.corpus, text="AAA", zone=self.z1)
+        self.t2 = Transcription.objects.create(type=ElementType.Word, corpus=self.corpus, text="BBB", zone=self.z2)
         ElementLink.objects.create(parent=self.page, child=self.t1, order=0)
         ElementLink.objects.create(parent=self.page, child=self.t2, order=1)
         refresh_sync_only_for_unit_tests()
diff --git a/arkindex/documents/tests/test_search_post.py b/arkindex/documents/tests/test_search_post.py
index 6c9dc214c4..8db37786c7 100644
--- a/arkindex/documents/tests/test_search_post.py
+++ b/arkindex/documents/tests/test_search_post.py
@@ -30,14 +30,42 @@ class TestSearchPostProcess(TestCase):
         ElementLink.objects.create(parent=self.vol2, child=self.p3, order=0)
 
         # Create a bunch of transcriptions
-        self.t1 = Transcription.objects.create(corpus=self.corpus, text="word", zone=Zone.objects.create(
-            polygon=[(10, 10), (20, 10), (20, 20), (10, 20), (10, 10)], image=self.img1))
-        self.t2 = Transcription.objects.create(corpus=self.corpus, text="word", zone=Zone.objects.create(
-            polygon=[(110, 110), (120, 110), (120, 120), (110, 120), (110, 110)], image=self.img1))
-        self.t3 = Transcription.objects.create(corpus=self.corpus, text="word", zone=Zone.objects.create(
-            polygon=[(210, 210), (220, 210), (220, 220), (210, 220), (210, 210)], image=self.img2))
-        self.t4 = Transcription.objects.create(corpus=self.corpus, text="word", zone=Zone.objects.create(
-            polygon=[(310, 210), (320, 310), (320, 320), (310, 320), (310, 310)], image=self.img3))
+        self.t1 = Transcription.objects.create(
+            corpus=self.corpus,
+            text="word",
+            type=ElementType.Word,
+            zone=Zone.objects.create(
+                polygon=[(10, 10), (20, 10), (20, 20), (10, 20), (10, 10)],
+                image=self.img1,
+            )
+        )
+        self.t2 = Transcription.objects.create(
+            corpus=self.corpus,
+            text="word",
+            type=ElementType.Word,
+            zone=Zone.objects.create(
+                polygon=[(110, 110), (120, 110), (120, 120), (110, 120), (110, 110)],
+                image=self.img1,
+            )
+        )
+        self.t3 = Transcription.objects.create(
+            corpus=self.corpus,
+            text="word",
+            type=ElementType.Word,
+            zone=Zone.objects.create(
+                polygon=[(210, 210), (220, 210), (220, 220), (210, 220), (210, 210)],
+                image=self.img2,
+            )
+        )
+        self.t4 = Transcription.objects.create(
+            corpus=self.corpus,
+            text="word",
+            type=ElementType.Word,
+            zone=Zone.objects.create(
+                polygon=[(310, 210), (320, 310), (320, 320), (310, 320), (310, 310)],
+                image=self.img3,
+            )
+        )
         ElementLink.objects.create(parent=self.p1, child=self.t1, order=0)
         ElementLink.objects.create(parent=self.p1, child=self.t2, order=1)
         ElementLink.objects.create(parent=self.p2, child=self.t3, order=2)
diff --git a/arkindex/documents/tests/test_text_create.py b/arkindex/documents/tests/test_transcription_create.py
similarity index 81%
rename from arkindex/documents/tests/test_text_create.py
rename to arkindex/documents/tests/test_transcription_create.py
index 817c7e5134..3bcbf790ba 100644
--- a/arkindex/documents/tests/test_text_create.py
+++ b/arkindex/documents/tests/test_transcription_create.py
@@ -9,7 +9,7 @@ from arkindex.documents.cache import refresh_sync_only_for_unit_tests
 import uuid
 
 
-class TestTextElementCreate(APITestCase):
+class TestTranscriptionCreate(APITestCase):
     """
     Tests for text element creation view
     """
@@ -25,13 +25,19 @@ class TestTextElementCreate(APITestCase):
         self.page = Page.objects.create(corpus=self.corpus, name="page", folio="page", zone=pagezone)
         self.ts_zone = Zone.objects.create(
             polygon=[(100, 200), (100, 300), (300, 300), (300, 200), (100, 200)], image=self.img)
-        self.ts = Transcription.objects.create(corpus=self.corpus, text="PAAMAYIM", score=0.5, zone=self.ts_zone)
+        self.ts = Transcription.objects.create(
+            corpus=self.corpus,
+            text="PAAMAYIM",
+            score=0.5,
+            zone=self.ts_zone,
+            type=ElementType.Word,
+        )
         ElementLink.objects.create(parent=self.page, child=self.ts, order=0)
         self.user = User.objects.create_user(email='user@user.com', password='P45$w0rD')
         refresh_sync_only_for_unit_tests()
 
     def test_require_login(self):
-        response = self.client.post(reverse('api:element'), format='json')
+        response = self.client.post(reverse('api:transcription-create'), format='json')
         self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
 
     @patch('arkindex.documents.api.Indexer')
@@ -40,7 +46,7 @@ class TestTextElementCreate(APITestCase):
         Checks the view creates transcriptions, zones, links, paths and runs ES indexing
         """
         self.client.force_login(self.user)
-        response = self.client.post(reverse('api:element'), format='json', data={
+        response = self.client.post(reverse('api:transcription-create'), format='json', data={
             "type": "word",
             "element": str(self.page.id),
             "polygon": [(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)],
@@ -61,7 +67,7 @@ class TestTextElementCreate(APITestCase):
         Checks the view does not let other element types to be created
         """
         self.client.force_login(self.user)
-        response = self.client.post(reverse('api:element'), format='json', data={
+        response = self.client.post(reverse('api:transcription-create'), format='json', data={
             "type": "register",
             "element": str(self.page.id),
             "polygon": [(0, 0), (0, 100), (100, 100), (100, 0), (0, 0)],
@@ -75,7 +81,7 @@ class TestTextElementCreate(APITestCase):
         Checks the view reuses zones when available
         """
         self.client.force_login(self.user)
-        response = self.client.post(reverse('api:element'), format='json', data={
+        response = self.client.post(reverse('api:transcription-create'), format='json', data={
             "type": "word",
             "element": str(self.page.id),
             "polygon": self.ts_zone.polygon,
@@ -90,7 +96,7 @@ class TestTextElementCreate(APITestCase):
         Checks the view updates transcriptions when they already exist
         """
         self.client.force_login(self.user)
-        response = self.client.post(reverse('api:element'), format='json', data={
+        response = self.client.post(reverse('api:transcription-create'), format='json', data={
             "type": "word",
             "element": str(self.page.id),
             "polygon": self.ts.zone.polygon,
@@ -119,27 +125,27 @@ class TestTextElementCreate(APITestCase):
 
         # Negative score
         post_data['score'] = -1
-        response = self.client.post(reverse('api:element'), format='json', data=post_data)
+        response = self.client.post(reverse('api:transcription-create'), format='json', data=post_data)
         self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
 
         # Score over 100%
         post_data['score'] = 2
-        response = self.client.post(reverse('api:element'), format='json', data=post_data)
+        response = self.client.post(reverse('api:transcription-create'), format='json', data=post_data)
         self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
 
         # Missing element
         post_data['score'] = 0.83
         post_data['element'] = str(uuid.uuid4())
-        response = self.client.post(reverse('api:element'), format='json', data=post_data)
+        response = self.client.post(reverse('api:transcription-create'), format='json', data=post_data)
         self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
 
         # Negative coordinates
         post_data['element'] = str(self.page.id)
         post_data['polygon'] = [(0, 0), (0, -100), (-100, -100), (-100, 0), (0, 0)]
-        response = self.client.post(reverse('api:element'), format='json', data=post_data)
+        response = self.client.post(reverse('api:transcription-create'), format='json', data=post_data)
         self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
 
         # Not enough polygon points
         post_data['polygon'] = [(0, 0), (100, 100)]
-        response = self.client.post(reverse('api:element'), format='json', data=post_data)
+        response = self.client.post(reverse('api:transcription-create'), format='json', data=post_data)
         self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
diff --git a/arkindex/images/importer.py b/arkindex/images/importer.py
index dda625b2f3..d907acc7aa 100644
--- a/arkindex/images/importer.py
+++ b/arkindex/images/importer.py
@@ -171,7 +171,7 @@ def bulk_transcriptions(image, parent, items, use_polygons=False):
     # Support ElementPath
     paths = ElementPath.objects.filter(element=parent).values_list('path', flat=True)
     if not paths:
-        paths = [[]] # Wonderful hack to handle no parents case
+        paths = [[]]  # Wonderful hack to handle no parents case
     ElementPath.objects.bulk_create(
         ElementPath(element=elt, path=[parent.id, ] + path)
         for elt in elements
diff --git a/arkindex/images/tests.py b/arkindex/images/tests.py
index f6fb60b6f9..289cd43ac3 100644
--- a/arkindex/images/tests.py
+++ b/arkindex/images/tests.py
@@ -1,5 +1,5 @@
 from django.test import TestCase
-from arkindex.documents.models import Corpus, Page, Transcription, Element, ElementType
+from arkindex.documents.models import Corpus, Page, Transcription, Element
 from arkindex.images.models import ImageServer, Image, Zone
 from arkindex.images.importer import bulk_transcriptions
 
-- 
GitLab