Skip to content

Commit

Permalink
tests are apprently all passing
Browse files Browse the repository at this point in the history
  • Loading branch information
rienafairefr committed Dec 23, 2016
1 parent db197ab commit 7e9a33c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 62 deletions.
17 changes: 9 additions & 8 deletions pynYNAB/scripts/csvimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def getsubcategory(categoryname):
logger.debug('searching for subcategory %s' % categoryname)
return subcategories[categoryname]
except KeyError:
get_logger(args).debug('Couldn''t find this category: %s' % categoryname)
logger.debug('Couldn''t find this category: %s' % categoryname)
exit(-1)

entities_account_id = None
Expand All @@ -125,22 +125,23 @@ def getsubcategory(categoryname):

imported_date = datetime.now().date()

get_logger(args).debug('OK starting the import from %s ' % os.path.abspath(args.csvfile))
logger.debug('OK starting the import from %s ' % os.path.abspath(args.csvfile))
with open(args.csvfile, 'r') as inputfile:
header = []
for i in range(0,nheaders):
for i in range(0, nheaders):
header.append(inputfile.readline())
for row in csv.reader(inputfile):
if sys.version[0] == '2':
row = [cell.decode('utf-8') for cell in row]
if all(map(lambda x: x.strip() == '', row)):
continue
get_logger(args).debug('read line %s' % row)
logger.debug('read line %s' % row)
result = csvrow(*list(schema.convert_row(*row, fail_fast=True)))
if 'account' in schema.headers:
entities_account_id = getaccount(result.account).id
if entities_account_id is None:
get_logger(args).error('No account id, the account %s in the an account column was not recognized'%result.account)
logger.error(
'No account id, the account %s in the an account column was not recognized' % result.account)
exit(-1)
if 'inflow' in schema.headers and 'outflow' in schema.headers:
amount = result.inflow - result.outflow
Expand Down Expand Up @@ -172,11 +173,11 @@ def getsubcategory(categoryname):
memo=memo,
source="Imported"
)
if args.import_duplicates or (not client.budget.be_transactions.containsduplicate(transaction)):
get_logger(args).debug('Appending transaction %s ' % transaction.get_dict())
if args.import_duplicates or (not transaction in client.budget.be_transactions):
logger.debug('Appending transaction %s ' % transaction.get_dict())
transactions.append(transaction)
else:
get_logger(args).debug('Duplicate transaction found %s ' % transaction.get_dict())
logger.debug('Duplicate transaction found %s ' % transaction.get_dict())

client.add_transactions(transactions)

Expand Down
20 changes: 11 additions & 9 deletions pynYNAB/scripts/ofximport.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def ofximport_main():
test_common_args(args)
do_ofximport(args)

def do_ofximport(args, client = None):

def do_ofximport(args, client=None):
if client is None:
client = clientfromargs(args)

Expand All @@ -31,7 +32,7 @@ def do_ofximport(args, client = None):
stmts = response.statements

accounts = client.budget.be_accounts
accountvsnotes={account.note:account for account in accounts if account.note is not None}
accountvsnotes = {account.note: account for account in accounts if account.note is not None}

for stmt in stmts:
key = stmt.account.bankid + ' ' + stmt.account.branchid + ' ' + stmt.account.acctid
Expand All @@ -53,28 +54,28 @@ def do_ofximport(args, client = None):
else:
for note in accountvsnotes:
if key in note:
account=accountvsnotes[note]
account = accountvsnotes[note]

imported_date=datetime.now().date()
imported_date = datetime.now().date()

