Projet

Général

Profil

0005-misc-split-backoffice_pages-tests.patch

Lauréline Guérin, 18 janvier 2022 15:41

Télécharger (20,2 ko)

Voir les différences:

Subject: [PATCH 5/8] misc: split backoffice_pages tests

 tests/backoffice_pages/test_all.py     | 150 -------------
 tests/backoffice_pages/test_columns.py |  51 +----
 tests/backoffice_pages/test_sort.py    | 278 +++++++++++++++++++++++++
 3 files changed, 283 insertions(+), 196 deletions(-)
 create mode 100644 tests/backoffice_pages/test_sort.py
tests/backoffice_pages/test_all.py
490 490
    resp = app.get('/backoffice/management/form-title/?limit=5&offset=toto', status=400)
491 491

  
492 492

  
493
def test_backoffice_listing_order(pub):
494
    if not pub.is_using_postgresql():
495
        pytest.skip('this requires SQL')
496
        return
497
    create_superuser(pub)
498
    create_environment(pub)
499
    formdef = FormDef.get_by_urlname('form-title')
500
    ids = []
501
    for i, formdata in enumerate(formdef.data_class().select(order_by='id')):
502
        if formdata.status in ('wf-new', 'wf-accepted'):
503
            ids.append(formdata.id)
504
            formdata.receipt_time = datetime.datetime(2015, 1, 1, 10, i).timetuple()
505
            # ordered with odd-numbered ids then even-numbered ids
506
            formdata.evolution[-1].time = datetime.datetime(2015, 2, 1, 10 + i % 2, i).timetuple()
507
            formdata.store()
508

  
509
    inversed_receipt_time_order = list(reversed([str(x) for x in sorted(ids)]))
510
    last_update_time_order = [
511
        str(x) for x in sorted(ids, key=lambda x: int(x) if int(x) % 2 else int(x) + 1000)
512
    ]
513

  
514
    app = login(get_app(pub))
515
    resp = app.get('/backoffice/management/form-title/')
516
    assert resp.text.count('data-link') == 17
517
    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
518
    assert ids == inversed_receipt_time_order
519

  
520
    resp = app.get('/backoffice/management/form-title/?order_by=receipt_time')
521
    assert resp.text.count('data-link') == 17
522
    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
523
    assert ids == list(reversed(inversed_receipt_time_order))
524

  
525
    resp = app.get('/backoffice/management/form-title/?order_by=last_update_time')
526
    assert resp.text.count('data-link') == 17
527
    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
528
    assert ids == last_update_time_order
529

  
530
    resp = app.get('/backoffice/management/form-title/?order_by=-last_update_time')
531
    assert resp.text.count('data-link') == 17
532
    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
533
    assert ids == list(reversed(last_update_time_order))
534

  
535
    if not pub.site_options.has_section('options'):
536
        pub.site_options.add_section('options')
537
        pub.site_options.set('options', 'default-sort-order', '-last_update_time')
538
        with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
539
            pub.site_options.write(fd)
540

  
541
    resp = app.get('/backoffice/management/form-title/')
542
    assert resp.text.count('data-link') == 17
543
    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
544
    assert ids == list(reversed(last_update_time_order))
545

  
546
    # try invalid values
547
    resp = app.get('/backoffice/management/form-title/?order_by=toto.plop', status=400)
548

  
549

  
550 493
def test_backoffice_listing_anonymised(pub):
551 494
    if not pub.is_using_postgresql():
552 495
        pytest.skip('this requires SQL')
......
3354 3297
    }
3355 3298

  
3356 3299

  
3357
def test_backoffice_criticality_in_formdef_listing(pub):
3358
    if not pub.is_using_postgresql():
3359
        pytest.skip('this requires SQL')
3360
        return
3361
    create_user(pub)
3362
    create_environment(pub)
3363

  
3364
    wf = Workflow.get_default_workflow()
3365
    wf.id = '2'
3366
    wf.criticality_levels = [
3367
        WorkflowCriticalityLevel(name='green'),
3368
        WorkflowCriticalityLevel(name='yellow'),
3369
        WorkflowCriticalityLevel(name='red'),
3370
    ]
3371
    wf.store()
3372
    formdef = FormDef.get_by_urlname('form-title')
3373
    formdef.workflow_id = wf.id
3374
    formdef.store()
3375

  
3376
    formdata1, formdata2, formdata3, formdata4 = [
3377
        x for x in formdef.data_class().select() if x.status == 'wf-new'
3378
    ][:4]
3379

  
3380
    formdata1.set_criticality_level(1)
3381
    formdata1.store()
