Skip to content

Commit

Permalink
assertRecordsetEquals is born!
Browse files Browse the repository at this point in the history
  • Loading branch information
smetl committed Jun 7, 2018
1 parent 03493cc commit af1e210
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 80 deletions.
39 changes: 0 additions & 39 deletions addons/account/tests/account_test_classes.py
Expand Up @@ -22,45 +22,6 @@ def setUp(self):
_logger.warn('Test skipped because there is no chart of account defined ...')
self.skipTest("No Chart of account found")

def check_complete_records(self, records, theorical_dicts):
''' Compare records with theorical list of dictionaries representing the expected results.
The dictionary uses field names as keys. Field names could be either applied on the record itself
(e.g. 'debit') or a composite path to another records (e.g. 'move_id.line_ids.debit').
Comparison for many2one field could be either an id [resp. a list of ids] or a dict [resp. a list of dict].
:param records: The records to compare.
:param theorical_dicts: The expected results as a list of dicts.
:return: True if all is equivalent, False otherwise.
'''
if len(records) != len(theorical_dicts):
raise ValidationError('Too many records to compare: %d != %d.' % (len(records), len(theorical_dicts)))

def _get_matching_record(record_values, theorical_dicts):
# Search for a theorical dict having same values as the record.
index = 0
for candidate in theorical_dicts:
if candidate == record_values:
return index
index += 1
return False

keys = list(theorical_dicts[0].keys())
for record_values in records.read(keys, load=False):
# remove 'id' field which is automatically added by read()
del(record_values['id'])

# search for matching values in theorical_dicts
matching_index = _get_matching_record(record_values, theorical_dicts)
if matching_index is not False:
del theorical_dicts[matching_index]
else:
raise ValidationError('Unexpected record found: %s.' % str(record_values))

# theorical_dicts should be empty, otherwise there are missing lines in checked records
if theorical_dicts:
raise ValidationError('Remaining theorical line (not found): %s)' % str(theorical_dicts))
return True

