import os from datetime import datetime, timezone from unittest import mock import pytest from slz_config.configs import DSPConfigConfig from slz_config.services import PrefixesService from . import FIXTURES_PATH @mock.patch( 'slz_config.services.datetime', wraps=datetime, utcnow=lambda: datetime(2020, 4, 18, 1, tzinfo=timezone.utc) ) @pytest.mark.parametrize( 'input_dates, expected', [ ( [ datetime(2020, 4, 15, 1, tzinfo=timezone.utc), datetime(2020, 4, 17, 1, tzinfo=timezone.utc) ], ['2020-04-15', '2020-04-16', '2020-04-17'], ), ( [datetime(2020, 4, 15, 1, tzinfo=timezone.utc)], ['2020-04-15', '2020-04-16', '2020-04-17', '2020-04-18'], ), ( [datetime(2020, 4, 20, 1, tzinfo=timezone.utc)], [], ), ] ) def test_generate_dates(_, input_dates, expected): actual = PrefixesService.generate_dates(*input_dates) assert expected == list(actual) @mock.patch( 'slz_config.services.datetime', wraps=datetime, utcnow=lambda: datetime(2020, 5, 13, 1, tzinfo=timezone.utc) ) @pytest.mark.parametrize( 'payload, expected', [ ( { 'dsp_config': os.path.join(FIXTURES_PATH, 'dsp_config.json'), 'prefixes_payload': { 'dsp': 'sme_max', 'date': '2020-03-(12|13)', } }, [ 'sme_max/album_mapping/v1/report_date=2020-03-12/report_licensor=sme', 'sme_max/album_mapping/v1/report_date=2020-03-13/report_licensor=sme', ] ), ( { 'dsp_config': os.path.join(FIXTURES_PATH, 'dsp_config.json'), 'prefixes_payload': { 'dsp': 'amazonadsupported', 'licensor': 'theorchard', 'version': 'v1', 'date': '2020-05-(12|13)', } }, [ 'amazonadsupported/activity/v1/' 'report_date=2020-05-12/report_licensor=theorchard', 'amazonadsupported/activity/v1/' 'report_date=2020-05-13/report_licensor=theorchard', 'amazonadsupported/playlist/v1/' 'report_date=2020-05-12/report_licensor=theorchard', 'amazonadsupported/playlist/v1/' 'report_date=2020-05-13/report_licensor=theorchard', 'amazonadsupported/user/v1/' 'report_date=2020-05-12/report_licensor=theorchard', 'amazonadsupported/user/v1/' 'report_date=2020-05-13/report_licensor=theorchard', ] ), ( { 'dsp_config': os.path.join(FIXTURES_PATH, 'dsp_config.json'), 'prefixes_payload': { 'dsp': '(sme_max|youtubereporting)', 'date': '2020-01-1[0-2]', } }, [ 'sme_max/album_mapping/v1/report_date=2020-01-10/report_licensor=sme', 'sme_max/album_mapping/v1/report_date=2020-01-11/report_licensor=sme', 'sme_max/album_mapping/v1/report_date=2020-01-12/report_licensor=sme', 'youtubereporting/asset/a1/report_date=2020-01-12/report_licensor=sme', ] ), ( { 'dsp_config': os.path.join(FIXTURES_PATH, 'dsp_config.json'), 'prefixes_payload': { 'dsp': 'apple', 'date': '2016-05-15', } }, [ 'apple/content/v1_0/report_date=2016-05-15/report_licensor=sme', 'apple/streams/v1_0/report_date=2016-05-15/report_licensor=sme', ] ), ] ) def test_generate_prefixes(_, payload, expected): config = DSPConfigConfig(path=payload['dsp_config']).read().values() service = PrefixesService(mock.Mock(), config, payload['prefixes_payload']) actual = service.generate_prefixes() assert actual == expected