Skip to content

Commit

Permalink
Hl 805 talpa api (#2241)
Browse files Browse the repository at this point in the history
* feat: api endpoint for Talpa robot with properly formatted CSV

* feat: Remove unused TalpaService class

* fix: Formatting issue
  • Loading branch information
rikuke committed Sep 6, 2023
1 parent f3fdb52 commit 6489e30
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 89 deletions.
38 changes: 31 additions & 7 deletions backend/benefit/applications/api/v1/application_batch_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.utils.translation import gettext_lazy as _
from django_filters import rest_framework as filters
from django_filters.widgets import CSVWidget
from drf_spectacular.utils import extend_schema
from drf_spectacular.utils import extend_schema, OpenApiParameter
from rest_framework import filters as drf_filters, status
from rest_framework.decorators import action
from rest_framework.permissions import AllowAny
Expand All @@ -25,7 +25,7 @@
)
from applications.models import Application, ApplicationBatch
from applications.services.ahjo_integration import export_application_batch
from applications.services.talpa_integration import TalpaService
from applications.services.applications_csv_report import ApplicationsCsvService
from common.authentications import RobotBasicAuthentication
from common.permissions import BFIsHandler
from shared.audit_log.viewsets import AuditLoggingModelViewSet
Expand Down Expand Up @@ -139,6 +139,20 @@ def export_batch(self, request, *args, **kwargs):
)
return response

@extend_schema(
parameters=[
OpenApiParameter(
name="skip_update",
description="Skip updating the batch status",
required=False,
type=str,
enum=["0", "1"],
),
],
description="""Get application batches for the TALPA robot.
Set skip_update=1 to skip updating the batch status to SENT_TO_TALPA""",
methods=["GET"],
)
@action(
methods=("GET",),
detail=False,
Expand All @@ -151,6 +165,8 @@ def talpa_export_batch(self, request, *args, **kwargs) -> StreamingHttpResponse:
"""
Export ApplicationBatch to CSV format for Talpa Robot
"""
skip_update = request.query_params.get("skip_update") == "1"

approved_batches = ApplicationBatch.objects.filter(
status=ApplicationBatchStatus.DECIDED_ACCEPTED
)
Expand All @@ -164,18 +180,26 @@ def talpa_export_batch(self, request, *args, **kwargs) -> StreamingHttpResponse:
},
status=status.HTTP_400_BAD_REQUEST,
)
talpa_service = TalpaService(approved_batches)
applications = Application.objects.filter(batch__in=approved_batches).order_by(
"company__name", "application_number"
)
csv_service = ApplicationsCsvService(applications, True)
file_name = format_lazy(
_("TALPA export {date}"),
date=timezone.now().strftime("%Y%m%d_%H%M%S"),
)