3382
    formdata1_str = '>%s<' % formdata1.get_display_id()
3383
    formdata2.set_criticality_level(2)
3384
    formdata2.store()
3385
    formdata2_str = '>%s<' % formdata2.get_display_id()
3386
    formdata3.set_criticality_level(2)
3387
    formdata3.store()
3388
    formdata3_str = '>%s<' % formdata3.get_display_id()
3389
    formdata4_str = '>%s<' % formdata4.get_display_id()
3390

  
3391
    app = login(get_app(pub))
3392
    resp = app.get('/backoffice/management/form-title/?order_by=-criticality_level&limit=100')
3393
    assert resp.text.index(formdata1_str) > resp.text.index(formdata2_str)
3394
    assert resp.text.index(formdata1_str) > resp.text.index(formdata3_str)
3395
    assert resp.text.index(formdata1_str) < resp.text.index(formdata4_str)
3396

  
3397
    resp = app.get('/backoffice/management/form-title/?order_by=criticality_level&limit=100')
3398
    assert resp.text.index(formdata1_str) < resp.text.index(formdata2_str)
3399
    assert resp.text.index(formdata1_str) < resp.text.index(formdata3_str)
3400
    assert resp.text.index(formdata1_str) > resp.text.index(formdata4_str)
3401

  
3402

  
3403
def test_backoffice_criticality_in_global_listing(pub):
3404
    if not pub.is_using_postgresql():
3405
        pytest.skip('this requires SQL')
3406
        return
3407

  
3408
    create_user(pub)
3409
    create_environment(pub)
3410

  
3411
    wf = Workflow.get_default_workflow()
3412
    wf.id = '2'
3413
    wf.criticality_levels = [
3414
        WorkflowCriticalityLevel(name='green'),
3415
        WorkflowCriticalityLevel(name='yellow'),
3416
        WorkflowCriticalityLevel(name='red'),
3417
    ]
3418
    wf.store()
3419
    formdef = FormDef.get_by_urlname('form-title')
3420
    formdef.workflow_id = wf.id
3421
    formdef.store()
3422

  
3423
    formdata1, formdata2, formdata3, formdata4 = [
3424
        x for x in formdef.data_class().select() if x.status == 'wf-new'
3425
    ][:4]
3426

  
3427
    formdata1.set_criticality_level(1)
3428
    formdata1.store()
3429
    formdata1_str = '>%s<' % formdata1.get_display_id()
3430
    formdata2.set_criticality_level(2)
3431
    formdata2.store()
3432
    formdata2_str = '>%s<' % formdata2.get_display_id()
3433
    formdata3.set_criticality_level(2)
3434
    formdata3.store()
3435
    formdata3_str = '>%s<' % formdata3.get_display_id()
3436
    formdata4_str = '>%s<' % formdata4.get_display_id()
3437

  
3438
    app = login(get_app(pub))
3439
    resp = app.get('/backoffice/management/listing?order_by=-criticality_level&limit=100')
3440
    assert resp.text.index(formdata1_str) > resp.text.index(formdata2_str)
3441
    assert resp.text.index(formdata1_str) > resp.text.index(formdata3_str)
3442
    assert resp.text.index(formdata1_str) < resp.text.index(formdata4_str)
3443

  
3444
    resp = app.get('/backoffice/management/listing?order_by=criticality_level&limit=100')
3445
    assert resp.text.index(formdata1_str) < resp.text.index(formdata2_str)
3446
    assert resp.text.index(formdata1_str) < resp.text.index(formdata3_str)
3447
    assert resp.text.index(formdata1_str) > resp.text.index(formdata4_str)
3448

  
3449

  
3450 3300
def test_backoffice_criticality_formdata_view(pub):
3451 3301
    create_user(pub)
3452 3302
    create_environment(pub)
tests/backoffice_pages/test_columns.py
471 471
    data_class = formdef.data_class()
472 472
    data_class.wipe()
473 473

  
474
    formdata1 = data_class()
