[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 = { _columns = {
'name': fields.char('Test Name', size=256, required=True, select=True, translate=True), 'name': fields.char('Test Name', size=256, required=True, select=True, translate=True),
'desc': fields.text('Test Description', 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'), 'active': fields.boolean('Active'),
'sequence': fields.integer('Sequence'), 'sequence': fields.integer('Sequence'),
} }

View File

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

View File

@ -22,10 +22,7 @@
import datetime import datetime
import time import time
import re
from report import report_sxw from report import report_sxw
from itertools import groupby
from operator import itemgetter
from tools.translate import _ from tools.translate import _
# #
# Use period and Journal for selection or resources # 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 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(): def reconciled_inv():
reconciled_inv_ids = self.pool.get('account.invoice').search(self.cr, self.uid, [('reconciled','=',True)]) """
return reconciled_inv_ids returns the list of invoices that are set as reconciled = True
"""
def get_parent(acc_id): return self.pool.get('account.invoice').search(self.cr, self.uid, [('reconciled','=',True)])
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
def order_columns(item, cols=None): 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: if cols is None:
cols = item.keys() cols = item.keys()
return [(col, item.get(col)) for col in cols if col in item.keys()] return [(col, item.get(col)) for col in cols if col in item.keys()]
localdict = { localdict = {
'cr': self.cr, 'cr': self.cr,
'_': _, 'uid': self.uid,
'reconciled_inv' : reconciled_inv, 'reconciled_inv': reconciled_inv, #specific function used in different tests
'group' : group, 'result': None, #used to store the result of the test
'get_parent' : get_parent, 'column_order': None, #used to choose the display order of columns (in case you are returning a list of dict)
'now': datetime.datetime.now(),
'result': None,
'column_order': None,
} }
exec code_exec in localdict exec code_exec in localdict
result = localdict['result'] result = localdict['result']
column_order = localdict.get('column_order', None) column_order = localdict.get('column_order', None)
@ -79,12 +73,12 @@ class report_assert_account(report_sxw.rml_parse):
if not result: if not result:
result = [_('The test was passed successfully')] result = [_('The test was passed successfully')]
else: else:
def _format(a): def _format(item):
if isinstance(a, dict): if isinstance(item, dict):
return ', '.join(["%s: %s" % (tup[0], tup[1]) for tup in order_columns(a, column_order)]) return ', '.join(["%s: %s" % (tup[0], tup[1]) for tup in order_columns(item, column_order)])
else: else:
return a return item
result = [_format(rec) for rec in result] result = [_(_format(rec)) for rec in result]
return 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" "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 "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