test_qweb_float.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # -*- coding: utf-8 -*-
  2. from odoo.tests import common
  3. class TestFloatExport(common.TransactionCase):
  4. def get_converter(self, name):
  5. FloatField = self.env['ir.qweb.field.float']
  6. _, precision = self.env['decimal.precision.test']._fields[name].get_digits(self.env) or (None, None)
  7. def converter(value, options=None):
  8. record = self.env['decimal.precision.test'].new({name: value})
  9. return FloatField.record_to_html(record, name, options or {})
  10. return converter
  11. def test_basic_float(self):
  12. converter = self.get_converter('float')
  13. self.assertEqual(
  14. converter(42.0),
  15. "42.0")
  16. self.assertEqual(
  17. converter(42.12345),
  18. "42.12345")
  19. converter = self.get_converter('float_2')
  20. self.assertEqual(
  21. converter(42.0),
  22. "42.00")
  23. self.assertEqual(
  24. converter(42.12345),
  25. "42.12")
  26. converter = self.get_converter('float') # don't use float_4 because the field value 42.12345 is already orm converted to 42.1235
  27. self.assertEqual(
  28. converter(42.0, {'precision': 4}),
  29. '42.0000')
  30. self.assertEqual(
  31. converter(42.12345, {'precision': 4}),
  32. '42.1235')
  33. def test_precision_domain(self):
  34. self.env['decimal.precision'].create({
  35. 'name': 'A',
  36. 'digits': 2,
  37. })
  38. self.env['decimal.precision'].create({
  39. 'name': 'B',
  40. 'digits': 6,
  41. })
  42. converter = self.get_converter('float')
  43. self.assertEqual(
  44. converter(42.0, {'decimal_precision': 'A'}),
  45. '42.00')
  46. self.assertEqual(
  47. converter(42.0, {'decimal_precision': 'B'}),
  48. '42.000000')
  49. converter = self.get_converter('float') # don't use float_4 because the field value 42.12345 is orm converted to 42.1235
  50. self.assertEqual(
  51. converter(42.12345, {'decimal_precision': 'A'}),
  52. '42.12')
  53. self.assertEqual(
  54. converter(42.12345, {'decimal_precision': 'B'}),
  55. '42.123450')