475
    formdata1.data = {
474
    formdata = data_class()
475
    formdata.data = {
476 476
        '8': {
477 477
            'data': [{'123': 'blah', '456': card.id, '456_display': card.default_digest}],
478 478
            'schema': {},  # not important here
479 479
        },
480 480
        '8_display': 'blah',
481 481
    }
482
    formdata1.just_created()
483
    formdata1.jump_status('new')
484
    formdata1.store()
482
    formdata.just_created()
483
    formdata.jump_status('new')
484
    formdata.store()
485 485

  
486 486
    app = login(get_app(pub))
487 487
    resp = app.get('/backoffice/management/form-title/')
......
508 508
    formdef.fields[0].max_items = 1
509 509
    formdef.store()
510 510

  
511
    card2 = carddef.data_class()()
512
    card2.data = {
513
        '1': 'Bar2',
514
        '2': 'Foo2',
515
    }
516
    card2.store()
517

  
518
    formdata2 = data_class()
519
    formdata2.data = {
520
        '8': {
521
            'data': [{'123': 'blah', '456': card2.id, '456_display': card2.default_digest}],
522
            'schema': {},  # not important here
523
        },
524
        '8_display': 'blah',
525
    }
526
    formdata2.just_created()
527
    formdata2.jump_status('new')
528
    formdata2.store()
529

  
530
    formdata3 = data_class()
531
    formdata3.data = {
532
        '8': {
533
            'data': [{'123': 'blah'}],
534
            'schema': {},  # not important here
535
        },
536
        '8_display': 'blah',
537
    }
538
    formdata3.just_created()
539
    formdata3.jump_status('new')
540
    formdata3.store()
541

  
542 511
    resp = app.get('/backoffice/management/form-title/')
543 512
    assert [x.text_content() for x in resp.pyquery('#columns-filter label')] == [
544 513
        'Number',
......
556 525
    resp = resp.forms['listing-settings'].submit()
557 526
    assert '<th data-field-sort-key="f8-123"><span>Block / Test</span></th>' in resp
558 527
    assert '<th data-field-sort-key="f8-456"><span>Block / card field</span></th>' in resp
559
    assert resp.text.count('<tr') == 4
560

  
561
    # XXX ordering is done on card id, to be fixed with #60742
562
    resp = app.get('/backoffice/management/form-title/?order_by=f8-456')
563
    ids = [int(x.strip('/')) for x in re.findall(r'data-link="(.*?)"', resp.text)]
564
    assert ids == [formdata1.id, formdata2.id, formdata3.id]
565

  
566
    resp = app.get('/backoffice/management/form-title/?order_by=-f8-456')
567
    ids = [int(x.strip('/')) for x in re.findall(r'data-link="(.*?)"', resp.text)]
568
    assert ids == [formdata3.id, formdata2.id, formdata1.id]
tests/backoffice_pages/test_sort.py
1
import datetime
2
import os
3
import re
4

  
5
import pytest
6

  
7
from wcs import fields
8
from wcs.blocks import BlockDef
9
from wcs.carddef import CardDef
10
from wcs.formdef import FormDef
11
from wcs.qommon.http_request import HTTPRequest
12
from wcs.workflows import Workflow, WorkflowCriticalityLevel
13

  
14
from ..utilities import clean_temporary_pub, create_temporary_pub, get_app, login
15
from .test_all import create_environment, create_superuser, create_user
16

  
17

  
18
@pytest.fixture
19
def pub(request, emails):
20
    pub = create_temporary_pub(sql_mode=True)
21

  
22
    req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
23
    pub.set_app_dir(req)
24
    pub.cfg['identification'] = {'methods': ['password']}
25
    pub.cfg['language'] = {'language': 'en'}
26
    pub.write_cfg()
27
    with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
28
        fd.write(
29
            '''
30
    [api-secrets]
31
    coucou = 1234
32
    '''
33
        )
34

  
35
    return pub
36

  
37

  
38
def teardown_module(module):
39
    clean_temporary_pub()
40

  
41

  
42
def test_backoffice_listing_order(pub):
43
    create_superuser(pub)
44
    create_environment(pub)
45
    formdef = FormDef.get_by_urlname('form-title')
46
    ids = []
47
    for i, formdata in enumerate(formdef.data_class().select(order_by='id')):
48
        if formdata.status in ('wf-new', 'wf-accepted'):
49
            ids.append(formdata.id)
50
            formdata.receipt_time = datetime.datetime(2015, 1, 1, 10, i).timetuple()
51
            # ordered with odd-numbered ids then even-numbered ids
52
            formdata.evolution[-1].time = datetime.datetime(2015, 2, 1, 10 + i % 2, i).timetuple()
53
            formdata.store()
54

  
55
    inversed_receipt_time_order = list(reversed([str(x) for x in sorted(ids)]))
56
    last_update_time_order = [
57
        str(x) for x in sorted(ids, key=lambda x: int(x) if int(x) % 2 else int(x) + 1000)
58
    ]
59

  
60
    app = login(get_app(pub))
61
    resp = app.get('/backoffice/management/form-title/')
62
    assert resp.text.count('data-link') == 17
63
    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
64
    assert ids == inversed_receipt_time_order
65

  
66
    resp = app.get('/backoffice/management/form-title/?order_by=receipt_time')
67
    assert resp.text.count('data-link') == 17
68
    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
69
    assert ids == list(reversed(inversed_receipt_time_order))
70

  
71
    resp = app.get('/backoffice/management/form-title/?order_by=last_update_time')
72
    assert resp.text.count('data-link') == 17
73
    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
74
    assert ids == last_update_time_order
75

  
76
    resp = app.get('/backoffice/management/form-title/?order_by=-last_update_time')
77
    assert resp.text.count('data-link') == 17
78
    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
79
    assert ids == list(reversed(last_update_time_order))
80

  
81
    if not pub.site_options.has_section('options'):
82
        pub.site_options.add_section('options')
83
        pub.site_options.set('options', 'default-sort-order', '-last_update_time')
84
        with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
85
            pub.site_options.write(fd)
86

  
87
    resp = app.get('/backoffice/management/form-title/')
88
    assert resp.text.count('data-link') == 17
89
    ids = [x.strip('/') for x in re.findall(r'data-link="(.*?)"', resp.text)]
90
    assert ids == list(reversed(last_update_time_order))
91

  
92
    # try invalid values
93
    resp = app.get('/backoffice/management/form-title/?order_by=toto.plop', status=400)
94

  
95

  
96
def test_backoffice_criticality_in_formdef_listing_order(pub):
97
    create_user(pub)
98
    create_environment(pub)
99

  
100
    wf = Workflow.get_default_workflow()
101
    wf.id = '2'
102
    wf.criticality_levels = [
103
        WorkflowCriticalityLevel(name='green'),
104
        WorkflowCriticalityLevel(name='yellow'),
105
        WorkflowCriticalityLevel(name='red'),
106
    ]
107
    wf.store()
108
    formdef = FormDef.get_by_urlname('form-title')
109
    formdef.workflow_id = wf.id
110
    formdef.store()
111

  
112
    formdata1, formdata2, formdata3, formdata4 = [
113
        x for x in formdef.data_class().select() if x.status == 'wf-new'
114
    ][:4]
115

  
116
    formdata1.set_criticality_level(1)
117
    formdata1.store()
118
    formdata1_str = '>%s<' % formdata1.get_display_id()
119
    formdata2.set_criticality_level(2)
120
    formdata2.store()
121
    formdata2_str = '>%s<' % formdata2.get_display_id()
122
    formdata3.set_criticality_level(2)
123
    formdata3.store()
124
    formdata3_str = '>%s<' % formdata3.get_display_id()
125
    formdata4_str = '>%s<' % formdata4.get_display_id()
126

  
127
    app = login(get_app(pub))
128
    resp = app.get('/backoffice/management/form-title/?order_by=-criticality_level&limit=100')
129
    assert resp.text.index(formdata1_str) > resp.text.index(formdata2_str)
130
    assert resp.text.index(formdata1_str) > resp.text.index(formdata3_str)
131
    assert resp.text.index(formdata1_str) < resp.text.index(formdata4_str)
132

  
133
    resp = app.get('/backoffice/management/form-title/?order_by=criticality_level&limit=100')
134
    assert resp.text.index(formdata1_str) < resp.text.index(formdata2_str)
135
    assert resp.text.index(formdata1_str) < resp.text.index(formdata3_str)
136
    assert resp.text.index(formdata1_str) > resp.text.index(formdata4_str)
137

  
138

  
139
def test_backoffice_criticality_in_global_listing_order(pub):
140

  
141
    create_user(pub)
142
    create_environment(pub)
143

  
144
    wf = Workflow.get_default_workflow()
145
    wf.id = '2'
146
    wf.criticality_levels = [
147
        WorkflowCriticalityLevel(name='green'),
148
        WorkflowCriticalityLevel(name='yellow'),
149
        WorkflowCriticalityLevel(name='red'),
150
    ]
151
    wf.store()
152
    formdef = FormDef.get_by_urlname('form-title')
153
    formdef.workflow_id = wf.id
154
    formdef.store()
155

  
156
    formdata1, formdata2, formdata3, formdata4 = [
157
        x for x in formdef.data_class().select() if x.status == 'wf-new'
158
    ][:4]
159

  
160
    formdata1.set_criticality_level(1)
161
    formdata1.store()
162
    formdata1_str = '>%s<' % formdata1.get_display_id()
163
    formdata2.set_criticality_level(2)
164
    formdata2.store()
165
    formdata2_str = '>%s<' % formdata2.get_display_id()
166
    formdata3.set_criticality_level(2)
167
    formdata3.store()
168
    formdata3_str = '>%s<' % formdata3.get_display_id()
169
    formdata4_str = '>%s<' % formdata4.get_display_id()
170

  
171
    app = login(get_app(pub))
172
    resp = app.get('/backoffice/management/listing?order_by=-criticality_level&limit=100')
173
    assert resp.text.index(formdata1_str) > resp.text.index(formdata2_str)
174
    assert resp.text.index(formdata1_str) > resp.text.index(formdata3_str)
175
    assert resp.text.index(formdata1_str) < resp.text.index(formdata4_str)
176

  
177
    resp = app.get('/backoffice/management/listing?order_by=criticality_level&limit=100')
178
    assert resp.text.index(formdata1_str) < resp.text.index(formdata2_str)
179
    assert resp.text.index(formdata1_str) < resp.text.index(formdata3_str)
180
    assert resp.text.index(formdata1_str) > resp.text.index(formdata4_str)
181

  
182

  
183
def test_backoffice_block_columns_order(pub):
184
    pub.user_class.wipe()
185
    create_superuser(pub)
186
    pub.role_class.wipe()
187
    role = pub.role_class(name='test')
188
    role.store()
189

  
190
    CardDef.wipe()
191
    carddef = CardDef()
192
    carddef.name = 'foo'
193
    carddef.fields = [
194
        fields.StringField(id='1', label='First Name', type='string', varname='first_name'),
195
        fields.StringField(id='2', label='Last Name', type='string', varname='last_name'),
196
    ]
197
    carddef.digest_templates = {'default': '{{ form_var_first_name }} {{ form_var_last_name }}'}
198
    carddef.store()
199
    carddef.data_class().wipe()
200
    card = carddef.data_class()()
201
    card.data = {
202
        '1': 'Foo',
203
        '2': 'Bar',
204
    }
205
    card.store()
206
    card2 = carddef.data_class()()
207
    card2.data = {
208
        '1': 'Bar2',
209
        '2': 'Foo2',
210
    }
211
    card2.store()
212

  
213
    BlockDef.wipe()
214
    block = BlockDef()
215
    block.name = 'foobar'
216
    block.fields = [
217
        fields.StringField(id='123', required=True, label='Test', type='string'),
218
        fields.ItemField(id='456', label='card field', type='item', data_source={'type': 'carddef:foo'}),
219
    ]
220
    block.store()
221

  
222
    FormDef.wipe()
223
    formdef = FormDef()
224
    formdef.name = 'form-title'
225
    formdef.fields = [
226
        fields.BlockField(id='8', label='Block', type='block:foobar', varname='data', max_items=3),
227
    ]
228
    formdef.workflow_roles = {'_receiver': role.id}
229
    formdef.store()
230

  
231
    data_class = formdef.data_class()
232
    data_class.wipe()
233

  
234
    formdata1 = data_class()
235
    formdata1.data = {
236
        '8': {
237
            'data': [{'123': 'blah', '456': card.id, '456_display': card.default_digest}],
238
            'schema': {},  # not important here
239
        },
240
        '8_display': 'blah',
241
    }
242
    formdata1.just_created()
243
    formdata1.jump_status('new')
244
    formdata1.store()
245

  
246
    formdata2 = data_class()
247
    formdata2.data = {
248
        '8': {
249
            'data': [{'123': 'blah', '456': card2.id, '456_display': card2.default_digest}],
250
            'schema': {},  # not important here
251
        },
252
        '8_display': 'blah',
253
    }
254
    formdata2.just_created()
255
    formdata2.jump_status('new')
256
    formdata2.store()
257

  
258
    formdata3 = data_class()
259
    formdata3.data = {
260
        '8': {
261
            'data': [{'123': 'blah'}],
262
            'schema': {},  # not important here
263
        },
264
        '8_display': 'blah',
265
    }
266
    formdata3.just_created()
267
    formdata3.jump_status('new')
268
    formdata3.store()
269

  
270
    # XXX ordering is done on card id, to be fixed with #60742
271
    app = login(get_app(pub))
272
    resp = app.get('/backoffice/management/form-title/?order_by=f8-456')
273
    ids = [int(x.strip('/')) for x in re.findall(r'data-link="(.*?)"', resp.text)]
274
    assert ids == [formdata1.id, formdata2.id, formdata3.id]
275

  
276
    resp = app.get('/backoffice/management/form-title/?order_by=-f8-456')
277
    ids = [int(x.strip('/')) for x in re.findall(r'data-link="(.*?)"', resp.text)]
278
    assert ids == [formdata3.id, formdata2.id, formdata1.id]
0
-