示例#1
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
示例#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_bool_casting():
    class Model(pydantic.BaseModel):
        field: bool

    field = Field(Model.__fields__['field'])
    factory, options = field.cast()
    assert factory == wtforms.fields.BooleanField
示例#4
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
示例#5
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
示例#6
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
示例#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_validators(person_model, dummy_field):
    age = Field(person_model.__fields__['age'])
    options = age.compute_options()
    dummy_field.data = 17
    with pytest.raises(wtforms.validators.ValidationError) as exc:
        for validator in options['validators']:
            validator(Form(), dummy_field)
    assert str(exc.value) == 'must be over 18 years old.'
示例#9
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')
示例#10
0
def test_complex_field_options(person_model):

    field = Field(person_model.__fields__['age'])
    options = field.compute_options()
    hamcrest.assert_that(options, hamcrest.has_entries({
            'default': person_model.__fields__['age'].default_factory,
            'description': '',
            'validators': hamcrest.contains_exactly(
                hamcrest.instance_of(wtforms.validators.Optional),
                hamcrest.instance_of(FieldValidator)
            ),
            'label': 'age'
        })
    )
示例#11
0
def test_typing_rich_field_options(userinfo_model):

    field = Field(userinfo_model.__fields__['email'])
    options = field.compute_options()
    hamcrest.assert_that(options, hamcrest.has_entries({
            'default': None,
            'description': '',
            'validators': hamcrest.contains_exactly(
                hamcrest.instance_of(wtforms.validators.Optional),
                hamcrest.instance_of(FieldValidator)
            ),
            'label': 'email'
        })
    )
示例#12
0
def test_cross_validation(person_model, dummy_field):
    identifier = Field(
        person_model.__fields__['identifier'])
    options = identifier.compute_options()
    dummy_field.data = 'klaus_kinski'

    for validator in options['validators']:
        validator(Form({'name': 'Klaus'}), dummy_field)

    with pytest.raises(wtforms.validators.ValidationError) as exc:
        for validator in options['validators']:
            validator(Form({'name': 'Christian'}), dummy_field)

    assert str(exc.value) == (
        'The identifier must contain the name in lowercase.')
示例#13
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')
示例#14
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')
示例#15
0
def model_fields(model, include=None, exclude=None) -> dict:
    if not include:
        include = frozenset(model.__fields__.keys())
    if not exclude:
        exclude = set()

    return {
        name: Field(field) for name, field in model.__fields__.items()
        if name in include and name not in exclude
    }
示例#16
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')
示例#17
0
def test_no_validators(person_model, dummy_field):
    name = Field(person_model.__fields__['name'])
    options = name.compute_options()
    dummy_field.data = 'Christian'
    options['validators'][0](Form(), dummy_field)
示例#18
0
def test_field_options(person_model):

    options = Field(
        person_model.__fields__['name']).compute_options()
    hamcrest.assert_that(options, hamcrest.has_entries({
            'default': 'Klaus',
            'description': '',
            'validators': hamcrest.contains_exactly(
                hamcrest.instance_of(wtforms.validators.Optional),
                hamcrest.instance_of(FieldValidator)
            ),
            'label': 'name'
        })
    )

    field = Field(person_model.__fields__['name'])
    field.required = True
    options = field.compute_options()
    hamcrest.assert_that(options, hamcrest.has_entries({
            'default': 'Klaus',
            'description': '',
            'validators': hamcrest.contains_exactly(
                hamcrest.instance_of(wtforms.validators.DataRequired),
                hamcrest.instance_of(FieldValidator)
            ),
            'label': 'name'
        })
    )

    field = Field(person_model.__fields__['name'])
    field.metadata['label'] = "This is a name"
    options = field.compute_options()
    hamcrest.assert_that(options, hamcrest.has_entries({
            'default': 'Klaus',
            'description': '',
            'validators': hamcrest.contains_exactly(
                hamcrest.instance_of(wtforms.validators.Optional),
                hamcrest.instance_of(FieldValidator),
            ),
            'label': 'This is a name'
        })
    )

    field = Field(person_model.__fields__['identifier'])
    options = field.compute_options()
    hamcrest.assert_that(options, hamcrest.has_entries({
            'default': None,
            'description': '',
            'validators': hamcrest.contains_exactly(
                hamcrest.instance_of(wtforms.validators.DataRequired),
                hamcrest.instance_of(FieldValidator)
            ),
            'label': 'identifier'
        })
    )