Skip to content

Commit

Permalink
Merge pull request #5301 from uktrade/feature/ew-breakdowns-financial…
Browse files Browse the repository at this point in the history
…-year

Output financial year for Win breakdown dataset.
  • Loading branch information
elcct authored Mar 28, 2024
2 parents 95ab095 + 4caeb9e commit b990660
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
7 changes: 6 additions & 1 deletion datahub/dataset/export_wins/test/test_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import date, timedelta

import pytest
from dateutil.relativedelta import relativedelta

from django.urls import reverse

Expand All @@ -9,6 +10,7 @@
from datahub.core.test_utils import (
format_date_or_datetime,
)
from datahub.core.utils import get_financial_year
from datahub.dataset.core.test import BaseDatasetViewTest
from datahub.export_win.constants import EXPORT_WINS_LEGACY_ID_START_VALUE
from datahub.export_win.models import HVC
Expand Down Expand Up @@ -71,11 +73,14 @@ class TestExportWinsBreakdownDatasetView(BaseDatasetViewTest):
factory = BreakdownFactory

def _assert_breakdown_matches_result(self, breakdown, result):
financial_year = get_financial_year(
breakdown.win.created_on + relativedelta(years=breakdown.year - 1),
)
assert result == {
'created_on': format_date_or_datetime(breakdown.created_on),
'id': breakdown.legacy_id,
'win__id': str(breakdown.win.id),
'year': breakdown.year,
'year': financial_year,
'value': breakdown.value,
'breakdown_type': breakdown.type.name,
}
Expand Down
27 changes: 24 additions & 3 deletions datahub/dataset/export_wins/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from django.contrib.postgres.expressions import ArraySubquery
from django.db.models import Count, F, Max
from django.db.models import Case, Count, ExpressionWrapper, F, Max, When
from django.db.models import IntegerField, OuterRef, Value
from django.db.models.functions import Cast, Concat
from django.db.models.functions import (
Cast,
Concat,
ExtractMonth,
ExtractYear,
)

from datahub.dataset.core.views import BaseDatasetView, BaseFilterDatasetView
from datahub.dataset.export_wins.pagination import HVCDatasetViewCursorPagination
Expand Down Expand Up @@ -53,15 +58,31 @@ class ExportWinsBreakdownsDatasetView(BaseDatasetView):
def get_dataset(self):
return (
Breakdown.objects.select_related('win, breakdown_type')
.annotate(
created_year=ExtractYear('win__created_on'),
created_month=ExtractMonth('win__created_on'),
provisional_financial_year=Case(
When(created_month__gte=4, then=F('created_year')),
default=ExpressionWrapper(
F('created_year') - 1,
output_field=IntegerField(),
),
output_field=IntegerField(),
),
financial_year=ExpressionWrapper(
F('provisional_financial_year') + (F('year') - 1),
output_field=IntegerField(),
),
)
.values(
'created_on',
'win__id',
'year',
'value',
breakdown_type=F('type__name'),
)
.annotate(
id=F('legacy_id'),
year=F('financial_year'),
)
)

Expand Down
3 changes: 2 additions & 1 deletion datahub/export_win/test/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ class BreakdownFactory(factory.django.DjangoModelFactory):
"""Breakdown factory."""

win = factory.SubFactory(WinFactory)
year = factory.fuzzy.FuzzyInteger(2022, 2050, 1)
# currently we store relative year to Win created date
year = factory.fuzzy.FuzzyInteger(1, 5, 1)
value = factory.fuzzy.FuzzyInteger(1000, 100000, 10)

class Meta:
Expand Down

0 comments on commit b990660

Please sign in to comment.