File: test_valid.py

package info (click to toggle)
python-confuse 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 652 kB
  • sloc: python: 3,065; makefile: 112; sh: 8
file content (690 lines) | stat: -rw-r--r-- 25,515 bytes parent folder | download
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
import enum
import os
import unittest
from collections.abc import Mapping, Sequence
from typing import Any

import pytest

import confuse

from . import _root


class ValidConfigTest(unittest.TestCase):
    def test_validate_simple_dict(self):
        config = _root({"foo": 5})
        valid = config.get({"foo": confuse.Integer()})
        assert valid["foo"] == 5

    def test_default_value(self):
        config = _root({})
        valid = config.get({"foo": confuse.Integer(8)})
        assert valid["foo"] == 8

    def test_undeclared_key_raises_keyerror(self):
        config = _root({"foo": 5})
        valid = config.get({"foo": confuse.Integer()})
        with pytest.raises(KeyError):
            valid["bar"]

    def test_undeclared_key_ignored_from_input(self):
        config = _root({"foo": 5, "bar": 6})
        valid = config.get({"foo": confuse.Integer()})
        with pytest.raises(KeyError):
            valid["bar"]

    def test_int_template_shortcut(self):
        config = _root({"foo": 5})
        valid = config.get({"foo": int})
        assert valid["foo"] == 5

    def test_int_default_shortcut(self):
        config = _root({})
        valid = config.get({"foo": 9})
        assert valid["foo"] == 9

    def test_attribute_access(self):
        config = _root({"foo": 5})
        valid = config.get({"foo": confuse.Integer()})
        assert valid.foo == 5

    def test_missing_required_value_raises_error_on_validate(self):
        config = _root({})
        with pytest.raises(confuse.NotFoundError):
            config.get({"foo": confuse.Integer()})

    def test_none_as_default(self):
        config = _root({})
        valid = config.get({"foo": confuse.Integer(None)})
        assert valid["foo"] is None

    def test_wrong_type_raises_error_on_validate(self):
        config = _root({"foo": "bar"})
        with pytest.raises(confuse.ConfigTypeError):
            config.get({"foo": confuse.Integer()})

    def test_validate_individual_value(self):
        config = _root({"foo": 5})
        valid = config["foo"].get(confuse.Integer())
        assert valid == 5

    def test_nested_dict_template(self):
        config = _root({"foo": {"bar": 9}})
        valid = config.get({"foo": {"bar": confuse.Integer()}})
        assert valid["foo"]["bar"] == 9

    def test_nested_attribute_access(self):
        config = _root({"foo": {"bar": 8}})
        valid = config.get({"foo": {"bar": confuse.Integer()}})
        assert valid.foo.bar == 8


