Skip to content

Commit

Permalink
Update bdd tests for destinations endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
saruniitr committed Dec 3, 2024
1 parent 19d0e67 commit c662270
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 163 deletions.
28 changes: 28 additions & 0 deletions api/data_workspace/v2/tests/bdd/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import uuid

from dateutil.parser import parse
from freezegun import freeze_time
from moto import mock_aws

from rest_framework import status
Expand Down Expand Up @@ -33,6 +34,7 @@
)
from api.cases.models import CaseType
from api.cases.tests.factories import FinalAdviceFactory
from api.cases.celery_tasks import update_cases_sla
from api.core.constants import (
ExporterPermissions,
GovPermissions,
Expand Down Expand Up @@ -272,6 +274,15 @@ def draft_application():
return draft_application


def run_processing_time_task(start, up_to):
processing_time_task_run_date_time = start.replace(hour=22, minute=30)
up_to = pytz.utc.localize(datetime.datetime.fromisoformat(up_to))
while processing_time_task_run_date_time <= up_to:
with freeze_time(processing_time_task_run_date_time):
update_cases_sla()
processing_time_task_run_date_time = processing_time_task_run_date_time + datetime.timedelta(days=1)


@pytest.fixture
def submit_application(api_client, exporter_headers, mocker):
def _submit_application(draft_application):
Expand Down Expand Up @@ -323,6 +334,23 @@ def when_the_application_is_submitted(submit_application, draft_standard_applica
return submit_application(draft_standard_application)


@when(parsers.parse("the application is issued at {timestamp}"), target_fixture="issued_application")
def when_the_application_is_issued_at(
issue_licence,
submitted_standard_application,
timestamp,
):
run_processing_time_task(submitted_standard_application.submitted_at, timestamp)

with freeze_time(timestamp):
issue_licence(submitted_standard_application)

submitted_standard_application.refresh_from_db()
issued_application = submitted_standard_application

return issued_application


@then(parsers.parse("the `{table_name}` table is empty"))
def empty_table(client, unpage_data, table_name):
metadata_url = reverse("data_workspace:v2:table-metadata")
Expand Down
125 changes: 5 additions & 120 deletions api/data_workspace/v2/tests/bdd/licences/test_destinations.py
Original file line number Diff line number Diff line change
@@ -1,125 +1,10 @@
import pytest
from pytest_bdd import given, scenarios, then, when
from datetime import datetime
from pytest_bdd import scenarios, when

from django.urls import reverse

from api.applications.models import PartyOnApplication
from api.licences.enums import LicenceStatus
from api.parties.enums import PartyType
from api.staticdata.statuses.enums import CaseStatusEnum

scenarios("../scenarios/destinations.feature")


@pytest.fixture()
def destinations_list_url():
return reverse("data_workspace:v2:dw-destinations-list")


@given("a standard licence is created", target_fixture="licence")
def standard_licence_created(standard_licence):
assert standard_licence.status == LicenceStatus.ISSUED
return standard_licence


@when("I fetch all destinations", target_fixture="destinations")
def fetch_all_destinations(destinations_list_url, unpage_data):
return unpage_data(destinations_list_url)


@then("the country code and type are included in the extract")
def country_code_and_type_included_in_extract(destinations):
party_on_application = PartyOnApplication.objects.get()
application_id = party_on_application.application_id
country_code = party_on_application.party.country.id
party_type = party_on_application.party.type

destination = {"application_id": str(application_id), "country_code": country_code, "type": party_type}
assert destination in destinations


@given("a licence with deleted party is created", target_fixture="licence_with_deleted_party")
def licence_with_deleted_party_created(licence_with_deleted_party):
assert licence_with_deleted_party.status == LicenceStatus.ISSUED
application = licence_with_deleted_party.case.baseapplication
assert PartyOnApplication.objects.filter(application=application).count() == 2


@then("the existing party is included in the extract")
def existing_party_included_in_extract(destinations):
existing_party_on_application = PartyOnApplication.objects.get(deleted_at__isnull=True)
application_id = existing_party_on_application.application_id
country_code = existing_party_on_application.party.country.id
party_type = existing_party_on_application.party.type

assert PartyOnApplication.objects.filter(application_id=application_id).count() == 2

destination = {"application_id": str(application_id), "country_code": country_code, "type": party_type}
assert destination in destinations
assert len(destinations) == 1


@then("the deleted party is not included in the extract")
def deleted_party_not_included_in_extract(destinations):
deleted_party_on_application = PartyOnApplication.objects.get(deleted_at__isnull=False)
application_id = deleted_party_on_application.application_id
country_code = deleted_party_on_application.party.country.id
party_type = deleted_party_on_application.party.type

assert PartyOnApplication.objects.filter(application_id=application_id).count() == 2

destination = {"application_id": str(application_id), "country_code": country_code, "type": party_type}
assert destination not in destinations
assert len(destinations) == 1


@given("a draft application is created")
def draft_application_created(draft_application):
assert draft_application.status.status == CaseStatusEnum.DRAFT
return draft_application


@then("the non-draft party is included in the extract")
def non_draft_party_is_included_in_extract(destinations):
non_draft_party_on_application = PartyOnApplication.objects.get(
application__status__status=CaseStatusEnum.FINALISED
)
application_id = non_draft_party_on_application.application_id
country_code = non_draft_party_on_application.party.country.id
party_type = non_draft_party_on_application.party.type

destination = {"application_id": str(application_id), "country_code": country_code, "type": party_type}
assert destination in destinations
assert len(destinations) == 1


@then("draft parties are not included in the extract")
def draft_parties_not_included_in_extract(destinations):
draft_parties_on_application = PartyOnApplication.objects.filter(application__status__status=CaseStatusEnum.DRAFT)

application_id = draft_parties_on_application.first().application_id

draft_end_user = draft_parties_on_application.get(party__type=PartyType.END_USER)
draft_end_user_country_code = draft_end_user.party.country.id
draft_end_user_party_type = draft_end_user.party.type

assert PartyOnApplication.objects.filter(application_id=application_id).count() == 2

draft_end_user_destination = {
"application_id": str(application_id),
"country_code": draft_end_user_country_code,
"type": draft_end_user_party_type,
}
assert draft_end_user_destination not in destinations

draft_consignee = draft_parties_on_application.get(party__type=PartyType.CONSIGNEE)
draft_consignee_country_code = draft_consignee.party.country.id
draft_consignee_party_type = draft_consignee.party.type

draft_consignee_destination = {
"application_id": str(application_id),
"country_code": draft_consignee_country_code,
"party_type": draft_consignee_party_type,
}
assert draft_consignee_destination not in destinations
assert len(destinations) == 1
@when("the parties are deleted")
def when_parties_are_deleted(issued_application):
issued_application.parties.update(deleted_at=datetime.now())
39 changes: 24 additions & 15 deletions api/data_workspace/v2/tests/bdd/scenarios/destinations.feature
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
@db
Feature: Destinations
Feature: destinations Table


Scenario: Draft application
Given a draft standard application
Then the `destinations` table is empty


Scenario: Check that the country code and type are included in the extract
Given a standard licence is created
When I fetch all destinations
Then the country code and type are included in the extract
Given a draft standard application with attributes:
| name | value |
| id | 03fb08eb-1564-4b68-9336-3ca8906543f9 |
When the application is submitted
And the application is issued at 2024-11-22T13:35:15
Then the `destinations` table has the following rows:
| application_id | country_code | type |
| 03fb08eb-1564-4b68-9336-3ca8906543f9 | IT | end_user |
| 03fb08eb-1564-4b68-9336-3ca8906543f9 | ES | consignee |

Scenario: Deleted parties are not included in the extract
Given a licence with deleted party is created
When I fetch all destinations
Then the existing party is included in the extract
And the deleted party is not included in the extract

Scenario: Draft applications are not included in the extract
Given a standard licence is created
And a draft application is created
When I fetch all destinations
Then the non-draft party is included in the extract
And draft parties are not included in the extract
Scenario: Deleted parties are not included in the extract
Given a draft standard application with attributes:
| name | value |
| id | 03fb08eb-1564-4b68-9336-3ca8906543f9 |
When the application is submitted
And the application is issued at 2024-11-22T13:35:15
And the parties are deleted
Then the `destinations` table is empty
29 changes: 1 addition & 28 deletions api/data_workspace/v2/tests/bdd/test_applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
GoodOnApplicationFactory,
PartyOnApplicationFactory,
)
from api.cases.celery_tasks import update_cases_sla
from api.cases.enums import (
AdviceLevel,
AdviceType,
Expand All @@ -30,20 +29,11 @@
UltimateEndUserFactory,
)
from api.staticdata.statuses.enums import CaseStatusEnum

from api.data_workspace.v2.tests.bdd.conftest import run_processing_time_task

scenarios("./scenarios/applications.feature")


def run_processing_time_task(start, up_to):
processing_time_task_run_date_time = start.replace(hour=22, minute=30)
up_to = pytz.utc.localize(datetime.datetime.fromisoformat(up_to))
while processing_time_task_run_date_time <= up_to:
with freeze_time(processing_time_task_run_date_time):
update_cases_sla()
processing_time_task_run_date_time = processing_time_task_run_date_time + datetime.timedelta(days=1)


@pytest.fixture
def submit_application(api_client, exporter_headers, mocker):
def _submit_application(draft_application):
Expand Down Expand Up @@ -169,23 +159,6 @@ def when_the_application_is_submitted_at(submit_application, draft_standard_appl
return submit_application(draft_standard_application)


@when(parsers.parse("the application is issued at {timestamp}"), target_fixture="issued_application")
def when_the_application_is_issued_at(
issue_licence,
submitted_standard_application,
timestamp,
):
run_processing_time_task(submitted_standard_application.submitted_at, timestamp)

with freeze_time(timestamp):
issue_licence(submitted_standard_application)

submitted_standard_application.refresh_from_db()
issued_application = submitted_standard_application

return issued_application


@when(parsers.parse("the application is refused at {timestamp}"), target_fixture="refused_application")
def when_the_application_is_refused_at(
submitted_standard_application,
Expand Down

0 comments on commit c662270

Please sign in to comment.