Repository: weni-ai/bothub-engine

Issue:
Bug in classmethod RepositoryTranslatedExample.count_entities reduce with multiple items in entities_list
Replace "reduce" for "for" in this [line](https://github.com/Ilhasoft/bothub-engine/blob/develop/bothub/common/models.py#L527).


Discussion:

Resolving patch:
diff --git a/bothub/common/models.py b/bothub/common/models.py
--- a/bothub/common/models.py
+++ b/bothub/common/models.py
@@ -2,8 +2,6 @@
 import base64
 import requests
 
-from functools import reduce
-
 from django.db import models
 from django.utils.translation import gettext as _
 from django.utils import timezone
@@ -524,12 +522,8 @@ def same_entities_validator(cls, a, b):
     @classmethod
     def count_entities(cls, entities_list, to_str=False):
         r = {}
-        reduce(
-            lambda current, next: current.update({
-                next.get('entity'): current.get('entity', 0) + 1,
-            }),
-            entities_list,
-            r)
+        for e in entities_list:
+            r.update({e.get('entity'): r.get('entity', 0) + 1})
         if to_str:
             r = ', '.join(map(
                 lambda x: '{} {}'.format(x[1], x[0]),


Test patch:
diff --git a/bothub/api/tests/test_translate.py b/bothub/api/tests/test_translate.py
--- a/bothub/api/tests/test_translate.py
+++ b/bothub/api/tests/test_translate.py
@@ -163,6 +163,41 @@ def test_entities_no_valid(self):
             len(content_data.get('entities')),
             1)
 
+    def test_entities_no_valid_2(self):
+        example = RepositoryExample.objects.create(
+            repository_update=self.repository.current_update(),
+            text='my name is douglas')
+        RepositoryExampleEntity.objects.create(
+            repository_example=self.example,
+            start=11,
+            end=18,
+            entity='name')
+        response, content_data = self.request(
+            {
+                'original_example': example.id,
+                'language': languages.LANGUAGE_PT,
+                'text': 'meu nome é douglas',
+                'entities': [
+                    {
+                        'start': 11,
+                        'end': 18,
+                        'entity': 'name',
+                    },
+                    {
+                        'start': 0,
+                        'end': 3,
+                        'entity': 'my',
+                    },
+                ],
+            },
+            self.owner_token)
+        self.assertEqual(
+            response.status_code,
+            status.HTTP_400_BAD_REQUEST)
+        self.assertEqual(
+            len(content_data.get('entities')),
+            1)
+
     def test_can_not_translate_to_same_language(self):
         response, content_data = self.request(
             {
diff --git a/bothub/common/tests.py b/bothub/common/tests.py
--- a/bothub/common/tests.py
+++ b/bothub/common/tests.py
@@ -159,6 +159,22 @@ def test_invalid_count_entities(self):
             translate.has_valid_entities,
             False)
 
+        RepositoryTranslatedExampleEntity.objects.create(
+            repository_translated_example=translate,
+            start=11,
+            end=18,
+            entity='name')
+
+        RepositoryTranslatedExampleEntity.objects.create(
+            repository_translated_example=translate,
+            start=0,
+            end=3,
+            entity='my')
+
+        self.assertEqual(
+            translate.has_valid_entities,
+            False)
+
     def test_invalid_how_entities(self):
         RepositoryExampleEntity.objects.create(
             repository_example=self.example,