File: test_field_many2one.py

package info (click to toggle)
tryton-server 7.0.40-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,748 kB
  • sloc: python: 53,502; xml: 5,194; sh: 803; sql: 217; makefile: 28
file content (494 lines) | stat: -rw-r--r-- 16,044 bytes parent folder | download | duplicates (3)
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
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import unittest

from sql import Join

from trytond.model.exceptions import DomainValidationError
from trytond.pool import Pool
from trytond.tests.test_tryton import activate_module, with_transaction


class FieldMany2OneTestCase(unittest.TestCase):
    "Test Field Many2One"

    @classmethod
    def setUpClass(cls):
        activate_module('tests')

    @with_transaction()
    def test_create_id(self):
        "Test create many2one with id"
        pool = Pool()
        Target = pool.get('test.many2one_target')
        Many2One = pool.get('test.many2one')
        target, = Target.create([{}])

        many2one, = Many2One.create([{
                    'many2one': target.id,
                    }])

        self.assertEqual(many2one.many2one, target)

    @with_transaction()
    def test_create_instance(self):
        "Test create many2one with instance"
        pool = Pool()
        Target = pool.get('test.many2one_target')
        Many2One = pool.get('test.many2one')
        target, = Target.create([{}])

        many2one, = Many2One.create([{
                    'many2one': target,
                    }])

        self.assertEqual(many2one.many2one, target)

    @with_transaction()
    def test_create_no_foreign_key(self):
        "Test create many2one without foreign key"
        pool = Pool()
        Target = pool.get('test.many2one_target_storage')
        Many2One = pool.get('test.many2one_no_foreign_key')

        many2one = Many2One(many2one=1)
        many2one.save()

        self.assertEqual(many2one.many2one, Target(1))

    @with_transaction()
    def test_create_with_domain_valid(self):
        "Test create many2one with valid domain"
        pool = Pool()
        Target = pool.get('test.many2one_target')
        Many2One = pool.get('test.many2one_domainvalidation')
        target, = Target.create([{'value': 6}])

        many2one, = Many2One.create([{
                    'many2one': target.id,
                    }])

        self.assertEqual(many2one.many2one, target)

    @with_transaction()
    def test_create_with_domain_invalid(self):
        "Test create many2one with invalid domain"
        pool = Pool()
        Target = pool.get('test.many2one_target')
        Many2One = pool.get('test.many2one_domainvalidation')
        target, = Target.create([{'value': 1}])

        with self.assertRaisesRegex(
                DomainValidationError,
                'The value "%s" for field "many2one" '
                'in ".*" of "Many2One Domain Validation"' % target.rec_name):
            Many2One.create([{
                        'many2one': target.id,
                        }])

    @with_transaction()
    def test_create_with_domain_inactive(self):
        "Test create many2one with domain and inactive target"
        pool = Pool()
        Target = pool.get('test.many2one_target')
        Many2One = pool.get('test.many2one_domainvalidation')
        target, = Target.create([{'value': 6, 'active': False}])

        many2one, = Many2One.create([{
                    'many2one': target.id,
                    }])

        self.assertEqual(many2one.many2one, target)

    @with_transaction()
    def test_search_order_default(self):
        "Test search order by many2one default"
        pool = Pool()
        Target = pool.get('test.many2one_target')
        Many2One = pool.get('test.many2one')
        for value in [5, 3, 2]:
            target, = Target.create([{'value': value}])
            Many2One.create([{'many2one': target}])

        records = Many2One.search([], order=[('many2one', 'ASC')])
        values = [r.many2one.value for r in records]

        self.assertListEqual(values, [2, 3, 5])

    @with_transaction()
    def test_search_order_id(self):
        "Test search order by many2one id"
        pool = Pool()
        Target = pool.get('test.many2one_target')
        Many2One = pool.get('test.many2one')
        target_ids = []
        for value in [5, 3, 2]:
            target, = Target.create([{'value': value}])
            Many2One.create([{'many2one': target}])
            target_ids.append(target.id)
        target_ids.sort()

        records = Many2One.search([], order=[('many2one.id', 'ASC')])
        ids = [r.many2one.id for r in records]

        self.assertListEqual(ids, target_ids)

    @with_transaction()
    def test_search_order_value(self):
        "Test search order by many2one value"
        pool = Pool()
        Target = pool.get('test.many2one_target')
        Many2One = pool.get('test.many2one')
        for value in [5, 3, 2]:
            target, = Target.create([{'value': value}])
            Many2One.create([{'many2one': target}])

        records = Many2One.search([], order=[('many2one.value', 'ASC')])
        values = [r.many2one.value for r in records]

        self.assertListEqual(values, [2, 3, 5])

    @with_transaction()
    def test_search_id(self):
        "Test search on many2one id"
        pool = Pool()
        Target = pool.get('test.many2one_target')
        Many2One = pool.get('test.many2one')

        target, = Target.create([{}])
        record, = Many2One.create([{'many2one': target.id}])

        result = Many2One.search([('many2one', '=', target.id)])
        self.assertListEqual(result, [record])

    @with_transaction()
    def _test_search_join(self, subquery_threshold=1_000):
        from trytond.model.fields import many2one

        pool = Pool()
        Target = pool.get('test.many2one_target')
        Many2One = pool.get('test.many2one')
        target1, target2 = Target.create([
                {'value': 1},
                {'value': 2},
                ])
        many2one1, many2one2 = Many2One.create([
                {'many2one': target1},
                {'many2one': target2},
                ])

        previous = many2one._subquery_threshold
        many2one._subquery_threshold = subquery_threshold
        self.addCleanup(setattr, many2one, '_subquery_threshold', previous)

        many2ones = Many2One.search([
                ('many2one.value', '=', 1),
                ])
        self.assertListEqual(many2ones, [many2one1])

        return Many2One.search([
                ('many2one.value', '=', 1),
                ], query=True)

    def test_search_join(self):
        "Test search by many2one join"
        query = self._test_search_join(0)
        self.assertIsInstance(query.from_[0], Join)

    def test_search_subquery(self):
        "Test search by many2one subquery"
        query = self._test_search_join()
        self.assertNotIsInstance(query.from_[0], Join)

    @with_transaction()
    def test_search_nested_null(self):
        "Test search by null many2one"
        pool = Pool()
        Many2One = pool.get('test.many2one')

        record, = Many2One.create([{'many2one': None}])

        result = Many2One.search([
                ('many2one.value', '=', 1),
                ])
        self.assertListEqual(result, [])

        result = Many2One.search([
                ('many2one.value', '!=', 1),
                ])
        self.assertListEqual(result, [record])

        result = Many2One.search([
                ('many2one.value', '!=', None),
                ])
        self.assertListEqual(result, [])

        result = Many2One.search([
                ('many2one.value', 'not in', [1]),
                ])
        self.assertListEqual(result, [record])

        result = Many2One.search([
                ('many2one.value', 'not in', [1, None]),
                ])
        self.assertListEqual(result, [])

    @with_transaction()
    def test_context_attribute(self):
        "Test context on many2one attribute"
        pool = Pool()
        Many2One = pool.get('test.many2one_context')
        Target = pool.get('test.many2one_context.target')

        target, = Target.create([{}])
        record, = Many2One.create([{
                    'target': target.id,
                    }])

        self.assertEqual(record.target.context, 'foo')

    @with_transaction()
    def test_context_read(self):
        "Test context on many2one read"
        pool = Pool()
        Many2One = pool.get('test.many2one_context')
        Target = pool.get('test.many2one_context.target')

        target, = Target.create([{}])
        record, = Many2One.create([{
                    'target': target.id,
                    }])
        data, = Many2One.read([record.id], ['target.context'])

        self.assertEqual(data['target.']['context'], 'foo')

    @with_transaction()
    def test_context_set(self):
        "Test context on many2one set"
        pool = Pool()
        Many2One = pool.get('test.many2one_context')
        Target = pool.get('test.many2one_context.target')

        target, = Target.create([{}])
        record = Many2One(target=target.id)

        self.assertEqual(record.target.context, 'foo')


