import argparse import pytest from feed_ingestion.flows.amazon_digital_services import exec # noqa:A004 from tests.testing_utils import (parametrize_by_dicts, raises_optionally, ) 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) @parametrize_by_dicts( 'cli, env, expected, raises', [ dict( case='licensor by command line', cli='--licensor {LICENSOR}', env={}, expected=dict( licensor='{LICENSOR}', ) ), dict( case='licensor by env', cli='', env={'LICENSOR': '{LICENSOR}'}, expected=dict( licensor='{LICENSOR}', ), ), dict( case='missing licensor', cli='', env={}, raises=SystemExit ) ] ) @pytest.mark.parametrize( 'licensor, licensor_is_valid', [ ('sme', True), ('theorchard', True), ('ALL', True), ('not_sme', False), ] ) def test_prepare_parser( self, cli, env, expected, raises, licensor, licensor_is_valid, clear_os_environ, monkeypatch): cli = cli.replace('{LICENSOR}', licensor) argv = cli.split() for key, value in env.items(): if value == '{LICENSOR}': value = licensor monkeypatch.setenv(key, value) if not licensor_is_valid: raises = SystemExit with raises_optionally(raises): parser = self.flow_exec.prepare_parser() args = parser.parse_args(argv) for key, value in expected.items(): if value == '{LICENSOR}': value = licensor assert getattr(args, key) == value @pytest.mark.parametrize( 'case_name, args, expected_licensors', [ [ 'one licensor', argparse.Namespace( licensor='sme', ), ['sme'], ], [ 'ALL licensors', argparse.Namespace( licensor='ALL', ), ['theorchard', 'sme'] ], ] ) def test_generate_contexts( self, case_name, args, expected_licensors): self.flow_exec.args = args contexts = list(self.flow_exec.generate_contexts()) expected_contexts = [ {'licensor': licensor} for licensor in expected_licensors] assert contexts == expected_contexts