[REF] account_test: refactoring

bzr revid: qdp-launchpad@openerp.com-20121206150903-cl19tda7r00xt1u4
This commit is contained in:
Quentin (OpenERP) 2012-12-06 16:09:03 +01:00
parent 2d123cbdc6
commit d3e2629631
4 changed files with 45 additions and 45 deletions

View File

@ -50,7 +50,7 @@ class accounting_assert_test(osv.osv):
_columns = {
'name': fields.char('Test Name', size=256, required=True, select=True, translate=True),
'desc': fields.text('Test Description', select=True, translate=True),
'code_exec': fields.text('Python code or SQL query', required=True),
'code_exec': fields.text('Python code', required=True),
'active': fields.boolean('Active'),
'sequence': fields.integer('Sequence'),
}

View File

@ -13,7 +13,7 @@
</tree>
</field>
</record>
<record model="ir.ui.view" id="account_assert_form">
<field name="name">Tests</field>
<field name="model">accounting.assert.test</field>
@ -39,20 +39,26 @@
</group>
<group string="Code Help">
<pre>
Code should always return a result value. If result is an empty list, it means that
the test is succesful. Otherwise it will print what is inside result.
Code must be python with correct indentation (if needed).
Here is a list of function that you can use in your test :
- group(lst, col) :
- reconciled_inv() : return the list of all reconciled invoices
- get_parent(acc_id) : get parent analytical account
- now() : return current datetime
Code should always set a variable named `result` with the result of your test, that can be a list or
a dictionary. If `result` is an empty list, it means that the test was succesful. Otherwise it will
try to translate and print what is inside `result`.
If the result of your test is a dictionary, you can set a variable named `column_order` to choose in
what order you want to print `result`'s content.
Should you need them, you can also use the following variables into your code:
* cr: cursor to the database
* uid: ID of the current user
In any ways, the code must be legal python statements with correct indentation (if needed).
Example:
sql = 'select id, name, ref, date from account_move_line where account_id in
(select id from account_account where type = 'view')'
cr.execute(sql)
result = cr.dictfetchall()
sql = '''SELECT id, name, ref, date
FROM account_move_line
WHERE account_id IN (SELECT id FROM account_account WHERE type = 'view')
'''
cr.execute(sql)
result = cr.dictfetchall()
</pre>
</group>
</page>
@ -61,7 +67,7 @@ result = cr.dictfetchall()
</form>
</field>
</record>
<record model="ir.actions.act_window" id="action_accounting_assert">
<field name="name">Accounting Tests</field>
<field name="res_model">accounting.assert.test</field>
@ -72,7 +78,7 @@ result = cr.dictfetchall()
</p>
</field>
</record>
<menuitem name="Accounting Tests" parent="account.menu_finance_reporting" id="menu_action_license" action="action_accounting_assert"/>
</data>

View File

@ -22,10 +22,7 @@
import datetime
import time
import re
from report import report_sxw
from itertools import groupby
from operator import itemgetter
from tools.translate import _
#
# Use period and Journal for selection or resources
@ -40,37 +37,34 @@ class report_assert_account(report_sxw.rml_parse):
})
def execute_code(self, code_exec):
def group(lst, col):
return dict((k, [v for v in itr]) for k, itr in groupby(sorted(lst, key=lambda x: x[col]), itemgetter(col)))
def reconciled_inv():
reconciled_inv_ids = self.pool.get('account.invoice').search(self.cr, self.uid, [('reconciled','=',True)])
return reconciled_inv_ids
def get_parent(acc_id):
acc_an_id = self.pool.get('account.analytic.account').browse(self.cr, self.uid, acc_id).parent_id
while acc_an_id.parent_id:
acc_an_id = acc_an_id.parent_id
return acc_an_id.id
"""
returns the list of invoices that are set as reconciled = True
"""
return self.pool.get('account.invoice').search(self.cr, self.uid, [('reconciled','=',True)])
def order_columns(item, cols=None):
"""
This function is used to display a dictionary as a string, with its columns in the order chosen.
:param item: dict
:param cols: list of field names
:returns: a list of tuples (fieldname: value) in a similar way that would dict.items() do except that the
returned values are following the order given by cols
:rtype: [(key, value)]
"""
if cols is None:
cols = item.keys()
return [(col, item.get(col)) for col in cols if col in item.keys()]
localdict = {
'cr': self.cr,
'_': _,
'reconciled_inv' : reconciled_inv,
'group' : group,
'get_parent' : get_parent,
'now': datetime.datetime.now(),
'result': None,
'column_order': None,
'uid': self.uid,
'reconciled_inv': reconciled_inv, #specific function used in different tests
'result': None, #used to store the result of the test
'column_order': None, #used to choose the display order of columns (in case you are returning a list of dict)
}
exec code_exec in localdict
result = localdict['result']
column_order = localdict.get('column_order', None)
@ -79,12 +73,12 @@ class report_assert_account(report_sxw.rml_parse):
if not result:
result = [_('The test was passed successfully')]
else:
def _format(a):
if isinstance(a, dict):
return ', '.join(["%s: %s" % (tup[0], tup[1]) for tup in order_columns(a, column_order)])
def _format(item):
if isinstance(item, dict):
return ', '.join(["%s: %s" % (tup[0], tup[1]) for tup in order_columns(item, column_order)])
else:
return a
result = [_format(rec) for rec in result]
return item
result = [_(_format(rec)) for rec in result]
return result

View File

@ -1,3 +1,3 @@
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
"access_accounting_assert_test","accounting.assert.test","model_accounting_assert_test",base.group_system,1,1,1,1
"access_accounting_assert_test","accounting.assert.test","model_accounting_assert_test",base.group_system,1,0,0,1
"access_accounting_assert_test_manager","accounting.assert.test","model_accounting_assert_test",account.group_account_manager,1,0,0,0

1 id name model_id:id group_id:id perm_read perm_write perm_create perm_unlink
2 access_accounting_assert_test accounting.assert.test model_accounting_assert_test base.group_system 1 1 0 1 0 1
3 access_accounting_assert_test_manager accounting.assert.test model_accounting_assert_test account.group_account_manager 1 0 0 0