def test_none_value(self):
        """ Test None value treatment for the float field"""

        score = Float(max_value=5.5)
        score._load(None)

        assert score.value == 0.0
示例#2
0
class Balance(BaseValueObject):
    """A composite amount object, containing two parts:
        * currency code - a three letter unique currency code
        * amount - a float value
    """

    currency = String(max_length=3, choices=Currency)
    amount = Float()

    def clean(self):
        errors = defaultdict(list)
        if self.amount and self.amount < -1000000000000.0:
            errors["amount"].append("cannot be less than 1 Trillion")
        return errors

    def replace(self, **kwargs):
        # FIXME Find a way to do this generically and move method to `BaseValueObject`
        currency = kwargs.pop("currency", None)
        amount = kwargs.pop("amount", None)
        return Balance(currency=currency or self.currency, amount=amount or self.amount)
    def test_max_value(self):
        """ Test maximum value validation for the float field"""

        with pytest.raises(ValidationError):
            score = Float(max_value=5.5)
            score._load(5.6)
 def test_type_validation(self):
     """ Test type checking validation for the Field"""
     with pytest.raises(ValidationError):
         score = Float()
         score._load('x')
    def test_init(self):
        """Test successful Float Field initialization"""

        score = Float()
        assert score is not None