-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_tui_create_folders.py
More file actions
652 lines (553 loc) · 22.4 KB
/
Copy pathtest_tui_create_folders.py
File metadata and controls
652 lines (553 loc) · 22.4 KB
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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import pytest
import test_utils
from tui_base import TuiBase
from datashuttle.configs import canonical_configs
from datashuttle.tui.app import TuiApp
from datashuttle.tui.screens.create_folder_settings import (
CreateFoldersSettingsScreen,
)
from datashuttle.tui.screens.project_manager import ProjectManagerScreen
class TestTuiCreateFolders(TuiBase):
# -------------------------------------------------------------------------
# General test Create Folders
# -------------------------------------------------------------------------
@pytest.mark.asyncio
@pytest.mark.parametrize("test_multi_input", [True, False])
async def test_create_folders_sub_and_ses(
self, setup_project_paths, test_multi_input
):
"""
Basic test that folders are created as expected through the TUI.
"""
# Define folders to create
if test_multi_input:
sub_text = "sub-001, sub-002"
sub_test_list = ["sub-001", "sub-002"]
ses_text = "ses-001,ses-010"
ses_test_list = ["ses-001", "ses-010"]
else:
sub_text = "sub-001"
sub_test_list = [sub_text]
ses_text = "ses-001"
ses_test_list = [ses_text]
tmp_config_path, tmp_path, project_name = setup_project_paths.values()
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
# Set up the TUI on the 'create' tab, filling the
# input with the subject and session folders to create.
await self.check_and_click_onto_existing_project(
pilot, project_name
)
project = pilot.app.screen.interface.project
await self.turn_off_all_datatype_checkboxes(pilot)
await self.fill_input(
pilot, "#create_folders_subject_input", sub_text
)
await self.create_folders_and_check_output(
pilot,
project,
subs=sub_test_list,
sessions=[],
folder_used=test_utils.get_all_broad_folders_used(value=False),
)
await self.fill_input(
pilot, "#create_folders_session_input", ses_text
)
# Create the folders and check these were properly created.
# Then, iterate through the datatype checkboxes, turning each
# one on and creating the folders, checking they are created successfully.
await self.create_folders_and_check_output(
pilot,
project,
subs=sub_test_list,
sessions=ses_test_list,
folder_used=test_utils.get_all_broad_folders_used(value=False),
)
await self.iterate_and_check_all_datatype_folders(
pilot, subs=sub_test_list, sessions=ses_test_list
)
await pilot.pause()
@pytest.mark.asyncio
async def test_create_folders_formatted_names(self, setup_project_paths):
"""
Test preview tooltips and create folders with _@DATE@ formatting.
The @TO@ key is not tested.
"""
tmp_config_path, tmp_path, project_name = setup_project_paths.values()
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
await self.check_and_click_onto_existing_project(
pilot, project_name
)
import re
# Fill the subject input with names with @DATE@ tags and
# check the tooltip displays the formatted value.
await self.fill_input(
pilot,
"#create_folders_subject_input",
"sub-001_@DATE@, sub-002_@DATE@",
)
# A lot of regexp magic required to dynamically
# process date str but handle the [ escape needed
# to avoid textual processing these as markdown style syntax
sub_1_regexp = r"sub-001_date-\d{8}"
sub_2_regexp = r"sub-002_date-\d{8}"
sub_tooltip_regexp = (
re.escape("Formatted names: \\['")
+ sub_1_regexp
+ re.escape("', '")
+ sub_2_regexp
+ re.escape("']")
)
sub_tooltip = pilot.app.screen.query_one(
"#create_folders_subject_input"
).tooltip
assert re.fullmatch(sub_tooltip_regexp, sub_tooltip)
# Similarly fill the session input and check the tooltip
await self.fill_input(
pilot, "#create_folders_session_input", "ses-001@TO@003_@DATE@"
)
ses_1_regexp = r"ses-001_date-\d{8}"
ses_2_regexp = r"ses-002_date-\d{8}"
ses_3_regexp = r"ses-003_date-\d{8}"
# Build pattern with escaped fixed parts, regex for date parts
ses_tooltip_regexp = (
re.escape("Formatted names: \\['")
+ ses_1_regexp
+ re.escape("', '")
+ ses_2_regexp
+ re.escape("', '")
+ ses_3_regexp
+ re.escape("']")
)
ses_tooltip = pilot.app.screen.query_one(
"#create_folders_session_input"
).tooltip
assert re.fullmatch(ses_tooltip_regexp, ses_tooltip)
# Create the folders and check the formatted
# version are created successfully.
await self.scroll_to_click_pause(
pilot,
"#create_folders_create_folders_button",
)
project = pilot.app.screen.interface.project
sub_level_names = sorted(
list((project.cfg["local_path"] / "rawdata").glob("sub-*"))
)
assert re.fullmatch(sub_1_regexp, sub_level_names[0].stem)
assert re.fullmatch(sub_2_regexp, sub_level_names[1].stem)
for sub in sub_level_names:
ses_level_names = sorted(
list(
(project.cfg["local_path"] / "rawdata" / sub).glob(
"ses-*"
)
)
)
assert re.fullmatch(ses_1_regexp, ses_level_names[0].stem)
assert re.fullmatch(ses_2_regexp, ses_level_names[1].stem)
assert re.fullmatch(ses_3_regexp, ses_level_names[2].stem)
await pilot.pause()
# -------------------------------------------------------------------------
# Test Validation
# -------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_create_folders_bad_validation_tooltips(
self, setup_project_paths
):
"""
Check that correct tooltips are displayed when
various invalid subject or session names are provided.
"""
tmp_config_path, tmp_path, project_name = setup_project_paths.values()
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
await self.check_and_click_onto_existing_project(
pilot, project_name
)
await self.fill_input(
pilot, "#create_folders_subject_input", "sub-abc"
)
assert (
pilot.app.screen.query_one(
"#create_folders_subject_input"
).tooltip
== "BAD_VALUE: The value for prefix sub in name sub-abc is not an integer."
)
await self.fill_input(
pilot, "#create_folders_session_input", "ses-001_ses-001"
)
# The validation is necessarily setup to validate both
# subject and session together, so the ses input displays
# the sub problem.
assert (
pilot.app.screen.query_one(
"#create_folders_session_input"
).tooltip
== "BAD_VALUE: The value for prefix sub in name sub-abc is not an integer."
)
await self.fill_input(
pilot, "#create_folders_subject_input", "sub-001"
)
assert (
pilot.app.screen.query_one(
"#create_folders_subject_input"
).tooltip
== "Formatted names: \['sub-001']"
)
assert (
pilot.app.screen.query_one(
"#create_folders_session_input"
).tooltip
== "DUPLICATE_PREFIX: The name: ses-001_ses-001 of contains more than one instance of the prefix ses."
)
await self.fill_input(
pilot, "#create_folders_session_input", "ses-001"
)
await self.scroll_to_click_pause(
pilot,
"#create_folders_create_folders_button",
)
await self.fill_input(
pilot, "#create_folders_subject_input", "sub-001_@DATE@"
)
assert (
"DUPLICATE_NAME: The prefix for sub-001_date-"
in pilot.app.screen.query_one(
"#create_folders_subject_input"
).tooltip
)
await pilot.pause()
@pytest.mark.asyncio
async def test_validation_error_and_bypass_validation(
self, setup_project_paths
):
"""
Test validation and bypass validation options by
first trying to create an invalid folder name, and
checking an error displays. Next, turn on 'bypass validation'
and check the folders are created despite being invalid.
"""
tmp_config_path, tmp_path, project_name = setup_project_paths.values()
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
await self.check_and_click_onto_existing_project(
pilot, project_name
)
await self.fill_input(
pilot, "#create_folders_subject_input", "sub-abc"
)
await self.fill_input(
pilot, "#create_folders_session_input", "ses-abc"
)
await self.scroll_to_click_pause(
pilot, "#create_folders_create_folders_button"
)
assert (
pilot.app.screen.query_one(
"#messagebox_message_label"
).renderable
== "BAD_VALUE: The value for prefix sub in name sub-abc is not an integer."
)
await self.close_messagebox(pilot)
assert not any(
list(
(
pilot.app.screen.interface.project.cfg["local_path"]
/ "rawdata"
).glob("*")
)
)
await self.scroll_to_click_pause(
pilot, "#create_folders_settings_button"
)
await self.scroll_to_click_pause(
pilot, "#create_folders_settings_bypass_validation_checkbox"
)
await self.scroll_to_click_pause(
pilot, "#create_folders_settings_close_button"
)
await self.scroll_to_click_pause(
pilot, "#create_folders_create_folders_button"
)
assert (
pilot.app.screen.interface.project.cfg["local_path"]
/ "rawdata"
/ "sub-abc"
).is_dir()
assert (
pilot.app.screen.interface.project.cfg["local_path"]
/ "rawdata"
/ "sub-abc"
/ "ses-abc"
).is_dir()
await pilot.pause()
# -------------------------------------------------------------------------
# Test Name Templates
# -------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_name_template_next_sub_or_ses_and_validation(
self, setup_project_paths
):
"""
Test validation and double-click for next sub / ses
values when 'name templates' is set in the 'Settings' window.
"""
tmp_config_path, tmp_path, project_name = setup_project_paths.values()
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
await self.check_and_click_onto_existing_project(
pilot, project_name
)
# Set some name template and check the tooltips
# indicate mismatches correctly
pilot.app.screen.interface.project.set_name_templates(
{"on": True, "sub": "sub-\d\d\d", "ses": "ses-...."}
)
await self.fill_input(
pilot, "#create_folders_subject_input", "sub-0001"
)
assert (
pilot.app.screen.query_one(
"#create_folders_subject_input"
).tooltip
== "TEMPLATE: The name: sub-0001 does not match the template: sub-\\d\\d\\d"
)
# It is expected that sub errors propagate to session input.
# This is because subject and ses must be validated at
# the same time, to check duplicate ses within subjects.
await self.fill_input(
pilot, "#create_folders_session_input", "ses-0001"
)
assert (
pilot.app.screen.query_one(
"#create_folders_session_input"
).tooltip
== "TEMPLATE: The name: sub-0001 does not match the template: sub-\\d\\d\\d"
)
# Try and make the folders, displaying a validation error.
await self.scroll_to_click_pause(
pilot, "#create_folders_create_folders_button"
)
assert pilot.app.screen.query_one(
"#messagebox_message_label"
).renderable == (
"TEMPLATE: The name: sub-0001 does not match the template: sub-\\d\\d\\d"
)
await self.close_messagebox(pilot)
# Now make the correct folders respecting the name templates
await self.fill_input(
pilot, "#create_folders_subject_input", "sub-001"
)
await self.scroll_to_click_pause(
pilot, "#create_folders_create_folders_button"
)
# Now fill in a bad ses name template, because we
# tested sub above but not ses.
await self.fill_input(
pilot, "#create_folders_session_input", "ses-001"
)
assert (
pilot.app.screen.query_one(
"#create_folders_subject_input"
).tooltip
== "Formatted names: \['sub-001']"
)
assert (
pilot.app.screen.query_one(
"#create_folders_session_input"
).tooltip
== "TEMPLATE: The name: ses-001 does not match the template: ses-...."
)
# Finally, double-click the input to suggest next
# ses / sub numbers, which should respect the name templates.
await self.double_click(
pilot, "#create_folders_subject_input", control=True
)
assert (
pilot.app.screen.query_one(
"#create_folders_subject_input"
).value
== "sub-\\d\\d\\d"
)
await self.double_click(
pilot, "#create_folders_session_input", control=True
)
assert (
pilot.app.screen.query_one(
"#create_folders_session_input"
).value
== "ses-...."
)
await self.double_click(
pilot, "#create_folders_subject_input", control=False
)
assert (
pilot.app.screen.query_one(
"#create_folders_subject_input"
).value
== "sub-002"
)
await self.fill_input(
pilot, "#create_folders_subject_input", "sub-001"
)
await self.double_click(
pilot, "#create_folders_session_input", control=False
)
assert (
pilot.app.screen.query_one(
"#create_folders_session_input"
).value
== "ses-0002"
)
await pilot.pause()
@pytest.mark.asyncio
async def test_get_next_sub_and_ses_no_template(self, setup_project_paths):
"""
Test the double click on Input correctly fills with the
next sub or ses (or prefix only when CTRL is pressed).
"""
tmp_config_path, tmp_path, project_name = setup_project_paths.values()
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
await self.setup_existing_project_create_tab_filled_sub_and_ses(
pilot, project_name, create_folders=True
)
# Double-click with CTRL modifier key
await self.double_click(
pilot, "#create_folders_subject_input", control=True
)
assert (
pilot.app.screen.query_one(
"#create_folders_subject_input"
).value
== "sub-"
)
await self.double_click(
pilot, "#create_folders_session_input", control=True
)
assert (
pilot.app.screen.query_one(
"#create_folders_session_input"
).value
== "ses-"
)
# Double click without CTRL modifier key.
await self.double_click(pilot, "#create_folders_subject_input")
assert (
pilot.app.screen.query_one(
"#create_folders_subject_input"
).value
== "sub-002"
)
await self.fill_input(
pilot, "#create_folders_subject_input", "sub-001"
)
await self.double_click(pilot, "#create_folders_session_input")
assert (
pilot.app.screen.query_one(
"#create_folders_session_input"
).value
== "ses-002"
)
await pilot.pause()
# -------------------------------------------------------------------------
# Test Top Level Folders
# -------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_create_folders_settings_top_level_folder(
self, setup_project_paths
):
"""
Check the folders are created in the correct top level
folder when this is changed in the 'Settings' screen.
"""
tmp_config_path, tmp_path, project_name = setup_project_paths.values()
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
# Open the CreateFoldersSettingsScreen
await self.setup_existing_project_create_tab_filled_sub_and_ses(
pilot, project_name, create_folders=False
)
await self.scroll_to_click_pause(
pilot, "#create_folders_settings_button"
)
assert isinstance(pilot.app.screen, CreateFoldersSettingsScreen)
# Check that switching the select updates the
# underlying config (rawdata)
assert (
pilot.app.screen.interface.tui_settings[
"top_level_folder_select"
]["create_tab"]
== "rawdata"
)
assert (
pilot.app.screen.query_one(
"#create_folders_settings_toplevel_select"
).value
== "rawdata"
)
# Switch the select
await pilot.click("#create_folders_settings_toplevel_select")
await pilot.click(
"#create_folders_settings_toplevel_select", offset=(2, 5)
)
await pilot.pause()
assert (
pilot.app.screen.interface.tui_settings[
"top_level_folder_select"
]["create_tab"]
== "derivatives"
)
assert (
pilot.app.screen.query_one(
"#create_folders_settings_toplevel_select"
).value
== "derivatives"
)
# Create folders and check they are created in the derivatives.
await self.scroll_to_click_pause(
pilot, "#create_folders_settings_close_button"
)
assert isinstance(pilot.app.screen, ProjectManagerScreen)
await self.scroll_to_click_pause(
pilot, "#create_folders_create_folders_button"
)
project = pilot.app.screen.interface.project
test_utils.check_folder_tree_is_correct(
base_folder=(project.cfg["local_path"] / "derivatives"),
subs=["sub-001"],
sessions=["ses-001"],
folder_used=test_utils.get_all_broad_folders_used(),
)
await pilot.pause()
# -------------------------------------------------------------------------
# Helpers
# -------------------------------------------------------------------------
async def iterate_and_check_all_datatype_folders(
self, pilot, subs, sessions
):
project = pilot.app.screen.interface.project
folder_used = test_utils.get_all_broad_folders_used(value=False)
for datatype in canonical_configs.get_broad_datatypes():
await self.scroll_to_click_pause(
pilot,
f"#create_{datatype}_checkbox",
)
folder_used[datatype] = True
await self.create_folders_and_check_output(
pilot, project, subs, sessions, folder_used
)
async def create_folders_and_check_output(
self, pilot, project, subs, sessions, folder_used
):
await self.scroll_to_click_pause(
pilot,
"#create_folders_create_folders_button",
)
test_utils.check_folder_tree_is_correct(
base_folder=test_utils.get_top_level_folder_path(project),
subs=subs,
sessions=sessions,
folder_used=folder_used,
)