class AsTemplateTest(unittest.TestCase):
    def test_plain_int_as_template(self):
        typ = confuse.as_template(int)
        assert isinstance(typ, confuse.Integer)
        assert typ.default == confuse.REQUIRED

    def test_concrete_int_as_template(self):
        typ = confuse.as_template(2)
        assert isinstance(typ, confuse.Integer)
        assert typ.default == 2

    def test_plain_string_as_template(self):
        typ = confuse.as_template(str)
        assert isinstance(typ, confuse.String)
        assert typ.default == confuse.REQUIRED

    def test_concrete_string_as_template(self):
        typ = confuse.as_template("foo")
        assert isinstance(typ, confuse.String)
        assert typ.default == "foo"

    def test_dict_as_template(self):
        typ = confuse.as_template({"key": 9})
        assert isinstance(typ, confuse.MappingTemplate)
        assert isinstance(typ.subtemplates["key"], confuse.Integer)
        assert typ.subtemplates["key"].default == 9

    def test_nested_dict_as_template(self):
        typ = confuse.as_template({"outer": {"inner": 2}})
        assert isinstance(typ, confuse.MappingTemplate)
        assert isinstance(typ.subtemplates["outer"], confuse.MappingTemplate)
        assert isinstance(
            typ.subtemplates["outer"].subtemplates["inner"], confuse.Integer
        )
        assert typ.subtemplates["outer"].subtemplates["inner"].default == 2

    def test_list_as_template(self):
        typ: confuse.OneOf[Any] = confuse.as_template(list())
        assert isinstance(typ, confuse.OneOf)
        assert typ.default == confuse.REQUIRED

    def test_set_as_template(self):
        typ: confuse.Choice[Any] = confuse.as_template(set())
        assert isinstance(typ, confuse.Choice)

    def test_enum_type_as_template(self):
        typ = confuse.as_template(enum.Enum)
        assert isinstance(typ, confuse.Choice)

    def test_float_type_as_tempalte(self):
        typ = confuse.as_template(float)
        assert isinstance(typ, confuse.Number)
        assert typ.default == confuse.REQUIRED

    def test_concrete_float_as_template(self):
        typ = confuse.as_template(2.0)
        assert isinstance(typ, confuse.Number)
        assert typ.default == 2.0

    def test_none_as_template(self):
        typ = confuse.as_template(None)
        assert type(typ) is confuse.Template
        assert typ.default is None

    def test_required_as_template(self):
        typ = confuse.as_template(confuse.REQUIRED)
        assert type(typ) is confuse.Template
        assert typ.default == confuse.REQUIRED

    def test_dict_type_as_template(self):
        typ = confuse.as_template(dict)
        assert isinstance(typ, confuse.TypeTemplate)
        assert typ.typ == Mapping
        assert typ.default == confuse.REQUIRED

    def test_list_type_as_template(self):
        typ = confuse.as_template(list)
        assert isinstance(typ, confuse.TypeTemplate)
        assert typ.typ == Sequence
        assert typ.default == confuse.REQUIRED

    def test_set_type_as_template(self):
        typ = confuse.as_template(set)
        assert isinstance(typ, confuse.TypeTemplate)
        assert typ.typ is set
        assert typ.default == confuse.REQUIRED

    def test_other_type_as_template(self):
        class MyClass:
            pass

        typ = confuse.as_template(MyClass)
        assert isinstance(typ, confuse.TypeTemplate)
        assert typ.typ == MyClass
        assert typ.default == confuse.REQUIRED


class StringTemplateTest(unittest.TestCase):
    def test_validate_string(self):
        config = _root({"foo": "bar"})
        valid = config.get({"foo": confuse.String()})
        assert valid["foo"] == "bar"

    def test_string_default_value(self):
        config = _root({})
        valid = config.get({"foo": confuse.String("baz")})
        assert valid["foo"] == "baz"

    def test_pattern_matching(self):
        config = _root({"foo": "bar", "baz": "zab"})
        valid = config.get({"foo": confuse.String(pattern="^ba.$")})
        assert valid["foo"] == "bar"
        with pytest.raises(confuse.ConfigValueError):
            config.get({"baz": confuse.String(pattern="!")})

    def test_string_template_shortcut(self):
        config = _root({"foo": "bar"})
        valid = config.get({"foo": str})
        assert valid["foo"] == "bar"

    def test_string_default_shortcut(self):
        config = _root({})
        valid = config.get({"foo": "bar"})
        assert valid["foo"] == "bar"

    def test_check_string_type(self):
        config = _root({"foo": 5})
        with pytest.raises(confuse.ConfigTypeError):
            config.get({"foo": confuse.String()})


class NumberTest(unittest.TestCase):
    def test_validate_int_as_number(self):
        config = _root({"foo": 2})
        valid = config["foo"].get(confuse.Number())
        assert isinstance(valid, int)
        assert valid == 2

    def test_validate_float_as_number(self):
        config = _root({"foo": 3.0})
        valid = config["foo"].get(confuse.Number())
        assert isinstance(valid, float)
        assert valid == 3.0

    def test_validate_string_as_number(self):
        config = _root({"foo": "bar"})
        with pytest.raises(confuse.ConfigTypeError):
            config["foo"].get(confuse.Number())


class ChoiceTest(unittest.TestCase):
    def test_validate_good_choice_in_list(self):
        config = _root({"foo": 2})
        valid = config["foo"].get(confuse.Choice([1, 2, 4, 8, 16]))
        assert valid == 2

    def test_validate_bad_choice_in_list(self):
        config = _root({"foo": 3})
        with pytest.raises(confuse.ConfigValueError):
            config["foo"].get(confuse.Choice([1, 2, 4, 8, 16]))

    def test_validate_good_choice_in_dict(self):
        config = _root({"foo": 2})
        valid = config["foo"].get(confuse.Choice({2: "two", 4: "four"}))
        assert valid == "two"

    def test_validate_bad_choice_in_dict(self):
        config = _root({"foo": 3})
        with pytest.raises(confuse.ConfigValueError):
            config["foo"].get(confuse.Choice({2: "two", 4: "four"}))