def ensure_account_property(self, property_name):
'''Ensure the ir.property targeting an account.account passed as parameter exists.
In case it's not: create it with a random account. This is useful when testing with
Expand Down
6 changes: 3 additions & 3 deletions addons/account/tests/test_account_move_taxes_edition.py
Expand Up @@ -56,7 +56,7 @@ def test_onchange_taxes_1(self):

move = move_form.save()

self.check_complete_records(move.line_ids, [
self.assertRecordsetEquals(move.line_ids, [
{'name': 'debit_line_1', 'debit': 1000.0, 'credit': 0.0, 'tax_ids': [self.percent_tax.id], 'tax_line_id': False},
{'name': 'tax_line', 'debit': 100.0, 'credit': 0.0, 'tax_ids': [], 'tax_line_id': self.percent_tax.id},
{'name': 'credit_line_1', 'debit': 0.0, 'credit': 1100.0, 'tax_ids': [], 'tax_line_id': False},
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_onchange_taxes_2(self):

move = move_form.save()

self.check_complete_records(move.line_ids, [
self.assertRecordsetEquals(move.line_ids, [
{'name': 'debit_line_1', 'debit': 2000.0, 'credit': 0.0, 'tax_ids': [self.percent_tax.id], 'tax_line_id': False},
{'name': 'tax_line', 'debit': 200.0, 'credit': 0.0, 'tax_ids': [], 'tax_line_id': self.percent_tax.id},
{'name': 'credit_line_1', 'debit': 0.0, 'credit': 2200.0, 'tax_ids': [], 'tax_line_id': False},
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_onchange_taxes_3(self):

move = move_form.save()

self.check_complete_records(move.line_ids, [
self.assertRecordsetEquals(move.line_ids, [
{'name': 'debit_line_1', 'debit': 1000.0, 'credit': 0.0, 'tax_ids': [self.percent_tax.id], 'tax_line_id': False},
{'name': 'tax_line', 'debit': 300.0, 'credit': 0.0, 'tax_ids': [], 'tax_line_id': self.percent_tax.id},
{'name': 'credit_line_1', 'debit': 0.0, 'credit': 1105.0, 'tax_ids': [], 'tax_line_id': False},
Expand Down
32 changes: 16 additions & 16 deletions addons/account/tests/test_payment.py
Expand Up @@ -102,7 +102,7 @@ def test_full_payment_process(self):
self.assertEqual(inv_1.state, 'paid')
self.assertEqual(inv_2.state, 'paid')

self.check_complete_records(payment.move_line_ids, [
self.assertRecordsetEquals(payment.move_line_ids, [
{'account_id': self.account_eur.id, 'debit': 300.0, 'credit': 0.0, 'amount_currency': 0, 'currency_id': False},
{'account_id': inv_1.account_id.id, 'debit': 0.0, 'credit': 300.0, 'amount_currency': 0, 'currency_id': False},
])
Expand All @@ -128,7 +128,7 @@ def test_internal_transfer_journal_usd_journal_eur(self):
'payment_method_id': self.payment_method_manual_out.id,
})
payment.post()
self.check_complete_records(payment.move_line_ids, [
self.assertRecordsetEquals(payment.move_line_ids, [
{'account_id': self.transfer_account.id, 'debit': 32.70, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_usd_id},
{'account_id': self.transfer_account.id, 'debit': 0.0, 'credit': 32.70, 'amount_currency': -50, 'currency_id': self.currency_usd_id},
{'account_id': self.account_eur.id, 'debit': 32.70, 'credit': 0.0, 'amount_currency': 0, 'currency_id': False},
Expand All @@ -148,7 +148,7 @@ def test_payment_chf_journal_usd(self):
})
payment.post()

self.check_complete_records(payment.move_line_ids, [
self.assertRecordsetEquals(payment.move_line_ids, [
{'account_id': self.account_usd.id, 'debit': 0.0, 'credit': 38.21, 'amount_currency': -58.42, 'currency_id': self.currency_usd_id},
{'account_id': self.partner_china_exp.property_account_payable_id.id, 'debit': 38.21, 'credit': 0.0, 'amount_currency': 50, 'currency_id': self.currency_chf_id},
])
Expand Down Expand Up @@ -195,20 +195,20 @@ def test_multiple_payments_00(self):
self.assertIsNotNone(inv_3_pay)
self.assertIsNotNone(inv_4_pay)

self.check_complete_records(inv_1_2_pay.move_line_ids, [
self.assertRecordsetEquals(inv_1_2_pay.move_line_ids, [
{'account_id': self.account_eur.id, 'debit': 600.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False},
{'account_id': inv_1.account_id.id, 'debit': 0.0, 'credit': 600.0, 'amount_currency': 0.0, 'currency_id': False},
])
self.assertEqual(inv_1.state, 'paid')
self.assertEqual(inv_2.state, 'paid')

self.check_complete_records(inv_3_pay.move_line_ids, [
self.assertRecordsetEquals(inv_3_pay.move_line_ids, [
{'account_id': self.account_eur.id, 'debit': 200.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False},
{'account_id': inv_1.account_id.id, 'debit': 0.0, 'credit': 200.0, 'amount_currency': 0.0, 'currency_id': False},
])
self.assertEqual(inv_3.state, 'paid')

self.check_complete_records(inv_4_pay.move_line_ids, [
self.assertRecordsetEquals(inv_4_pay.move_line_ids, [
{'account_id': self.account_eur.id, 'debit': 0.0, 'credit': 50.0, 'amount_currency': 0.0, 'currency_id': False},
{'account_id': inv_1.account_id.id, 'debit': 50.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False},
])
Expand All @@ -234,7 +234,7 @@ def test_multiple_payments_01(self):

self.assertEqual(payment_id.state, 'posted')

self.check_complete_records(payment_id.move_line_ids, [
self.assertRecordsetEquals(payment_id.move_line_ids, [
{'account_id': self.account_eur.id, 'debit': 450.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False},
{'account_id': inv_1.account_id.id, 'debit': 0.0, 'credit': 450.0, 'amount_currency': 0.0, 'currency_id': False},
])
Expand Down Expand Up @@ -322,12 +322,12 @@ def test_multiple_receivables(self):
self.assertTrue(inv_1_pay)
self.assertTrue(inv_2_pay)

self.check_complete_records(inv_1_pay.move_line_ids, [
self.assertRecordsetEquals(inv_1_pay.move_line_ids, [
{'account_id': self.account_eur.id, 'debit': 250.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False},
{'account_id': inv_1.account_id.id, 'debit': 0.0, 'credit': 250.0, 'amount_currency': 0.0, 'currency_id': False},
])

self.check_complete_records(inv_2_pay.move_line_ids, [
self.assertRecordsetEquals(inv_2_pay.move_line_ids, [
{'account_id': self.account_eur.id, 'debit': 300.0, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False},
{'account_id': inv_3.account_id.id, 'debit': 0.0, 'credit': 300.0, 'amount_currency': 0.0, 'currency_id': False},
])
Expand All @@ -352,7 +352,7 @@ def test_payment_and_writeoff_in_other_currency_1(self):
'invoice_ids': [(4, invoice.id, None)]
})
payment.post()
self.check_complete_records(payment.move_line_ids, [
self.assertRecordsetEquals(payment.move_line_ids, [
{'account_id': self.account_eur.id, 'debit': 16.35, 'credit': 0.0, 'amount_currency': 25.0, 'currency_id': self.currency_usd_id},
{'account_id': self.account_payable.id, 'debit': 8.65, 'credit': 0.0, 'amount_currency': 13.22, 'currency_id': self.currency_usd_id},
{'account_id': self.account_receivable.id, 'debit': 0.0, 'credit': 25.0, 'amount_currency': -38.22, 'currency_id': self.currency_usd_id},
Expand All @@ -379,7 +379,7 @@ def test_payment_and_writeoff_in_other_currency_1(self):
'invoice_ids': [(4, invoice.id, None)]
})
payment.post()
self.check_complete_records(payment.move_line_ids, [
self.assertRecordsetEquals(payment.move_line_ids, [
{'account_id': self.account_eur.id, 'debit': 0.0, 'credit': 16.35, 'amount_currency': -25.0, 'currency_id': self.currency_usd_id},
{'account_id': self.account_payable.id, 'debit': 0.0, 'credit': 8.65, 'amount_currency': -13.22, 'currency_id': self.currency_usd_id},
{'account_id': self.account_receivable.id, 'debit': 25.0, 'credit': 0.0, 'amount_currency': 38.22, 'currency_id': self.currency_usd_id},
Expand All @@ -404,7 +404,7 @@ def test_payment_and_writeoff_in_other_currency_2(self):
'name': time.strftime('%Y') + '-07-15'})

invoice = self.create_invoice(amount=5325.6, type='in_invoice', currency_id=self.currency_usd_id, partner=self.partner_agrolait.id)
self.check_complete_records(invoice.move_id.line_ids, [
self.assertRecordsetEquals(invoice.move_id.line_ids, [
{'account_id': self.account_revenue.id, 'debit': 5950.39, 'credit': 0.0, 'amount_currency': 5325.6, 'currency_id': self.currency_usd_id},
{'account_id': self.account_receivable.id, 'debit': 0.0, 'credit': 5950.39, 'amount_currency': -5325.6, 'currency_id': self.currency_usd_id},
])
Expand All @@ -422,13 +422,13 @@ def test_payment_and_writeoff_in_other_currency_2(self):
'invoice_ids': [(4, invoice.id, None)]
})
payment.post()
self.check_complete_records(payment.move_line_ids, [
self.assertRecordsetEquals(payment.move_line_ids, [
{'debit': 0.0, 'credit': 6051.14, 'amount_currency': -5325.0, 'currency_id': self.currency_usd_id},
{'debit': 0.0, 'credit': 0.68, 'amount_currency': -0.6, 'currency_id': self.currency_usd_id},
{'debit': 6051.82, 'credit': 0.0, 'amount_currency': 5325.6, 'currency_id': self.currency_usd_id},
])
exchange_lines = payment.move_line_ids[-1].full_reconcile_id.exchange_move_id.line_ids
self.check_complete_records(exchange_lines, [
self.assertRecordsetEquals(exchange_lines, [
{'debit': 101.43, 'credit': 0.0, 'account_id': self.diff_expense_account.id},
{'debit': 0.0, 'credit': 101.43, 'account_id': self.account_receivable.id},
])
Expand All @@ -455,7 +455,7 @@ def test_payment_and_writeoff_in_other_currency_3(self):
'name': time.strftime('%Y') + '-06-26'})

invoice = self.create_invoice(amount=247590.4, type='out_invoice', currency_id=self.currency_eur_id, partner=self.partner_agrolait.id)
self.check_complete_records(invoice.move_id.line_ids, [
self.assertRecordsetEquals(invoice.move_id.line_ids, [
{'account_id': self.account_receivable.id, 'debit': 247590.4, 'credit': 0.0, 'amount_currency': 0.0, 'currency_id': False},
{'account_id': self.account_revenue.id, 'debit': 0.0, 'credit': 247590.4, 'amount_currency': 0.0, 'currency_id': False},
])
Expand All @@ -474,7 +474,7 @@ def test_payment_and_writeoff_in_other_currency_3(self):
'name': 'test_payment_and_writeoff_in_other_currency_3',
})
payment.post()
self.check_complete_records(payment.move_line_ids, [
self.assertRecordsetEquals(payment.move_line_ids, [
{'account_id': self.account_eur.id, 'debit': 253116.0, 'credit': 0.0, 'amount_currency': 267.0, 'currency_id': self.currency_usd_id},
{'account_id': self.account_revenue.id, 'debit': 0.0, 'credit': 5526.84, 'amount_currency': -5.83, 'currency_id': self.currency_usd_id},
{'account_id': self.account_receivable.id, 'debit': 0.0, 'credit': 247589.16, 'amount_currency': -261.17, 'currency_id': self.currency_usd_id},
Expand Down

0 comments on commit af1e210

Please sign in to comment.