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 # Force partner_categ.copy() to copy children
63 self.pool['res.partner.category']._columns['child_ids'].copy = True
65 "1.0 Setup test partner categories: parent root"
67 !record {model: res.partner.category, id: test_categ_root}:
70 "1.1 Setup test partner categories: parent category"
72 !record {model: res.partner.category, id: test_categ_0}:
74 parent_id: test_categ_root
76 "1.2 Setup test partner categories: child 1"
78 !record {model: res.partner.category, id: test_categ_1}:
80 parent_id: test_categ_0
82 "1.3 Setup test partner categories: child 2"
84 !record {model: res.partner.category, id: test_categ_2}:
86 parent_id: test_categ_0
88 "1.4 Setup test partner categories: child 2-1"
90 !record {model: res.partner.category, id: test_categ_21}:
92 parent_id: test_categ_2
94 2. Duplicate the parent category and verify that the children have been duplicated too and are below the new parent
96 !python {model: res.partner.category}: |
97 self._columns['child_ids'].copy = True # force copying children for test
98 new_id = self.copy(cr, uid, ref('test_categ_0'))
99 new_struct = self.search(cr, uid, [('parent_id', 'child_of', new_id)])
100 assert len(new_struct) == 4, "After duplication, the new object must have the childs records"
101 old_struct = self.search(cr, uid, [('parent_id', 'child_of', ref('test_categ_0'))])
102 assert len(old_struct) == 4, "After duplication, previous record must have old childs records only"
103 assert (not set(new_struct).intersection(old_struct)), "After duplication, nodes should not be mixed"
105 3. Duplicate the children then reassign them to the new parent (1st method) and check the parent_store structure.
107 !python {model: res.partner.category}: |
108 new_child1_id = self.copy(cr, uid, ref('test_categ_1'))
109 new_child2_id = self.copy(cr, uid, ref('test_categ_2'))
110 new_id = self.copy(cr, uid, ref('test_categ_0'), {'child_ids': []})
111 self.write(cr, uid, [new_child1_id, new_child2_id], {'parent_id': new_id})
112 new_struct = self.search(cr, uid, [('parent_id', 'child_of', new_id)])
113 assert len(new_struct) == 4, "After duplication, the new object must have the childs records"
114 old_struct = self.search(cr, uid, [('parent_id', 'child_of', ref('test_categ_0'))])
115 assert len(old_struct) == 4, "After duplication, previous record must have old childs records only"
116 assert (not set(new_struct).intersection(old_struct)), "After duplication, nodes should not be mixed"
118 4. Duplicate the children then reassign them to the new parent (2nd method) and check the parent_store structure.
120 !python {model: res.partner.category}: |
121 new_child1_id = self.copy(cr, uid, ref('test_categ_1'))
122 new_child2_id = self.copy(cr, uid, ref('test_categ_2'))
123 old_struct = self.search(cr, uid, [('parent_id', 'child_of', ref('test_categ_0'))])
124 new_id = self.copy(cr, uid, ref('test_categ_0'), {'child_ids': [(6,0,[new_child1_id, new_child2_id])]})
125 new_struct = self.search(cr, uid, [('parent_id', 'child_of', new_id)])
126 assert len(new_struct) == 4, "After duplication, the new object must have the childs records"
127 old_struct = self.search(cr, uid, [('parent_id', 'child_of', ref('test_categ_0'))])
128 assert len(old_struct) == 4, "After duplication, previous record must have old childs records only"
129 assert (not set(new_struct).intersection(old_struct)), "After duplication, nodes should not be mixed"
131 5. Duplicate the children then reassign them to the new parent (3rd method) and make sure the parent_store structure is still right.
133 !python {model: res.partner.category}: |
134 new_child1_id = self.copy(cr, uid, ref('test_categ_1'))
135 new_child2_id = self.copy(cr, uid, ref('test_categ_2'))
136 new_id = self.copy(cr, uid, ref('test_categ_0'), {'child_ids': []})
137 self.write(cr, uid, [new_id], {'child_ids': [(4,new_child1_id), (4,new_child2_id)]})
138 new_struct = self.search(cr, uid, [('parent_id', 'child_of', new_id)])
139 assert len(new_struct) == 4, "After duplication, the new object must have the childs records"
140 old_struct = self.search(cr, uid, [('parent_id', 'child_of', ref('test_categ_0'))])
141 assert len(old_struct) == 4, "After duplication, previous record must have old childs records only"
142 assert (not set(new_struct).intersection(old_struct)), "After duplication, nodes should not be mixed"
144 6. Restore pool state after the test
146 !python {model: res.partner.category}: |
147 self.pool._init = True
148 self.pool['res.partner.category']._columns['child_ids'].copy = False
151 "Float precision tests: verify that float rounding methods are working correctly via res.currency"
153 !python {model: res.currency}: |
154 from openerp.tools import float_repr
155 from math import log10
156 currency = self.browse(cr, uid, ref('base.EUR'))
157 def try_round(amount, expected, self=self, cr=cr, currency=currency, float_repr=float_repr,
159 digits = max(0,-int(log10(currency.rounding)))
160 result = float_repr(self.round(cr, 1, currency, amount), precision_digits=digits)
161 assert result == expected, 'Rounding error: got %s, expected %s' % (result, expected)
162 try_round(2.674,'2.67')
163 try_round(2.675,'2.68') # in Python 2.7.2, round(2.675,2) gives 2.67
164 try_round(-2.675,'-2.68') # in Python 2.7.2, round(2.675,2) gives 2.67
165 try_round(0.001,'0.00')
166 try_round(-0.001,'-0.00')
167 try_round(0.0049,'0.00') # 0.0049 is closer to 0 than to 0.01, so should round down
168 try_round(0.005,'0.01') # the rule is to round half away from zero
169 try_round(-0.005,'-0.01') # the rule is to round half away from zero
171 def try_zero(amount, expected, self=self, cr=cr, currency=currency):
172 assert self.is_zero(cr, 1, currency, amount) == expected, "Rounding error: %s should be zero!" % amount
173 try_zero(0.01, False)
174 try_zero(-0.01, False)
175 try_zero(0.001, True)
176 try_zero(-0.001, True)
177 try_zero(0.0046, True)
178 try_zero(-0.0046, True)
179 try_zero(2.68-2.675, False) # 2.68 - 2.675 = 0.005 -> rounds to 0.01
180 try_zero(2.68-2.676, True) # 2.68 - 2.675 = 0.004 -> rounds to 0.0
181 try_zero(2.676-2.68, True) # 2.675 - 2.68 = -0.004 -> rounds to -0.0
182 try_zero(2.675-2.68, False) # 2.675 - 2.68 = -0.005 -> rounds to -0.01
184 def try_compare(amount1, amount2, expected, self=self, cr=cr, currency=currency):
185 assert self.compare_amounts(cr, 1, currency, amount1, amount2) == expected, \
186 "Rounding error, compare_amounts(%s,%s) should be %s" % (amount1, amount2, expected)
187 try_compare(0.001, 0.001, 0)
188 try_compare(-0.001, -0.001, 0)
189 try_compare(0.001, 0.002, 0)
190 try_compare(-0.001, -0.002, 0)
191 try_compare(2.675, 2.68, 0)
192 try_compare(2.676, 2.68, 0)
193 try_compare(-2.676, -2.68, 0)
194 try_compare(2.674, 2.68, -1)
195 try_compare(-2.674, -2.68, 1)
196 try_compare(3, 2.68, 1)
197 try_compare(-3, -2.68, -1)
198 try_compare(0.01, 0, 1)
199 try_compare(-0.01, 0, -1)
202 "Float precision tests: verify that float rounding methods are working correctly via tools"
204 !python {model: res.currency}: |
205 from openerp.tools import float_compare, float_is_zero, float_round, float_repr
206 def try_round(amount, expected, precision_digits=3, float_round=float_round, float_repr=float_repr, rounding_method='HALF-UP'):
207 result = float_repr(float_round(amount, precision_digits=precision_digits, rounding_method=rounding_method),
208 precision_digits=precision_digits)
209 assert result == expected, 'Rounding error: got %s, expected %s' % (result, expected)
210 try_round(2.6745, '2.675')
211 try_round(-2.6745, '-2.675')
212 try_round(2.6744, '2.674')
213 try_round(-2.6744, '-2.674')
214 try_round(0.0004, '0.000')
215 try_round(-0.0004, '-0.000')
216 try_round(357.4555, '357.456')
217 try_round(-357.4555, '-357.456')
218 try_round(457.4554, '457.455')
219 try_round(-457.4554, '-457.455')
221 # Try some rounding value with rounding method UP instead of HALF-UP
222 # We use 8.175 because when normalizing 8.175 with precision_digits=3 it gives
223 # us 8175,0000000001234 as value, and if not handle correctly the rounding UP
224 # value will be incorrect (should be 8,175 and not 8,176)
225 try_round(8.175, '8.175', rounding_method='UP')
226 try_round(8.1751, '8.176', rounding_method='UP')
227 try_round(-8.175, '-8.175', rounding_method='UP')
228 try_round(-8.1751, '-8.175', rounding_method='UP')
230 # Extended float range test, inspired by Cloves Almeida's test on bug #882036.
231 fractions = [.0, .015, .01499, .675, .67499, .4555, .4555, .45555]
232 expecteds = ['.00', '.02', '.01', '.68', '.67', '.46', '.456', '.4556']
233 precisions = [2, 2, 2, 2, 2, 2, 3, 4]
234 # Note: max precision for double floats is 53 bits of precision or
235 # 17 significant decimal digits
236 for magnitude in range(7):
237 for i in xrange(len(fractions)):
238 frac, exp, prec = fractions[i], expecteds[i], precisions[i]
240 for x in xrange(0,10000,97):
241 n = x * 10**magnitude
242 f = sign * (n + frac)
243 f_exp = ('-' if f != 0 and sign == -1 else '') + str(n) + exp
244 try_round(f, f_exp, precision_digits=prec)
247 def try_zero(amount, expected, float_is_zero=float_is_zero):
248 assert float_is_zero(amount, precision_digits=3) == expected, "Rounding error: %s should be zero!" % amount
249 try_zero(0.0002, True)
250 try_zero(-0.0002, True)
251 try_zero(0.00034, True)
252 try_zero(0.0005, False)
253 try_zero(-0.0005, False)
254 try_zero(0.0008, False)
255 try_zero(-0.0008, False)
257 def try_compare(amount1, amount2, expected, float_compare=float_compare):
258 assert float_compare(amount1, amount2, precision_digits=3) == expected, \
259 "Rounding error, compare_amounts(%s,%s) should be %s" % (amount1, amount2, expected)
260 try_compare(0.0003, 0.0004, 0)
261 try_compare(-0.0003, -0.0004, 0)
262 try_compare(0.0002, 0.0005, -1)
263 try_compare(-0.0002, -0.0005, 1)
264 try_compare(0.0009, 0.0004, 1)
265 try_compare(-0.0009, -0.0004, -1)
266 try_compare(557.4555, 557.4556, 0)
267 try_compare(-557.4555, -557.4556, 0)
268 try_compare(657.4444, 657.445, -1)
269 try_compare(-657.4444, -657.445, 1)
271 # Rounding to unusual rounding units (e.g. coin values)
272 def try_round(amount, expected, precision_rounding=None, float_round=float_round, float_repr=float_repr):
273 result = float_repr(float_round(amount, precision_rounding=precision_rounding),
275 assert result == expected, 'Rounding error: got %s, expected %s' % (result, expected)
276 try_round(-457.4554, '-457.45', precision_rounding=0.05)
277 try_round(457.444, '457.50', precision_rounding=0.5)
278 try_round(457.3, '455.00', precision_rounding=5)
279 try_round(457.5, '460.00', precision_rounding=5)
280 try_round(457.1, '456.00', precision_rounding=3)
283 "Float precision tests: check that proper rounding is performed for float persistence"
285 !python {model: res.currency}: |
286 currency = self.browse(cr, uid, ref('base.EUR'))
287 res_currency_rate = self.pool.get('res.currency.rate')
288 from openerp.tools import float_compare, float_is_zero, float_round, float_repr
289 def try_roundtrip(value, expected, self=self, cr=cr, currency=currency,
290 res_currency_rate=res_currency_rate):
291 rate_id = res_currency_rate.create(cr, 1, {'name':'2000-01-01',
293 'currency_id': currency.id})
294 rate = res_currency_rate.read(cr, 1, [rate_id], ['rate'])[0]['rate']
295 assert rate == expected, 'Roundtrip error: got %s back from db, expected %s' % (rate, expected)
296 # res.currency.rate uses 6 digits of precision by default
297 try_roundtrip(2.6748955, 2.674896)
298 try_roundtrip(-2.6748955, -2.674896)
299 try_roundtrip(10000.999999, 10000.999999)
300 try_roundtrip(-10000.999999, -10000.999999)
303 "Float precision tests: verify that invalid parameters are forbidden"
305 !python {model: res.currency}: |
306 from openerp.tools import float_compare, float_is_zero, float_round
308 float_is_zero(0.01, precision_digits=3, precision_rounding=0.01)
309 except AssertionError:
312 float_compare(0.01, 0.02, precision_digits=3, precision_rounding=0.01)
313 except AssertionError:
316 float_round(0.01, precision_digits=3, precision_rounding=0.01)
317 except AssertionError:
320 Test res.groups name search
322 !python {model: res.groups}: |
323 all_groups = self.search(cr, uid, [])
324 full_names = [(group.id, group.full_name) for group in self.browse(cr, uid, all_groups)]
325 group_ids = self.search(cr, uid, [('full_name', 'like', '%Sale%')])
326 assert set(group_ids) == set([id for (id, full_name) in full_names if 'Sale' in full_name]), "did not match search for 'Sale'"
327 group_ids = self.search(cr, uid, [('full_name', 'like', '%Technical%')])
328 assert set(group_ids) == set([id for (id, full_name) in full_names if 'Technical' in full_name]), "did not match search for 'Technical'"
329 group_ids = self.search(cr, uid, [('full_name', 'like', '%Sales /%')])
330 assert set(group_ids) == set([id for (id, full_name) in full_names if 'Sales /' in full_name]), "did not match search for 'Sales /'"
331 group_ids = self.search(cr, uid, [('full_name', 'in', ['Administration / Access Rights','Contact Creation'])])
332 assert group_ids, "did not match search for 'Administration / Access Rights' and 'Contact Creation'"