Skip to content

Commit

Permalink
Add: Unittest and error handleling
Browse files Browse the repository at this point in the history
  • Loading branch information
zMardone committed Jan 19, 2024
1 parent 1bc0d1d commit a76d3df
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
2 changes: 1 addition & 1 deletion bothub/api/v2/translation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def retrieve(self, request, *args, **kwargs): # pragma: no cover
end=entity.end + count_entity,
)
count_entity += len(entity.entity.value) + 4

translated = RepositoryTranslatedExample.objects.filter(
original_example=example.pk, language=for_the_language
)
Expand Down
29 changes: 19 additions & 10 deletions bothub/common/usecase/repositorylog/export.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.http import HttpResponse

from openpyxl import Workbook
from openpyxl.writer.excel import save_virtual_workbook


class ExportRepositoryLogUseCase:
Expand All @@ -13,17 +14,23 @@ def _create_xlsx_workbook(
wb = Workbook()
ws = wb.active

ws['A1'] = 'Repository'
ws['B1'] = 'Date'
ws['C1'] = 'Action'
ws['D1'] = 'User'
ws['A1'] = 'Text'
ws['B1'] = 'Created At'
ws['C1'] = 'Intent'
ws['D1'] = 'Confidence'
ws['E1'] = 'Entities'
ws['F1'] = 'Entities List'

if repository_logs is None:
return wb

row = 2
for repository_log in repository_logs:
ws['A{}'.format(row)] = repository_log.repository.name
ws['B{}'.format(row)] = repository_log.date
ws['C{}'.format(row)] = repository_log.action
ws['D{}'.format(row)] = repository_log.user.username
ws['A{}'.format(row)] = repository_log.nlp_log.text
ws['B{}'.format(row)] = repository_log.created_at
ws['C{}'.format(row)] = repository_log.nlp_log.intent.name
ws['D{}'.format(row)] = repository_log.nlp_log.intent.confidence
ws['F{}'.format(row)] = repository_log.nlp_log.entities
row += 1

return wb
Expand All @@ -34,8 +41,10 @@ def create_xlsx_response(
) -> HttpResponse:

wb = self._create_xlsx_workbook(repository_logs)
response = HttpResponse(content_type='application/vnd.ms-excel')
response = HttpResponse(
content=save_virtual_workbook(wb),
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
response['Content-Disposition'] = 'attachment; filename=repository_logs.xlsx'
wb.save(response)

return response
Empty file.
60 changes: 60 additions & 0 deletions bothub/common/usecase/repositorylog/tests/test_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import unittest
from unittest.mock import Mock
from django.http import HttpResponse
from openpyxl import Workbook
from ..export import ExportRepositoryLogUseCase


class TestExportRepositoryLogUseCase(unittest.TestCase):

def setUp(self):
self.use_case = ExportRepositoryLogUseCase()
self.mock_repository_logs = [
Mock(
nlp_log=Mock(
text='text',
intent=Mock(
name='intent',
confidence='confidence'
),
entities='entities'
), created_at='created_at'
)
]
for log in self.mock_repository_logs:
log.nlp_log.intent.name = 'intent_name'

def test_create_xlsx_workbook(self):
# Chama a função _create_xlsx_workbook
result = self.use_case._create_xlsx_workbook(self.mock_repository_logs)
self.assertIsInstance(result, Workbook)

def test_create_xlsx_response(self):
result = self.use_case.create_xlsx_response(self.mock_repository_logs)
self.assertIsInstance(result, HttpResponse)
self.assertEqual(
result['Content-Type'],
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
self.assertEqual(
result['Content-Disposition'],
'attachment; filename=repository_logs.xlsx'
)

def test_create_xlsx_workbook_with_none(self):

result = self.use_case._create_xlsx_workbook(None)
self.assertIsInstance(result, Workbook)

def test_create_xlsx_response_with_none(self):

result = self.use_case.create_xlsx_response(None)
self.assertIsInstance(result, HttpResponse)
self.assertEqual(
result['Content-Type'],
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
)
self.assertEqual(
result['Content-Disposition'],
'attachment; filename=repository_logs.xlsx'
)

0 comments on commit a76d3df

Please sign in to comment.