def test_spawn(): desc = 'foo' f = Field(description=desc) f1 = f.spawn(required=True) # test spawn attributes are changed assert f.required is False assert f1.required is True assert f.description == f1.description # test spawn instances do not share key attr assert f.key is None class P0(ParamSet): key0 = f assert f.key == 'key0' class P1(ParamSet): key1 = f.spawn(null=True) assert f.key == 'key0' f.spawn(key='key2') assert f.key == 'key0'
def test_spawn(): desc = 'foo' f = Field(description=desc) f1 = f.spawn(required=True) assert f.required is False assert f1.required is True assert f.description == f1.description
def test_field_null_values(): f0 = Field(null_values=(None, 0), null=True) assert None is f0.validate(None) assert None is f0.validate(0) assert '' == f0.validate('') f1 = f0.spawn(null=False) with pytest.raises(ValueError): f1.validate(None) with pytest.raises(ValueError): f1.validate(0) assert '' == f0.validate('') # though null is not allowed, value will never be determined as null, so `null=False` won't take any effect f2 = Field(null_values=(), null=False) assert None is f2.validate(None)
class P(ParamSet): f0 = Field(required=True) f1 = Field()
def test_field_null(): f0 = Field(null=True) assert None is f0.validate(None) assert None is f0.validate('') assert None is f0.validate(u_('')) # 0 is not null, only '' and None are assert 0 is f0.validate(0) f1 = Field(null=False) with pytest.raises(ValueError): f1.validate(None) with pytest.raises(ValueError): f1.validate('') with pytest.raises(ValueError): f1.validate(u_('')) assert 0 is f1.validate(0)
class P(ParamSet): check_addtional = True f0 = Field()
class P(ParamSet): f0 = Field(key='0f')
def test_field_choices(): f0 = Field(null=False, choices=[1, '2', '']) # null=False priority is higher than choices with pytest.raises(ValueError): f0.validate('') f0.validate(1) f0.validate('2') with pytest.raises(ValueError): f0.validate('1') with pytest.raises(ValueError): f0.validate(2)
def test_field_convert(): f = Field() f.validate('whatever', convert=True) class MultiTypeField(Field): value_type = (bool, int) f = MultiTypeField() f.validate('True', convert=True) f.validate('1', convert=True) f.validate(False, convert=True) f.validate(0, convert=True)
class P(ParamSet): f0 = Field() f1 = Field(default=1)
class P(ParamSet): f0 = Field(required=True, default=f0_default) f1 = Field()
class P(ParamSet): no_additional_keys = True f0 = Field()
class ItemParams(ParamSet): a = Field(required=True)