|
1 | 1 | import pytest |
| 2 | +from copy import deepcopy |
| 3 | +from datetime import datetime |
| 4 | +from unittest import mock |
2 | 5 |
|
3 | 6 | from caching.private_api.crawler.request_payload_builder import RequestPayloadBuilder |
4 | 7 |
|
@@ -367,3 +370,133 @@ def test_build_plot_data(self): |
367 | 370 | assert plot_data["label"] == plot_value["label"] |
368 | 371 | assert plot_data["line_colour"] == plot_value["line_colour"] |
369 | 372 | 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