class OneOfTest(unittest.TestCase):
    def test_default_value(self):
        config = _root({})
        valid = config["foo"].get(confuse.OneOf([], default="bar"))
        assert valid == "bar"

    def test_validate_good_choice_in_list(self):
        config = _root({"foo": 2})
        valid: str | int = config["foo"].get(
            confuse.OneOf(
                [
                    confuse.String(),
                    confuse.Integer(),
                ]
            )
        )
        assert valid == 2

    def test_validate_first_good_choice_in_list(self):
        config = _root({"foo": 3.14})
        valid: str | int = config["foo"].get(
            confuse.OneOf(
                [
                    confuse.Integer(),
                    confuse.Number(),
                ]
            )
        )
        assert valid == 3

    def test_validate_no_choice_in_list(self):
        config = _root({"foo": None})
        with pytest.raises(confuse.ConfigValueError):
            config["foo"].get(
                confuse.OneOf(
                    [
                        confuse.String(),
                        confuse.Integer(),
                    ]
                )
            )

    def test_validate_bad_template(self):
        class BadTemplate:
            pass

        config = _root({})
        with pytest.raises(ValueError, match="cannot convert to template"):
            config.get(confuse.OneOf([BadTemplate()]))
        del BadTemplate


class StrSeqTest(unittest.TestCase):
    def test_string_list(self):
        config = _root({"foo": ["bar", "baz"]})
        valid = config["foo"].get(confuse.StrSeq())
        assert valid == ["bar", "baz"]

    def test_string_tuple(self):
        config = _root({"foo": ("bar", "baz")})
        valid = config["foo"].get(confuse.StrSeq())
        assert valid == ["bar", "baz"]

    def test_whitespace_separated_string(self):
        config = _root({"foo": "bar   baz"})
        valid = config["foo"].get(confuse.StrSeq())
        assert valid == ["bar", "baz"]

    def test_invalid_type(self):
        config = _root({"foo": 9})
        with pytest.raises(confuse.ConfigTypeError):
            config["foo"].get(confuse.StrSeq())

    def test_invalid_sequence_type(self):
        config = _root({"foo": ["bar", 2126]})
        with pytest.raises(confuse.ConfigTypeError):
            config["foo"].get(confuse.StrSeq())


