def test_localization(self): field = DecimalField() self.assertEqual('', field.localize(None)) self.assertEqual('', field.localize('')) self.assertEqual('1.34', field.localize(Decimal('1.34'))) self.assertEqual('1,111,000.34', field.localize(Decimal('1111000.34'))) self.assertEqual('1,111,000.33', field.localize(Decimal('1111000.33')))
def test_validation(self): field = DecimalField() field._set_attr_name('n') self.assertIsNone(field.validate(None)) self.assertIsNone(field.validate('')) self.assertIsNone(field.validate('0')) self.assertIsNone(field.validate('1')) self.assertIsNone(field.validate(Decimal('1.32'))) self.assertEqual('Must be a number', field.validate('foo')) self.assertEqual('Must be a number', field.validate('123h')) self.assertEqual('Must be a number', field.validate('0x456'))
def fill_with_model(self, model, *fields): dct = super(ItemForm, self).fill_with_model(model, *fields) dct['total'] = DecimalField().localize(model.total()) return dct
def test_validation_upper(self): field = DecimalField(upper=1) field._set_attr_name('n') self.assertIsNone(field.validate('1')) self.assertEqual('Must be less than 1', field.validate('2'))
def test_validation_lower(self): field = DecimalField(lower=1) field._set_attr_name('n') self.assertIsNone(field.validate('1')) self.assertEqual('Must be greater than 1', field.validate('0'))
def test_normalization(self): field = DecimalField() self.assertIsNone(field.normalize(None)) self.assertIsNone(field.normalize('')) self.assertEqual(Decimal('1.34'), field.normalize('1.339999999')) self.assertEqual(Decimal('0.00'), field.normalize('0')) self.assertEqual(Decimal('1.34'), field.normalize('1.34')) self.assertEqual(Decimal('1.34'), field.normalize(Decimal('1.34'))) self.assertEqual(Decimal('1000.34'), field.normalize('1,000.34')) self.assertEqual(Decimal('1111000.34'), field.normalize('1,111,000.34')) self.assertEqual(Decimal('1111000.34'), field.normalize('1,111,000.3399999')) field = DecimalField(decimal_places=3) self.assertEqual(Decimal('1.340'), field.normalize('1.339999999'))
def test_zero(self): field = DecimalField() self.assertEqual('0', field.localize(Decimal('0.0'))) self.assertEqual('0', field.localize(Decimal('0')))