Skip to content

Commit 1a3ee80

Browse files
committed
CDD-3447 - Fix faioing tests and coverage
1 parent bcdbf46 commit 1a3ee80

7 files changed

Lines changed: 437 additions & 7 deletions

File tree

cms/dynamic_content/blocks_deconstruction.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,10 @@ def get_all_selected_topics_from_chart_blocks(
438438
"""
439439
topics = set()
440440
for block in chart_blocks:
441-
topic = (
442-
block["static_fields"]["topic"]
443-
if is_dual_category_chart_block(block)
444-
else block["value"]["topic"]
445-
)
446-
447-
topics.add(topic)
441+
if is_dual_category_chart_block(block):
442+
topics.add(block["static_fields"]["topic"])
443+
else:
444+
topics.update(plot["value"]["topic"] for plot in block["chart"])
448445

449446
return topics
450447

tests/conftest.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,60 @@ def example_chart_block() -> dict[str, str | list[dict]]:
439439
}
440440

441441

442+
@pytest.fixture
443+
def example_dual_category_chart_block() -> dict[str, str | list[dict]]:
444+
return {
445+
"title": "Lead headline rates by age and sex",
446+
"x_axis": "age",
447+
"x_axis_title": "Age groups",
448+
"primary_field_values": [
449+
"00-01",
450+
"01-04",
451+
"05-09",
452+
"05-11",
453+
"05-14",
454+
"10-14",
455+
],
456+
"y_axis": "metric",
457+
"y_axis_title": "Genders by age",
458+
"y_axis_minimum_value": 0,
459+
"y_axis_maximum_value": None,
460+
"chart_type": "stacked_bar",
461+
"static_fields": {
462+
"topic": "Lead",
463+
"metric": "lead_headline_ratesByAgeSex",
464+
"geography": "England",
465+
"geography_type": "Nation",
466+
"sex": "all",
467+
"age": "all",
468+
"stratum": "default",
469+
"date_from": "2020-05-21",
470+
"date_to": "2026-05-21",
471+
},
472+
"secondary_category": "sex",
473+
"segments": [
474+
{
475+
"type": "segment",
476+
"value": {
477+
"secondary_field_value": "f",
478+
"colour": "COLOUR_1_DARK_BLUE",
479+
"label": "",
480+
},
481+
"id": "b0ead98b-4102-48f6-b94e-ff7bcffe1dc4",
482+
},
483+
{
484+
"type": "segment",
485+
"value": {
486+
"secondary_field_value": "m",
487+
"colour": "COLOUR_1_DARK_BLUE",
488+
"label": "Males",
489+
},
490+
"id": "c1fbe09c-5213-59f7-c05f-ff8cdffe2ed5",
491+
},
492+
],
493+
}
494+
495+
442496
@pytest.fixture
443497
def example_headline_chart_block() -> dict[str, str | list[dict]]:
444498
return {

tests/unit/caching/private_api/crawler/dynamic_block_crawler/test_process_individual_blocks.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,115 @@ def test_process_download_for_chart_block_delegates_calls_successfully(
317317
data=expected_data
318318
)
319319

320+
def test_process_dual_category_chart_block_hits_dual_category_tables_endpoint(
321+
self,
322+
example_dual_category_chart_block: dict[str, str | list[dict]],
323+
dynamic_content_block_crawler_with_mocked_internal_api_client: DynamicContentBlockCrawler,
324+
):
325+
"""
326+
Given a dual category chart block
327+
When `process_chart_block()` is called
328+
from an instance of `DynamicContentBlockCrawler`
329+
Then the call is delegated to the `hit_dual_category_tables_endpoint()`
330+
on the `InternalAPIClient`
331+
"""
332+
# Given
333+
spy_internal_api_client: mock.Mock = (
334+
dynamic_content_block_crawler_with_mocked_internal_api_client._internal_api_client
335+
)
336+
request_payload_builder = (
337+
dynamic_content_block_crawler_with_mocked_internal_api_client._request_payload_builder
338+
)
339+
340+
# When
341+
dynamic_content_block_crawler_with_mocked_internal_api_client.process_chart_block(
342+
chart_block=example_dual_category_chart_block,
343+
)
344+
345+
# Then
346+
expected_tables_request_data = (
347+
request_payload_builder.build_dual_category_tables_request_data(
348+
chart_block=example_dual_category_chart_block
349+
)
350+
)
351+
spy_internal_api_client.hit_dual_category_tables_endpoint.assert_called_once_with(
352+
data=expected_tables_request_data
353+
)
354+
355+
def test_process_dual_category_chart_block_hits_dual_category_charts_endpoint(
356+
self,
357+
example_dual_category_chart_block: dict[str, str | list[dict]],
358+
dynamic_content_block_crawler_with_mocked_internal_api_client: DynamicContentBlockCrawler,
359+
):
360+
"""
361+
Given a dual category chart block
362+
When `process_chart_block()` is called
363+
from an instance of `DynamicContentBlockCrawler`
364+
Then the call is delegated to the `hit_dual_category_charts_endpoint()`
365+
on the `InternalAPIClient`
366+
"""
367+
# Given
368+
spy_internal_api_client: mock.Mock = (
369+
dynamic_content_block_crawler_with_mocked_internal_api_client._internal_api_client
370+
)
371+
request_payload_builder = (
372+
dynamic_content_block_crawler_with_mocked_internal_api_client._request_payload_builder
373+
)
374+
375+
# When
376+
dynamic_content_block_crawler_with_mocked_internal_api_client.process_chart_block(
377+
chart_block=example_dual_category_chart_block,
378+
)
379+
380+
# Then
381+
expected_calls = [
382+
mock.call(
383+
data=request_payload_builder.build_dual_category_chart_request_data(
384+
chart_block=example_dual_category_chart_block,
385+
chart_is_double_width=chart_is_double_width,
386+
)
387+
)
388+
for chart_is_double_width in (True, False)
389+
]
390+
spy_internal_api_client.hit_dual_category_charts_endpoint.assert_has_calls(
391+
calls=expected_calls, any_order=True
392+
)
393+
394+
def test_process_download_for_dual_category_chart_block_delegates_calls_successfully(
395+
self,
396+
example_dual_category_chart_block: dict[str, str | list[dict]],
397+
dynamic_content_block_crawler_with_mocked_internal_api_client: DynamicContentBlockCrawler,
398+
):
399+
"""
400+
Given a dual category chart block
401+
When the `process_download_for_chart_block()` is called
402+
from an instance of the `DynamicContentBlockCrawler`
403+
Then the call is delegated to the `hit_dual_category_downloads_endpoint()`
404+
on the `InternalAPIClient`
405+
"""
406+
# Given
407+
file_format = "json"
408+
spy_internal_api_client: mock.Mock = (
409+
dynamic_content_block_crawler_with_mocked_internal_api_client._internal_api_client
410+
)
411+
request_payload_builder = RequestPayloadBuilder()
412+
413+
# When
414+
dynamic_content_block_crawler_with_mocked_internal_api_client.process_download_for_chart_block(
415+
chart_block=example_dual_category_chart_block, file_format=file_format
416+
)
417+
418+
# Then
419+
expected_data: dict = (
420+
request_payload_builder.build_dual_category_downloads_request_data(
421+
chart_block=example_dual_category_chart_block,
422+
file_format=file_format,
423+
)
424+
)
425+
spy_internal_api_client.hit_dual_category_downloads_endpoint.assert_called_once_with(
426+
data=expected_data
427+
)
428+
320429
def test_process_global_filter(
321430
self,
322431
example_global_filter: dict,

tests/unit/caching/private_api/crawler/test_request_payload_builder.py

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import pytest
2+
from copy import deepcopy
3+
from datetime import datetime
4+
from unittest import mock
25

36
from caching.private_api.crawler.request_payload_builder import RequestPayloadBuilder
47

@@ -367,3 +370,133 @@ def test_build_plot_data(self):
367370
assert plot_data["label"] == plot_value["label"]
368371
assert plot_data["line_colour"] == plot_value["line_colour"]
369372
assert plot_data["line_type"] == plot_value["line_type"]
373+
374+
@pytest.mark.parametrize(
375+
"chart_is_double_width, expected_chart_width", ([(True, 1100), (False, 515)])
376+
)
377+
def test_build_dual_category_chart_request_data(
378+
self,
379+
chart_is_double_width: bool,
380+
expected_chart_width: int,
381+
example_dual_category_chart_block: dict[str, str | list[dict]],
382+
):
383+
"""
384+
Given a dual category chart block
385+
When `build_dual_category_chart_request_data()` is called
386+
from an instance of `RequestPayloadBuilder`
387+
Then the correct dict is returned
388+
"""
389+
# Given
390+
chart_block_data = example_dual_category_chart_block
391+
request_payload_builder = RequestPayloadBuilder()
392+
393+
# When
394+
chart_request_data = (
395+
request_payload_builder.build_dual_category_chart_request_data(
396+
chart_block=chart_block_data,
397+
chart_is_double_width=chart_is_double_width,
398+
)
399+
)
400+
401+
# Then
402+
base_request_data = request_payload_builder.base_dual_category_request_data(
403+
chart_block_data
404+
)
405+
expected_chart_request_data = {
406+
**base_request_data,
407+
"file_format": "svg",
408+
"chart_height": 260,
409+
"chart_width": expected_chart_width,
410+
"x_axis_title": chart_block_data.get("x_axis_title", ""),
411+
"y_axis_title": chart_block_data.get("y_axis_title", ""),
412+
"y_axis_minimum_value": chart_block_data.get("y_axis_minimum_value", None),
413+
"y_axis_maximum_value": chart_block_data.get("y_axis_maximum_value", None),
414+
"chart_type": chart_block_data["chart_type"],
415+
"legend_title": chart_block_data["title"],
416+
}
417+
assert chart_request_data == expected_chart_request_data
418+
419+
def test_build_dual_category_tables_request_data(
420+
self,
421+
example_dual_category_chart_block: dict[str, str | list[dict]],
422+
):
423+
"""
424+
Given a dual category chart block
425+
When `build_dual_category_tables_request_data()` is called
426+
from an instance of `RequestPayloadBuilder`
427+
Then the correct dict is returned
428+
"""
429+
# Given
430+
chart_block_data = example_dual_category_chart_block
431+
request_payload_builder = RequestPayloadBuilder()
432+
433+
# When
434+
tables_request_data = (
435+
request_payload_builder.build_dual_category_tables_request_data(
436+
chart_block=chart_block_data
437+
)
438+
)
439+
440+
# Then
441+
expected_tables_request_data = (
442+
request_payload_builder.base_dual_category_request_data(chart_block_data)
443+
)
444+
assert tables_request_data == expected_tables_request_data
445+
446+
def test_build_dual_category_downloads_request_data(
447+
self,
448+
example_dual_category_chart_block: dict[str, str | list[dict]],
449+
):
450+
"""
451+
Given a dual category chart block
452+
When `build_dual_category_downloads_request_data()` is called
453+
from an instance of `RequestPayloadBuilder`
454+
Then the correct dict is returned
455+
"""
456+
# Given
457+
chart_block_data = example_dual_category_chart_block
458+
file_format = "csv"
459+
request_payload_builder = RequestPayloadBuilder()
460+
461+
# When
462+
downloads_request_data = (
463+
request_payload_builder.build_dual_category_downloads_request_data(
464+
chart_block=chart_block_data, file_format=file_format
465+
)
466+
)
467+
468+
# Then
469+
base_request_data = request_payload_builder.base_dual_category_request_data(
470+
chart_block_data
471+
)
472+
expected_downloads_request_data = {
473+
**base_request_data,
474+
"file_format": file_format,
475+
}
476+
assert downloads_request_data == expected_downloads_request_data
477+
478+
def test_base_dual_category_request_data_defaults_date_to_when_none(
479+
self,
480+
example_dual_category_chart_block: dict[str, str | list[dict]],
481+
):
482+
"""
483+
Given a dual category chart block with no `date_to` in static fields
484+
When `base_dual_category_request_data()` is called from `RequestPayloadBuilder`
485+
Then today's date is applied to `date_to`
486+
"""
487+
# Given
488+
chart_block_data = deepcopy(example_dual_category_chart_block)
489+
chart_block_data["static_fields"] = chart_block_data["static_fields"].copy()
490+
chart_block_data["static_fields"]["date_to"] = None
491+
492+
# When
493+
with mock.patch(
494+
"caching.private_api.crawler.request_payload_builder.datetime"
495+
) as mock_datetime:
496+
mock_datetime.now.return_value = datetime(2026, 6, 26)
497+
request_data = RequestPayloadBuilder.base_dual_category_request_data(
498+
chart_block_data
499+
)
500+
501+
# Then
502+
assert request_data["static_fields"]["date_to"] == "2026-06-26"

0 commit comments

Comments
 (0)