2 To check that common dangerous operations are not allowed by the safe_eval mechanism, attempt to
3 evaluate unauthorized expressions, and verify that they trigger an error.
5 1. Try a few common expressions to verify they work with safe_eval
7 !python {model: ir.model}: |
8 from openerp.tools.safe_eval import safe_eval
9 expected = (1, {"a": 9 * 2}, (True, False, None))
10 actual = safe_eval('(1, {"a": 9 * 2}, (True, False, None))')
11 assert actual == expected, "Simple python expressions are not working with safe_eval"
13 2. Try simple literal definition to verify it works with literal_eval
15 !python {model: ir.model}: |
17 expected = (1, {"a": 9}, (True, False, None))
18 actual = ast.literal_eval('(1, {"a": 9}, (True, False, None))')
19 assert actual == expected, "Simple python expressions are not working with literal_eval"
21 3. Try arithmetic expression in literal_eval to verify it does not work
23 !python {model: ir.model}: |
26 ast.literal_eval('(1, {"a": 2*9}, (True, False, None))')
27 assert False, "literal_eval should not accept arithmetic expressions"
31 4. Try forbidden expressions in literal_eval to verify they are not allowed
33 !python {model: ir.model}: |
36 ast.literal_eval('{"a": True.__class__}')
37 assert False, "literal_eval should accept only literals"
41 5. Try forbidden expressions in safe_eval to verify they are not allowed (open)
43 !python {model: ir.model}: |
44 from openerp.tools.safe_eval import safe_eval
45 from openerp.tools.misc import mute_logger
47 with mute_logger('openerp.tools.safe_eval'):
48 safe_eval('open("/etc/passwd","r")')
49 assert False, "safe_eval should not allow calling open() builtin"
54 "ORM test: verify that parent_store computation are going right"
56 0. Emulate normal behavior of tree structure storing
58 !python {model: res.partner.category}: |
59 # pretend the pool has finished loading to avoid deferring parent_store computation
60 self.pool._init = False
62 "1.0 Setup test partner categories: parent root"
64 !record {model: res.partner.category, id: test_categ_root}:
67 "1.1 Setup test partner categories: parent category"
69 !record {model: res.partner.category, id: test_categ_0}:
71 parent_id: test_categ_root
73 "1.2 Setup test partner categories: child 1"
75 !record {model: res.partner.category, id: test_categ_1}:
77 parent_id: test_categ_0
79 "1.3 Setup test partner categories: child 2"
81 !record {model: res.partner.category, id: test_categ_2}:
83 parent_id: test_categ_0
85 "1.4 Setup test partner categories: child 2-1"
87 !record {model: res.partner.category, id: test_categ_21}:
89 parent_id: test_categ_2
91 2. Duplicate the parent category and verify that the children have been duplicated too and are below the new parent
93 !python {model: res.partner.category}: |
94 new_id = self.copy(cr, uid, ref('test_categ_0'))
95 new_struct = self.search(cr, uid, [('parent_id', 'child_of', new_id)])
96 assert len(new_struct) == 4, "After duplication, the new object must have the childs records"
97 old_struct = self.search(cr, uid, [('parent_id', 'child_of', ref('test_categ_0'))])
98 assert len(old_struct) == 4, "After duplication, previous record must have old childs records only"
99 assert (not set(new_struct).intersection(old_struct)), "After duplication, nodes should not be mixed"
101 3. Duplicate the children then reassign them to the new parent (1st method) and check the parent_store structure.
103 !python {model: res.partner.category}: |
104 new_child1_id = self.copy(cr, uid, ref('test_categ_1'))
105 new_child2_id = self.copy(cr, uid, ref('test_categ_2'))
106 new_id = self.copy(cr, uid, ref('test_categ_0'), {'child_ids': []})
107 self.write(cr, uid, [new_child1_id, new_child2_id], {'parent_id': new_id})
108 new_struct = self.search(cr, uid, [('parent_id', 'child_of', new_id)])
109 assert len(new_struct) == 4, "After duplication, the new object must have the childs records"
110 old_struct = self.search(cr, uid, [('parent_id', 'child_of', ref('test_categ_0'))])
111 assert len(old_struct) == 4, "After duplication, previous record must have old childs records only"
112 assert (not set(new_struct).intersection(old_struct)), "After duplication, nodes should not be mixed"
114 4. Duplicate the children then reassign them to the new parent (2nd method) and check the parent_store structure.
116 !python {model: res.partner.category}: |
117 new_child1_id = self.copy(cr, uid, ref('test_categ_1'))
118 new_child2_id = self.copy(cr, uid, ref('test_categ_2'))
119 old_struct = self.search(cr, uid, [('parent_id', 'child_of', ref('test_categ_0'))])
120 new_id = self.copy(cr, uid, ref('test_categ_0'), {'child_ids': [(6,0,[new_child1_id, new_child2_id])]})
121 new_struct = self.search(cr, uid, [('parent_id', 'child_of', new_id)])
122 assert len(new_struct) == 4, "After duplication, the new object must have the childs records"
123 old_struct = self.search(cr, uid, [('parent_id', 'child_of', ref('test_categ_0'))])
124 assert len(old_struct) == 4, "After duplication, previous record must have old childs records only"
125 assert (not set(new_struct).intersection(old_struct)), "After duplication, nodes should not be mixed"
127 5. Duplicate the children then reassign them to the new parent (3rd method) and make sure the parent_store structure is still right.
129 !python {model: res.partner.category}: |
130 new_child1_id = self.copy(cr, uid, ref('test_categ_1'))
131 new_child2_id = self.copy(cr, uid, ref('test_categ_2'))
132 new_id = self.copy(cr, uid, ref('test_categ_0'), {'child_ids': []})
133 self.write(cr, uid, [new_id], {'child_ids': [(4,new_child1_id), (4,new_child2_id)]})
134 new_struct = self.search(cr, uid, [('parent_id', 'child_of', new_id)])
135 assert len(new_struct) == 4, "After duplication, the new object must have the childs records"
136 old_struct = self.search(cr, uid, [('parent_id', 'child_of', ref('test_categ_0'))])
137 assert len(old_struct) == 4, "After duplication, previous record must have old childs records only"
138 assert (not set(new_struct).intersection(old_struct)), "After duplication, nodes should not be mixed"
140 6. Restore pool state after the test
142 !python {model: res.partner.category}: |
143 self.pool._init = True
146 "Float precision tests: verify that float rounding methods are working correctly via res.currency"
148 !python {model: res.currency}: |
149 from openerp.tools import float_repr
150 from math import log10
151 currency = self.browse(cr, uid, ref('base.EUR'))
152 def try_round(amount, expected, self=self, cr=cr, currency=currency, float_repr=float_repr,
154 digits = max(0,-int(log10(currency.rounding)))
155 result = float_repr(self.round(cr, 1, currency, amount), precision_digits=digits)
156 assert result == expected, 'Rounding error: got %s, expected %s' % (result, expected)
157 try_round(2.674,'2.67')
158 try_round(2.675,'2.68') # in Python 2.7.2, round(2.675,2) gives 2.67
159 try_round(-2.675,'-2.68') # in Python 2.7.2, round(2.675,2) gives 2.67
160 try_round(0.001,'0.00')
161 try_round(-0.001,'-0.00')
162 try_round(0.0049,'0.00') # 0.0049 is closer to 0 than to 0.01, so should round down
163 try_round(0.005,'0.01') # the rule is to round half away from zero
164 try_round(-0.005,'-0.01') # the rule is to round half away from zero
166 def try_zero(amount, expected, self=self, cr=cr, currency=currency):
167 assert self.is_zero(cr, 1, currency, amount) == expected, "Rounding error: %s should be zero!" % amount
168 try_zero(0.01, False)
169 try_zero(-0.01, False)
170 try_zero(0.001, True)
171 try_zero(-0.001, True)
172 try_zero(0.0046, True)
173 try_zero(-0.0046, True)
174 try_zero(2.68-2.675, False) # 2.68 - 2.675 = 0.005 -> rounds to 0.01
175 try_zero(2.68-2.676, True) # 2.68 - 2.675 = 0.004 -> rounds to 0.0
176 try_zero(2.676-2.68, True) # 2.675 - 2.68 = -0.004 -> rounds to -0.0
177 try_zero(2.675-2.68, False) # 2.675 - 2.68 = -0.005 -> rounds to -0.01
179 def try_compare(amount1, amount2, expected, self=self, cr=cr, currency=currency):
180 assert self.compare_amounts(cr, 1, currency, amount1, amount2) == expected, \
181 "Rounding error, compare_amounts(%s,%s) should be %s" % (amount1, amount2, expected)
182 try_compare(0.001, 0.001, 0)
183 try_compare(-0.001, -0.001, 0)
184 try_compare(0.001, 0.002, 0)
185 try_compare(-0.001, -0.002, 0)
186 try_compare(2.675, 2.68, 0)
187 try_compare(2.676, 2.68, 0)
188 try_compare(-2.676, -2.68, 0)
189 try_compare(2.674, 2.68, -1)
190 try_compare(-2.674, -2.68, 1)
191 try_compare(3, 2.68, 1)
192 try_compare(-3, -2.68, -1)
193 try_compare(0.01, 0, 1)
194 try_compare(-0.01, 0, -1)
197 "Float precision tests: verify that float rounding methods are working correctly via tools"
199 !python {model: res.currency}: |
200 from openerp.tools import float_compare, float_is_zero, float_round, float_repr
201 def try_round(amount, expected, precision_digits=3, float_round=float_round, float_repr=float_repr):
202 result = float_repr(float_round(amount, precision_digits=precision_digits, rounding_method=rounding_method),
203 assert result == expected, 'Rounding error: got %s, expected %s' % (result, expected)
204 try_round(2.6745, '2.675')
205 try_round(-2.6745, '-2.675')
206 try_round(2.6744, '2.674')
207 try_round(-2.6744, '-2.674')
208 try_round(0.0004, '0.000')
209 try_round(-0.0004, '-0.000')
210 try_round(357.4555, '357.456')
211 try_round(-357.4555, '-357.456')
212 try_round(457.4554, '457.455')
213 try_round(-457.4554, '-457.455')
215 # Try some rounding value with rounding method UP instead of HALF-UP
216 # We use 8.175 because when normalizing 8.175 with precision_digits=3 it gives
217 # us 8175,0000000001234 as value, and if not handle correctly the rounding UP
218 # value will be incorrect (should be 8,175 and not 8,176)
219 try_round(8.175, '8.175', rounding_method='UP')
220 try_round(8.1751, '8.176', rounding_method='UP')
221 try_round(-8.175, '-8.175', rounding_method='UP')
222 try_round(-8.1751, '-8.176', rounding_method='UP')
223 try_round(-6.000, '-6.000', rounding_method='UP')
224 try_round(1.8, '2', 0, rounding_method='UP')
225 try_round(-1.8, '-2', 0, rounding_method='UP')
227 # Extended float range test, inspired by Cloves Almeida's test on bug #882036.
228 fractions = [.0, .015, .01499, .675, .67499, .4555, .4555, .45555]
229 expecteds = ['.00', '.02', '.01', '.68', '.67', '.46', '.456', '.4556']
230 precisions = [2, 2, 2, 2, 2, 2, 3, 4]
231 # Note: max precision for double floats is 53 bits of precision or
232 # 17 significant decimal digits
233 for magnitude in range(7):
234 for i in xrange(len(fractions)):
235 frac, exp, prec = fractions[i], expecteds[i], precisions[i]
237 for x in xrange(0,10000,97):
238 n = x * 10**magnitude
239 f = sign * (n + frac)
240 f_exp = ('-' if f != 0 and sign == -1 else '') + str(n) + exp
241 try_round(f, f_exp, precision_digits=prec)
244 def try_zero(amount, expected, float_is_zero=float_is_zero):
245 assert float_is_zero(amount, precision_digits=3) == expected, "Rounding error: %s should be zero!" % amount
246 try_zero(0.0002, True)
247 try_zero(-0.0002, True)
248 try_zero(0.00034, True)
249 try_zero(0.0005, False)
250 try_zero(-0.0005, False)
251 try_zero(0.0008, False)
252 try_zero(-0.0008, False)
254 def try_compare(amount1, amount2, expected, float_compare=float_compare):
255 assert float_compare(amount1, amount2, precision_digits=3) == expected, \
256 "Rounding error, compare_amounts(%s,%s) should be %s" % (amount1, amount2, expected)
257 try_compare(0.0003, 0.0004, 0)
258 try_compare(-0.0003, -0.0004, 0)
259 try_compare(0.0002, 0.0005, -1)
260 try_compare(-0.0002, -0.0005, 1)
261 try_compare(0.0009, 0.0004, 1)
262 try_compare(-0.0009, -0.0004, -1)
263 try_compare(557.4555, 557.4556, 0)
264 try_compare(-557.4555, -557.4556, 0)
265 try_compare(657.4444, 657.445, -1)
266 try_compare(-657.4444, -657.445, 1)
268 # Rounding to unusual rounding units (e.g. coin values)
269 def try_round(amount, expected, precision_rounding=None, float_round=float_round, float_repr=float_repr):
270 result = float_repr(float_round(amount, precision_rounding=precision_rounding),
272 assert result == expected, 'Rounding error: got %s, expected %s' % (result, expected)
273 try_round(-457.4554, '-457.45', precision_rounding=0.05)
274 try_round(457.444, '457.50', precision_rounding=0.5)
275 try_round(457.3, '455.00', precision_rounding=5)
276 try_round(457.5, '460.00', precision_rounding=5)
277 try_round(457.1, '456.00', precision_rounding=3)
280 "Float precision tests: check that proper rounding is performed for float persistence"
282 !python {model: res.currency}: |
283 currency = self.browse(cr, uid, ref('base.EUR'))
284 res_currency_rate = self.pool.get('res.currency.rate')
285 from openerp.tools import float_compare, float_is_zero, float_round, float_repr
286 def try_roundtrip(value, expected, self=self, cr=cr, currency=currency,
287 res_currency_rate=res_currency_rate):
288 rate_id = res_currency_rate.create(cr, 1, {'name':'2000-01-01',
290 'currency_id': currency.id})
291 rate = res_currency_rate.read(cr, 1, rate_id, ['rate'])['rate']
292 assert rate == expected, 'Roundtrip error: got %s back from db, expected %s' % (rate, expected)
293 # res.currency.rate uses 6 digits of precision by default
294 try_roundtrip(2.6748955, 2.674896)
295 try_roundtrip(-2.6748955, -2.674896)
296 try_roundtrip(10000.999999, 10000.999999)
297 try_roundtrip(-10000.999999, -10000.999999)
300 "Float precision tests: verify that invalid parameters are forbidden"
302 !python {model: res.currency}: |
303 from openerp.tools import float_compare, float_is_zero, float_round
305 float_is_zero(0.01, precision_digits=3, precision_rounding=0.01)
306 except AssertionError:
309 float_compare(0.01, 0.02, precision_digits=3, precision_rounding=0.01)
310 except AssertionError:
313 float_round(0.01, precision_digits=3, precision_rounding=0.01)
314 except AssertionError:
317 Test res.groups name search
319 !python {model: res.groups}: |
320 all_groups = self.search(cr, uid, [])
321 full_names = [(group.id, group.full_name) for group in self.browse(cr, uid, all_groups)]
322 group_ids = self.search(cr, uid, [('full_name', 'like', '%Sale%')])
323 assert set(group_ids) == set([id for (id, full_name) in full_names if 'Sale' in full_name]), "did not match search for 'Sale'"
324 group_ids = self.search(cr, uid, [('full_name', 'like', '%Technical%')])
325 assert set(group_ids) == set([id for (id, full_name) in full_names if 'Technical' in full_name]), "did not match search for 'Technical'"
326 group_ids = self.search(cr, uid, [('full_name', 'like', '%Sales /%')])
327 assert set(group_ids) == set([id for (id, full_name) in full_names if 'Sales /' in full_name]), "did not match search for 'Sales /'"
328 group_ids = self.search(cr, uid, [('full_name', 'in', ['Administration / Access Rights','Contact Creation'])])
329 assert group_ids, "did not match search for 'Administration / Access Rights' and 'Contact Creation'"