for ofx_transaction in stmt.transactions:
payee_name = ofx_transaction.name if ofx_transaction.payee is None else ofx_transaction.payee
try:
payee = next(p for p in client.budget.be_payees if p.name == payee_name)
except StopIteration:
payee=Payee(
payee = Payee(
name=payee_name
)
client.budget.be_payees.append(payee)
client.sync()

# use ftid so we don't import duplicates
if not any(ofx_transaction.fitid in transaction.memo for transaction in client.budget.be_transactions if
if not any(ofx_transaction.fitid in transaction.memo for transaction in
client.budget.be_transactions if
transaction.memo is not None and transaction.entities_account_id == account.id):

transaction = Transaction(
date=ofx_transaction.dtposted,
memo=ofx_transaction.memo + ' '+ofx_transaction.fitid,
memo=ofx_transaction.memo + ' ' + ofx_transaction.fitid,
imported_payee=payee_name,
entities_payee_id=payee.id,
imported_date=imported_date,
Expand All @@ -85,5 +86,6 @@ def do_ofximport(args, client = None):
)
client.add_transaction(transaction)


if __name__ == "__main__":
ofximport_main()
ofximport_main()
34 changes: 16 additions & 18 deletions scripts_tests/test_csvimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pynYNAB.schema.Entity import ComplexEncoder
from pynYNAB.schema.budget import Transaction
from pynYNAB.scripts.csvimport import do_csvimport
from test_live.common_Live import CommonLive, needs_account
from test_live.common import CommonLive, needs_account


class TestCsv(CommonLive):
Expand All @@ -31,7 +31,7 @@ def test_duplicate(self):
parser = configargparse.getArgumentParser('pynYNAB')
args = parser.parse_known_args()[0]
args.schema = 'example'
args.csvfile = os.path.join(gettempdir(),'data.csv')
args.csvfile = os.path.join(gettempdir(), 'data.csv')
args.accountname = None
args.import_duplicates = False
args.logginglevel = 'debug'
Expand All @@ -42,21 +42,21 @@ def test_duplicate(self):
with open(args.csvfile, mode='w') as f:
f.writelines(content)

transaction=self.getTr(datetime(year=2016, month=2, day=1).date(), 'Super Pants Inc.', -20, 'Buying pants', 'Credit')
transaction = self.getTr(datetime(year=2016, month=2, day=1).date(), 'Super Pants Inc.', -20, 'Buying pants',
'Credit')

for i in range(2):
do_csvimport(args)
self.reload()
identical=[tr2 for tr2 in self.client.budget.be_transactions if transaction._hash() == tr2._hash()]
print('Transactions with same hash: %s'%len(identical))
self.assertTrue(len(identical) == 1)
nduplicates = self.client.budget.be_transactions.count(transaction)
self.assertEqual(nduplicates, 1)

@needs_account('Cash')
def test_duplicateForced(self):
parser = configargparse.getArgumentParser('pynYNAB')
args = parser.parse_known_args()[0]
args.schema = 'example'
args.csvfile = os.path.join(gettempdir(),'data.csv')
args.csvfile = os.path.join(gettempdir(), 'data.csv')
args.accountname = None
args.import_duplicates = True
args.logginglevel = 'debug'
Expand All @@ -67,13 +67,15 @@ def test_duplicateForced(self):
with open(args.csvfile, mode='w') as f:
f.writelines(content)

transaction=self.getTr(datetime(year=2016, month=2, day=1).date(), 'Super Pants Inc.', -20, 'Buying pants', 'Cash')
transaction = self.getTr(datetime(year=2016, month=2, day=1).date(), 'Super Pants Inc.', -20, 'Buying pants',
'Cash')

do_csvimport(args)
self.reload()
do_csvimport(args)
self.reload()
self.assertTrue(len([tr2 for tr2 in self.client.budget.be_transactions if transaction._hash() == tr2._hash()]) == 2)
nduplicates = self.client.budget.be_transactions.count(transaction)
self.assertEqual(nduplicates, 2)

@needs_account('Cash')
@needs_account('Checking Account')
Expand All @@ -82,9 +84,9 @@ def test_import(self):
parser = configargparse.getArgumentParser('pynYNAB')
args = parser.parse_known_args()[0]
args.schema = 'example'
args.csvfile = os.path.join(gettempdir(),'data.csv')
args.csvfile = os.path.join(gettempdir(), 'data.csv')
args.accountname = None
args.import_duplicates=False
args.import_duplicates = False
args.logginglevel = 'debug'

content = """Date,Payee,Amount,Memo,Account
Expand All @@ -95,17 +97,13 @@ def test_import(self):
with open(args.csvfile, mode='w') as f:
f.writelines(content)

Transactions = [
transactions = [
self.getTr(datetime(year=2016, month=2, day=1).date(), 'Super Pants Inc.', -20, 'Buying pants', 'Cash'),
self.getTr(datetime(year=2016, month=2, day=2).date(), 'Thai Restaurant', -10, 'Food', 'Checking Account'),
self.getTr(datetime(year=2016, month=2, day=3).date(), '', 10, 'Saving!', 'Savings'),
]

do_csvimport(args)
self.reload()
for tr in Transactions:
print(json.dumps(tr, cls=ComplexEncoder))
print(json.dumps(
[tr2 for tr2 in self.client.budget.be_transactions if tr2.amount == tr.amount],
cls=ComplexEncoder))
self.assertTrue(self.client.budget.be_transactions.containsduplicate(tr))
for transaction in transactions:
self.assertIn(transaction, self.client.budget.be_transactions)
42 changes: 16 additions & 26 deletions scripts_tests/test_ofximport.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from pynYNAB.schema.Entity import ComplexEncoder
from pynYNAB.schema.budget import Transaction
from pynYNAB.scripts.ofximport import do_ofximport
from test_live.common_Live import CommonLive
from test_live.common import CommonLive


class TestOFX(CommonLive):
Expand Down Expand Up @@ -92,32 +92,22 @@ def testiimport(self):
account.note = 'great note key[%s]key' % key
self.client.sync()

def get_tr(date, payee, amount, memo, accountparameter):
return Transaction(
entities_account_id=accountparameter.id,
date=date,
entities_payee_id=self.util_add_payee_by_name_if_doesnt_exist(payee).id,
imported_payee=payee,
source='Imported',
check_number='0003445',
memo=memo,
amount=amount,
cash_amount=amount,
imported_date=imported_date
)
payee = self.util_add_payee_by_name_if_doesnt_exist('CHEQUE')
amount = -491.09

Transactions = [
get_tr(datetime(year=2013, month=3, day=12).date(), 'CHEQUE', -491.09, 'CHEQUE 13071099780237330004',
account),
]
transaction = Transaction(
entities_account=account,
date=datetime(year=2013, month=3, day=12).date(),
entities_payee=payee,
imported_payee=payee,
source='Imported',
check_number='0003445',
memo='CHEQUE 13071099780237330004',
amount=amount,
imported_date=imported_date
)

do_ofximport(args)
self.reload()
for tr in Transactions:
print('Should have been imported:')
print(json.dumps(tr, cls=ComplexEncoder))
print('Found in the register:')
print(json.dumps([tr2 for tr2 in self.client.budget.be_transactions if tr2.amount == tr.amount],
cls=ComplexEncoder))
self.assertTrue(self.client.budget.be_transactions.containsduplicate(tr),
msg='couldnt find a transaction with the same hash after ofx import')
self.assertIn(transaction, self.client.budget.be_transactions,
msg='couldnt find an imported transaction after ofx import')
4 changes: 3 additions & 1 deletion test_live/testlive.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ def test_add_transfer(self):
)
self.client.budget.be_transactions.append(transaction1)
self.client.budget.be_transactions.append(transaction2)
self.client.sync()
self.reload()
self.assertIn(transaction1, self.client.budget.be_transactions)
self.assertIn(transaction2, self.client.budget.be_transactions)
self.client.budget.be_transactions.remove(transaction1)
self.client.budget.be_transactions.remove(transaction2)
self.reload()
self.client.sync()

@needs_account()
def test_add_deletetransactions(self):
Expand Down Expand Up @@ -163,6 +164,7 @@ def test_add_splittransactions(self):
self.client.budget.be_transactions.append(transaction)
self.client.budget.be_subtransactions.append(sub1)
self.client.budget.be_subtransactions.append(sub2)
self.client.sync()
self.reload()
self.assertIn(transaction, self.client.budget.be_transactions)
self.assertIn(sub1, self.client.budget.be_subtransactions)
Expand Down

0 comments on commit 7e9a33c

Please sign in to comment.