-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_tui_configs.py
More file actions
522 lines (430 loc) · 19.2 KB
/
Copy pathtest_tui_configs.py
File metadata and controls
522 lines (430 loc) · 19.2 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
import copy
from pathlib import Path
from time import monotonic
import pytest
import test_utils
from tui_base import TuiBase
from datashuttle.configs import load_configs
from datashuttle.tui.app import TuiApp
from datashuttle.tui.screens.modal_dialogs import (
SelectDirectoryTreeScreen,
)
from datashuttle.tui.screens.project_manager import ProjectManagerScreen
class TestTuiConfigs(TuiBase):
# -------------------------------------------------------------------------
# Test New Project Configs
# -------------------------------------------------------------------------
@pytest.mark.asyncio
@pytest.mark.parametrize("kwargs_set", [1, 2])
async def test_make_new_project_configs(
self,
empty_project_paths,
kwargs_set,
):
"""Check the ConfigsContent when making a new project. This contains
many widgets shared with the ConfigsContent on the tab page, however
also includes an additional information banner and input for the
project name.
Here check these widgets are display correctly, and fill them. Next
check the config widgets are empty, then fill the widgets, save,
and check the interface.project and saved configs match the new
settings.
"""
tmp_config_path, tmp_path, project_name = empty_project_paths.values()
kwargs = {
"local_path": (tmp_path / "local" / project_name).as_posix(),
# not used in TUI, set to `make_config_file` defaults.
"central_path": (tmp_path / "central" / project_name).as_posix(),
}
if kwargs_set == 1:
kwargs.update(
{
"connection_method": "local_filesystem",
"central_host_id": None,
"central_host_username": None,
}
)
elif kwargs_set == 2:
kwargs.update(
{
"connection_method": "ssh",
"central_host_id": "@test.host.id",
"central_host_username": "test_username",
}
)
Path(kwargs["local_path"]).parent.mkdir(parents=True)
Path(kwargs["central_path"]).parent.mkdir(parents=True)
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
# Select a new project, check NewProjectScreen is
# displayed correctly.
await self.scroll_to_click_pause(
pilot, "#mainwindow_new_project_button"
)
# Get the ConfigsContent and check all configs are displayed
# correctly. `check_new_project_configs` checks empty defaults
# are displayed, then updates with the kwargs and checks.
configs_content = pilot.app.screen.query_one(
"#new_project_configs_content"
)
await self.check_new_project_configs(
pilot, project_name, configs_content, kwargs
)
# Save the configs and check the correct messages are shown.
await self.scroll_to_click_pause(
pilot,
"#configs_save_configs_button",
)
# if SSH is set, then the config window remains up and the
# 'setup ssh' button is enabled. Otherwise, the screen
# will automatically move to the project page.
if kwargs["connection_method"] == "ssh":
assert (
pilot.app.screen.query_one(
"#messagebox_message_label"
).renderable
== "A datashuttle project has now been created.\n\n Next, "
"setup the SSH connection. Once complete, navigate to "
"the 'Main Menu' and proceed to the project page, "
"where you will be able to create and transfer "
"project folders."
)
await self.close_messagebox(pilot)
assert (
pilot.app.screen.query_one(
"#configs_setup_ssh_connection_button"
).visible
is True
)
else:
assert (
pilot.app.screen.query_one(
"#messagebox_message_label"
).renderable
== "A datashuttle project has now been created.\n\n "
"Next proceed to the project page, where you will "
"be able to create and transfer project folders."
)
await self.close_messagebox(pilot)
assert (
pilot.app.screen.query_one(
"#configs_go_to_project_screen_button"
).visible
is True
)
await self.scroll_to_click_pause(
pilot, "#configs_go_to_project_screen_button"
)
assert isinstance(pilot.app.screen, ProjectManagerScreen)
project = pilot.app.screen.interface.project
assert (
pilot.app.screen.interface.project.project_name == project_name
)
# After saving, check all configs are correct on the DataShuttle
# instance as well as the stored configs.
test_utils.check_configs(
project,
kwargs,
tmp_config_path / project_name / "config.yaml",
)
await pilot.pause()
# -------------------------------------------------------------------------
# Test Existing Project Configs
# -------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_update_config_on_project_manager_screen(
self, setup_project_paths
):
"""Test the ConfigsContent on the project manager tab screen.
The project is set up in the fixture, navigate to the project page.
Check that the default configs are displayed. Change all the configs,
save, and check these are updated on the config file and on the
`project` stored in `interface`.
Next, exit out of the project with the "Main Menu" button, go back
into the project and check the new configs are displayed.
"""
tmp_config_path, tmp_path, project_name = setup_project_paths.values()
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
# Navigate to the existing project and click onto the
# configs tab.
await self.check_and_click_onto_existing_project(
pilot, project_name
)
await self.switch_tab(pilot, "configs")
configs_content = pilot.app.screen.query_one(
"#tabscreen_configs_content"
)
# Now get the default datashuttle configs, and check they match
# those displayed on the ConfigsContent.
project_cfg = copy.deepcopy(pilot.app.screen.interface.project.cfg)
load_configs.convert_str_and_pathlib_paths(
project_cfg, "path_to_str"
)
await self.check_configs_widgets_match_configs(
configs_content, project_cfg
)
# Now we make some new settings, and set the ConfigsContent.
# Make sure they are all different to the existing configs,
# then save and check the configs on the DataShuttle instance
# and file are updated.
local_path = tmp_path / f"some-random-path/{project_name}"
central_path = tmp_path / f"some-random-path2/{project_name}"
local_path.mkdir(parents=True)
central_path.mkdir(parents=True)
new_kwargs = {
"local_path": local_path.as_posix(),
"central_path": central_path.as_posix(),
"connection_method": "ssh",
"central_host_id": "random_host",
"central_host_username": "random_username",
}
for key in new_kwargs:
# The purpose is to update to completely new configs
assert new_kwargs[key] != project_cfg[key]
await self.set_configs_content_widgets(pilot, new_kwargs)
await self.check_configs_widgets_match_configs(
configs_content, new_kwargs
)
await self.scroll_to_click_pause(
pilot,
"#configs_save_configs_button",
)
assert (
pilot.app.screen.query_one(
"#messagebox_message_label"
).renderable
== "Configs saved."
)
await self.close_messagebox(pilot)
test_utils.check_configs(
pilot.app.screen.interface.project,
new_kwargs,
tmp_config_path / project_name / "config.yaml",
)
# Finally, use "Main Menu" button to go back to the home screen,
# navigate back to the project and check the new configs are now
# displayed.
await self.scroll_to_click_pause(pilot, "#all_main_menu_buttons")
assert pilot.app.screen.id == "_default"
await self.check_and_click_onto_existing_project(
pilot, project_name
)
await self.switch_tab(pilot, "transfer")
configs_content = pilot.app.screen.query_one(
"#tabscreen_configs_content"
)
await self.check_configs_widgets_match_configs(
configs_content, new_kwargs
)
await pilot.pause()
# -------------------------------------------------------------------------
# Test the config page widgets
# -------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_configs_select_path(self, monkeypatch):
"""Test the 'Select' buttons / DirectoryTree on the ConfigsContent.
These are used to select folders that are filled into the Input.
Open the select dialog, select a folder, check the path is
filled into the Input. There is one for both local
and central path.
When SSH is selected, the central path 'Select' should be disabled,
as it only makes sense to choose this for local filesystem transfer.
"""
self.monkeypatch_print(monkeypatch)
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
# Select the page and ConfigsContent for setting up new project
await self.scroll_to_click_pause(
pilot, "#mainwindow_new_project_button"
)
configs_content = pilot.app.screen.query_one(
"#new_project_configs_content"
)
local_path_button = pilot.app.screen.query_one(
"#configs_local_path_select_button"
)
central_path_button = pilot.app.screen.query_one(
"#configs_central_path_select_button"
)
# For central and local path selects, click the button, select
# the first folder from the DirectoryTree and check the input is filled.
for select_button, path_input_id in zip(
[local_path_button, central_path_button],
["#configs_local_path_input", "#configs_central_path_input"],
):
await self.scroll_to_click_pause(
pilot,
f"#{select_button.id}",
)
assert isinstance(pilot.app.screen, SelectDirectoryTreeScreen)
tree = pilot.app.screen.query_one(
"#select_directory_tree_directory_tree"
)
root_path = tree.root.data.path
pilot.app.screen.click_info.prev_click_widget_id = tree.id
pilot.app.screen.click_info.prev_click_time = monotonic()
pilot.app.screen.on_directory_tree_directory_selected(
tree.DirectorySelected(tree.root, root_path)
)
await pilot.pause()
assert (
configs_content.query_one(path_input_id).value
== root_path.as_posix()
)
await pilot.pause()
# Check the central path only is not displayed in SSH mode.
assert local_path_button.display is True
assert central_path_button.display is True
await self.scroll_to_click_pause(pilot, "#configs_ssh_radiobutton")
assert local_path_button.display is True
assert central_path_button.display is False
await pilot.pause()
@pytest.mark.asyncio
async def test_bad_configs_screen_input(self, empty_project_paths):
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
# Select a new project, check NewProjectScreen is displayed correctly.
await self.scroll_to_click_pause(
pilot, "#mainwindow_new_project_button"
)
await self.fill_input(pilot, "#configs_name_input", "a@@")
await self.fill_input(pilot, "#configs_local_path_input", "a")
await self.fill_input(pilot, "#configs_central_path_input", "b")
await self.scroll_to_click_pause(
pilot, "#configs_save_configs_button"
)
assert (
pilot.app.screen.query_one(
"#messagebox_message_label"
).renderable
== "The project name must contain alphanumeric characters only."
)
await pilot.pause()
# -------------------------------------------------------------------------
# Test project name is number
# -------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_project_name_is_number(self, empty_project_paths):
"""
Make a project that has a number name, and check the project screen
can be loaded.
"""
app = TuiApp()
async with app.run_test(size=self.tui_size()) as pilot:
# Set up a project with a numerical project name
project_name = "123"
await self.scroll_to_click_pause(
pilot, "#mainwindow_new_project_button"
)
await self.fill_input(pilot, "#configs_name_input", project_name)
await self.fill_input(pilot, "#configs_local_path_input", "a")
await self.fill_input(pilot, "#configs_central_path_input", "b")
await self.scroll_to_click_pause(
pilot, "#configs_save_configs_button"
)
# Go back to main menu and load the project screen
await self.close_messagebox(pilot)
await self.scroll_to_click_pause(pilot, "#all_main_menu_buttons")
await self.check_and_click_onto_existing_project(
pilot, project_name
)
await pilot.pause()
# -------------------------------------------------------------------------
# Helpers
# -------------------------------------------------------------------------
async def check_configs_widgets_match_configs(
self, configs_content, kwargs
):
"""Check that the widgets of the TUI configs match those found
in `kwargs`.
"""
# Local Path ----------------------------------------------------------
assert (
configs_content.query_one("#configs_local_path_input").value
== kwargs["local_path"]
)
# Connection Method ---------------------------------------------------
label = (
"SSH"
if kwargs["connection_method"] == "ssh"
else "Local Filesystem"
)
assert (
configs_content.query_one(
"#configs_connect_method_radioset"
).pressed_button.label._text
== label
)
if kwargs["connection_method"] == "ssh":
# Central Host ID -------------------------------------------------
assert (
configs_content.query_one(
"#configs_central_host_id_input"
).value
== kwargs["central_host_id"]
)
# Central Host Username -------------------------------------------
assert (
configs_content.query_one(
"#configs_central_host_username_input"
).value
== kwargs["central_host_username"]
)
# Central Path --------------------------------------------------------
assert (
configs_content.query_one("#configs_central_path_input").value
== kwargs["central_path"]
)
async def set_configs_content_widgets(self, pilot, kwargs):
"""Given a dict of options that can be set on the configs TUI
in kwargs, set all configs widgets according to kwargs.
"""
# Local Path ----------------------------------------------------------
await self.fill_input(
pilot, "#configs_local_path_input", kwargs["local_path"]
)
# Connection Method ---------------------------------------------------
if kwargs["connection_method"] == "ssh":
await self.scroll_to_click_pause(pilot, "#configs_ssh_radiobutton")
# Central Host ID -------------------------------------------------
await self.fill_input(
pilot,
"#configs_central_host_id_input",
kwargs["central_host_id"],
)
# Central Host Username -------------------------------------------
await self.fill_input(
pilot,
"#configs_central_host_username_input",
kwargs["central_host_username"],
)
# Central Path --------------------------------------------------------
await self.fill_input(
pilot, "#configs_central_path_input", kwargs["central_path"]
)
async def check_new_project_configs(
self, pilot, project_name, configs_content, kwargs
):
"""Check the configs displayed on the TUI match those found in `kwargs`.
Also, check the widgets unique to ConfigsContent on the
configs selection for a new project.
"""
# Project Name --------------------------------------------------------
await self.fill_input(pilot, "#configs_name_input", project_name)
assert (
configs_content.query_one("#configs_name_input").value
== project_name
)
# Shared Config Widgets -----------------------------------------------
default_kwargs = {
"local_path": "",
"central_path": "",
"connection_method": "local_filesystem",
}
await self.check_configs_widgets_match_configs(
configs_content, default_kwargs
)
await self.set_configs_content_widgets(pilot, kwargs)
await self.check_configs_widgets_match_configs(configs_content, kwargs)
await pilot.pause()