import argparse import datetime import pytest from feed_ingestion.flows.itunes import exec # noqa:A004 class TestFlowExec: @pytest.fixture(autouse=True) def setup(self): self.flow_exec = exec.FlowExec() def test_prepare_parser_args(self): parser = self.flow_exec.prepare_parser() expected_args = { 'licensor', 'context_date', 'days', 'skip', 'reload', 'check_status', } args = {action.dest for action in parser._actions} assert expected_args.issubset(args), expected_args.difference(args) @pytest.mark.parametrize( 'case_name, command_line, env, expected_args_props', [ ( 'licensor by command line', '--licensor {LICENSOR}', {}, dict( licensor='{LICENSOR}', ) ), ( 'licensor by env', '', {'LICENSOR': '{LICENSOR}'}, dict( licensor='{LICENSOR}', ), ), ] ) @pytest.mark.parametrize( 'licensor', ['sme', 'theorchard', 'awal', 'ALL'] ) def test_parse_args( self, licensor, case_name, command_line, env, expected_args_props, clear_os_environ, monkeypatch): command_line = command_line.replace('{LICENSOR}', licensor) argv = command_line.split() for key, value in env.items(): if value == '{LICENSOR}': value = licensor monkeypatch.setenv(key, value) parser = self.flow_exec.prepare_parser() args = parser.parse_args(argv) for key, value in expected_args_props.items(): if value == '{LICENSOR}': value = licensor assert getattr(args, key) == value @pytest.mark.parametrize( 'case_name, command_line, env, expected_exception', [ ( 'wrong licensor by command line', '--licensor not_sme', {}, SystemExit ), ( 'wrong licensor by ENV', '', {'LICENSOR': 'not_sme'}, SystemExit ), ( 'no licensor', '', {}, SystemExit ), ] ) def test_parse_args_negative( self, case_name, command_line, env, expected_exception, clear_os_environ, monkeypatch): argv = command_line.split() for key, value in env.items(): monkeypatch.setenv(key, value) with pytest.raises(expected_exception): parser = self.flow_exec.prepare_parser() parser.parse_args(argv) @pytest.mark.parametrize( 'case_name, args, expected_contexts', [ ( 'one licensor', argparse.Namespace( licensor='sme' ), [ {'licensor': 'sme'}, ] ), ( 'ALL licensors', argparse.Namespace( licensor='ALL' ), [ {'licensor': 'theorchard'}, {'licensor': 'sme'}, {'licensor': 'awal'} ] ), ] ) def test_generate_contexts(self, case_name, args, expected_contexts): self.flow_exec.args = args assert list(self.flow_exec.generate_contexts()) == expected_contexts @pytest.mark.parametrize( 'licensor', exec.config.licensors ) def test_is_context_completed(self, licensor, mocker): context = {'licensor': licensor} parent_call_mock = mocker.patch.object( exec.exec_utils.CheckStatusMixin, 'is_context_completed') self.flow_exec.args = argparse.Namespace() self.flow_exec.is_context_completed( context=context, date=datetime.date(2024, 11, 1)) expected = 'POPULATED_RAW_TABLE' \ if licensor == 'altafonte' \ else 'INGESTED' assert self.flow_exec.args == argparse.Namespace( expected_status=expected) assert parent_call_mock.called