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
|
from typing import Optional, cast
from dataclasses import dataclass, field
import pytest
from grpclib.config import Configuration, _range
from grpclib.config import _DEFAULT, _with_defaults, _positive, _validate
from grpclib.config import _optional, _chain, _of_type
def test_custom_default():
@dataclass
class Config:
param: Optional[int] = field(
default=_DEFAULT,
metadata={
'my-default': 42,
},
)
cfg = Config()
assert cfg.param is _DEFAULT
cfg_with_defaults = _with_defaults(cfg, 'my-default')
assert cfg_with_defaults.param == 42
def test_missing_default():
@dataclass
class Config:
param: Optional[int] = field(
default=_DEFAULT,
)
cfg = Config()
with pytest.raises(KeyError):
_with_defaults(cfg, 'my-default')
def test_default_none():
@dataclass
class Config:
param: Optional[int] = field(
default=_DEFAULT,
metadata={
'my-default': None,
},
)
cfg = Config()
assert cfg.param is _DEFAULT
cfg_with_defaults = _with_defaults(cfg, 'my-default')
assert cfg_with_defaults.param is None
def test_validate():
@dataclass
class Config:
foo: Optional[float] = field(
default=None,
metadata={
'validate': _optional(_chain(_of_type(int, float), _positive)),
},
)
def __post_init__(self):
_validate(self)
assert Config().foo is None
assert Config(foo=0.123).foo == 0.123
assert Config(foo=42).foo == 42
with pytest.raises(ValueError, match='"foo" should be positive'):
assert Config(foo=0)
with pytest.raises(TypeError, match='"foo" should be of type'):
assert Config(foo='a')
def test_configuration():
# all params should be optional
config = Configuration()
_with_defaults(config, 'client-default')
_with_defaults(config, 'server-default')
def test_change_default():
# all params should be optional
@dataclass
class Config:
foo: Optional[float] = field(
default=cast(None, _DEFAULT),
metadata={
'validate': _optional(_chain(_of_type(int, float), _positive)),
'test-default': 1234,
},
)
def __post_init__(self):
_validate(self)
assert _with_defaults(Config(foo=1), 'test-default').foo == 1
def test_range():
@dataclass
class Config:
foo: int = field(
default=42,
metadata={
'validate': _chain(_of_type(int), _range(1, 99)),
},
)
def __post_init__(self):
_validate(self)
Config()
Config(foo=1)
Config(foo=99)
with pytest.raises(ValueError, match='should be less or equal to 99'):
Config(foo=100)
with pytest.raises(ValueError, match='should be higher or equal to 1'):
Config(foo=0)
|