-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Custom env var value casters for birch.""" | ||
|
||
|
||
def true_false_caster(val): | ||
"""Casts 'TRUE', 'true', etc. to True, all other strings to False.""" | ||
try: | ||
return val.lower() == 'true' | ||
except AttributeError: | ||
raise ValueError(f"Bad value {val} attempted to cast to bool!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"""Testing default caster functionality for birch.""" | ||
|
||
import pytest | ||
|
||
from birch import Birch | ||
from birch.casters import true_false_caster | ||
|
||
from .test_core import ( | ||
NSPACE4, | ||
) | ||
|
||
|
||
def test_default_casters(): | ||
cfg = Birch( | ||
namespace=NSPACE4, | ||
default_casters={ | ||
'biil': true_false_caster, | ||
'bool': true_false_caster, | ||
'baal': true_false_caster, | ||
'shik': { | ||
'shuk': int, | ||
} | ||
}, | ||
) | ||
val = cfg['pik'] | ||
assert isinstance(val, str) | ||
val = cfg['biil'] | ||
assert isinstance(val, bool) | ||
assert val is True | ||
val = cfg['bool'] | ||
assert isinstance(val, bool) | ||
assert val is False | ||
with pytest.raises(ValueError): | ||
val = cfg['baal'] | ||
val = cfg['shik__shuk'] | ||
assert isinstance(val, int) | ||
assert val == 8 | ||
val = cfg['shik']['shuk'] | ||
assert isinstance(val, str) | ||
assert val == '8' |