class FieldMany2OneTreeTestCase(unittest.TestCase):
    "Test Field Many2One Tree"
    model_name = 'test.many2one_tree'

    @classmethod
    def setUpClass(cls):
        activate_module('tests')

    def create_tree(self, Many2One):
        self.root1, self.root2 = Many2One.create([{}, {}])
        self.first1, self.first2, self.first3, self.first4 = Many2One.create([
                {'many2one': self.root1},
                {'many2one': self.root1},
                {'many2one': self.root2},
                {'many2one': self.root2},
                ])
        self.second1, self.second2, self.second3, self.second4 = (
            Many2One.create([
                {'many2one': self.first1},
                {'many2one': self.first1},
                {'many2one': self.first2},
                {'many2one': self.first2},
                ]))

    @with_transaction()
    def test_search_child_of_root1(self):
        "Test search many2one child of root1"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'child_of', [self.root1.id]),
                ])

        self.assertListEqual(
            sorted(result),
            sorted([self.root1,
                    self.first1, self.first2,
                    self.second1, self.second2, self.second3, self.second4]))

    @with_transaction()
    def test_search_not_child_of_root1(self):
        "Test search many2one not child of root1"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'not child_of', [self.root1.id]),
                ])

        self.assertListEqual(
            sorted(result),
            sorted([self.root2, self.first3, self.first4]))

    @with_transaction()
    def test_search_child_of_second1(self):
        "Test search many2one child of second1"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'child_of', [self.second1.id]),
                ])

        self.assertListEqual(result, [self.second1])

    @with_transaction()
    def test_search_not_child_of_second1(self):
        "Test search many2one not child of second1"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'not child_of', [self.second1.id]),
                ])

        self.assertListEqual(
            sorted(result),
            sorted([self.root1, self.root2,
                    self.first1, self.first2, self.first3, self.first4,
                    self.second2, self.second3, self.second4]))

    @with_transaction()
    def test_search_child_of_empty(self):
        "Test search many2one child of empty"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'child_of', []),
                ])

        self.assertListEqual(result, [])

    @with_transaction()
    def test_search_not_child_of_empty(self):
        "Test search many2one not child of empty"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'not child_of', []),
                ])

        self.assertListEqual(
            sorted(result),
            sorted([self.root1, self.root2,
                    self.first1, self.first2, self.first3, self.first4,
                    self.second1, self.second2, self.second3, self.second4]))

    @with_transaction()
    def test_search_child_of_none(self):
        "Test search many2one child of None"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'child_of', [None]),
                ])

        self.assertListEqual(result, [])

    @with_transaction()
    def test_search_parent_of_root1(self):
        "Test search many2one parent of root1"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'parent_of', [self.root1.id]),
                ])

        self.assertListEqual(result, [self.root1])

    @with_transaction()
    def test_search_not_parent_of_root1(self):
        "Test search many2one not parent of root1"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'not parent_of', [self.root1.id]),
                ])

        self.assertListEqual(
            sorted(result),
            sorted([self.root2,
                    self.first1, self.first2, self.first3, self.first4,
                    self.second1, self.second2, self.second3, self.second4]))

    @with_transaction()
    def test_search_parent_of_second4(self):
        "Test search many2one parent of second4"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'parent_of', [self.second4.id]),
                ])

        self.assertListEqual(
            sorted(result),
            sorted([self.root1, self.first2, self.second4]))

    @with_transaction()
    def test_search_not_parent_of_second4(self):
        "Test search many2one not parent of second4"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'not parent_of', [self.second4.id]),
                ])

        self.assertListEqual(
            sorted(result),
            sorted([self.root2,
                    self.first1, self.first3, self.first4,
                    self.second1, self.second2, self.second3]))

    @with_transaction()
    def test_search_parent_of_empty(self):
        "Test search many2one parent of empty"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'parent_of', []),
                ])

        self.assertListEqual(result, [])

    @with_transaction()
    def test_search_not_parent_of_empty(self):
        "Test search many2one not parent of empty"
        Many2One = Pool().get(self.model_name)
        self.create_tree(Many2One)

        result = Many2One.search([
                ('many2one', 'not parent_of', []),
                ])

        self.assertListEqual(
            sorted(result),
            sorted([self.root1, self.root2,
                    self.first1, self.first2, self.first3, self.first4,
                    self.second1, self.second2, self.second3, self.second4]))


class FieldMany2OneMPTTTestCase(FieldMany2OneTreeTestCase):
    "Test Field Many2One MPTT"
    model_name = 'test.many2one_mptt'


class FieldMany2OnePathTestCase(FieldMany2OneTreeTestCase):
    "Test Field Many2One Path"
    model_name = 'test.many2one_path'