-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
from datetime import datetime | ||
|
||
import parsers | ||
import pytest | ||
|
||
from firefly_cli.transaction import Transaction | ||
|
||
|
||
class TestTransaction: | ||
parser = parsers.get_add_parser() | ||
|
||
@pytest.mark.parametrize( | ||
"fields, fields_missing", | ||
[ | ||
({"transaction": ["3,mocha,bank1,expense1"]}, []), | ||
({"transaction": ["3, mocha , bank1 , expense1 "]}, []), | ||
({"transaction": [", mocha,, expense1"]}, ["amount", "source_name"]), | ||
({"transaction": ["3,mocha, bank1"]}, ["destination_name"]), | ||
({"transaction": ["3,mocha, bank1,"]}, ["destination_name"]), | ||
( | ||
{"transaction": ["mocha,, expense1"]}, | ||
["description", "destination_name"], | ||
), | ||
], | ||
) | ||
def test_mandatory_fields_missing(self, fields, fields_missing): | ||
"""Asserts if the missing fields are the ones expected.""" | ||
|
||
transaction = Transaction(**fields) | ||
transaction.parse_inline_transaction_to_attributes() | ||
|
||
assert transaction.mandatory_fields_missing() == fields_missing | ||
|
||
@pytest.mark.parametrize( | ||
"fields, arg_str", | ||
[ | ||
( | ||
{ | ||
"transaction": [" 3 , mocha , bank1 , expense1 "], | ||
"date": datetime.strptime("1970-01-01", "%Y-%m-%d").astimezone(), | ||
"type": "withdrawal", | ||
}, | ||
" 3 , mocha , bank1 , expense1 --date 1970-01-01", | ||
) | ||
], | ||
) | ||
def test_constructor_from_argparse(self, fields, arg_str): | ||
"""Asserts if Transaction is the same if constructed from argparse or dict.""" | ||
|
||
transaction = Transaction(**fields) | ||
|
||
args = TestTransaction.parser.parse_args([a for a in arg_str.split(" ") if a]) | ||
|
||
transaction_argparse = Transaction.from_argparse(args) | ||
|
||
transaction.parse_inline_transaction_to_attributes() | ||
transaction_argparse.parse_inline_transaction_to_attributes() | ||
|
||
assert transaction == transaction_argparse | ||
|
||
@pytest.mark.parametrize( | ||
"arg_str, fields_expected", | ||
[ | ||
( | ||
"3, mocha, bank1, expense1 --date 1970-01-01 --source_name bank2", | ||
{ | ||
"transaction": [""], | ||
"amount": "3", | ||
"description": "mocha", | ||
"source_name": "bank2", | ||
"destination_name": "expense1", | ||
"date": datetime.strptime("1970-01-01", "%Y-%m-%d").astimezone(), | ||
"type": "withdrawal", | ||
}, | ||
), | ||
( | ||
"3, mocha, bank1 --date 1970-01-01 --destination_name expense3 --source_name bank2", | ||
{ | ||
"transaction": [""], | ||
"amount": "3", | ||
"description": "mocha", | ||
"source_name": "bank2", | ||
"destination_name": "expense3", | ||
"date": datetime.strptime("1970-01-01", "%Y-%m-%d").astimezone(), | ||
"type": "withdrawal", | ||
}, | ||
), | ||
( | ||
"3, mocha, bank1 --date 1970-01-01 --source_name bank2 --type deposit", | ||
{ | ||
"transaction": [""], | ||
"amount": "3", | ||
"description": "mocha", | ||
"source_name": "bank2", | ||
"date": datetime.strptime("1970-01-01", "%Y-%m-%d").astimezone(), | ||
"type": "deposit", | ||
}, | ||
), | ||
], | ||
) | ||
def test_parse_inline_transaction_to_attributes(self, arg_str, fields_expected): | ||
|
||
args = TestTransaction.parser.parse_args([a for a in arg_str.split(" ") if a]) | ||
|
||
transaction = Transaction.from_argparse(args) | ||
transaction.parse_inline_transaction_to_attributes() | ||
transaction.transaction = None | ||
|
||
transaction_expected = Transaction(**fields_expected) | ||
transaction_expected.parse_inline_transaction_to_attributes() | ||
transaction_expected.transaction = None | ||
|
||
assert transaction == transaction_expected |