|
1 |
| -import datetime |
2 | 1 | import json
|
3 | 2 | import re
|
| 3 | +from datetime import date, datetime |
4 | 4 | from itertools import cycle, islice
|
5 | 5 | from unittest import mock
|
6 | 6 |
|
|
9 | 9 | import sqlalchemy
|
10 | 10 | from django.core import management
|
11 | 11 | from django.test import override_settings
|
| 12 | +from freezegun import freeze_time |
12 | 13 | from import_export import results
|
13 | 14 | from sqlalchemy import TIMESTAMP, Column, MetaData, String, Table
|
14 | 15 |
|
@@ -301,10 +302,10 @@ def metadata_last_release_raw_data():
|
301 | 302 | return {
|
302 | 303 | 'table_name': 'trade__uk_goods_nsa trade__uk_services_nsa trade__uk_totals_sa xxx'.split(),
|
303 | 304 | 'last_release': [
|
304 |
| - datetime.date(2018, 12, 30), |
305 |
| - datetime.date(2019, 12, 30), |
306 |
| - datetime.date(2020, 12, 30), |
307 |
| - datetime.date(2021, 12, 30), |
| 305 | + date(2018, 12, 30), |
| 306 | + date(2019, 12, 30), |
| 307 | + date(2020, 12, 30), |
| 308 | + date(2021, 12, 30), |
308 | 309 | ],
|
309 | 310 | }
|
310 | 311 |
|
@@ -423,25 +424,34 @@ def test_import_metadata_source_data_filter_tables():
|
423 | 424 |
|
424 | 425 |
|
425 | 426 | @pytest.mark.django_db
|
426 |
| -@mock.patch('dataservices.management.commands.helpers.MarketGuidesDataIngestionCommand.should_ingestion_run') |
| 427 | +@pytest.mark.parametrize( |
| 428 | + 'env, review_requested_x_times', |
| 429 | + [('dev', 0), ('staging', 3), ('uat', 0), ('production', 0)], |
| 430 | +) |
427 | 431 | @mock.patch('dataservices.management.commands.import_market_guides_data.call_command')
|
428 |
| -def test_import_market_guides_data(mock_call_command, mock_should_run): |
429 |
| - command_list = [ |
430 |
| - 'import_uk_total_trade_data', |
431 |
| - 'import_uk_trade_in_goods_data', |
432 |
| - 'import_uk_trade_in_services_data', |
433 |
| - ] |
434 |
| - mock_should_run.return_value = False |
435 |
| - management.call_command('import_market_guides_data', '--write') |
436 |
| - assert mock_call_command.call_count == 0 |
437 |
| - |
438 |
| - mock_should_run.return_value = True |
439 |
| - management.call_command('import_market_guides_data', '--write') |
440 |
| - assert mock_call_command.call_count == 6 |
441 |
| - |
442 |
| - for command in command_list: |
443 |
| - assert command in str(mock_call_command.call_args_list) |
444 |
| - assert 'write=True' in str(mock_call_command.call_args_list) |
| 432 | +@mock.patch('dataservices.management.commands.helpers.MarketGuidesDataIngestionCommand.should_ingestion_run') |
| 433 | +@mock.patch('dataservices.management.commands.import_market_guides_data.send_review_request_message') |
| 434 | +def test_import_market_guides_data( |
| 435 | + mock_send_review_request, mock_should_run, mock_call_command, env, review_requested_x_times |
| 436 | +): |
| 437 | + with override_settings(APP_ENVIRONMENT=env): |
| 438 | + command_list = [ |
| 439 | + 'import_uk_total_trade_data', |
| 440 | + 'import_uk_trade_in_goods_data', |
| 441 | + 'import_uk_trade_in_services_data', |
| 442 | + ] |
| 443 | + mock_should_run.return_value = False |
| 444 | + management.call_command('import_market_guides_data', '--write') |
| 445 | + assert mock_call_command.call_count == 0 |
| 446 | + |
| 447 | + mock_should_run.return_value = True |
| 448 | + management.call_command('import_market_guides_data', '--write') |
| 449 | + assert mock_call_command.call_count == 6 |
| 450 | + assert mock_send_review_request.call_count == review_requested_x_times |
| 451 | + |
| 452 | + for command in command_list: |
| 453 | + assert command in str(mock_call_command.call_args_list) |
| 454 | + assert 'write=True' in str(mock_call_command.call_args_list) |
445 | 455 |
|
446 | 456 |
|
447 | 457 | @pytest.mark.django_db
|
@@ -511,24 +521,32 @@ def workspace_data():
|
511 | 521 | 'trade__uk_goods_nsa',
|
512 | 522 | ],
|
513 | 523 | 'source_data_modified_utc': [
|
514 |
| - datetime.datetime(2023, 6, 10), |
| 524 | + datetime(2023, 9, 3), |
515 | 525 | ],
|
516 |
| - 'dataflow_swapped_tables_utc': [datetime.datetime(2023, 6, 10)], |
| 526 | + 'dataflow_swapped_tables_utc': [datetime(2023, 9, 3)], |
517 | 527 | }
|
518 | 528 |
|
519 | 529 |
|
520 | 530 | @pytest.mark.parametrize(
|
521 |
| - 'view_date, expected', |
522 |
| - [('2023-04-27T00:00:00', True), ('2023-06-10T00:00:00', False), ('2023-07-01T00:00:00', False)], |
| 531 | + 'env, view_date, swap_date, expected', |
| 532 | + [ |
| 533 | + ('staging', datetime(2023, 9, 12), datetime(2023, 9, 13), True), |
| 534 | + ('staging', datetime(2023, 9, 14), datetime(2023, 9, 13), False), |
| 535 | + ('production', datetime(2023, 9, 6), datetime(2023, 9, 6), False), |
| 536 | + ('production', datetime(2023, 9, 1), datetime(2023, 9, 2), True), |
| 537 | + ], |
523 | 538 | )
|
524 | 539 | @mock.patch('dataservices.management.commands.helpers.MarketGuidesDataIngestionCommand.get_view_metadata')
|
525 | 540 | @mock.patch('dataservices.management.commands.helpers.MarketGuidesDataIngestionCommand.get_dataflow_metadata')
|
526 |
| -def test_helper_should_ingest_run(dataflow_mock, view_mock, view_date, expected, workspace_data): |
527 |
| - m = MarketGuidesDataIngestionCommand() |
528 |
| - dataflow_mock.return_value = pd.DataFrame(workspace_data) |
529 |
| - view_mock.return_value = view_date |
530 |
| - actual = m.should_ingestion_run('UKMarketTrendsView', 'trade__uk_goods_nsa') |
531 |
| - assert actual == expected |
| 541 | +@freeze_time('2023-09-14T15:21:10') |
| 542 | +def test_helper_should_ingest_run(dataflow_mock, view_mock, env, view_date, swap_date, expected, workspace_data): |
| 543 | + with override_settings(APP_ENVIRONMENT=env): |
| 544 | + m = MarketGuidesDataIngestionCommand() |
| 545 | + workspace_data['dataflow_swapped_tables_utc'] = swap_date |
| 546 | + dataflow_mock.return_value = pd.DataFrame(workspace_data) |
| 547 | + view_mock.return_value = view_date.strftime('%Y-%m-%dT%H:%M:%S') |
| 548 | + actual = m.should_ingestion_run('UKMarketTrendsView', 'trade__uk_goods_nsa') |
| 549 | + assert actual == expected |
532 | 550 |
|
533 | 551 |
|
534 | 552 | @pytest.mark.django_db
|
@@ -576,22 +594,22 @@ def test_helper_get_dataflow_metadata():
|
576 | 594 | m.engine.execute(
|
577 | 595 | tbl.insert().values(
|
578 | 596 | table_name='trade__uk_goods_nsa',
|
579 |
| - source_data_modified_utc=datetime.date(2023, 1, 1), |
580 |
| - dataflow_swapped_tables_utc=datetime.date(2023, 1, 1), |
| 597 | + source_data_modified_utc=date(2023, 1, 1), |
| 598 | + dataflow_swapped_tables_utc=date(2023, 1, 1), |
581 | 599 | )
|
582 | 600 | )
|
583 | 601 | m.engine.execute(
|
584 | 602 | tbl.insert().values(
|
585 | 603 | table_name='trade__uk_goods_nsa',
|
586 |
| - source_data_modified_utc=datetime.date(2023, 4, 1), |
587 |
| - dataflow_swapped_tables_utc=datetime.date(2023, 4, 1), |
| 604 | + source_data_modified_utc=date(2023, 4, 1), |
| 605 | + dataflow_swapped_tables_utc=date(2023, 4, 1), |
588 | 606 | )
|
589 | 607 | )
|
590 | 608 | m.engine.execute(
|
591 | 609 | tbl.insert().values(
|
592 | 610 | table_name='trade__uk_services_nsa',
|
593 |
| - source_data_modified_utc=datetime.date(2023, 6, 1), |
594 |
| - dataflow_swapped_tables_utc=datetime.date(2023, 6, 1), |
| 611 | + source_data_modified_utc=date(2023, 6, 1), |
| 612 | + dataflow_swapped_tables_utc=date(2023, 6, 1), |
595 | 613 | )
|
596 | 614 | )
|
597 | 615 | result = m.get_dataflow_metadata('trade__uk_goods_nsa')
|
|
0 commit comments