forked from dask/dask-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_options.py
593 lines (448 loc) · 15.9 KB
/
test_options.py
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
import pytest
import yaml
import dask_gateway.options as client_options
import dask_gateway_server.options as server_options
from dask_gateway_server.options import FrozenAttrDict
from dask_gateway_server.models import User
def test_string():
field = "field_name"
default = "default_value"
target = "target_name"
label = "Field Name"
# Default values
s_opt = server_options.String(field)
assert s_opt.default == ""
assert s_opt.label == field
assert s_opt.target == field
# Specified values propogate
s_opt = server_options.String(field, default=default, target=target, label=label)
assert s_opt.default == default
assert s_opt.target == target
assert s_opt.label == label
def check(opt):
assert opt.validate("hello") == "hello"
with pytest.raises(TypeError):
opt.validate(1)
# Server-side validation
check(s_opt)
# Serialization works
spec = s_opt.json_spec()
c_opt = client_options.Field._from_spec(spec)
assert isinstance(c_opt, client_options.String)
assert c_opt.value == default
assert c_opt.label == label
# Client-side validation
check(c_opt)
def test_bool():
field = "field_name"
default = True
target = "target_name"
label = "Field Name"
# Default values
s_opt = server_options.Bool(field)
assert s_opt.default is False
assert s_opt.label == field
assert s_opt.target == field
# Specified values propogate
s_opt = server_options.Bool(field, default=default, target=target, label=label)
assert s_opt.default == default
assert s_opt.target == target
assert s_opt.label == label
def check(opt):
assert opt.validate(True) is True
with pytest.raises(TypeError):
opt.validate(1)
# Server-side validation
check(s_opt)
# Serialization works
spec = s_opt.json_spec()
c_opt = client_options.Field._from_spec(spec)
assert isinstance(c_opt, client_options.Bool)
assert c_opt.value == default
assert c_opt.label == label
# Client-side validation
check(c_opt)
def test_int():
field = "field_name"
default = 10
target = "target_name"
label = "Field Name"
min = 10
max = 20
# Default values
s_opt = server_options.Integer(field)
assert s_opt.default == 0
assert s_opt.label == field
assert s_opt.target == field
assert s_opt.min is None
assert s_opt.max is None
# Specified values propogate
s_opt = server_options.Integer(
field, default=default, target=target, label=label, min=min, max=max
)
assert s_opt.default == default
assert s_opt.target == target
assert s_opt.label == label
assert s_opt.min == min
assert s_opt.max == max
def check(opt):
assert opt.validate(15) == 15
assert opt.validate(10) == 10
assert opt.validate(20) == 20
with pytest.raises(TypeError):
opt.validate("1")
with pytest.raises(ValueError):
opt.validate(min - 1)
with pytest.raises(ValueError):
opt.validate(max + 1)
# Server-side validation
check(s_opt)
# Serialization works
spec = s_opt.json_spec()
c_opt = client_options.Field._from_spec(spec)
assert isinstance(c_opt, client_options.Integer)
assert c_opt.value == default
assert c_opt.label == label
assert s_opt.min == min
assert s_opt.max == max
# Client-side validation
check(c_opt)
def test_float():
field = "field_name"
default = 10.0
target = "target_name"
label = "Field Name"
min = 10
max = 20
# Default values
s_opt = server_options.Float(field)
assert s_opt.default == 0
assert s_opt.label == field
assert s_opt.target == field
assert s_opt.min is None
assert s_opt.max is None
# Specified values propogate
s_opt = server_options.Float(
field, default=default, target=target, label=label, min=min, max=max
)
assert s_opt.default == default
assert s_opt.target == target
assert s_opt.label == label
assert s_opt.min == min
assert s_opt.max == max
assert isinstance(s_opt.min, float)
assert isinstance(s_opt.max, float)
assert isinstance(s_opt.default, float)
def check(opt):
assert opt.validate(15.5) == 15.5
assert opt.validate(10) == 10.0
assert opt.validate(20) == 20.0
with pytest.raises(TypeError):
opt.validate("1")
with pytest.raises(ValueError):
opt.validate(min - 1)
with pytest.raises(ValueError):
opt.validate(max + 1)
# Server-side validation
check(s_opt)
# Serialization works
spec = s_opt.json_spec()
c_opt = client_options.Field._from_spec(spec)
assert isinstance(c_opt, client_options.Float)
assert c_opt.value == default
assert c_opt.label == label
assert s_opt.min == min
assert s_opt.max == max
# Client-side validation
check(c_opt)
def test_select():
field = "field_name"
default = "banana"
options = ["apple", "banana", ("orange", 1)]
target = "target_name"
label = "Field Name"
# Default values
s_opt = server_options.Select(field, options)
assert s_opt.default == "apple"
assert s_opt.label == field
assert s_opt.target == field
# Specified values propogate
s_opt = server_options.Select(
field, options, default=default, target=target, label=label
)
assert s_opt.default == default
assert s_opt.target == target
assert s_opt.label == label
def check(opt):
assert opt.validate("apple") == "apple"
assert opt.validate("orange") == "orange"
with pytest.raises(TypeError):
opt.validate(1)
with pytest.raises(ValueError):
opt.validate("grape")
# Server-side validation
check(s_opt)
# Bad server-side constructors
with pytest.raises(TypeError):
server_options.Select(field, 1)
with pytest.raises(TypeError):
server_options.Select(field, [1, 2, 3])
with pytest.raises(ValueError):
server_options.Select(field, [])
# Serialization works
spec = s_opt.json_spec()
c_opt = client_options.Field._from_spec(spec)
assert isinstance(c_opt, client_options.Select)
assert c_opt.value == default
assert c_opt.options == ("apple", "banana", "orange")
assert c_opt.label == label
# Client-side validation
check(c_opt)
# Bad client-side constructors
with pytest.raises(TypeError):
client_options.Select(field, default, options=1)
with pytest.raises(TypeError):
client_options.Select(field, default, options=[1, 2, 3])
with pytest.raises(ValueError):
client_options.Select(field, default, options=[])
def test_mapping():
field = "field_name"
default = {"some": "default"}
target = "target_name"
label = "Field Name"
# Default values
s_opt = server_options.Mapping(field)
assert s_opt.default == {}
assert s_opt.label == field
assert s_opt.target == field
# Specified values propogate
s_opt = server_options.Mapping(field, default=default, target=target, label=label)
assert s_opt.default is default
assert s_opt.target == target
assert s_opt.label == label
# default is copied when returned to server
assert s_opt.get_default() == default
assert s_opt.get_default() is not default
def check(opt):
value = {"a": {"nested": "mapping"}, "b": 2}
assert opt.validate(value) == value
with pytest.raises(TypeError):
opt.validate(1)
# Server-side validation
check(s_opt)
# Serialization works
spec = s_opt.json_spec()
c_opt = client_options.Field._from_spec(spec)
assert isinstance(c_opt, client_options.Mapping)
assert c_opt.value == default
assert c_opt.label == label
# Client-side validation
check(c_opt)
def test_frozen_attr_dict():
d = {"valid": 1, "not valid": 2, "for": 3}
ad = FrozenAttrDict(d)
# getattr
assert ad.valid == 1
with pytest.raises(AttributeError):
ad.missing
# getitem
assert ad["not valid"] == 2
# setattr
with pytest.raises(Exception):
ad.valid = 1
# setitem
with pytest.raises(TypeError):
ad["valid"] = 1
# len
assert len(ad) == len(d)
# iter
assert list(ad) == list(d)
# coversion
assert dict(ad) == d
# dir
tabs = dir(ad)
assert "valid" in tabs
assert "not valid" not in tabs
assert "for" not in tabs
@pytest.fixture
def server_opts():
return server_options.Options(
server_options.Integer("integer_field", 1, target="integer_field2"),
server_options.Float("float_field", 2.5, min=2, max=4),
server_options.Select("select_field", [("a", 5), "b", ("c", 10)]),
)
def test_server_options_get_specification(server_opts):
spec = server_opts.get_specification()
c_opts = client_options.Options._from_spec(spec)
assert set(c_opts) == {"integer_field", "float_field", "select_field"}
def test_server_options_parse_options(server_opts):
res = server_opts.parse_options({})
assert res == {"integer_field": 1, "float_field": 2.5, "select_field": "a"}
res = server_opts.parse_options({"select_field": "c", "float_field": 3})
assert res == {"integer_field": 1, "float_field": 3.0, "select_field": "c"}
with pytest.raises(TypeError):
server_opts.parse_options(None)
with pytest.raises(ValueError):
server_opts.parse_options({"extra1": 1, "extra2": 2, "integer_field": 3})
def test_server_options_get_configuration(server_opts):
options = {"integer_field": 2}
sol = {"integer_field2": 2, "float_field": 2.5, "select_field": 5}
user = User("alice")
# Default handler
config = server_opts.get_configuration(options, user)
assert config == sol
# Custom handler, no user
def handler(options):
assert isinstance(options, FrozenAttrDict)
assert dict(options) == sol
return {"a": 1}
server_opts.handler = handler
assert server_opts.get_configuration(options, user) == {"a": 1}
# Custom handler, with user
def handler(options, user):
assert isinstance(options, FrozenAttrDict)
assert dict(options) == sol
return {"a": 1, "username": user.name}
server_opts.handler = handler
assert server_opts.get_configuration(options, user) == {"a": 1, "username": "alice"}
@pytest.fixture
def client_opts():
return client_options.Options(
client_options.Integer("worker_cores", 1, min=1, max=4, label="Worker Cores"),
client_options.Float(
"worker_memory", 1, min=1, max=8, label="Worker Memory (GB)"
),
client_options.Integer("scheduler_cores", 1, label="Scheduler Cores"),
client_options.Float("scheduler_memory", 1, label="Scheduler Memory (GB)"),
client_options.Select(
"environment", "basic", options=["basic", "ml"], label="Conda Environment"
),
client_options.Bool("keep_logs", False, label="Keep Logs"),
client_options.String("queue", "default", label="Queue"),
client_options.Mapping(
"environ", {"default": "vals"}, label="Environment variables"
),
)
def get_widget_for_field(options, field):
pytest.importorskip("ipywidgets")
widget = options._widget()
for n, f in enumerate(options._fields):
if f == field:
break
else:
raise ValueError("No field %s" % field)
# This is fragile to the formatting of the widgets
input = widget.children[1].children[3 * n + 1]
status = widget.children[1].children[3 * n + 2]
return input, status
def test_client_options(client_opts):
# Key and attribute access works
client_opts.worker_cores = 2
assert client_opts.worker_cores == 2
client_opts["worker_cores"] = 1
assert client_opts.worker_cores == 1
# Cannot delete options
with pytest.raises(TypeError):
del client_opts.worker_cores
with pytest.raises(TypeError):
del client_opts["worker_cores"]
# Cannot get/set unknown options
with pytest.raises(AttributeError):
client_opts.unknown
with pytest.raises(AttributeError):
client_opts.unknown = 1
with pytest.raises(KeyError):
client_opts["unknown"]
with pytest.raises(KeyError):
client_opts["unknown"] = 1
# Values are type checked
with pytest.raises(TypeError):
client_opts.worker_cores = 1.5
assert client_opts.worker_cores == 1
with pytest.raises(ValueError):
client_opts.environment = "unknown"
assert client_opts.environment == "basic"
# Tab completion works
tabs = dir(client_opts)
assert "worker_cores" in tabs
assert "queue" in tabs
# Other mapping methods
assert len(client_opts) == len(client_opts._fields)
assert len(dict(client_opts)) == len(client_opts._fields)
def test_client_options_pretty_printing(client_opts):
pretty = pytest.importorskip("IPython.lib.pretty")
res = pretty.pretty(client_opts)
assert "worker_cores" in res
assert "Options<" in res
def test_client_options_widget(client_opts):
ipywidgets = pytest.importorskip("ipywidgets")
widget = client_opts._widget()
assert isinstance(widget, ipywidgets.Box)
# widget is cached
assert widget is client_opts._widget()
def check_opt_widget(opts, field, val1, val2, *bad_values):
widget, _ = get_widget_for_field(opts, field)
assert widget.value == getattr(opts, field)
# widget to opts
widget.value = val1
assert getattr(opts, field) == val1
# opts to widget
setattr(opts, field, val2)
assert widget.value == val2
for val in bad_values:
with pytest.raises(Exception):
setattr(opts, field, val)
# Unchanged
assert getattr(opts, field) == val2
assert widget.value == val2
def test_client_bool_widget(client_opts):
check_opt_widget(client_opts, "keep_logs", True, False, "bad value")
def test_client_string_widget(client_opts):
check_opt_widget(client_opts, "queue", "queue-one", "queue-two", 1)
def test_client_select_widget(client_opts):
check_opt_widget(client_opts, "environment", "ml", "basic", "unknown", 1)
def test_client_integer_widget(client_opts):
check_opt_widget(client_opts, "worker_cores", 2, 3, 10, -1, "bad value")
check_opt_widget(client_opts, "scheduler_cores", 2, 3, "bad value")
def test_client_float_widget(client_opts):
check_opt_widget(client_opts, "worker_memory", 2.5, 3.5, 100, -1, "bad value")
check_opt_widget(client_opts, "scheduler_memory", 2.5, 3.5, "bad value")
def test_client_mapping_widget(client_opts):
pytest.importorskip("ipywidgets")
textarea, status = get_widget_for_field(client_opts, "environ")
val1 = {"hello": "world"}
val2 = {"some": {"nested": "values"}, "other": 2}
text2 = yaml.safe_dump(val2)
test_cases = [({}, ""), (val1, yaml.safe_dump(val1)), (val2, text2)]
# widget to opts
for val, text in test_cases:
textarea.value = text
assert client_opts.environ == val
assert status.value == ""
# opts to widget
for val, text in test_cases:
client_opts.environ = val
assert textarea.value == text
assert status.value == ""
# Validation in opts
for val in [None, 1, [1, 2, 3], {"a": object()}]:
with pytest.raises(Exception):
client_opts.environ = val
# Unchanged
assert client_opts.environ == val2
assert textarea.value == text2
assert status.value == ""
# Validation in widget
for text in ["{", "1", "[1, 2, 3]"]:
textarea.value = text
# Unchanged
assert client_opts.environ == val2
assert status.value != ""
# Resets status once valid again
textarea.value = "HELLO: WORLD"
assert client_opts.environ == {"HELLO": "WORLD"}
assert status.value == ""
# Whitespace is ignored
textarea.value = " "
assert client_opts.environ == {}
assert status.value == ""