def afterSetUp(self): self.field = FloatField("test_field") self.widget = self.field.widget self.validator = self.field.validator
class TestFloatField(ERP5TypeTestCase): """Tests Float field """ def getTitle(self): return "Float Field" def afterSetUp(self): self.field = FloatField("test_field") self.widget = self.field.widget self.validator = self.field.validator def test_format_thousand_separator_point(self): self.field.values["input_style"] = "-1 234.5" self.assertEqual("1 000.0", self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_coma(self): self.field.values["input_style"] = "-1 234,5" self.assertEqual("1 000,0", self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_point_coma(self): self.field.values["input_style"] = "-1.234,5" self.assertEqual("1.000,0", self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_coma_point(self): self.field.values["input_style"] = "-1,234.5" self.assertEqual("1,000.0", self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_first_separator(self): # test for an edge case bug bug, ",100,000.0" was displayed (with leading coma) self.field.values["input_style"] = "-1,234.5" self.assertEqual("100,000.0", self.widget.format_value(self.field, 100000)) self.assertEqual("-100,000.0", self.widget.format_value(self.field, -100000)) def test_format_with_separator_and_precision0(self): self.field.values["input_style"] = "-1,234.5" self.field.values["precision"] = 0 self.assertEqual("-1,000", self.widget.format_value(self.field, -1000.25)) self.assertEqual("-1,000", self.widget.format_value(self.field, -1000.49)) self.assertEqual("-1,001", self.widget.format_value(self.field, -1000.99)) self.assertEqual("-1,001", self.widget.format_value(self.field, -1000.80)) self.assertEqual("-1,001", self.widget.format_value(self.field, -1000.70)) self.assertEqual("-1,001", self.widget.format_value(self.field, -1000.60)) self.assertEqual("-1,001", self.widget.format_value(self.field, -1000.59)) self.assertEqual("-1,001", self.widget.format_value(self.field, -1000.51)) # this is not -1,001 (is this a specification?) self.assertEqual("-1,000", self.widget.format_value(self.field, -1000.50)) def test_format_percent_style(self): self.field.values["input_style"] = "-12.3%" self.assertEqual("10.0%", self.widget.format_value(self.field, 0.1)) def test_format_precision(self): self.field.values["precision"] = 0 self.assertEqual("12", self.widget.format_value(self.field, 12.34)) # value is rounded self.assertEqual("13", self.widget.format_value(self.field, 12.9)) field_value_cache.clear() # call this before changing internal field values. self.field.values["precision"] = 2 self.assertEqual("0.01", self.widget.format_value(self.field, 0.011)) # value is rounded self.assertEqual("0.01", self.widget.format_value(self.field, 0.009999)) self.assertEqual("1.00", self.widget.format_value(self.field, sum([0.1] * 10))) self.assertEqual("566.30", self.widget.format_value(self.field, 281.80 + 54.50 + 230.00)) def test_format_no_precision(self): self.assertEqual("7.2", self.widget.format_value(self.field, 7.2)) self.assertEqual("0.009999", self.widget.format_value(self.field, 0.009999)) self.assertEqual("1000.0", self.widget.format_value(self.field, 1000)) def test_render_view(self): self.field.values["input_style"] = "-1 234.5" self.field.values["precision"] = 2 self.field.values["editable"] = 0 self.assertEqual("1 000.00", self.field.render(1000)) def test_render_dict(self): self.field.values["input_style"] = "-1 234.5" self.field.values["precision"] = 4 self.assertEqual(dict(query=0.12345, format="0.0000", type="float"), self.field.render_dict(0.12345)) # this also work when using , as decimal separator self.field.values["input_style"] = "-1.234,5" self.assertEqual(dict(query=0.12345, format="0.0000", type="float"), self.field.render_dict(0.12345)) def test_render_string_value(self): self.field.values["precision"] = 2 self.field.values["editable"] = 0 self.assertEqual("12.34", self.field.render("12.34")) self.assertEqual("not float", self.field.render("not float")) def test_percent_style_render_string_value(self): self.field.values["input_style"] = "-12.3%" self.field.values["editable"] = 0 self.assertEqual("-12.34%", self.field.render("-0.1234")) self.assertEqual("not float", self.field.render("not float")) def test_render_big_numbers(self): self.field.values["precision"] = 2 self.field.values["editable"] = 0 self.assertEqual("10000000000000.00", self.field.render(10000000000000)) self.assertEqual("1e+20", self.field.render(1e20)) def test_validate_thousand_separator_point(self): self.field.values["input_style"] = "-1 234.5" self.portal.REQUEST.set("field_test_field", "1 000.0") self.assertEqual(1000, self.validator.validate(self.field, "field_test_field", self.portal.REQUEST)) def test_validate_thousand_separator_coma(self): self.field.values["input_style"] = "-1 234,5" self.portal.REQUEST.set("field_test_field", "1 000,0") self.assertEqual(1000, self.validator.validate(self.field, "field_test_field", self.portal.REQUEST)) def test_validate_thousand_separator_point_coma(self): self.field.values["input_style"] = "-1.234,5" self.portal.REQUEST.set("field_test_field", "1.000,0") self.assertEqual(1000, self.validator.validate(self.field, "field_test_field", self.portal.REQUEST)) def test_validate_thousand_separator_coma_point(self): self.field.values["input_style"] = "-1,234.5" self.portal.REQUEST.set("field_test_field", "1,000.0") self.assertEqual(1000, self.validator.validate(self.field, "field_test_field", self.portal.REQUEST)) def test_validate_percent_style(self): self.field.values["input_style"] = "-12.3%" self.portal.REQUEST.set("field_test_field", "10.0%") self.assertEqual(0.1, self.validator.validate(self.field, "field_test_field", self.portal.REQUEST)) def test_validate_not_float(self): self.portal.REQUEST.set("field_test_field", "not_float") self.assertRaises(ValidationError, self.validator.validate, self.field, "field_test_field", self.portal.REQUEST) def test_validate_two_comma(self): self.field.values["input_style"] = "-1.234,5" self.portal.REQUEST.set("field_test_field", "1,000,0") self.assertRaises(ValidationError, self.validator.validate, self.field, "field_test_field", self.portal.REQUEST) def test_validate_two_dots(self): self.field.values["input_style"] = "-1,234.5" self.portal.REQUEST.set("field_test_field", "1.000.0") self.assertRaises(ValidationError, self.validator.validate, self.field, "field_test_field", self.portal.REQUEST) def test_validate_precision0(self): # Check the consistency among the precision and user inputs self.field.values["input_style"] = "-1,234.5" self.field.values["precision"] = 0 self.portal.REQUEST.set("field_test_field", "1.00") self.assertRaises(ValidationError, self.validator.validate, self.field, "field_test_field", self.portal.REQUEST) def test_validate_precision0_with_percent(self): # Check the precision and user inputs when the style is '%' self.field.values["input_style"] = "-12.5%" self.field.values["precision"] = 1 self.assertEqual("12.5%", self.widget.format_value(self.field, 0.125)) self.portal.REQUEST.set("field_test_field", "0.1255") self.assertRaises(ValidationError, self.validator.validate, self.field, "field_test_field", self.portal.REQUEST) def test_render_odt(self): self.field.values["input_style"] = "-1 234.5" self.field.values["default"] = 1000 self.assertEqual("1 000.0", self.field.render_odt(as_string=False).text) def test_render_odg(self): self.field.values["input_style"] = "-1 234.5" self.field.values["default"] = 1000 test_value = self.field.render_odg(as_string=False).xpath( "%s/text()" % ODG_XML_WRAPPING_XPATH, namespaces=NSMAP )[0] self.assertEqual("1 000.0", test_value) def test_render_odt_variable(self): self.field.values["default"] = 1000.0 node = self.field.render_odt_variable(as_string=False) self.assertEqual(node.get("{%s}value-type" % NSMAP["office"]), "float") self.assertEqual(node.get("{%s}value" % NSMAP["office"]), str(1000.0)) def test_fullwidth_number_conversion(self): self.portal.REQUEST.set("field_test_field", "123.45") self.assertEqual(123.45, self.validator.validate(self.field, "field_test_field", self.portal.REQUEST)) def test_fullwidth_minus_number_conversion(self): self.portal.REQUEST.set("field_test_field", "−123.45") self.assertEqual(-123.45, self.validator.validate(self.field, "field_test_field", self.portal.REQUEST))
def afterSetUp(self): self.field = FloatField('test_field') self.widget = self.field.widget self.validator = self.field.validator
class TestFloatField(ERP5TypeTestCase): """Tests Float field """ def getTitle(self): return "Float Field" def afterSetUp(self): self.field = FloatField('test_field') self.widget = self.field.widget self.validator = self.field.validator def test_format_thousand_separator_point(self): self.field.values['input_style'] = '-1 234.5' self.assertEquals('1 000.0', self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_coma(self): self.field.values['input_style'] = '-1 234,5' self.assertEquals('1 000,0', self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_point_coma(self): self.field.values['input_style'] = '-1.234,5' self.assertEquals('1.000,0', self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_coma_point(self): self.field.values['input_style'] = '-1,234.5' self.assertEquals('1,000.0', self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_first_separator(self): # test for an edge case bug bug, ",100,000.0" was displayed (with leading coma) self.field.values['input_style'] = '-1,234.5' self.assertEquals('100,000.0', self.widget.format_value(self.field, 100000)) self.assertEquals('-100,000.0', self.widget.format_value(self.field, -100000)) def test_format_with_separator_and_precision0(self): self.field.values['input_style'] = '-1,234.5' self.field.values['precision'] = 0 self.assertEquals('-1,000', self.widget.format_value(self.field, -1000.25)) self.assertEquals('-1,000', self.widget.format_value(self.field, -1000.49)) self.assertEquals('-1,001', self.widget.format_value(self.field, -1000.99)) self.assertEquals('-1,001', self.widget.format_value(self.field, -1000.80)) self.assertEquals('-1,001', self.widget.format_value(self.field, -1000.70)) self.assertEquals('-1,001', self.widget.format_value(self.field, -1000.60)) self.assertEquals('-1,001', self.widget.format_value(self.field, -1000.59)) self.assertEquals('-1,001', self.widget.format_value(self.field, -1000.51)) # this is not -1,001 (is this a specification?) self.assertEquals('-1,000', self.widget.format_value(self.field, -1000.50)) def test_format_percent_style(self): self.field.values['input_style'] = '-12.3%' self.assertEquals('10.0%', self.widget.format_value(self.field, 0.1)) def test_format_precision(self): self.field.values['precision'] = 0 self.assertEquals('12', self.widget.format_value(self.field, 12.34)) # value is rounded self.assertEquals('13', self.widget.format_value(self.field, 12.9)) field_value_cache.clear() # call this before changing internal field values. self.field.values['precision'] = 2 self.assertEquals('0.01', self.widget.format_value(self.field, 0.011)) # value is rounded self.assertEquals('0.01', self.widget.format_value(self.field, 0.009999)) self.assertEquals('1.00', self.widget.format_value(self.field, sum([0.1] * 10))) self.assertEquals('566.30', self.widget.format_value(self.field, 281.80 + 54.50 + 230.00)) def test_format_no_precision(self): self.assertEquals('7.2', self.widget.format_value(self.field, 7.2)) self.assertEquals('0.009999', self.widget.format_value(self.field, 0.009999)) self.assertEquals('1000.0', self.widget.format_value(self.field, 1000)) def test_render_view(self): self.field.values['input_style'] = '-1 234.5' self.field.values['precision'] = 2 self.field.values['editable'] = 0 self.assertEquals('1 000.00', self.field.render(1000)) def test_render_dict(self): self.field.values['input_style'] = '-1 234.5' self.field.values['precision'] = 4 self.assertEquals(dict(query=0.12345, format='0.0000', type='float'), self.field.render_dict(0.12345)) # this also work when using , as decimal separator self.field.values['input_style'] = '-1.234,5' self.assertEquals(dict(query=0.12345, format='0.0000', type='float'), self.field.render_dict(0.12345)) def test_render_string_value(self): self.field.values['precision'] = 2 self.field.values['editable'] = 0 self.assertEquals('12.34', self.field.render("12.34")) self.assertEquals('not float', self.field.render("not float")) def test_percent_style_render_string_value(self): self.field.values['input_style'] = '-12.3%' self.field.values['editable'] = 0 self.assertEquals('-12.34%', self.field.render("-0.1234")) self.assertEquals('not float', self.field.render("not float")) def test_render_big_numbers(self): self.field.values['precision'] = 2 self.field.values['editable'] = 0 self.assertEquals('10000000000000.00', self.field.render(10000000000000)) self.assertEquals('1e+20', self.field.render(1e+20)) def test_validate_thousand_separator_point(self): self.field.values['input_style'] = '-1 234.5' self.portal.REQUEST.set('field_test_field', '1 000.0') self.assertEquals(1000, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_validate_thousand_separator_coma(self): self.field.values['input_style'] = '-1 234,5' self.portal.REQUEST.set('field_test_field', '1 000,0') self.assertEquals(1000, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_validate_thousand_separator_point_coma(self): self.field.values['input_style'] = '-1.234,5' self.portal.REQUEST.set('field_test_field', '1.000,0') self.assertEquals(1000, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_validate_thousand_separator_coma_point(self): self.field.values['input_style'] = '-1,234.5' self.portal.REQUEST.set('field_test_field', '1,000.0') self.assertEquals(1000, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_validate_percent_style(self): self.field.values['input_style'] = '-12.3%' self.portal.REQUEST.set('field_test_field', '10.0%') self.assertEquals(0.1, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_validate_not_float(self): self.portal.REQUEST.set('field_test_field', 'not_float') self.assertRaises(ValidationError, self.validator.validate, self.field, 'field_test_field', self.portal.REQUEST) def test_validate_two_comma(self): self.field.values['input_style'] = '-1.234,5' self.portal.REQUEST.set('field_test_field', '1,000,0') self.assertRaises(ValidationError, self.validator.validate, self.field, 'field_test_field', self.portal.REQUEST) def test_validate_two_dots(self): self.field.values['input_style'] = '-1,234.5' self.portal.REQUEST.set('field_test_field', '1.000.0') self.assertRaises(ValidationError, self.validator.validate, self.field, 'field_test_field', self.portal.REQUEST) def test_render_odt(self): self.field.values['input_style'] = '-1 234.5' self.field.values['default'] = 1000 self.assertEquals('1 000.0', self.field.render_odt(as_string=False).text) def test_render_odg(self): self.field.values['input_style'] = '-1 234.5' self.field.values['default'] = 1000 test_value = self.field.render_odg(as_string=False)\ .xpath('%s/text()' % ODG_XML_WRAPPING_XPATH, namespaces=NSMAP)[0] self.assertEquals('1 000.0', test_value) def test_render_odt_variable(self): self.field.values['default'] = 1000.0 node = self.field.render_odt_variable(as_string=False) self.assertEquals(node.get('{%s}value-type' % NSMAP['office']), 'float') self.assertEquals(node.get('{%s}value' % NSMAP['office']), str(1000.0)) def test_fullwidth_number_conversion(self): self.portal.REQUEST.set('field_test_field', '123.45') self.assertEquals(123.45, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_fullwidth_minus_number_conversion(self): self.portal.REQUEST.set('field_test_field', '−123.45') self.assertEquals(-123.45, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST))
class TestFloatField(ERP5TypeTestCase): """Tests Float field """ def getTitle(self): return "Float Field" def afterSetUp(self): self.field = FloatField('test_field') self.widget = self.field.widget self.validator = self.field.validator def test_format_thousand_separator_point(self): self.field.values['input_style'] = '-1 234.5' self.assertEqual('1 000.0', self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_coma(self): self.field.values['input_style'] = '-1 234,5' self.assertEqual('1 000,0', self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_point_coma(self): self.field.values['input_style'] = '-1.234,5' self.assertEqual('1.000,0', self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_coma_point(self): self.field.values['input_style'] = '-1,234.5' self.assertEqual('1,000.0', self.widget.format_value(self.field, 1000)) def test_format_thousand_separator_first_separator(self): # test for an edge case bug bug, ",100,000.0" was displayed (with leading coma) self.field.values['input_style'] = '-1,234.5' self.assertEqual('100,000.0', self.widget.format_value(self.field, 100000)) self.assertEqual('-100,000.0', self.widget.format_value(self.field, -100000)) def test_format_with_separator_and_precision0(self): self.field.values['input_style'] = '-1,234.5' self.field.values['precision'] = 0 self.assertEqual('-1,000', self.widget.format_value(self.field, -1000.25)) self.assertEqual('-1,000', self.widget.format_value(self.field, -1000.49)) self.assertEqual('-1,001', self.widget.format_value(self.field, -1000.99)) self.assertEqual('-1,001', self.widget.format_value(self.field, -1000.80)) self.assertEqual('-1,001', self.widget.format_value(self.field, -1000.70)) self.assertEqual('-1,001', self.widget.format_value(self.field, -1000.60)) self.assertEqual('-1,001', self.widget.format_value(self.field, -1000.59)) self.assertEqual('-1,001', self.widget.format_value(self.field, -1000.51)) # this is not -1,001 (is this a specification?) self.assertEqual('-1,000', self.widget.format_value(self.field, -1000.50)) def test_format_percent_style(self): self.field.values['input_style'] = '-12.3%' self.assertEqual('10.0%', self.widget.format_value(self.field, 0.1)) def test_format_precision(self): self.field.values['precision'] = 0 self.assertEqual('12', self.widget.format_value(self.field, 12.34)) # value is rounded self.assertEqual('13', self.widget.format_value(self.field, 12.9)) field_value_cache.clear( ) # call this before changing internal field values. self.field.values['precision'] = 2 self.assertEqual('0.01', self.widget.format_value(self.field, 0.011)) # value is rounded self.assertEqual('0.01', self.widget.format_value(self.field, 0.009999)) self.assertEqual('1.00', self.widget.format_value(self.field, sum([0.1] * 10))) self.assertEqual( '566.30', self.widget.format_value(self.field, 281.80 + 54.50 + 230.00)) def test_format_no_precision(self): self.assertEqual('7.2', self.widget.format_value(self.field, 7.2)) self.assertEqual('0.009999', self.widget.format_value(self.field, 0.009999)) self.assertEqual('1000.0', self.widget.format_value(self.field, 1000)) def test_render_view(self): self.field.values['input_style'] = '-1 234.5' self.field.values['precision'] = 2 self.field.values['editable'] = 0 self.assertEqual('1 000.00', self.field.render(1000)) def test_render_dict(self): self.field.values['input_style'] = '-1 234.5' self.field.values['precision'] = 4 self.assertEqual(dict(query=0.12345, format='0.0000', type='float'), self.field.render_dict(0.12345)) # this also work when using , as decimal separator self.field.values['input_style'] = '-1.234,5' self.assertEqual(dict(query=0.12345, format='0.0000', type='float'), self.field.render_dict(0.12345)) def test_render_string_value(self): self.field.values['precision'] = 2 self.field.values['editable'] = 0 self.assertEqual('12.34', self.field.render("12.34")) self.assertEqual('not float', self.field.render("not float")) def test_percent_style_render_string_value(self): self.field.values['input_style'] = '-12.3%' self.field.values['editable'] = 0 self.assertEqual('-12.34%', self.field.render("-0.1234")) self.assertEqual('not float', self.field.render("not float")) def test_render_big_numbers(self): self.field.values['precision'] = 2 self.field.values['editable'] = 0 self.assertEqual('10000000000000.00', self.field.render(10000000000000)) self.assertEqual('1e+20', self.field.render(1e+20)) def test_validate_thousand_separator_point(self): self.field.values['input_style'] = '-1 234.5' self.portal.REQUEST.set('field_test_field', '1 000.0') self.assertEqual( 1000, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_validate_thousand_separator_coma(self): self.field.values['input_style'] = '-1 234,5' self.portal.REQUEST.set('field_test_field', '1 000,0') self.assertEqual( 1000, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_validate_thousand_separator_point_coma(self): self.field.values['input_style'] = '-1.234,5' self.portal.REQUEST.set('field_test_field', '1.000,0') self.assertEqual( 1000, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_validate_thousand_separator_coma_point(self): self.field.values['input_style'] = '-1,234.5' self.portal.REQUEST.set('field_test_field', '1,000.0') self.assertEqual( 1000, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_validate_percent_style(self): self.field.values['input_style'] = '-12.3%' self.portal.REQUEST.set('field_test_field', '10.0%') self.assertEqual( 0.1, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_validate_not_float(self): self.portal.REQUEST.set('field_test_field', 'not_float') self.assertRaises(ValidationError, self.validator.validate, self.field, 'field_test_field', self.portal.REQUEST) def test_validate_two_comma(self): self.field.values['input_style'] = '-1.234,5' self.portal.REQUEST.set('field_test_field', '1,000,0') self.assertRaises(ValidationError, self.validator.validate, self.field, 'field_test_field', self.portal.REQUEST) def test_validate_two_dots(self): self.field.values['input_style'] = '-1,234.5' self.portal.REQUEST.set('field_test_field', '1.000.0') self.assertRaises(ValidationError, self.validator.validate, self.field, 'field_test_field', self.portal.REQUEST) def test_validate_precision0(self): # Check the consistency among the precision and user inputs self.field.values['input_style'] = '-1,234.5' self.field.values['precision'] = 0 self.portal.REQUEST.set('field_test_field', '1.00') self.assertRaises(ValidationError, self.validator.validate, self.field, 'field_test_field', self.portal.REQUEST) def test_validate_precision0_with_percent(self): # Check the precision and user inputs when the style is '%' self.field.values['input_style'] = '-12.5%' self.field.values['precision'] = 1 self.assertEqual('12.5%', self.widget.format_value(self.field, 0.125)) self.portal.REQUEST.set('field_test_field', '0.1255') self.assertRaises(ValidationError, self.validator.validate, self.field, 'field_test_field', self.portal.REQUEST) def test_render_odt(self): self.field.values['input_style'] = '-1 234.5' self.field.values['default'] = 1000 self.assertEqual('1 000.0', self.field.render_odt(as_string=False).text) def test_render_odg(self): self.field.values['input_style'] = '-1 234.5' self.field.values['default'] = 1000 test_value = self.field.render_odg(as_string=False)\ .xpath('%s/text()' % ODG_XML_WRAPPING_XPATH, namespaces=NSMAP)[0] self.assertEqual('1 000.0', test_value) def test_render_odt_variable(self): self.field.values['default'] = 1000.0 node = self.field.render_odt_variable(as_string=False) self.assertEqual(node.get('{%s}value-type' % NSMAP['office']), 'float') self.assertEqual(node.get('{%s}value' % NSMAP['office']), str(1000.0)) def test_fullwidth_number_conversion(self): self.portal.REQUEST.set('field_test_field', '123.45') self.assertEqual( 123.45, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST)) def test_fullwidth_minus_number_conversion(self): self.portal.REQUEST.set('field_test_field', '−123.45') self.assertEqual( -123.45, self.validator.validate(self.field, 'field_test_field', self.portal.REQUEST))