Improve and test bad config file handling

This commit is contained in:
Gabriel Augendre 2020-02-29 13:35:51 +01:00
parent a32e568dde
commit b1e80994b4
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4
6 changed files with 78 additions and 16 deletions

View file

@ -59,20 +59,21 @@ def push_transactions(transactions, account):
try:
config.read(config_file)
except configparser.Error as e:
click.secho(f"Error while parsing config file: {str(e)}", fg="red", bold=True)
click.secho("Opening the file...")
click.pause()
click.edit(filename=config_file)
click.secho("Exiting...", fg="red", bold=True)
sys.exit(1)
section = config[account]
budget_id = section["budget"]
return handle_config_file_error(config_file, e)
try:
section = config[account]
budget_id = section["budget"]
token = section["token"]
account = section["account"]
except KeyError as e:
return handle_config_file_error(config_file, e)
url = f"{BASE_URL}/budgets/{budget_id}/transactions"
for transaction in transactions:
transaction["account_id"] = section["account"]
transaction["account_id"] = account
data = {"transactions": transactions}
token = section["token"]
headers = {"Authorization": f"Bearer {token}"}
res = requests.post(url, json=data, headers=headers)
@ -95,3 +96,12 @@ def push_transactions(transactions, account):
click.secho(
f"{len(duplicates)} transactions ignored (duplicates).", fg="yellow"
)
def handle_config_file_error(config_file, e):
click.secho(f"Error while parsing config file: {str(e)}", fg="red", bold=True)
click.secho("Opening the file...")
click.pause()
click.edit(filename=config_file)
click.secho("Exiting...", fg="red", bold=True)
sys.exit(1)

View file

@ -0,0 +1,9 @@
[DEFAULT]
token = <YOUR API TOKEN>
budget = <YOUR BUDGET ID>
[bpvf]
account = <YOUR BPVF ACCOUNT ID>
[ce]
account = <YOUR CE ACCOUNT ID>

View file

@ -0,0 +1,11 @@
[DEFAULT]
token = <YOUR API TOKEN>
budget = <YOUR BUDGET ID>
[bpvf]
account = <YOUR BPVF ACCOUNT ID>
[revolut]
[ce]
account = <YOUR CE ACCOUNT ID>

View file

@ -0,0 +1,11 @@
[DEFAULT]
token = <YOUR API TOKEN>
[bpvf]
account = <YOUR BPVF ACCOUNT ID>
[revolut]
account = <YOUR REVOLUT ACCOUNT ID>
[ce]
account = <YOUR CE ACCOUNT ID>

View file

@ -0,0 +1,11 @@
[DEFAULT]
budget = <YOUR BUDGET ID>
[bpvf]
account = <YOUR BPVF ACCOUNT ID>
[revolut]
account = <YOUR REVOLUT ACCOUNT ID>
[ce]
account = <YOUR CE ACCOUNT ID>

View file

@ -64,12 +64,22 @@ class ConfigTestCase(unittest.TestCase):
)
@mock.patch("ofx_processor.utils.ynab.DEFAULT_CONFIG_DIR", "tests/samples")
def test_broken_config_file(self, edit):
runner = CliRunner()
result = runner.invoke(self.cli, ["revolut", "tests/samples/revolut.csv"])
expected_filename = "tests/samples/config_broken_duplicate_key.ini"
self.assertIn("Error while parsing config file", result.output)
edit.assert_called_once_with(filename=expected_filename)
broken_files = [
"config_broken_duplicate_key.ini",
"config_broken_missing_account.ini",
"config_broken_missing_account_key.ini",
"config_broken_missing_budget.ini",
"config_broken_missing_token.ini",
]
for name in broken_files:
with mock.patch("ofx_processor.utils.ynab.DEFAULT_CONFIG_FILENAME", name):
runner = CliRunner()
result = runner.invoke(
self.cli, ["revolut", "tests/samples/revolut.csv"]
)
expected_filename = ynab.get_config_file_name()
self.assertIn("Error while parsing config file", result.output)
edit.assert_called_with(filename=expected_filename)
@mock.patch("ofx_processor.utils.ynab.DEFAULT_CONFIG_FILENAME", "file.ini")
@mock.patch("ofx_processor.utils.ynab.DEFAULT_CONFIG_DIR", "some/config/folder")