예제 #1
0
def test_email_casting():
    class Model(pydantic.BaseModel):
        field: pydantic.networks.EmailStr

    field = Field(Model.__fields__['field'])
    factory, options = field.cast()
    assert factory == wtforms.fields.html5.EmailField
예제 #2
0
def test_time_casting():
    class Model(pydantic.BaseModel):
        field: datetime.time

    field = Field(Model.__fields__['field'])
    factory, options = field.cast()
    assert factory == wtforms.fields.html5.TimeField
예제 #3
0
def test_password_casting():
    class Model(pydantic.BaseModel):
        field: pydantic.SecretStr

    field = Field(Model.__fields__['field'])
    factory, options = field.cast()
    assert factory == wtforms.fields.PasswordField
예제 #4
0
def test_float_casting():
    class Model(pydantic.BaseModel):
        field: float

    field = Field(Model.__fields__['field'])
    factory, options = field.cast()
    assert factory == wtforms.fields.FloatField
예제 #5
0
def test_bool_casting():
    class Model(pydantic.BaseModel):
        field: bool

    field = Field(Model.__fields__['field'])
    factory, options = field.cast()
    assert factory == wtforms.fields.BooleanField
예제 #6
0
def test_str_casting():
    class Model(pydantic.BaseModel):
        field: str

    field = Field(Model.__fields__['field'])
    factory, options = field.cast()
    assert factory == wtforms.fields.StringField
예제 #7
0
def test_int_casting():
    class Model(pydantic.BaseModel):
        field: int

    field = Field(Model.__fields__['field'])
    factory, options = field.cast()
    assert factory == wtforms.fields.IntegerField
예제 #8
0
def test_multiple_literal_casting():
    class Model(pydantic.BaseModel):
        multiple: typing.List[typing.Literal['complex', 'complicated']]

    field = Field(Model.__fields__['multiple'])
    factory, options = field.cast()
    assert factory == MultiCheckboxField
    assert options['choices'] == [('complex', 'complex'),
                                  ('complicated', 'complicated')]
    assert options['coerce']('complex')
    with pytest.raises(ValueError):
        assert options['coerce']('other value')
예제 #9
0
def test_literal_casting():
    class Model(pydantic.BaseModel):
        unique: typing.Literal['singleton']
        multiple: typing.Literal['complex', 'complicated']

    field = Field(Model.__fields__['unique'])
    factory, options = field.cast()
    assert factory == wtforms.fields.SelectField
    assert options['choices'] == [('singleton', 'singleton')]
    assert options['coerce']('singleton')
    with pytest.raises(ValueError):
        assert options['coerce']('other value')

    field = Field(Model.__fields__['multiple'])
    factory, options = field.cast()
    assert factory == wtforms.fields.SelectField
    assert options['choices'] == [('complex', 'complex'),
                                  ('complicated', 'complicated')]
    assert options['coerce']('complex')
    with pytest.raises(ValueError):
        assert options['coerce']('other value')
예제 #10
0
def test_enum_casting():
    class MyChoices(enum.Enum):
        foo = 'Foo'
        bar = 'Bar'

    class Model(pydantic.BaseModel):
        field: MyChoices

    field = Field(Model.__fields__['field'])
    factory, options = field.cast()
    assert factory == wtforms.fields.SelectField
    assert options['choices'] == [('foo', 'Foo'), ('bar', 'Bar')]
    assert options['coerce']('foo')
    with pytest.raises(ValueError):
        assert options['coerce']('test')
예제 #11
0
def test_multiple_enum_casting():
    class MyChoices(enum.Enum):
        foo = 'Foo'
        bar = 'Bar'

    class Model(pydantic.BaseModel):
        field1: typing.List[MyChoices]
        field2: typing.Set[MyChoices]
        field3: typing.Tuple[MyChoices]

    for fname in ('field1', 'field2', 'field3'):
        field = Field(Model.__fields__[fname])
        factory, options = field.cast()
        assert factory == MultiCheckboxField
        assert options['choices'] == [('foo', 'Foo'), ('bar', 'Bar')]
        assert options['coerce']('foo')
        with pytest.raises(ValueError):
            assert options['coerce']('test')