Skip to content

Commit 308b461

Browse files
committed
refactor: address PR review feedback
chore: cleanup chore: cleanup
1 parent 8bb2cdf commit 308b461

13 files changed

Lines changed: 288 additions & 555 deletions

File tree

bc_obps/common/migrations/0131_update_dashboard_data.py

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.db import migrations
2+
3+
from common.utils import reset_dashboard_data
4+
5+
6+
def reset_dashboard_data_fn(_, schema_editor):
7+
reset_dashboard_data()
8+
9+
10+
class Migration(migrations.Migration):
11+
dependencies = [
12+
("common", "0131_V5_15_1"),
13+
]
14+
15+
operations = [
16+
migrations.RunPython(
17+
code=reset_dashboard_data_fn,
18+
),
19+
]

bc_obps/registration/tests/endpoints/_operations/test_reportable.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class TestOperationsPreviousReportableEndpoint(CommonTestSetup):
99
@patch("service.operation_service.OperationService.list_previous_reportable_operations")
10-
def test_list_previous_reportable_operations(
10+
def test_calls_and_returns_the_service_method_output(
1111
self,
1212
mock_list_previous_reportable_operations,
1313
):
@@ -40,11 +40,15 @@ def test_list_previous_reportable_operations(
4040

4141
assert response.status_code == 200
4242
assert len(response.json()) == 1
43-
assert response.json()[0].get("operation_name") == operation.name
44-
assert response.json()[0].get("operation_id") == str(operation.id)
45-
assert response.json()[0].get("reporting_year") == 2023
46-
assert response.json()[0].get("registration_purposes") == [
47-
Operation.Purposes.REPORTING_OPERATION,
43+
assert response.json() == [
44+
{
45+
"operation_id": str(operation.id),
46+
"operation_name": operation.name,
47+
"reporting_year": 2023,
48+
"registration_purposes": [
49+
str(Operation.Purposes.REPORTING_OPERATION),
50+
],
51+
}
4852
]
4953

5054
mock_list_previous_reportable_operations.assert_called_once()

bc_obps/reporting/tests/api/test_reports.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,6 @@ def test_create_report_for_reporting_year(
258258

259259
# Verify that the selected registration purpose was passed to the service
260260
_, kwargs = mock_create_report_for_reporting_year.call_args
261+
assert kwargs["data"].operation_id == operation.id
262+
assert kwargs["data"].reporting_year == 2023
261263
assert kwargs["data"].registration_purpose == Operation.Purposes.REPORTING_OPERATION

bc_obps/service/operation_designated_operator_timeline_service.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,21 @@ def get_operation_designated_operator_for_reporting_year(
6767
def get_operation_designated_operators_for_reporting_years(
6868
cls,
6969
operation_ids: set[UUID],
70-
reporting_years: set[int],
70+
min_year: int,
71+
max_year: int,
7172
) -> dict[tuple[UUID, int], OperationDesignatedOperatorTimelinePlus]:
7273
"""
73-
Bulk version of get_operation_designated_operator_for_reporting_year
74+
Bulk version of get_operation_designated_operator_for_reporting_year.
7475
75-
It is intended for scenarios where many operation/reporting year
76-
combinations need to be evaluated, such as determining the list of reportable operations
76+
Returns designated operator timelines for every operation/year combination
77+
between min_year and max_year, inclusive.
7778
78-
Returns a lookup keyed by (operation_id, reporting_year)
79+
Returns a lookup keyed by (operation_id, reporting_year).
7980
"""
80-
81-
if not operation_ids or not reporting_years:
81+
if not operation_ids:
8282
return {}
8383

84-
min_year = min(reporting_years)
85-
max_year = max(reporting_years)
84+
reporting_years = range(min_year, max_year + 1)
8685

8786
timelines = (
8887
OperationDesignatedOperatorTimeline.objects.select_related(
@@ -100,24 +99,24 @@ def get_operation_designated_operators_for_reporting_years(
10099
lookup: dict[tuple[UUID, int], OperationDesignatedOperatorTimelinePlus] = {}
101100

102101
for timeline in timelines:
103-
start_date = timeline.start_date
104-
end_date = timeline.end_date
105-
106-
if start_date is None:
102+
if timeline.start_date is None:
107103
continue
108104

105+
start_year = timeline.start_date.year
106+
end_year = timeline.end_date.year if timeline.end_date else None
107+
109108
for reporting_year in reporting_years:
110-
if start_date.year > reporting_year:
109+
if start_year > reporting_year:
111110
continue
112111

113-
if end_date is not None and end_date.year <= reporting_year:
112+
if end_year is not None and end_year <= reporting_year:
114113
continue
115114

116115
lookup[(timeline.operation_id, reporting_year)] = OperationDesignatedOperatorTimelinePlus(
117116
operation=timeline.operation,
118117
operator=timeline.operator,
119-
start_date=start_date,
120-
end_date=end_date,
118+
start_date=timeline.start_date,
119+
end_date=timeline.end_date,
121120
)
122121

123122
return lookup

bc_obps/service/operation_service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,8 @@ def list_previous_reportable_operations(
909909
designations_lookup = (
910910
OperationDesignatedOperatorTimelineService.get_operation_designated_operators_for_reporting_years(
911911
operation_ids=all_operation_ids,
912-
reporting_years=year_values,
912+
min_year=min(year_values),
913+
max_year=max(year_values),
913914
)
914915
)
915916

bc_obps/service/tests/test_operation_designated_operator_timeline_service.py

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import pytest
22
from django.utils import timezone
33
from model_bakery import baker
4-
from service.operation_designated_operator_timeline_service import OperationDesignatedOperatorTimelineService
4+
from service.operation_designated_operator_timeline_service import (
5+
OperationDesignatedOperatorTimelinePlus,
6+
OperationDesignatedOperatorTimelineService,
7+
)
58

69
pytestmark = pytest.mark.django_db
710

@@ -94,59 +97,67 @@ def test_get_operation_designated_operator_for_reporting_year():
9497
assert result_none is None
9598

9699
@staticmethod
97-
def test_get_operation_designated_operators_for_reporting_years():
98-
operation = baker.make_recipe('registration.tests.utils.operation')
99-
operator1 = baker.make_recipe('registration.tests.utils.operator')
100-
operator2 = baker.make_recipe('registration.tests.utils.operator')
100+
def test_get_operation_designated_operators_for_reporting_years_returns_matching_timelines():
101+
operation = baker.make_recipe("registration.tests.utils.operation")
102+
operator1 = baker.make_recipe("registration.tests.utils.operator")
103+
operator2 = baker.make_recipe("registration.tests.utils.operator")
101104

102105
timeline1 = baker.make_recipe(
103-
'registration.tests.utils.operation_designated_operator_timeline',
106+
"registration.tests.utils.operation_designated_operator_timeline",
104107
operation=operation,
105108
operator=operator1,
106-
start_date=timezone.datetime(2024, 6, 1).date(),
107-
end_date=timezone.datetime(2025, 5, 31).date(),
109+
start_date=timezone.make_aware(timezone.datetime(2024, 6, 1)),
110+
end_date=timezone.make_aware(timezone.datetime(2025, 5, 31)),
108111
)
109112
timeline2 = baker.make_recipe(
110-
'registration.tests.utils.operation_designated_operator_timeline',
113+
"registration.tests.utils.operation_designated_operator_timeline",
111114
operation=operation,
112115
operator=operator2,
113-
start_date=timezone.datetime(2025, 5, 31).date(),
116+
start_date=timezone.make_aware(timezone.datetime(2025, 5, 31)),
114117
end_date=None,
115118
)
116119

117120
result = OperationDesignatedOperatorTimelineService.get_operation_designated_operators_for_reporting_years(
118121
operation_ids={operation.id},
119-
reporting_years={2023, 2024, 2025},
122+
min_year=2023,
123+
max_year=2025,
120124
)
121125

126+
assert len(result) == 2
122127
assert (operation.id, 2023) not in result
123128

124-
result_2024 = result[(operation.id, 2024)]
125-
assert result_2024.operation == timeline1.operation
126-
assert result_2024.operator == timeline1.operator
127-
assert result_2024.start_date == timeline1.start_date
128-
assert result_2024.end_date == timeline1.end_date
129-
assert result_2024.has_been_transferred is True
130-
131-
result_2025 = result[(operation.id, 2025)]
132-
assert result_2025.operation == timeline2.operation
133-
assert result_2025.operator == timeline2.operator
134-
assert result_2025.start_date == timeline2.start_date
135-
assert result_2025.end_date == timeline2.end_date
136-
assert result_2025.has_been_transferred is False
137-
138-
empty_result = (
139-
OperationDesignatedOperatorTimelineService.get_operation_designated_operators_for_reporting_years(
140-
operation_ids=set(),
141-
reporting_years={2024, 2025},
142-
)
143-
)
144-
assert empty_result == {}
145-
146-
empty_result = (
147-
OperationDesignatedOperatorTimelineService.get_operation_designated_operators_for_reporting_years(
148-
operation_ids={operation.id},
149-
reporting_years=set(),
150-
)
151-
)
152-
assert empty_result == {}
129+
assert result[(operation.id, 2024)] == OperationDesignatedOperatorTimelinePlus(
130+
operation=timeline1.operation,
131+
operator=timeline1.operator,
132+
start_date=timeline1.start_date,
133+
end_date=timeline1.end_date,
134+
)
135+
136+
assert result[(operation.id, 2025)] == OperationDesignatedOperatorTimelinePlus(
137+
operation=timeline2.operation,
138+
operator=timeline2.operator,
139+
start_date=timeline2.start_date,
140+
end_date=timeline2.end_date,
141+
)
142+
143+
@staticmethod
144+
def test_get_operation_designated_operators_for_reporting_years_returns_empty_when_operation_ids_empty():
145+
result = OperationDesignatedOperatorTimelineService.get_operation_designated_operators_for_reporting_years(
146+
operation_ids=set(),
147+
min_year=2024,
148+
max_year=2025,
149+
)
150+
151+
assert result == {}
152+
153+
@staticmethod
154+
def test_get_operation_designated_operators_for_reporting_years_returns_empty_when_year_range_is_empty():
155+
operation = baker.make_recipe("registration.tests.utils.operation")
156+
157+
result = OperationDesignatedOperatorTimelineService.get_operation_designated_operators_for_reporting_years(
158+
operation_ids={operation.id},
159+
min_year=2025,
160+
max_year=2024,
161+
)
162+
163+
assert result == {}

0 commit comments

Comments
 (0)