mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
4f4e22ef47
* POC of error context and message * Move type errors to the `errors.py` module; Change errors interface a bit * Rename `.as_dict()` to `.dict()` * Fix `PydanticErrorMixin` constructor * Rename `exceptions.py` to `error_wrappers.py` * Do not include nullable `ctx` * Fix tests * Added `int_validator`; Added `IntegerError` * Added `float_validator`; Added `FloatError` * Get rid of `__mro__` in prior of `exc.code` * Removed `min_number_size` and `max_number_size` from config (#174) * Added `NumberMinSizeError` and `NumberMaxSizeError` * Added `NoneIsNotAllowedError` * Added `EnumError` * Added `path_validator`; Added `PathError` * Added `DictError` * Added `ListError` * Added `TupleError` * Added `SetError` * Added `datetime` related errors * Added `bytes` and `str` related errors * Added `SequenceError` * Improved code coverage * Display error context in string representation of validation error * Redefine error message templates using config * Review fixes * Updated changelog
106 lines
2.4 KiB
Python
106 lines
2.4 KiB
Python
from typing import List, Set
|
|
|
|
import pytest
|
|
|
|
from pydantic import BaseModel, BaseSettings, NoneStr, ValidationError
|
|
from pydantic.env_settings import SettingsError
|
|
|
|
|
|
class SimpleSettings(BaseSettings):
|
|
apple: str = ...
|
|
|
|
|
|
def test_sub_env(env):
|
|
env.set('APP_APPLE', 'hello')
|
|
s = SimpleSettings()
|
|
assert s.apple == 'hello'
|
|
|
|
|
|
def test_sub_env_override(env):
|
|
env.set('APP_APPLE', 'hello')
|
|
s = SimpleSettings(apple='goodbye')
|
|
assert s.apple == 'goodbye'
|
|
|
|
|
|
def test_sub_env_missing():
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
SimpleSettings()
|
|
assert exc_info.value.flatten_errors() == [
|
|
{
|
|
'loc': ('apple',),
|
|
'msg': 'none is not an allow value',
|
|
'type': 'type_error.none.not_allowed',
|
|
},
|
|
]
|
|
|
|
|
|
def test_other_setting(env):
|
|
with pytest.raises(ValidationError):
|
|
SimpleSettings(apple='a', foobar=42)
|
|
|
|
|
|
def test_env_with_aliass(env):
|
|
class Settings(BaseSettings):
|
|
apple: str = ...
|
|
|
|
class Config:
|
|
fields = {
|
|
'apple': 'BOOM'
|
|
}
|
|
env.set('BOOM', 'hello')
|
|
assert Settings().apple == 'hello'
|
|
|
|
|
|
class DateModel(BaseModel):
|
|
pips: bool = False
|
|
|
|
|
|
class ComplexSettings(BaseSettings):
|
|
apples: List[str] = []
|
|
bananas: Set[int] = set()
|
|
carrots: dict = {}
|
|
date: DateModel = DateModel()
|
|
|
|
|
|
def test_list(env):
|
|
env.set('APP_APPLES', '["russet", "granny smith"]')
|
|
s = ComplexSettings()
|
|
assert s.apples == ['russet', 'granny smith']
|
|
assert s.date.pips is False
|
|
|
|
|
|
def test_set_dict_model(env):
|
|
env.set('APP_BANANAS', '[1, 2, 3, 3]')
|
|
env.set('APP_CARROTS', '{"a": null, "b": 4}')
|
|
env.set('APP_DATE', '{"pips": true}')
|
|
s = ComplexSettings()
|
|
assert s.bananas == {1, 2, 3}
|
|
assert s.carrots == {'a': None, 'b': 4}
|
|
assert s.date.pips is True
|
|
|
|
|
|
def test_invalid_json(env):
|
|
env.set('APP_APPLES', '["russet", "granny smith",]')
|
|
with pytest.raises(SettingsError):
|
|
ComplexSettings()
|
|
|
|
|
|
def test_required_sub_model(env):
|
|
class Settings(BaseSettings):
|
|
foobar: DateModel
|
|
|
|
with pytest.raises(ValidationError):
|
|
Settings()
|
|
env.set('APP_FOOBAR', '{"pips": "TRUE"}')
|
|
s = Settings()
|
|
assert s.foobar.pips is True
|
|
|
|
|
|
def test_non_class(env):
|
|
class Settings(BaseSettings):
|
|
foobar: NoneStr
|
|
|
|
env.set('APP_FOOBAR', 'xxx')
|
|
s = Settings()
|
|
assert s.foobar == 'xxx'
|