response = StreamingHttpResponse(
talpa_service.get_csv_string_lines_generator(), content_type="text/csv"
csv_service.get_csv_string_lines_generator(True),
content_type="text/csv",
)
response["Content-Disposition"] = "attachment; filename={file_name}.csv".format(
file_name=file_name
response["Content-Disposition"] = "attachment; filename={filename}.csv".format(
filename=file_name
)
approved_batches.all().update(status=ApplicationBatchStatus.SENT_TO_TALPA)
# for easier testing in the test environment do not update the batches as sent_to_talpa
# remove this when TALPA integration is ready for production
if not skip_update:
approved_batches.all().update(status=ApplicationBatchStatus.SENT_TO_TALPA)
return response

@action(methods=["PATCH"], detail=False)
Expand Down
37 changes: 0 additions & 37 deletions backend/benefit/applications/services/talpa_integration.py

This file was deleted.

16 changes: 14 additions & 2 deletions backend/benefit/applications/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from applications.enums import ApplicationStatus, BenefitType
from applications.models import Application
from applications.services.applications_csv_report import ApplicationsCsvService
from applications.services.talpa_integration import TalpaService
from applications.tests.factories import (
ApplicationBatchFactory,
ApplicationFactory,
Expand Down Expand Up @@ -72,7 +71,7 @@ def application_batch():

@pytest.fixture
def talpa_service(application_batch):
return TalpaService([application_batch])
return ApplicationsCsvService([application_batch])


@pytest.fixture
Expand Down Expand Up @@ -100,6 +99,19 @@ def applications_csv_service_with_one_application(applications_csv_service):
return ApplicationsCsvService(Application.objects.filter(pk=application1.pk))


@pytest.fixture
def pruned_applications_csv_service():
# retrieve the objects through the default manager so that annotations are added
application1 = DecidedApplicationFactory(application_number=100001)
application2 = DecidedApplicationFactory(application_number=100002)
return ApplicationsCsvService(
Application.objects.filter(pk__in=[application1.pk, application2.pk]).order_by(
"application_number"
),
True,
)


@pytest.fixture
def pruned_applications_csv_service_with_one_application(
applications_csv_service, application_batch
Expand Down
102 changes: 59 additions & 43 deletions backend/benefit/applications/tests/test_talpa_integration.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import datetime
import decimal

import pytest

from applications.services.talpa_integration import TalpaService
from applications.tests.common import (
check_csv_cell_list_lines_generator,
check_csv_string_lines_generator,
Expand All @@ -14,89 +11,108 @@
from helsinkibenefit.tests.conftest import * # noqa


def test_talpa_lines(talpa_service):
csv_lines = list(talpa_service.get_csv_cell_list_lines_generator())
assert talpa_service.get_applications().count() == 2
def test_talpa_lines(applications_csv_service):
csv_lines = list(applications_csv_service.get_csv_cell_list_lines_generator())
assert applications_csv_service.applications.count() == 2
assert len(csv_lines) == 3
assert csv_lines[0][0] == "Application number"
assert csv_lines[0][0] == "Hakemusnumero"
assert (
csv_lines[1][0] == applications_csv_service.applications[0].application_number
)
assert (
csv_lines[1][0] == talpa_service.get_applications().first().application_number
csv_lines[2][0] == applications_csv_service.applications[1].application_number
)
assert csv_lines[2][0] == talpa_service.get_applications()[1].application_number


def test_talpa_csv_cell_list_lines_generator(talpa_service):
check_csv_cell_list_lines_generator(talpa_service, expected_row_count_with_header=3)
def test_talpa_csv_cell_list_lines_generator(pruned_applications_csv_service):
check_csv_cell_list_lines_generator(
pruned_applications_csv_service, expected_row_count_with_header=3
)


def test_talpa_csv_string_lines_generator(talpa_service):
check_csv_string_lines_generator(talpa_service, expected_row_count_with_header=3)
def test_talpa_csv_string_lines_generator(pruned_applications_csv_service):
check_csv_string_lines_generator(
pruned_applications_csv_service, expected_row_count_with_header=3
)


def test_talpa_csv_output(talpa_service):
csv_lines = split_lines_at_semicolon(talpa_service.get_csv_string())
assert csv_lines[0][0] == '"Application number"'
for idx, col in enumerate(TalpaService.CSV_COLUMNS):
def test_talpa_csv_output(pruned_applications_csv_service_with_one_application):
csv_lines = split_lines_at_semicolon(
pruned_applications_csv_service_with_one_application.get_csv_string()
)
assert csv_lines[0][0] == '"Hakemusnumero"'
for idx, col in enumerate(
pruned_applications_csv_service_with_one_application.CSV_COLUMNS
):
assert csv_lines[0][idx] == f'"{col.heading}"'

assert (
int(csv_lines[1][0])
== talpa_service.get_applications().first().application_number
== pruned_applications_csv_service_with_one_application.applications.first().application_number
)


def test_talpa_csv_non_ascii_characters(talpa_service_with_one_application):
application = talpa_service_with_one_application.get_applications().first()
def test_talpa_csv_non_ascii_characters(
pruned_applications_csv_service_with_one_application,
):
application = (
pruned_applications_csv_service_with_one_application.applications.first()
)
application.company_name = "test äöÄÖtest"
application.save()
csv_lines = split_lines_at_semicolon(
talpa_service_with_one_application.get_csv_string()
pruned_applications_csv_service_with_one_application.get_csv_string()
)
assert csv_lines[1][3] == '"test äöÄÖtest"' # string is quoted


def test_talpa_csv_delimiter(talpa_service_with_one_application):
application = talpa_service_with_one_application.get_applications().first()
def test_talpa_csv_delimiter(pruned_applications_csv_service_with_one_application):
application = (
pruned_applications_csv_service_with_one_application.applications.first()
)
application.company_name = "test;12"
application.save()
assert ';"test;12";' in talpa_service_with_one_application.get_csv_string()
assert (
';"test;12";'
in pruned_applications_csv_service_with_one_application.get_csv_string()
)


def test_talpa_csv_decimal(talpa_service_with_one_application):
application = talpa_service_with_one_application.get_applications().first()
def test_talpa_csv_decimal(pruned_applications_csv_service_with_one_application):
application = (
pruned_applications_csv_service_with_one_application.applications.first()
)
application.calculation.calculated_benefit_amount = decimal.Decimal("123.45")
application.calculation.save()
csv_lines = split_lines_at_semicolon(
talpa_service_with_one_application.get_csv_string()
pruned_applications_csv_service_with_one_application.get_csv_string()
)
assert csv_lines[1][8] == "123.45"


def test_talpa_csv_date(talpa_service_with_one_application):
application = talpa_service_with_one_application.get_applications().first()
def test_talpa_csv_date(pruned_applications_csv_service_with_one_application):
application = (
pruned_applications_csv_service_with_one_application.get_applications().first()
)
application.batch.decision_date = datetime.date(2021, 8, 27)
application.batch.save()
csv_lines = split_lines_at_semicolon(
talpa_service_with_one_application.get_csv_string()
pruned_applications_csv_service_with_one_application.get_csv_string()
)
assert csv_lines[1][11] == '"2021-08-27"'
assert csv_lines[1][12] == '"2021-08-27"'


def test_talpa_csv_missing_data(talpa_service_with_one_application):
application = talpa_service_with_one_application.get_applications().first()
application.batch.decision_date = None
application.batch.save()
with pytest.raises(ValueError):
talpa_service_with_one_application.get_csv_string()


def test_write_talpa_csv_file(talpa_service, tmp_path):
application = talpa_service.get_applications().first()
def test_write_talpa_csv_file(
pruned_applications_csv_service_with_one_application, tmp_path
):
application = (
pruned_applications_csv_service_with_one_application.applications.first()
)
application.company_name = "test äöÄÖtest"
application.save()
output_file = tmp_path / "output.csv"
talpa_service.write_csv_file(output_file)
pruned_applications_csv_service_with_one_application.write_csv_file(output_file)
with open(output_file, encoding="utf-8") as f:
contents = f.read()
assert contents.startswith('"Application number";"Organization type"')
assert contents.startswith('"Hakemusnumero";"Työnantajan tyyppi"')
assert "äöÄÖtest" in contents

0 comments on commit 6489e30

Please sign in to comment.