7
7
from typing import Any
8
8
9
9
import marshmallow
10
+ import marshmallow_dataclass
10
11
import pytest
11
12
from pytest_mock import MockerFixture
12
13
@@ -21,26 +22,55 @@ class SimpleConfig:
21
22
value : int
22
23
23
24
24
- def test_load_config_dataclass () -> None :
25
+ @marshmallow_dataclass .dataclass
26
+ class MmSimpleConfig :
27
+ """A simple marshmallow_dataclass configuration class for testing."""
28
+
29
+ name : str = dataclasses .field (metadata = {"validate" : lambda s : s .startswith ("test" )})
30
+ value : int
31
+
32
+
33
+ @pytest .mark .parametrize (
34
+ "config_class" ,
35
+ [SimpleConfig , MmSimpleConfig ],
36
+ ids = ["dataclass" , "marshmallow_dataclass" ],
37
+ )
38
+ def test_load_config_dataclass (
39
+ config_class : type [SimpleConfig ] | type [MmSimpleConfig ],
40
+ ) -> None :
25
41
"""Test that load_config loads a configuration into a configuration class."""
26
42
config : dict [str , Any ] = {"name" : "test" , "value" : 42 }
27
43
28
- loaded_config = load_config (SimpleConfig , config )
29
- assert loaded_config == SimpleConfig (name = "test" , value = 42 )
44
+ loaded_config = load_config (config_class , config )
45
+ assert loaded_config == config_class (name = "test" , value = 42 )
30
46
31
47
config ["name" ] = "not test"
32
48
with pytest .raises (marshmallow .ValidationError ):
33
- _ = load_config (SimpleConfig , config )
49
+ _ = load_config (config_class , config )
34
50
35
51
36
- def test_load_config_load_None () -> None :
52
+ @pytest .mark .parametrize (
53
+ "config_class" ,
54
+ [SimpleConfig , MmSimpleConfig ],
55
+ ids = ["dataclass" , "marshmallow_dataclass" ],
56
+ )
57
+ def test_load_config_load_None (
58
+ config_class : type [SimpleConfig ] | type [MmSimpleConfig ],
59
+ ) -> None :
37
60
"""Test that load_config raises ValidationError if the configuration is None."""
38
61
config : dict [str , Any ] = {}
39
62
with pytest .raises (marshmallow .ValidationError ):
40
- _ = load_config (SimpleConfig , config .get ("loggers" , None ))
63
+ _ = load_config (config_class , config .get ("loggers" , None ))
41
64
42
65
43
- def test_load_config_with_base_schema () -> None :
66
+ @pytest .mark .parametrize (
67
+ "config_class" ,
68
+ [SimpleConfig , MmSimpleConfig ],
69
+ ids = ["dataclass" , "marshmallow_dataclass" ],
70
+ )
71
+ def test_load_config_with_base_schema (
72
+ config_class : type [SimpleConfig ] | type [MmSimpleConfig ],
73
+ ) -> None :
44
74
"""Test that load_config loads a configuration using a base schema."""
45
75
46
76
class _MyBaseSchema (marshmallow .Schema ):
@@ -53,11 +83,11 @@ class Meta:
53
83
54
84
config : dict [str , Any ] = {"name" : "test" , "value" : 42 , "extra" : "extra" }
55
85
56
- loaded_config = load_config (SimpleConfig , config , base_schema = _MyBaseSchema )
57
- assert loaded_config == SimpleConfig (name = "test" , value = 42 )
86
+ loaded_config = load_config (config_class , config , base_schema = _MyBaseSchema )
87
+ assert loaded_config == config_class (name = "test" , value = 42 )
58
88
59
89
with pytest .raises (marshmallow .ValidationError ):
60
- _ = load_config (SimpleConfig , config )
90
+ _ = load_config (config_class , config )
61
91
62
92
63
93
def test_load_config_type_hints (mocker : MockerFixture ) -> None :
0 commit comments