def test_decide_message_outputs(monkeypatch): def mock_compose_output_gmail_sender(creds): assert creds == 'sup' return 'test gmail sender' monkeypatch.setattr(shell, "compose_output_gmail_sender", mock_compose_output_gmail_sender) options = Options(args.get_parsed_args(['--stdout-email', '--skip-send'])) outputs = shell.decide_message_outputs(options=options, creds='sup') assert len(outputs) == 1, "should have a stdout sender" # pylint: disable=comparison-with-callable assert outputs[0] == shell.output_stdout_email options = Options(args.get_parsed_args(['--stdout-email'])) outputs = shell.decide_message_outputs(options=options, creds='sup') assert len(outputs) == 2, "should have both stdout and gmail senders" # pylint: disable=comparison-with-callable assert outputs[0] == shell.output_stdout_email assert outputs[1] == 'test gmail sender' options = Options(args.get_parsed_args([])) outputs = shell.decide_message_outputs(options=options, creds='sup') assert len(outputs) == 1, "should have a gmail sender" assert outputs[0] == 'test gmail sender' options = Options(args.get_parsed_args(['--skip-send'])) outputs = shell.decide_message_outputs(options=options, creds='sup') assert len(outputs) == 0, "should have zero senders"
def test_validate_creds_heuristic_for_when_not_used(monkeypatch): options = Options(args.get_parsed_args(['--stdin', '--skip-send'])) config = Config() monkeypatch.setattr(shell, 'needs_valid_creds', lambda _: False) new_config, creds = shell.validate_creds(options, config) assert new_config is config assert creds is None
def test_process(monkeypatch): mock_sheet = {'mock_sheet': {}} output_sheet_called = False def mock_input_source(options, config, creds): assert options.group == 'test' assert config is not None assert creds is None return lambda: [mock_sheet] def mock_output_sheet(options, config, markdown_outputs, message_outputs, sheet): nonlocal output_sheet_called output_sheet_called = True assert options.group == 'test' assert config is not None assert markdown_outputs is not None assert message_outputs is not None assert sheet == mock_sheet monkeypatch.setattr(shell, "decide_input_source", mock_input_source) monkeypatch.setattr(shell, "output_sheet", mock_output_sheet) monkeypatch.setattr(shell, "needs_valid_creds", lambda _: False) monkeypatch.setattr(shell, "load_config", lambda _: Config()) monkeypatch.setattr(shell, "compose_output_gmail_sender", lambda _: lambda _: None) opts = Options(args.get_parsed_args(['--test'])) shell.process(opts) assert output_sheet_called
def test_decide_input_from_stdin(): """Given the --stdin argument, Then the input source should be stdin.""" options = Options(args.get_parsed_args(['--stdin'])) config = Config() creds = None # pylint: disable=comparison-with-callable assert shell.decide_input_source(options, config, creds) == shell.input_sheet_from_stdin
def test_output_no_message_handlers(output_fixture): config = Config(keys={'a': 1, 'b': 2}) options = Options( args.get_parsed_args(['-d', '2019-10-17', '--all-keys', '--test'])) markdown_outputs = [output_fixture.stub_markdown_output] message_outputs = [] shell.output_sheet(options, config, markdown_outputs, message_outputs, output_fixture.sheet) assert output_fixture.markdown_called assert not output_fixture.message_called
def test_validate_creds(monkeypatch): options = Options(args.get_parsed_args(['--all-keys'])) config = Config() monkeypatch.setattr(config_module, 'find_config_file', lambda _: None) monkeypatch.setattr(Config, 'validate', lambda _: None) monkeypatch.setattr(Config, 'creds', 'sup') monkeypatch.setattr(auth, 'serialize', lambda _: 'sup') monkeypatch.setattr(Config, 'set_serialized_creds', lambda _self, _: _self) monkeypatch.setattr(Config, 'save_to_file', lambda _self, _: None) new_config, creds = shell.validate_creds(options, config) assert new_config is config assert creds == 'sup'
def test_google_sheets_fetcher_called_with_ids(monkeypatch): monkeypatch.setattr(fetcher, "values", lambda sheet_id, _: 'fetched_' + sheet_id) monkeypatch.setattr(api, "sheets", lambda creds: None) config = Config(keys={'a': '1', 'b': '2'}) options = Options(args.get_parsed_args(['--all-keys'])) sheets_fetcher = shell.compose_input_google_sheets(options=options, config=config, creds='sup') sheets = sheets_fetcher() assert set(sheets) == {'fetched_1', 'fetched_2'}
def test_decide_input_from_google(monkeypatch): """Given --stdin is not set, Then the input source should be Google Sheets.""" def mock_sheets_fetcher(): """fake Google Sheets fetcher""" def mock_input_google_sheets(options, config, creds): """internal function that generates a google sheets fetcher""" assert not options.stdin assert config is not None assert creds == 'uh... sure' return mock_sheets_fetcher monkeypatch.setattr(shell, "compose_input_google_sheets", mock_input_google_sheets) opts = Options(args.get_parsed_args([])) cfg = Config() # pylint: disable=comparison-with-callable assert shell.decide_input_source(opts, cfg, 'uh... sure') == mock_sheets_fetcher mock_sheets_fetcher() # 100% coverage
def test_decide_markdown_outputs(): options = Options(args.get_parsed_args(['--stdout-markdown'])) subject = shell.decide_markdown_outputs(options) assert subject == [shell.output_stdout_markdown]
def test_gets_selected_sheet_ids(): options = Options(args.get_parsed_args(['-k', 'a', 'b'])) config = Config(keys={'a': '1', 'b': '2', 'c': '3'}) assert shell.google_sheet_ids(options, config) == {'1', '2'}
def test_gets_all_sheet_ids(): options = Options(args.get_parsed_args(['--all-keys'])) config = Config(keys={'a': 1, 'b': 2}) assert shell.google_sheet_ids(options, config) == {1, 2}
def test_returns_cmdline_value_if_no_method_defined(stub): options = Options(stub(a='hello')) assert options.a == 'hello'
def test_log_level_returns_info_for_verbose_warn_by_default(stub): assert Options(stub(verbose=True)).log_level == logging.INFO assert Options(stub()).log_level == logging.WARNING
def test_get_date_returns_one_day_later_for_dryrun_group(stub): options = Options(stub(date='2018-04-05', dryrun=1)) assert options.send_date == '2018-04-06'
def test_send_date_is_the_same_as_date(stub): options = Options(stub(date='2018-04-05', active=1)) assert options.send_date == '2018-04-05'
def test_group_yields_whichever_group_is_passed(stub): assert Options(stub(active=1)).group == 'active' assert Options(stub(dryrun=1)).group == 'dryrun' assert Options(stub(test=1)).group == 'test' assert Options( stub()).group is None # Perhaps an exception is better here.