1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
import itertools
import pstats
from cProfile import Profile
from odoo import fields, Command
from odoo.tests import common
class CreatorCase(common.TransactionCase):
model_name = False
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.model = None
def setUp(self):
super().setUp()
self.model = self.env[self.model_name]
def make(self, value, context=None):
return self.model.with_context(**(context or {})).create({'value': value})
def export(self, value, fields=('value',), context=None):
record = self.make(value, context=context)
self.env.invalidate_all()
return record._export_rows([f.split('/') for f in fields])
class test_xids(CreatorCase):
model_name = 'export.boolean'
def test_no_module(self):
record = self.make(True)
# add existing xid without module
self.env['ir.model.data'].create(
{
'module': '',
'name': 'x',
'model': self.model_name,
'res_id': record.id,
}
)
self.env.invalidate_all()
self.assertEqual(record._export_rows([['id'], ['value']]), [['x', True]])
class test_boolean_field(CreatorCase):
model_name = 'export.boolean'
def test_true(self):
self.assertEqual(self.export(True), [[True]])
def test_false(self):
"""``False`` value to boolean fields is unique in being exported as a
(unicode) string, not a boolean
"""
self.assertEqual(self.export(False), [[False]])
class test_integer_field(CreatorCase):
model_name = 'export.integer'
def test_empty(self):
self.assertEqual(self.model.search([]).ids, [], "Test model should have no records")
def test_0(self):
self.assertEqual(self.export(0), [[0]])
def test_basic_value(self):
self.assertEqual(self.export(42), [[42]])
def test_negative(self):
self.assertEqual(self.export(-32), [[-32]])
def test_huge(self):
self.assertEqual(self.export(2**31 - 1), [[2147483647]])
class test_float_field(CreatorCase):
model_name = 'export.float'
def test_0(self):
self.assertEqual(self.export(0.0), [[0.0]])
def test_epsilon(self):
self.assertEqual(self.export(0.000000000027), [[0.000000000027]])
def test_negative(self):
self.assertEqual(self.export(-2.42), [[-2.42]])
def test_positive(self):
self.assertEqual(self.export(47.36), [[47.36]])
def test_big(self):
self.assertEqual(self.export(87654321.4678), [[87654321.4678]])
class test_decimal_field(CreatorCase):
model_name = 'export.decimal'
def test_0(self):
self.assertEqual(self.export(0.0), [[0.0]])
def test_epsilon(self):
"""epsilon gets sliced to 0 due to precision"""
self.assertEqual(self.export(0.000000000027), [[0.0]])
def test_negative(self):
self.assertEqual(self.export(-2.42), [[-2.42]])
def test_positive(self):
self.assertEqual(self.export(47.36), [[47.36]])
def test_big(self):
self.assertEqual(self.export(87654321.4678), [[87654321.468]])
class test_string_field(CreatorCase):
model_name = 'export.string.bounded'
def test_empty(self):
self.assertEqual(self.export(""), [['']])
def test_within_bounds(self):
self.assertEqual(self.export("foobar"), [["foobar"]])
def test_out_of_bounds(self):
self.assertEqual(self.export("C for Sinking, Java for Drinking, Smalltalk for Thinking. ...and Power to the Penguin!"), [["C for Sinking, J"]])
class test_unbound_string_field(CreatorCase):
model_name = 'export.string'
def test_empty(self):
self.assertEqual(self.export(""), [['']])
def test_small(self):
self.assertEqual(self.export("foobar"), [["foobar"]])
def test_big(self):
self.assertEqual(
self.export(
"We flew down weekly to meet with IBM, but they "
"thought the way to measure software was the amount "
"of code we wrote, when really the better the "
"software, the fewer lines of code."
),
[
[
"We flew down weekly to meet with IBM, but they thought the "
"way to measure software was the amount of code we wrote, "
"when really the better the software, the fewer lines of "
"code."
]
],
)
class test_text(CreatorCase):
model_name = 'export.text'
def test_empty(self):
self.assertEqual(self.export(""), [['']])
def test_small(self):
self.assertEqual(self.export("foobar"), [["foobar"]])
def test_big(self):
self.assertEqual(
self.export("So, `bind' is `let' and monadic programming is equivalent to programming in the A-normal form. That is indeed all there is to monads"),
[["So, `bind' is `let' and monadic programming is equivalent to programming in the A-normal form. That is indeed all there is to monads"]],
)
def test_numeric(self):
self.assertEqual(self.export(42), [["42"]])
class test_date(CreatorCase):
model_name = 'export.date'
def test_empty(self):
self.assertEqual(self.export(False), [['']])
def test_basic(self):
self.assertEqual(self.export('2011-11-07'), [[fields.Date.from_string('2011-11-07')]])
class test_datetime(CreatorCase):
model_name = 'export.datetime'
def test_empty(self):
self.assertEqual(self.export(False), [['']])
def test_basic(self):
"""Export value with no TZ set on the user"""
self.env.user.write({'tz': False})
self.assertEqual(self.export('2011-11-07 21:05:48'), [[fields.Datetime.from_string('2011-11-07 21:05:48')]])
def test_tz(self):
"""Export converts the value in the user's TZ
.. note:: on the other hand, export uses user lang for display_name
"""
self.assertEqual(self.export('2011-11-07 21:05:48', context={'tz': 'Pacific/Norfolk'}), [[fields.Datetime.from_string('2011-11-08 08:35:48')]])
class test_selection(CreatorCase):
model_name = 'export.selection'
translations_fr = [
("Qux", "toto"),
("Bar", "titi"),
("Foo", "tete"),
]
def test_empty(self):
self.assertEqual(self.export(False), [['']])
def test_value(self):
"""selections export the *label* for their value"""
self.assertEqual(self.export('2'), [["Bar"]])
def test_localized_export(self):
self.env['res.lang']._activate_lang('fr_FR')
ir_field = self.env['ir.model.fields']._get('export.selection', 'value')
selection = ir_field.selection_ids
translations = dict(self.translations_fr)
for sel_fr, sel in zip(selection.with_context(lang='fr_FR'), selection):
sel_fr.name = translations.get(sel.name, sel_fr.name)
self.assertEqual(self.export('2', context={'lang': 'fr_FR'}), [['titi']])
class test_selection_function(CreatorCase):
model_name = 'export.selection.function'
def test_empty(self):
self.assertEqual(self.export(False), [['']])
def test_value(self):
# selection functions export the *value* itself
self.assertEqual(self.export('1'), [['1']])
self.assertEqual(self.export('3'), [['3']])
self.assertEqual(self.export('0'), [['0']])
class test_m2o(CreatorCase):
model_name = 'export.many2one'
def test_empty(self):
self.assertEqual(self.export(False), [['']])
def test_basic(self):
"""Exported value is the display_name of the related object"""
record = self.env['export.integer'].create({'value': 42})
self.assertEqual(self.export(record.id), [[record.display_name]])
def test_path(self):
"""Can recursively export fields of m2o via path"""
record = self.env['export.integer'].create({'value': 42})
self.assertEqual(self.export(record.id, fields=['value/.id', 'value/value']), [[str(record.id), 42]])
def test_external_id(self):
record = self.env['export.integer'].create({'value': 42})
# Expecting the m2o target model name in the external id,
# not this model's name
self.assertRegex(self.export(record.id, fields=['value/id'])[0][0], '__export__.export_integer_%d_[0-9a-f]{8}' % record.id)
def test_identical(self):
m2o = self.env['export.integer'].create({'value': 42}).id
records = self.make(m2o) | self.make(m2o) | self.make(m2o) | self.make(m2o)
self.env.invalidate_all()
xp = [r[0] for r in records._export_rows([['value', 'id']])]
self.assertEqual(len(xp), 4)
self.assertRegex(xp[0], '__export__.export_integer_%d_[0-9a-f]{8}' % m2o)
self.assertEqual(set(xp), {xp[0]})
class test_reference(CreatorCase):
model_name = 'export.reference'
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.ref_record = cls.env['export.integer'].create({'value': 42})
cls.ref_value = f"{cls.ref_record._name},{cls.ref_record.id}"
def test_empty(self):
self.assertEqual(self.export(False), [['']])
def test_import_compat(self):
self.assertEqual(self.export(self.ref_value), [[self.ref_value]])
def test_false_import_compat(self):
self.assertEqual(self.export(self.ref_value, context={'import_compat': False}), [[self.ref_record.display_name]])
class test_o2m(CreatorCase):
model_name = 'export.one2many'
commands = [
Command.create({'value': 4, 'str': 'record1'}),
Command.create({'value': 42, 'str': 'record2'}),
Command.create({'value': 36, 'str': 'record3'}),
Command.create({'value': 4, 'str': 'record4'}),
Command.create({'value': 13, 'str': 'record5'}),
]
names = ['export.one2many.child:%d' % d['value'] for c, _, d in commands]
def test_empty(self):
self.assertEqual(self.export(False), [['']])
def test_single(self):
self.assertEqual(
self.export([Command.create({'value': 42})]),
# display_name result
[['export.one2many.child:42']],
)
def test_single_subfield(self):
self.assertEqual(self.export([Command.create({'value': 42})], fields=['value', 'value/value']), [['export.one2many.child:42', 42]])
def test_integrate_one_in_parent(self):
self.assertEqual(self.export([Command.create({'value': 42})], fields=['const', 'value/value']), [[4, 42]])
def test_multiple_records(self):
self.assertEqual(
self.export(self.commands, fields=['const', 'value/value']),
[
[4, 4],
['', 42],
['', 36],
['', 4],
['', 13],
],
)
def test_multiple_records_name(self):
self.assertEqual(
self.export(self.commands, fields=['const', 'value']),
[
[4, 'export.one2many.child:4'],
['', 'export.one2many.child:42'],
['', 'export.one2many.child:36'],
['', 'export.one2many.child:4'],
['', 'export.one2many.child:13'],
],
)
def test_multiple_records_id(self):
export = self.export(self.commands, fields=['const', 'value/.id'])
records = self.env['export.one2many.child'].search([])
self.assertEqual(
export,
[
[4, str(records[0].id)],
['', str(records[1].id)],
['', str(records[2].id)],
['', str(records[3].id)],
['', str(records[4].id)],
],
)
def test_multiple_records_with_name_before(self):
self.assertEqual(
self.export(self.commands, fields=['const', 'value', 'value/value']),
[
[4, 'export.one2many.child:4', 4],
['', 'export.one2many.child:42', 42],
['', 'export.one2many.child:36', 36],
['', 'export.one2many.child:4', 4],
['', 'export.one2many.child:13', 13],
],
)
def test_multiple_records_with_name_after(self):
self.assertEqual(
self.export(self.commands, fields=['const', 'value/value', 'value']),
[
[4, 4, 'export.one2many.child:4'],
['', 42, 'export.one2many.child:42'],
['', 36, 'export.one2many.child:36'],
['', 4, 'export.one2many.child:4'],
['', 13, 'export.one2many.child:13'],
],
)
def test_multiple_subfields_neighbour(self):
self.assertEqual(
self.export(self.commands, fields=['const', 'value/str', 'value/value']),
[
[4, 'record1', 4],
['', 'record2', 42],
['', 'record3', 36],
['', 'record4', 4],
['', 'record5', 13],
],
)
def test_multiple_subfields_separated(self):
self.assertEqual(
self.export(self.commands, fields=['value/str', 'const', 'value/value']),
[
['record1', 4, 4],
['record2', '', 42],
['record3', '', 36],
['record4', '', 4],
['record5', '', 13],
],
)
class test_o2m_multiple(CreatorCase):
model_name = 'export.one2many.multiple'
def make(self, value=None, **values):
if value is not None:
values['value'] = value
return self.model.create(values)
def export(self, value=None, fields=('child1', 'child2'), context=None, **values):
record = self.make(value, **values)
return record._export_rows([f.split('/') for f in fields])
def test_empty(self):
self.assertEqual(self.export(child1=False, child2=False), [['', '']])
def test_single_per_side(self):
self.assertEqual(self.export(child1=False, child2=[Command.create({'value': 42})]), [['', 'export.one2many.child.2:42']])
self.assertEqual(self.export(child1=[Command.create({'value': 43})], child2=False), [['export.one2many.child.1:43', '']])
self.assertEqual(self.export(child1=[Command.create({'value': 43})], child2=[Command.create({'value': 42})]), [['export.one2many.child.1:43', 'export.one2many.child.2:42']])
def test_single_integrate_subfield(self):
fields = ['const', 'child1/value', 'child2/value']
self.assertEqual(self.export(child1=False, child2=[Command.create({'value': 42})], fields=fields), [[36, '', 42]])
self.assertEqual(self.export(child1=[Command.create({'value': 43})], child2=False, fields=fields), [[36, 43, '']])
self.assertEqual(self.export(child1=[Command.create({'value': 43})], child2=[Command.create({'value': 42})], fields=fields), [[36, 43, 42]])
def test_multiple(self):
"""With two "concurrent" o2ms, exports the first line combined, then
exports the rows for the first o2m, then the rows for the second o2m.
"""
fields = ['const', 'child1/value', 'child2/value']
child1 = [Command.create({'value': v, 'str': 'record%.02d' % index}) for index, v in zip(itertools.count(), [4, 42, 36, 4, 13])]
child2 = [Command.create({'value': v, 'str': 'record%.02d' % index}) for index, v in zip(itertools.count(10), [8, 12, 8, 55, 33, 13])]
self.assertEqual(
self.export(child1=child1, child2=False, fields=fields),
[
[36, 4, ''],
['', 42, ''],
['', 36, ''],
['', 4, ''],
['', 13, ''],
],
)
self.assertEqual(
self.export(child1=False, child2=child2, fields=fields),
[
[36, '', 8],
['', '', 12],
['', '', 8],
['', '', 55],
['', '', 33],
['', '', 13],
],
)
self.assertEqual(
self.export(child1=child1, child2=child2, fields=fields),
[
[36, 4, 8],
['', 42, ''],
['', 36, ''],
['', 4, ''],
['', 13, ''],
['', '', 12],
['', '', 8],
['', '', 55],
['', '', 33],
['', '', 13],
],
)
class test_m2m(CreatorCase):
model_name = 'export.many2many'
commands = [
Command.create({'value': 4, 'str': 'record000'}),
Command.create({'value': 42, 'str': 'record001'}),
Command.create({'value': 36, 'str': 'record010'}),
Command.create({'value': 4, 'str': 'record011'}),
Command.create({'value': 13, 'str': 'record100'}),
]
names = ['export.many2many.other:%d' % d['value'] for c, _, d in commands]
def test_empty(self):
self.assertEqual(self.export(False), [['']])
def test_single(self):
self.assertEqual(
self.export([Command.create({'value': 42})]),
# display_name result
[['export.many2many.other:42']],
)
def test_single_subfield(self):
self.assertEqual(self.export([Command.create({'value': 42})], fields=['value', 'value/value'], context={'import_compat': False}), [['export.many2many.other:42', 42]])
def test_integrate_one_in_parent(self):
self.assertEqual(self.export([Command.create({'value': 42})], fields=['const', 'value/value'], context={'import_compat': False}), [[4, 42]])
def test_multiple_records(self):
self.assertEqual(
self.export(self.commands, fields=['const', 'value/value'], context={'import_compat': False}),
[
[4, 4],
['', 42],
['', 36],
['', 4],
['', 13],
],
)
def test_multiple_records_name(self):
self.assertEqual(
self.export(self.commands, fields=['const', 'value']),
[
[4, 'export.many2many.other:4,export.many2many.other:42,export.many2many.other:36,export.many2many.other:4,export.many2many.other:13'],
],
)
self.assertEqual(
self.export(self.commands, fields=['const', 'value'], context={'import_compat': False}),
[
[4, 'export.many2many.other:4'],
['', 'export.many2many.other:42'],
['', 'export.many2many.other:36'],
['', 'export.many2many.other:4'],
['', 'export.many2many.other:13'],
],
)
def test_multiple_records_subfield(self):
r = self.make(self.commands)
xid = (
self.env['ir.model.data']
.create(
{
'name': 'whopwhopwhop',
'module': '__t__',
'model': r._name,
'res_id': r.id,
}
)
.complete_name
)
[
self.env['ir.model.data']
.create(
{
'name': sub.str,
'module': '__t__',
'model': sub._name,
'res_id': sub.id,
}
)
.complete_name
for sub in r.value
]
self.env.invalidate_all()
self.assertEqual(r._export_rows([['value', 'id']]), [['__t__.record000,__t__.record001,__t__.record010,__t__.record011,__t__.record100']])
self.assertEqual(r.with_context(import_compat=True)._export_rows([['value', 'id']]), [['__t__.record000,__t__.record001,__t__.record010,__t__.record011,__t__.record100']])
self.assertEqual(r.with_context(import_compat=True)._export_rows([['value'], ['value', 'id']]), [['', '__t__.record000,__t__.record001,__t__.record010,__t__.record011,__t__.record100']])
self.assertEqual(
r.with_context(import_compat=False)._export_rows([['id'], ['value', 'id'], ['value', 'value']]),
[[xid, '__t__.record000', 4], ['', '__t__.record001', 42], ['', '__t__.record010', 36], ['', '__t__.record011', 4], ['', '__t__.record100', 13]],
)
self.assertEqual(
r.with_context(import_compat=False)._export_rows([['id'], ['value', 'value'], ['value', 'id']]),
[[xid, 4, '__t__.record000'], ['', 42, '__t__.record001'], ['', 36, '__t__.record010'], ['', 4, '__t__.record011'], ['', 13, '__t__.record100']],
)
class test_function(CreatorCase):
model_name = 'export.function'
def test_value(self):
"""Exports value normally returned by accessing the function field"""
self.assertEqual(self.export(42), [[3]])
@common.tagged('-standard', 'bench')
class test_xid_perfs(common.TransactionCase):
def setUp(self):
super().setUp()
self.profile = Profile()
@self.addCleanup
def _dump():
stats = pstats.Stats(self.profile)
stats.strip_dirs()
stats.sort_stats('cumtime')
stats.print_stats(20)
self.profile = None
def test_basic(self):
Model = self.env['export.integer']
for i in range(10000):
Model.create({'value': i})
self.env.invalidate_all()
records = Model.search([])
self.profile.runcall(records._export_rows, [['id'], ['value']])
def test_m2o_single(self):
rid = self.env['export.integer'].create({'value': 42}).id
Model = self.env['export.many2one']
for _ in range(10000):
Model.create({'value': rid})
self.env.invalidate_all()
records = Model.search([])
self.profile.runcall(records._export_rows, [['id'], ['value', 'id']])
def test_m2o_each(self):
Model = self.env['export.many2one']
Integer = self.env['export.integer']
for i in range(10000):
Model.create({'value': Integer.create({'value': i}).id})
self.env.invalidate_all()
records = Model.search([])
self.profile.runcall(records._export_rows, [['id'], ['value', 'id']])
|