@@ -90,12 +90,17 @@ def test_list_cast(self, set_env, env: environs.Env):
90
90
set_env ({"LIST" : "1,2,3" })
91
91
assert env .list ("LIST" ) == ["1" , "2" , "3" ]
92
92
93
- def test_list_with_default_from_string (self , env : environs .Env ):
94
- assert env .list ("LIST" , "1,2" ) == ["1" , "2" ]
95
-
96
93
def test_list_with_default_from_list (self , env : environs .Env ):
97
94
assert env .list ("LIST" , ["1" ]) == ["1" ]
98
95
96
+ # https://github.com/sloria/environs/issues/270
97
+ def test_list_with_default_list_and_subcast (self , set_env , env : environs .Env ):
98
+ expected = [("a" , "b" ), ("b" , "c" )]
99
+ assert (
100
+ env .list ("LIST" , expected , subcast = lambda s : tuple (s .split (":" )))
101
+ == expected
102
+ )
103
+
99
104
# https://github.com/sloria/environs/issues/298
100
105
def test_list_with_default_none (self , env : environs .Env ):
101
106
assert env .list ("LIST" , default = None ) is None
@@ -174,10 +179,7 @@ def custom_tuple(value: str):
174
179
"DICT" , subcast_keys = custom_tuple , subcast_values = custom_tuple
175
180
) == {("1" , "1" ): ("foo" , "bar" )}
176
181
177
- def test_dict_with_default_from_string (self , set_env , env : environs .Env ):
178
- assert env .dict ("DICT" , "key1=1,key2=2" ) == {"key1" : "1" , "key2" : "2" }
179
-
180
- def test_dict_with_default_from_dict (self , set_env , env : environs .Env ):
182
+ def test_dict_with_dict_default (self , env : environs .Env ):
181
183
assert env .dict ("DICT" , {"key1" : "1" }) == {"key1" : "1" }
182
184
183
185
def test_dict_with_equal (self , set_env , env : environs .Env ):
@@ -212,9 +214,6 @@ def test_invalid_json_raises_error(self, set_env, env: environs.Env):
212
214
def test_json_default (self , set_env , env : environs .Env ):
213
215
assert env .json ("JSON" , {"foo" : "bar" }) == {"foo" : "bar" }
214
216
assert env .json ("JSON" , ["foo" , "bar" ]) == ["foo" , "bar" ]
215
- with pytest .raises (environs .EnvError ) as exc :
216
- env .json ("JSON" , "invalid" ) # type: ignore[call-overload]
217
- assert "Not valid JSON." in exc .value .args [0 ]
218
217
219
218
def test_datetime_cast (self , set_env , env : environs .Env ):
220
219
dtime = dt .datetime .now (dt .timezone .utc )
@@ -230,10 +229,6 @@ def test_date_cast(self, set_env, env: environs.Env):
230
229
set_env ({"DATE" : date .isoformat ()})
231
230
assert env .date ("DATE" ) == date
232
231
233
- @pytest .mark .xfail (
234
- MARSHMALLOW_VERSION .major < 4 ,
235
- reason = "marshmallow 3 does not allow all fields to accept internal types" ,
236
- )
237
232
@pytest .mark .parametrize (
238
233
("method_name" , "value" ),
239
234
[
@@ -242,16 +237,13 @@ def test_date_cast(self, set_env, env: environs.Env):
242
237
pytest .param ("time" , dt .time (1 , 2 , 3 ), id = "time" ),
243
238
],
244
239
)
245
- def test_default_can_be_set_to_internal_type (
240
+ def test_default_set_to_internal_type (
246
241
self , env : environs .Env , method_name : str , value
247
242
):
248
243
method = getattr (env , method_name )
249
244
assert method ("NOTFOUND" , value ) == value
250
245
251
246
def test_timedelta_cast (self , set_env , env : environs .Env ):
252
- # default values may be in serialized form
253
- assert env .timedelta ("TIMEDELTA" , "42" ) == dt .timedelta (seconds = 42 ) # type: ignore[call-overload]
254
- assert env .timedelta ("TIMEDELTA" , 42 ) == dt .timedelta (seconds = 42 ) # type: ignore[call-overload]
255
247
# marshmallow 4 preserves float values as microseconds
256
248
if MARSHMALLOW_VERSION .major >= 4 :
257
249
set_env ({"TIMEDELTA" : "42.9" })
@@ -490,8 +482,9 @@ def test_can_add_marshmallow_validator(self, set_env, env: environs.Env):
490
482
env ("NODE_ENV" , validate = validate .OneOf (["development" , "production" ]))
491
483
492
484
def test_validator_can_raise_enverror (self , set_env , env : environs .Env ):
485
+ set_env ({"NODE_ENV" : "test" })
493
486
with pytest .raises (environs .EnvError ) as excinfo :
494
- env ("NODE_ENV" , "development" , validate = always_fail )
487
+ env ("NODE_ENV" , validate = always_fail )
495
488
assert "something went wrong" in excinfo .value .args [0 ]
496
489
497
490
def test_failed_vars_are_not_serialized (self , set_env , env : environs .Env ):
@@ -974,21 +967,6 @@ def test_recursive_expands(self, env: environs.Env, set_env):
974
967
)
975
968
assert env .str ("PGURL" ) == "postgres://gnarvaja:secret@localhost"
976
969
977
- def test_default_expands (self , env : environs .Env , set_env ):
978
- set_env (
979
- {
980
- "MAIN" : "${SUBSTI}" ,
981
- "SUBSTI" : "substivalue" ,
982
- }
983
- )
984
- assert env .str ("NOT_SET" , "${SUBSTI}" ) == "substivalue"
985
- assert env .str ("NOT_SET" , "${MAIN}" ) == "substivalue"
986
- assert env .str ("NOT_SET" , "${NOT_SET2:-set2}" ) == "set2"
987
- with pytest .raises (
988
- environs .EnvError , match = 'Environment variable "NOT_SET2" not set'
989
- ):
990
- assert env .str ("NOT_SET" , "${NOT_SET2}" )
991
-
992
970
def test_escaped_expand (self , env : environs .Env , set_env ):
993
971
set_env ({"ESCAPED_EXPAND" : r"\${ESCAPED}" , "ESCAPED" : "fail" })
994
972
assert env .str ("ESCAPED_EXPAND" ) == r"${ESCAPED}"
0 commit comments