Browse Source

[IMP] mis_builder: add value rendering tests, and tune the code

pull/189/head
Stéphane Bidoul 9 years ago
parent
commit
eb409cbd99
  1. 3
      mis_builder/CHANGES.rst
  2. 16
      mis_builder/models/mis_report.py
  3. 1
      mis_builder/tests/__init__.py
  4. 2
      mis_builder/tests/test_fetch_query.py
  5. 81
      mis_builder/tests/test_render.py

3
mis_builder/CHANGES.rst

@ -13,6 +13,9 @@ Part of the work for this release has been done at the Sorrento sprint
April 26-29, 2016. The rest (ie a major refactoring) has been done in
the weeks after.
* [CHG] The divider suffix (k, M, etc) is not inserted automatically anymore
because it is inconsistent when working with prefixes; you need to add it
manually in the suffix
* [IMP] AccountingExpressionProcessor now supports 'balu' expressions
to obtain the unallocated profit/loss of previous fiscal years;
get_unallocated_pl is the corresponding convenience method

16
mis_builder/models/mis_report.py

@ -621,20 +621,22 @@ class MisReportKpi(models.Model):
def _render_num(self, lang, value, divider,
dp, prefix, suffix, sign='-'):
divider_label = _get_selection_label(
self._columns['divider'].selection, divider)
if divider_label == '1':
divider_label = ''
# format number following user language
value = round(value / float(divider or 1), dp) or 0
value = lang.format(
'%%%s.%df' % (sign, dp),
value,
grouping=True)
value = u'%s\N{NO-BREAK SPACE}%s\N{NO-BREAK SPACE}%s%s' % \
(prefix or '', value, divider_label, suffix or '')
value = value.replace('-', u'\N{NON-BREAKING HYPHEN}')
return value
if prefix:
prefix = prefix + u'\N{NO-BREAK SPACE}'
else:
prefix = ''
if suffix:
suffix = u'\N{NO-BREAK SPACE}' + suffix
else:
suffix = ''
return prefix + value + suffix
class MisReportSubkpi(models.Model):

1
mis_builder/tests/__init__.py

@ -7,5 +7,6 @@ from . import test_aep
from . import test_aggregate
from . import test_fetch_query
from . import test_mis_safe_eval
from . import test_render
from . import test_simple_array
from . import test_utc_midnight

2
mis_builder/tests/test_fetch_query.py

@ -21,7 +21,7 @@ class TestFetchQuery(common.TransactionCase):
'parent_row_id': None,
'row_id': u'total_test',
'cols': [{'val': 0,
'val_r': u'\xa00\xa0',
'val_r': u'0',
'val_c': u'total_test = len(test)',
}]
}],

81
mis_builder/tests/test_render.py

@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
# © 2016 ACSONE SA/NV (<http://acsone.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
import openerp.tests.common as common
from ..models.accounting_none import AccountingNone
class TestRendering(common.TransactionCase):
def setUp(self):
super(TestRendering, self).setUp()
self.kpi = self.env['mis.report.kpi'].create(dict(
name='testkpi',
description='Test KPI',
type='num',
dp=0,
))
self.lang = self.env['res.lang'].search([('code', '=', 'en_US')])[0]
def test_render(self):
self.assertEquals(u'1', self.kpi.render(self.lang, 1))
self.assertEquals(u'1', self.kpi.render(self.lang, 1.1))
self.assertEquals(u'2', self.kpi.render(self.lang, 1.6))
self.kpi.dp = 2
self.assertEquals(u'1.00', self.kpi.render(self.lang, 1))
self.assertEquals(u'1.10', self.kpi.render(self.lang, 1.1))
self.assertEquals(u'1.60', self.kpi.render(self.lang, 1.6))
self.assertEquals(u'1.61', self.kpi.render(self.lang, 1.606))
self.assertEquals(u'12,345.67', self.kpi.render(self.lang, 12345.67))
def test_render_negative(self):
# non breaking hyphen
self.assertEquals(u'\u20111', self.kpi.render(self.lang, -1))
def test_render_zero(self):
self.assertEquals(u'0', self.kpi.render(self.lang, 0))
self.assertEquals(u'', self.kpi.render(self.lang, None))
self.assertEquals(u'', self.kpi.render(self.lang, AccountingNone))
def test_render_suffix(self):
self.kpi.suffix = u''
self.assertEquals(u'1\xa0', self.kpi.render(self.lang, 1))
self.kpi.suffix = u'k€'
self.kpi.divider = '1e3'
self.assertEquals(u'1\xa0k€', self.kpi.render(self.lang, 1000))
def test_render_prefix(self):
self.kpi.prefix = u'$'
self.assertEquals(u'$\xa01', self.kpi.render(self.lang, 1))
self.kpi.prefix = u'k$'
self.kpi.divider = '1e3'
self.assertEquals(u'k$\xa01', self.kpi.render(self.lang, 1000))
def test_render_divider(self):
self.kpi.divider = '1e3'
self.kpi.dp = 0
self.assertEquals(u'1', self.kpi.render(self.lang, 1000))
self.kpi.divider = '1e6'
self.kpi.dp = 3
self.assertEquals(u'0.001', self.kpi.render(self.lang, 1000))
self.kpi.divider = '1e-3'
self.kpi.dp = 0
self.assertEquals(u'1,000', self.kpi.render(self.lang, 1))
self.kpi.divider = '1e-6'
self.kpi.dp = 0
self.assertEquals(u'1,000,000', self.kpi.render(self.lang, 1))
def test_render_pct(self):
self.kpi.type = 'pct'
self.assertEquals(u'100\xa0%', self.kpi.render(self.lang, 1))
self.assertEquals(u'50\xa0%', self.kpi.render(self.lang, 0.5))
self.kpi.dp = 2
self.assertEquals(u'51.23\xa0%', self.kpi.render(self.lang, 0.5123))
def test_render_string(self):
self.kpi.type = 'str'
self.assertEquals(u'', self.kpi.render(self.lang, ''))
self.assertEquals(u'', self.kpi.render(self.lang, None))
self.assertEquals(u'abcdé', self.kpi.render(self.lang, u'abcdé'))
Loading…
Cancel
Save