class FilenameTest(unittest.TestCase):
    def test_default_value(self):
        config = _root({})
        valid = config["foo"].get(confuse.Filename("foo/bar"))
        assert valid == "foo/bar"

    def test_default_none(self):
        config = _root({})
        valid = config["foo"].get(confuse.Filename(None))
        assert valid is None

    def test_missing_required_value(self):
        config = _root({})
        with pytest.raises(confuse.NotFoundError):
            config["foo"].get(confuse.Filename())

    def test_filename_relative_to_working_dir(self):
        config = _root({"foo": "bar"})
        valid = config["foo"].get(confuse.Filename(cwd="/dev/null"))
        assert valid == os.path.realpath("/dev/null/bar")

    def test_filename_relative_to_sibling(self):
        config = _root({"foo": "/", "bar": "baz"})
        valid = config.get(
            {"foo": confuse.Filename(), "bar": confuse.Filename(relative_to="foo")}
        )
        assert valid.foo == os.path.realpath("/")
        assert valid.bar == os.path.realpath("/baz")

    def test_filename_working_dir_overrides_sibling(self):
        config = _root({"foo": "bar"})
        valid = config.get(
            {"foo": confuse.Filename(cwd="/dev/null", relative_to="baz")}
        )
        assert valid.foo == os.path.realpath("/dev/null/bar")

    def test_filename_relative_to_sibling_with_recursion(self):
        config = _root({"foo": "/", "bar": "r", "baz": "z"})
        with pytest.raises(confuse.ConfigTemplateError):
            config.get(
                {
                    "foo": confuse.Filename(relative_to="bar"),
                    "bar": confuse.Filename(relative_to="baz"),
                    "baz": confuse.Filename(relative_to="foo"),
                }
            )

    def test_filename_relative_to_self(self):
        config = _root({"foo": "bar"})
        with pytest.raises(confuse.ConfigTemplateError):
            config.get({"foo": confuse.Filename(relative_to="foo")})

    def test_filename_relative_to_sibling_needs_siblings(self):
        config = _root({"foo": "bar"})
        with pytest.raises(confuse.ConfigTemplateError):
            config["foo"].get(confuse.Filename(relative_to="bar"))

    def test_filename_relative_to_sibling_needs_template(self):
        config = _root({"foo": "/", "bar": "baz"})
        with pytest.raises(confuse.ConfigTemplateError):
            config.get({"bar": confuse.Filename(relative_to="foo")})

    def test_filename_with_non_file_source(self):
        config = _root({"foo": "foo/bar"})
        valid = config["foo"].get(confuse.Filename())
        assert valid == os.path.join(os.getcwd(), "foo", "bar")

    def test_filename_with_file_source(self):
        source = confuse.ConfigSource({"foo": "foo/bar"}, filename="/baz/config.yaml")
        config = _root(source)
        config.config_dir = lambda: "/config/path"  # type: ignore[attr-defined]
        valid = config["foo"].get(confuse.Filename())
        assert valid == os.path.realpath("/config/path/foo/bar")

    def test_filename_with_default_source(self):
        source = confuse.ConfigSource(
            {"foo": "foo/bar"}, filename="/baz/config.yaml", default=True
        )
        config = _root(source)
        config.config_dir = lambda: "/config/path"  # type: ignore[attr-defined]
        valid = config["foo"].get(confuse.Filename())
        assert valid == os.path.realpath("/config/path/foo/bar")

    def test_filename_use_config_source_dir(self):
        source = confuse.ConfigSource(
            {"foo": "foo/bar"}, filename="/baz/config.yaml", base_for_paths=True
        )
        config = _root(source)
        config.config_dir = lambda: "/config/path"  # type: ignore[attr-defined]
        valid = config["foo"].get(confuse.Filename())
        assert valid == os.path.realpath("/baz/foo/bar")

    def test_filename_in_source_dir(self):
        source = confuse.ConfigSource({"foo": "foo/bar"}, filename="/baz/config.yaml")
        config = _root(source)
        config.config_dir = lambda: "/config/path"  # type: ignore[attr-defined]
        valid = config["foo"].get(confuse.Filename(in_source_dir=True))
        assert valid == os.path.realpath("/baz/foo/bar")

    def test_filename_in_source_dir_overrides_in_app_dir(self):
        source = confuse.ConfigSource({"foo": "foo/bar"}, filename="/baz/config.yaml")
        config = _root(source)
        config.config_dir = lambda: "/config/path"  # type: ignore[attr-defined]
        valid = config["foo"].get(confuse.Filename(in_source_dir=True, in_app_dir=True))
        assert valid == os.path.realpath("/baz/foo/bar")

    def test_filename_in_app_dir_non_file_source(self):
        source = confuse.ConfigSource({"foo": "foo/bar"})
        config = _root(source)
        config.config_dir = lambda: "/config/path"  # type: ignore[attr-defined]
        valid = config["foo"].get(confuse.Filename(in_app_dir=True))
        assert valid == os.path.realpath("/config/path/foo/bar")

    def test_filename_in_app_dir_overrides_config_source_dir(self):
        source = confuse.ConfigSource(
            {"foo": "foo/bar"}, filename="/baz/config.yaml", base_for_paths=True
        )
        config = _root(source)
        config.config_dir = lambda: "/config/path"  # type: ignore[attr-defined]
        valid = config["foo"].get(confuse.Filename(in_app_dir=True))
        assert valid == os.path.realpath("/config/path/foo/bar")

    def test_filename_wrong_type(self):
        config = _root({"foo": 8})
        with pytest.raises(confuse.ConfigTypeError):
            config["foo"].get(confuse.Filename())


