You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.8 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 ACSONE SA/NV (<http://acsone.eu>)
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. import odoo.tests.common as common
  5. from ..models.accounting_none import AccountingNone
  6. from ..models.mis_report_style import (
  7. TYPE_NUM, TYPE_PCT, TYPE_STR, CMP_DIFF, CMP_PCT
  8. )
  9. class TestRendering(common.TransactionCase):
  10. def setUp(self):
  11. super(TestRendering, self).setUp()
  12. self.style_obj = self.env['mis.report.style']
  13. self.kpi_obj = self.env['mis.report.kpi']
  14. self.style = self.style_obj.create(dict(
  15. name='teststyle',
  16. ))
  17. self.lang = self.env['res.lang'].search([('code', '=', 'en_US')])[0]
  18. def _render(self, value, type=TYPE_NUM):
  19. style_props = self.style_obj.merge([self.style])
  20. return self.style_obj.render(self.lang, style_props, type, value)
  21. def _compare_and_render(self, value, base_value,
  22. type=TYPE_NUM, compare_method=CMP_PCT):
  23. style_props = self.style_obj.merge([self.style])
  24. return self.style_obj.compare_and_render(self.lang, style_props,
  25. type, compare_method,
  26. value, base_value)[:2]
  27. def test_render(self):
  28. self.assertEquals(u'1', self._render(1))
  29. self.assertEquals(u'1', self._render(1.1))
  30. self.assertEquals(u'2', self._render(1.6))
  31. self.style.dp_inherit = False
  32. self.style.dp = 2
  33. self.assertEquals(u'1.00', self._render(1))
  34. self.assertEquals(u'1.10', self._render(1.1))
  35. self.assertEquals(u'1.60', self._render(1.6))
  36. self.assertEquals(u'1.61', self._render(1.606))
  37. self.assertEquals(u'12,345.67', self._render(12345.67))
  38. def test_render_negative(self):
  39. # non breaking hyphen
  40. self.assertEquals(u'\u20111', self._render(-1))
  41. def test_render_zero(self):
  42. self.assertEquals(u'0', self._render(0))
  43. self.assertEquals(u'', self._render(None))
  44. self.assertEquals(u'', self._render(AccountingNone))
  45. def test_render_suffix(self):
  46. self.style.suffix_inherit = False
  47. self.style.suffix = u''
  48. self.assertEquals(u'1\xa0', self._render(1))
  49. self.style.suffix = u'k€'
  50. self.style.divider_inherit = False
  51. self.style.divider = '1e3'
  52. self.assertEquals(u'1\xa0k€', self._render(1000))
  53. def test_render_prefix(self):
  54. self.style.prefix_inherit = False
  55. self.style.prefix = u'$'
  56. self.assertEquals(u'$\xa01', self._render(1))
  57. self.style.prefix = u'k$'
  58. self.style.divider_inherit = False
  59. self.style.divider = '1e3'
  60. self.assertEquals(u'k$\xa01', self._render(1000))
  61. def test_render_divider(self):
  62. self.style.divider_inherit = False
  63. self.style.divider = '1e3'
  64. self.style.dp_inherit = False
  65. self.style.dp = 0
  66. self.assertEquals(u'1', self._render(1000))
  67. self.style.divider = '1e6'
  68. self.style.dp = 3
  69. self.assertEquals(u'0.001', self._render(1000))
  70. self.style.divider = '1e-3'
  71. self.style.dp = 0
  72. self.assertEquals(u'1,000', self._render(1))
  73. self.style.divider = '1e-6'
  74. self.style.dp = 0
  75. self.assertEquals(u'1,000,000', self._render(1))
  76. def test_render_pct(self):
  77. self.assertEquals(u'100\xa0%', self._render(1, TYPE_PCT))
  78. self.assertEquals(u'50\xa0%', self._render(0.5, TYPE_PCT))
  79. self.style.dp_inherit = False
  80. self.style.dp = 2
  81. self.assertEquals(u'51.23\xa0%', self._render(0.5123, TYPE_PCT))
  82. def test_render_string(self):
  83. self.assertEquals(u'', self._render('', TYPE_STR))
  84. self.assertEquals(u'', self._render(None, TYPE_STR))
  85. self.assertEquals(u'abcdé', self._render(u'abcdé', TYPE_STR))
  86. def test_compare_num_pct(self):
  87. self.assertEquals((1.0, u'+100.0\xa0%'),
  88. self._compare_and_render(100, 50))
  89. self.assertEquals((0.5, u'+50.0\xa0%'),
  90. self._compare_and_render(75, 50))
  91. self.assertEquals((0.5, u'+50.0\xa0%'),
  92. self._compare_and_render(-25, -50))
  93. self.assertEquals((1.0, u'+100.0\xa0%'),
  94. self._compare_and_render(0, -50))
  95. self.assertEquals((2.0, u'+200.0\xa0%'),
  96. self._compare_and_render(50, -50))
  97. self.assertEquals((-0.5, u'\u201150.0\xa0%'),
  98. self._compare_and_render(25, 50))
  99. self.assertEquals((-1.0, u'\u2011100.0\xa0%'),
  100. self._compare_and_render(0, 50))
  101. self.assertEquals((-2.0, u'\u2011200.0\xa0%'),
  102. self._compare_and_render(-50, 50))
  103. self.assertEquals((-0.5, u'\u201150.0\xa0%'),
  104. self._compare_and_render(-75, -50))
  105. self.assertEquals((AccountingNone, u''),
  106. self._compare_and_render(50, AccountingNone))
  107. self.assertEquals((AccountingNone, u''),
  108. self._compare_and_render(50, None))
  109. self.assertEquals((-1.0, u'\u2011100.0\xa0%'),
  110. self._compare_and_render(AccountingNone, 50))
  111. self.assertEquals((-1.0, u'\u2011100.0\xa0%'),
  112. self._compare_and_render(None, 50))
  113. def test_compare_num_diff(self):
  114. self.assertEquals((25, u'+25'),
  115. self._compare_and_render(75, 50,
  116. TYPE_NUM, CMP_DIFF))
  117. self.assertEquals((-25, u'\u201125'),
  118. self._compare_and_render(25, 50,
  119. TYPE_NUM, CMP_DIFF))
  120. self.style.suffix_inherit = False
  121. self.style.suffix = u''
  122. self.assertEquals((-25, u'\u201125\xa0'),
  123. self._compare_and_render(25, 50,
  124. TYPE_NUM, CMP_DIFF))
  125. self.style.suffix = u''
  126. self.assertEquals((50.0, u'+50'),
  127. self._compare_and_render(50, AccountingNone,
  128. TYPE_NUM, CMP_DIFF))
  129. self.assertEquals((50.0, u'+50'),
  130. self._compare_and_render(50, None,
  131. TYPE_NUM, CMP_DIFF))
  132. self.assertEquals((-50.0, u'\u201150'),
  133. self._compare_and_render(AccountingNone, 50,
  134. TYPE_NUM, CMP_DIFF))
  135. self.assertEquals((-50.0, u'\u201150'),
  136. self._compare_and_render(None, 50,
  137. TYPE_NUM, CMP_DIFF))
  138. def test_compare_pct(self):
  139. self.assertEquals((0.25, u'+25\xa0pp'),
  140. self._compare_and_render(0.75, 0.50, TYPE_PCT))