コード例 #1
0
    def test_number_type_with_localized_settings(self):
        _type = types.NumberType(self.field)
        for value in [
                '10,000.00', '10,000,000.23', '10.23', '1,000', '100%', '1000‰'
        ]:
            self.assertTrue(_type.cast(value))

        self.field['groupChar'] = '#'
        _type = types.NumberType(self.field)
        for value in ['10#000.00', '10#000#000.23', '10.23', '1#000']:
            self.assertTrue(_type.cast(value))

        self.field['decimalChar'] = '@'
        _type = types.NumberType(self.field)
        for value in ['10#000@00', '10#000#000@23', '10@23', '1#000']:
            self.assertTrue(_type.cast(value))
コード例 #2
0
    def test_constraints_pattern_valid_value(self):
        '''value is valid for pattern'''
        value = '7.123'
        field = self._make_default_field(type='number',
                                         constraints={"pattern": "7.[0-9]{3}"})

        _type = types.NumberType(field)

        self.assertEqual(_type.cast(value), decimal.Decimal(value))
コード例 #3
0
    def test_constraints_enum_valid_value(self):
        '''value is in enum array'''
        value = 5
        field = self._make_default_field(
            type='number', constraints={'enum': ["1.0", "3.0", "5.0"]})

        _type = types.NumberType(field)

        self.assertEqual(_type.cast(value), value)
コード例 #4
0
    def test_number_type_with_currency_format_false(self):
        value1 = '10,000a.00'
        value2 = '10+000.00'
        value3 = '$10:000.00'
        self.field['format'] = 'currency'
        _type = types.NumberType(self.field)

        self.assertRaises(exceptions.InvalidCastError, _type.cast, value1)
        self.assertRaises(exceptions.InvalidCastError, _type.cast, value2)
        self.assertRaises(exceptions.InvalidCastError, _type.cast, value3)
コード例 #5
0
    def test_number_type_with_currency_format_true(self):
        self.field['format'] = 'currency'
        _type = types.NumberType(self.field)
        for value in [
                '10,000.00',
                '10,000,000.00',
                '$10000.00',
                '  10,000.00 €',
        ]:
            self.assertTrue(_type.cast(value))

        self.field['decimalChar'] = ','
        self.field['groupChar'] = ' '

        _type = types.NumberType(self.field)
        for value in [
                '10 000,00',
                '10 000 000,00',
                '10000,00 ₪',
                '  10 000,00 £',
        ]:
            self.assertTrue(_type.cast(value))
コード例 #6
0
    def test_constraints_pattern_invalid_value(self):
        '''value is invalid for pattern'''
        value = '7.12'
        field = self._make_default_field(type='number',
                                         constraints={"pattern": "7.[0-9]{3}"})

        _type = types.NumberType(field)

        with pytest.raises(exceptions.ConstraintError) as e:
            _type.cast(value)
        self.assertEqual(
            e.value.msg, "The value for field 'Name' "
            "must match the pattern")
コード例 #7
0
    def test_constraints_enum_invalid_value(self):
        '''value is not in enum array'''
        value = 2
        field = self._make_default_field(
            type='number', constraints={'enum': ["1.0", "3.0", "5.0"]})

        _type = types.NumberType(field)

        with pytest.raises(exceptions.ConstraintError) as e:
            _type.cast(value)
        self.assertEqual(
            e.value.msg, "The value for field 'Name' "
            "must be in the enum array")
コード例 #8
0
 def test_number_type_with_already_cast_value(self):
     for value in [1, 1.0, Decimal(1)]:
         for format in ['default', 'currency']:
             self.field['format'] = format
             _type = types.NumberType(self.field)
             self.assertEqual(_type.cast(value), value)
コード例 #9
0
    def test_number_type_simple_false(self):
        value = 'string'
        _type = types.NumberType(self.field)

        self.assertRaises(exceptions.InvalidCastError, _type.cast, value)
コード例 #10
0
    def test_number_type_simple_true(self):
        value = '10.00'
        _type = types.NumberType(self.field)

        self.assertEquals(_type.cast(value), Decimal(value))