class PathTest(unittest.TestCase):
    def test_path_value(self):
        import pathlib

        config = _root({"foo": "foo/bar"})
        valid = config["foo"].get(confuse.Path())
        assert valid == pathlib.Path(os.path.abspath("foo/bar"))

    def test_default_value(self):
        import pathlib

        config = _root({})
        valid = config["foo"].get(confuse.Path("foo/bar"))
        assert valid == pathlib.Path("foo/bar")

    def test_default_none(self):
        config = _root({})
        valid = config["foo"].get(confuse.Path(None))
        assert valid is None

    def test_missing_required_value(self):
        config = _root({})
        with pytest.raises(confuse.NotFoundError):
            config["foo"].get(confuse.Path())


class BaseTemplateTest(unittest.TestCase):
    def test_base_template_accepts_any_value(self):
        config = _root({"foo": 4.2})
        valid: float = config["foo"].get(confuse.Template())
        assert valid == 4.2

    def test_base_template_required(self):
        config = _root({})
        with pytest.raises(confuse.NotFoundError):
            config["foo"].get(confuse.Template())

    def test_base_template_with_default(self):
        config = _root({})
        valid = config["foo"].get(confuse.Template("bar"))
        assert valid == "bar"


class TypeTemplateTest(unittest.TestCase):
    def test_correct_type(self):
        config = _root({"foo": set()})
        valid = config["foo"].get(confuse.TypeTemplate(set))
        assert valid == set()

    def test_incorrect_type(self):
        config = _root({"foo": dict()})
        with pytest.raises(confuse.ConfigTypeError):
            config["foo"].get(confuse.TypeTemplate(set))

    def test_missing_required_value(self):
        config = _root({})
        with pytest.raises(confuse.NotFoundError):
            config["foo"].get(confuse.TypeTemplate(set))

    def test_default_value(self):
        config = _root({})
        valid = config["foo"].get(confuse.TypeTemplate(set, {1, 2}))
        assert valid == {1, 2}


class SequenceTest(unittest.TestCase):
    def test_int_list(self):
        config = _root({"foo": [1, 2, 3]})
        valid = config["foo"].get(confuse.Sequence(int))
        assert valid == [1, 2, 3]

    def test_dict_list(self):
        config = _root({"foo": [{"bar": 1, "baz": 2}, {"bar": 3, "baz": 4}]})
        valid = config["foo"].get(confuse.Sequence({"bar": int, "baz": int}))
        assert valid == [{"bar": 1, "baz": 2}, {"bar": 3, "baz": 4}]

    def test_invalid_item(self):
        config = _root({"foo": [{"bar": 1, "baz": 2}, {"bar": 3, "bak": 4}]})
        with pytest.raises(confuse.NotFoundError):
            config["foo"].get(confuse.Sequence({"bar": int, "baz": int}))

    def test_wrong_type(self):
        config = _root({"foo": {"one": 1, "two": 2, "three": 3}})
        with pytest.raises(confuse.ConfigTypeError):
            config["foo"].get(confuse.Sequence(int))

    def test_missing(self):
        config = _root({"foo": [1, 2, 3]})
        valid = config["bar"].get(confuse.Sequence(int))
        assert valid == []


class MappingValuesTest(unittest.TestCase):
    def test_int_dict(self):
        config = _root({"foo": {"one": 1, "two": 2, "three": 3}})
        valid = config["foo"].get(confuse.MappingValues(int))
        assert valid == {"one": 1, "two": 2, "three": 3}

    def test_dict_dict(self):
        config = _root(
            {"foo": {"first": {"bar": 1, "baz": 2}, "second": {"bar": 3, "baz": 4}}}
        )
        valid: dict[str, dict[str, int]] = config["foo"].get(
            confuse.MappingValues({"bar": int, "baz": int})
        )
        assert valid == {"first": {"bar": 1, "baz": 2}, "second": {"bar": 3, "baz": 4}}

    def test_invalid_item(self):
        config = _root(
            {"foo": {"first": {"bar": 1, "baz": 2}, "second": {"bar": 3, "bak": 4}}}
        )
        with pytest.raises(confuse.NotFoundError):
            config["foo"].get(confuse.MappingValues({"bar": int, "baz": int}))

    def test_wrong_type(self):
        config = _root({"foo": [1, 2, 3]})
        with pytest.raises(confuse.ConfigTypeError):
            config["foo"].get(confuse.MappingValues(int))

    def test_missing(self):
        config = _root({"foo": {"one": 1, "two": 2, "three": 3}})
        valid = config["bar"].get(confuse.MappingValues(int))
        assert valid == {}


