def test_print(self, mocked_logger): mocked_logger.info = mock.MagicMock() # Default config file exists; expect it to be loaded run(command="print", period=self.period, config=None) mocked_logger.info.assert_called_once_with("") mocked_logger.info.reset_mock() # Remove default config file os.remove(TEST_CONFIG_FILEPATH) # No config is loaded at all run(command="print", period=1900, config=None) mocked_logger.info.assert_called_once_with("")
def test_print(self, mocked_logger): mocked_logger.info = mock.MagicMock() # Default config file exists; expect it to be loaded run(command="print", period=self.period, config=None) mocked_logger.info.assert_called_once_with("") mocked_logger.info.reset_mock() # Remove default config file os.remove(TEST_CONFIG_FILEPATH) # No config is loaded at all run(command="print", period=1900, config=None) mocked_logger.info.assert_called_once_with("") # The custom config modified the global state which affects other # tests... CategoryEntry.DEFAULT_NAME = "unspecified"
def cli_run(self, command_line, log_method="info", format_args=()): """Wrapper around cli.run() function. Adds convenient command line options (period and config filepath). Executes the actual run() function while patching the module logger info and error methods to catch their call arguments. 'command_line' is a string of the form that financeager is called from the command line with. 'format_args' are optional objects that are formatted into the command line string. Must be passed as tuple if more than one. If information about an added/update/removed/copied element was to be logged, the corresponding ID is matched from the log call arguments to the specified 'log_method' and returned. Otherwise the raw log call is returned. """ if not isinstance(format_args, tuple): format_args = (format_args, ) args = command_line.format(*format_args).split() command = args[0] # Exclude option from subcommand parsers that would be confused if command not in ["copy", "list"]: args.extend(["--period", self.period]) args.extend(["--config", TEST_CONFIG_FILEPATH]) with mock.patch("financeager.cli.logger") as mocked_logger: # Mock relevant methods mocked_logger.info = mock.MagicMock() mocked_logger.error = mock.MagicMock() exit_code = run(**_parse_command(args)) # Record for optional detailed analysis in test methods self.log_call_args_list = {} for method in ["info", "error"]: self.log_call_args_list[method] = \ getattr(mocked_logger, method).call_args_list # Get first of the args of the first call of specified log method printed_content = self.log_call_args_list[log_method][0][0][0] # Verify exit code after assigning log_call_args_list member self.assertEqual(exit_code, SUCCESS if log_method == "info" else FAILURE) if command in ["add", "update", "rm", "copy"] and\ isinstance(printed_content, str): m = re.match(self.eid_pattern, printed_content) self.assertIsNotNone(m) return int(m.group(2)) # Convert Exceptions to string return str(printed_content)
def test_print(self, mocked_logger): mocked_logger.info = mock.MagicMock() formatting_options = dict(stacked_layout=False, entry_sort="name", category_sort="value") # Default config file exists; expect it to be loaded run(command="list", config=None, **formatting_options) mocked_logger.info.assert_called_once_with("") mocked_logger.info.reset_mock() # Remove default config file os.remove(TEST_CONFIG_FILEPATH) # No config is loaded at all run(command="list", config=None, **formatting_options) mocked_logger.info.assert_called_once_with("") # The custom config modified the global state which affects other # tests... CategoryEntry.DEFAULT_NAME = "unspecified"
def cli_run(self, command_line, log_method="info", format_args=()): """Wrapper around cli.run() function. Adds convenient command line options (pocket and config filepath). Executes the actual run() function while patching the module logger info and error methods to catch their call arguments. 'command_line' is a string of the form that financeager is called from the command line with. 'format_args' are optional objects that are formatted into the command line string. Must be passed as tuple if more than one. If information about an added/update/removed/copied element was to be logged, the corresponding ID is matched from the log call arguments to the specified 'log_method' and returned. Otherwise the raw log call is returned. """ self.info.reset_mock() self.error.reset_mock() if not isinstance(format_args, tuple): format_args = (format_args, ) args = command_line.format(*format_args).split() command = args[0] # Exclude option from subcommand parsers that would be confused if command not in ["copy", "pockets"]: args.extend(["--pocket", str(self.pocket)]) args.extend(["--config-filepath", TEST_CONFIG_FILEPATH]) sinks = clients.Client.Sinks(self.info, self.error) # Procedure similar to cli.main() params = cli._parse_command(args) plugins = [main.main()] configuration = config.Configuration(params.pop("config_filepath"), plugins=plugins) exit_code = cli.run(sinks=sinks, configuration=configuration, plugins=plugins, **params) # Get first of the args of the call of specified log method response = getattr(self, log_method).call_args[0][0] # Verify exit code self.assertEqual(exit_code, cli.SUCCESS if log_method == "info" else cli.FAILURE) # Immediately return str messages if isinstance(response, str): return response # Convert Exceptions to string if isinstance(response, Exception): return str(response) if command in ["add", "update", "remove", "copy"]: return response["id"] if command in ["get", "list", "pockets"] and log_method == "info": return cli._format_response( response, command, default_category=configuration.get_option( "FRONTEND", "default_category")) return response
name = row["Beguenstigter/Zahlungspflichtiger"] name = name.lower() except AttributeError: # last row (Abschluss/Entgeltabrechnung) contains no data continue value = row["Betrag"].replace(",", ".") date = dt.strptime(row["Buchungstag"], "%d.%m.%y") month_day = date.strftime(PERIOD_DATE_FORMAT) for word in trigger_words: if word in name: print("{} {}: {}".format(month_day, word, value)) run(command="add", name=word, value=value, date=month_day, period=date.year, category=categories.get(word, "groceries")) break else: entries_rest.append((month_day, name, value)) f.close() # show anything that was not added if entries_rest: print(80 * "=") for entry in entries_rest: print("{} {}: {}".format(*entry))