"""Unit tests for Ritmogestion tasks.""" import datetime import os from unittest.mock import call, MagicMock, patch import pytest from feed_ingestion.flows.ritmogestion import tasks @pytest.mark.parametrize( 'date, expected_week_number', [ ('2021-12-30', 52), ('2021-12-31', 1), ('2022-01-01', 1), ('2022-01-06', 1), ('2021-01-01', 1), ('2019-12-26', 52), ('2019-12-27', 1), ('2020-01-02', 1), ('2020-01-03', 2), ('2020-12-25', 53), ('2020-12-31', 53), ] ) def test_week_number(date, expected_week_number): """Test week number.""" date_obj = datetime.datetime.strptime(date, '%Y-%m-%d') assert tasks.week_number(date_obj) == expected_week_number @patch('feed_ingestion.flows.ritmogestion.tasks.garcon_feed_status') @pytest.mark.parametrize( 'date, expected', [ [ '2022-05-11', {'stop': True, 'message': 'Weekday for 2022-05-11 00:00:00 should be Friday'} ], [ '2022-05-06', {'date': '2022-05-06', 'drop_file_name': 'ritmogestion_topstreaming_20220506.csv', 'fixed_file_name': 'ritmogestion_topstreaming_20220506_fixed.csv', 'fact_analytics_error_table': 'fact_analytics_error', 'fact_analytics_table': 'fact_analytics', 'feed_name': 'ritmogestion', 's3_archive_path': 'Ritmogestion/archives/2022-05-06/', 's3_path': 's3://dev-cucumbers/Ritmogestion/archives/2022-05-06/', 's3_full_path_fixed': 's3://dev-cucumbers/Ritmogestion/archives/2022-05-06/' 'ritmogestion_topstreaming_20220506_fixed.csv', 'secrets_path': 'swf-ritmogestion', 'staging_raw_table': 'staging_raw_ritmogestion', 'week_number': 19, 'year': 2022} ], ] ) def test_bootstrap(garcon_feed_status_mock, date, expected): """Test bootstrap.""" result = tasks.bootstrap(MagicMock(), date) assert result == expected @patch('feed_ingestion.flows.ritmogestion.tasks.task_status') @patch('feed_ingestion.flows.ritmogestion.tasks.boto3') @patch('feed_ingestion.flows.ritmogestion.tasks.' '_download_file_from_http_server') def test_fetch_from_http(task_status_mock, boto3_mock, download_file_from_http_server_mock): """Test fetch_from_http.""" date = '2022-05-06' expected = { 'source_files_dict': { 'files': [ { 'file_name': 'ritmogestion_topstreaming_220506_fixed.csv', 'file_size': 123, 'found': True } ] } } boto3_mock.client().get_object().__getitem__.return_value = 123 result = tasks.fetch_from_http( activity=MagicMock(), feed_name='ritmogestion', date=date, s3_archive_path='Ritmogestion/archives/2022-05-06/', drop_file_name='ritmogestion_topstreaming_220506.csv', fixed_file_name='ritmogestion_topstreaming_220506_fixed.csv', year=2022, week_number=19, ) assert result == expected @patch('feed_ingestion.flows.ritmogestion.tasks.requests') @patch('builtins.open') def test_download_file_from_http_server(open_mock, requests_mock, monkeypatch): """Test download_file_from_http_server.""" monkeypatch.setattr(tasks.config, 'source_username', 'fake_user') monkeypatch.setattr(tasks.config, 'source_password', 'fake_password') response_mock = MagicMock(name='response') session_mock = MagicMock() requests_mock.session.return_value.__enter__.return_value = session_mock session_mock.get.return_value = response_mock expected = (response_mock, '/tmp/ritmogestion.csv') result = tasks._download_file_from_http_server(2022, 16, MagicMock()) assert result == expected open_mock.assert_called_with('/tmp/ritmogestion.csv', 'wb') session_mock.mock_calls = [ call.get('https://www.ritmogestion.es/'), call.post( 'https://www.ritmogestion.es/login', data={ 'login[username]': 'fake_user', 'login[password]': 'fake_password' }), call.post().raise_for_status(), call.post( 'https://www.ritmogestion.es/listas/filter/action', data={ 'listas_filter[tipo_lista][text]': 'V7', 'listas_filter[añodesde][text]': 2022, 'listas_filter[semanadesde][text]': 16, 'listas_filter[añohasta][text]': 2022, 'listas_filter[semanahasta][text]': 16, 'listas_filter[semanaactualdesde][text]': '', 'listas_filter[semanaactualhasta][text]': '', 'listas_filter[artista][text]': '', 'listas_filter[titulo][text]': '', 'listas_filter[sello][text]': ''} ), call.post().raise_for_status(), call.get('https://www.ritmogestion.es/listas.csv', stream=True) ] def test_fix_csv(): """Test fix_csv.""" line = '"932";""BESM; MALMÖ 040"";""1;2"";"1,04";"97.047";"1.400.909"' result = tasks.fix_csv_row(line) assert result == '"932";"BESM; MALMÖ 040";"1;2";"1.04";"97047";"1400909"' def test_fix_csv_row(tmp_path): """Test fix_csv_row.""" dirname = os.path.dirname(__file__) sample_csv = 'sample_source.csv' sample_path = os.path.join(dirname, sample_csv) result_csv = 'sample_fixed.csv' result_path = os.path.join(tmp_path, result_csv) tasks.fix_csv( source_filename=sample_path, target_filename=result_path, ) expected_csv = 'sample_fixed_expected.csv' expected_path = os.path.join(dirname, expected_csv) with open(expected_path, encoding='ISO-8859-1') as expected_file, \ open(result_path, encoding='ISO-8859-1') as result_file: assert result_file.read() == expected_file.read() os.remove(result_path)