class OptionalTest(unittest.TestCase):
    def test_optional_string_valid_type(self):
        config = _root({"foo": "bar"})
        valid = config["foo"].get(confuse.Optional(confuse.String()))
        assert valid == "bar"

    def test_optional_string_invalid_type(self):
        config = _root({"foo": 5})
        with pytest.raises(confuse.ConfigTypeError):
            config["foo"].get(confuse.Optional(confuse.String()))

    def test_optional_string_null(self):
        config = _root({"foo": None})
        valid = config["foo"].get(confuse.Optional(confuse.String()))
        assert valid is None

    def test_optional_string_null_default_value(self):
        config = _root({"foo": None})
        valid = config["foo"].get(confuse.Optional(confuse.String(), "baz"))
        assert valid == "baz"

    def test_optional_string_null_string_provides_default(self):
        config = _root({"foo": None})
        valid = config["foo"].get(confuse.Optional(confuse.String("baz")))
        assert valid == "baz"

    def test_optional_string_null_string_default_override(self):
        config = _root({"foo": None})
        valid = config["foo"].get(
            confuse.Optional(confuse.String("baz"), default="bar")
        )
        assert valid == "bar"

    def test_optional_string_allow_missing_no_explicit_default(self):
        config = _root({})
        valid = config["foo"].get(confuse.Optional(confuse.String()))
        assert valid is None

    def test_optional_string_allow_missing_default_value(self):
        config = _root({})
        valid = config["foo"].get(confuse.Optional(confuse.String(), "baz"))
        assert valid == "baz"

    def test_optional_string_missing_not_allowed(self):
        config = _root({})
        with pytest.raises(confuse.NotFoundError):
            config["foo"].get(confuse.Optional(confuse.String(), allow_missing=False))

    def test_optional_string_null_missing_not_allowed(self):
        config = _root({"foo": None})
        valid = config["foo"].get(
            confuse.Optional(confuse.String(), allow_missing=False)
        )
        assert valid is None

    def test_optional_mapping_template_valid(self):
        config = _root({"foo": {"bar": 5, "baz": "bak"}})
        template = {"bar": confuse.Integer(), "baz": confuse.String()}
        valid = config.get({"foo": confuse.Optional(template)})
        assert valid["foo"]
        assert valid["foo"]["bar"] == 5
        assert valid["foo"]["baz"] == "bak"

    def test_optional_mapping_template_invalid(self):
        config = _root({"foo": {"bar": 5, "baz": 10}})
        template = {"bar": confuse.Integer(), "baz": confuse.String()}
        with pytest.raises(confuse.ConfigTypeError):
            config.get({"foo": confuse.Optional(template)})

    def test_optional_mapping_template_null(self):
        config = _root({"foo": None})
        template = {"bar": confuse.Integer(), "baz": confuse.String()}
        valid = config.get({"foo": confuse.Optional(template)})
        assert valid["foo"] is None

    def test_optional_mapping_template_null_default_value(self):
        config = _root({"foo": None})
        template = {"bar": confuse.Integer(), "baz": confuse.String()}
        valid = config.get({"foo": confuse.Optional(template, {})})
        assert isinstance(valid["foo"], dict)

    def test_optional_mapping_template_allow_missing_no_explicit_default(self):
        config = _root({})
        template = {"bar": confuse.Integer(), "baz": confuse.String()}
        valid = config.get({"foo": confuse.Optional(template)})
        assert valid["foo"] is None

    def test_optional_mapping_template_allow_missing_default_value(self):
        config = _root({})
        template = {"bar": confuse.Integer(), "baz": confuse.String()}
        valid = config.get({"foo": confuse.Optional(template, {})})
        assert isinstance(valid["foo"], dict)

    def test_optional_mapping_template_missing_not_allowed(self):
        config = _root({})
        template = {"bar": confuse.Integer(), "baz": confuse.String()}
        with pytest.raises(confuse.NotFoundError):
            config.get({"foo": confuse.Optional(template, allow_missing=False)})

    def test_optional_mapping_template_null_missing_not_allowed(self):
        config = _root({"foo": None})
        template = {"bar": confuse.Integer(), "baz": confuse.String()}
        valid = config.get({"foo": confuse.Optional(template, allow_missing=False)})
        assert valid["foo"] is None