Skip to content

Commit d3e2629

Browse files
author
Quentin (OpenERP)
committed
[REF] account_test: refactoring
bzr revid: [email protected]
1 parent 2d123cb commit d3e2629

File tree

4 files changed

+45
-45
lines changed

4 files changed

+45
-45
lines changed

addons/account_test/account_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class accounting_assert_test(osv.osv):
5050
_columns = {
5151
'name': fields.char('Test Name', size=256, required=True, select=True, translate=True),
5252
'desc': fields.text('Test Description', select=True, translate=True),
53-
'code_exec': fields.text('Python code or SQL query', required=True),
53+
'code_exec': fields.text('Python code', required=True),
5454
'active': fields.boolean('Active'),
5555
'sequence': fields.integer('Sequence'),
5656
}

addons/account_test/account_test_view.xml

+21-15
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</tree>
1414
</field>
1515
</record>
16-
16+
1717
<record model="ir.ui.view" id="account_assert_form">
1818
<field name="name">Tests</field>
1919
<field name="model">accounting.assert.test</field>
@@ -39,20 +39,26 @@
3939
</group>
4040
<group string="Code Help">
4141
<pre>
42-
Code should always return a result value. If result is an empty list, it means that
43-
the test is succesful. Otherwise it will print what is inside result.
44-
Code must be python with correct indentation (if needed).
45-
Here is a list of function that you can use in your test :
46-
- group(lst, col) :
47-
- reconciled_inv() : return the list of all reconciled invoices
48-
- get_parent(acc_id) : get parent analytical account
49-
- now() : return current datetime
42+
Code should always set a variable named `result` with the result of your test, that can be a list or
43+
a dictionary. If `result` is an empty list, it means that the test was succesful. Otherwise it will
44+
try to translate and print what is inside `result`.
45+
46+
If the result of your test is a dictionary, you can set a variable named `column_order` to choose in
47+
what order you want to print `result`'s content.
48+
49+
Should you need them, you can also use the following variables into your code:
50+
* cr: cursor to the database
51+
* uid: ID of the current user
52+
53+
In any ways, the code must be legal python statements with correct indentation (if needed).
5054

5155
Example:
52-
sql = 'select id, name, ref, date from account_move_line where account_id in
53-
(select id from account_account where type = 'view')'
54-
cr.execute(sql)
55-
result = cr.dictfetchall()
56+
sql = '''SELECT id, name, ref, date
57+
FROM account_move_line
58+
WHERE account_id IN (SELECT id FROM account_account WHERE type = 'view')
59+
'''
60+
cr.execute(sql)
61+
result = cr.dictfetchall()
5662
</pre>
5763
</group>
5864
</page>
@@ -61,7 +67,7 @@ result = cr.dictfetchall()
6167
</form>
6268
</field>
6369
</record>
64-
70+
6571
<record model="ir.actions.act_window" id="action_accounting_assert">
6672
<field name="name">Accounting Tests</field>
6773
<field name="res_model">accounting.assert.test</field>
@@ -72,7 +78,7 @@ result = cr.dictfetchall()
7278
</p>
7379
</field>
7480
</record>
75-
81+
7682
<menuitem name="Accounting Tests" parent="account.menu_finance_reporting" id="menu_action_license" action="action_accounting_assert"/>
7783

7884
</data>

addons/account_test/report/account_test_report.py

+22-28
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222

2323
import datetime
2424
import time
25-
import re
2625
from report import report_sxw
27-
from itertools import groupby
28-
from operator import itemgetter
2926
from tools.translate import _
3027
#
3128
# Use period and Journal for selection or resources
@@ -40,37 +37,34 @@ def __init__(self, cr, uid, name, context):
4037
})
4138

4239
def execute_code(self, code_exec):
43-
def group(lst, col):
44-
return dict((k, [v for v in itr]) for k, itr in groupby(sorted(lst, key=lambda x: x[col]), itemgetter(col)))
45-
4640
def reconciled_inv():
47-
reconciled_inv_ids = self.pool.get('account.invoice').search(self.cr, self.uid, [('reconciled','=',True)])
48-
return reconciled_inv_ids
49-
50-
def get_parent(acc_id):
51-
acc_an_id = self.pool.get('account.analytic.account').browse(self.cr, self.uid, acc_id).parent_id
52-
while acc_an_id.parent_id:
53-
acc_an_id = acc_an_id.parent_id
54-
return acc_an_id.id
41+
"""
42+
returns the list of invoices that are set as reconciled = True
43+
"""
44+
return self.pool.get('account.invoice').search(self.cr, self.uid, [('reconciled','=',True)])
5545

5646
def order_columns(item, cols=None):
47+
"""
48+
This function is used to display a dictionary as a string, with its columns in the order chosen.
49+
50+
:param item: dict
51+
:param cols: list of field names
52+
:returns: a list of tuples (fieldname: value) in a similar way that would dict.items() do except that the
53+
returned values are following the order given by cols
54+
:rtype: [(key, value)]
55+
"""
5756
if cols is None:
5857
cols = item.keys()
5958
return [(col, item.get(col)) for col in cols if col in item.keys()]
6059

6160
localdict = {
6261
'cr': self.cr,
63-
'_': _,
64-
'reconciled_inv' : reconciled_inv,
65-
'group' : group,
66-
'get_parent' : get_parent,
67-
'now': datetime.datetime.now(),
68-
'result': None,
69-
'column_order': None,
62+
'uid': self.uid,
63+
'reconciled_inv': reconciled_inv, #specific function used in different tests
64+
'result': None, #used to store the result of the test
65+
'column_order': None, #used to choose the display order of columns (in case you are returning a list of dict)
7066
}
71-
7267
exec code_exec in localdict
73-
7468
result = localdict['result']
7569
column_order = localdict.get('column_order', None)
7670

@@ -79,12 +73,12 @@ def order_columns(item, cols=None):
7973
if not result:
8074
result = [_('The test was passed successfully')]
8175
else:
82-
def _format(a):
83-
if isinstance(a, dict):
84-
return ', '.join(["%s: %s" % (tup[0], tup[1]) for tup in order_columns(a, column_order)])
76+
def _format(item):
77+
if isinstance(item, dict):
78+
return ', '.join(["%s: %s" % (tup[0], tup[1]) for tup in order_columns(item, column_order)])
8579
else:
86-
return a
87-
result = [_format(rec) for rec in result]
80+
return item
81+
result = [_(_format(rec)) for rec in result]
8882

8983
return result
9084

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"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,1,1
2+
"access_accounting_assert_test","accounting.assert.test","model_accounting_assert_test",base.group_system,1,0,0,1
33
"access_accounting_assert_test_manager","accounting.assert.test","model_accounting_assert_test",account.group_account_manager,1,0,0,0

0 commit comments

Comments
 (0)