Browse Source

[IMP] mis_builder: comparison rendering tests and tweaks

pull/189/head
Stéphane Bidoul 9 years ago
parent
commit
67ea6c8ce1
  1. 2
      mis_builder/CHANGES.rst
  2. 8
      mis_builder/models/mis_report.py
  3. 61
      mis_builder/tests/test_render.py

2
mis_builder/CHANGES.rst

@ -13,6 +13,8 @@ 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] Percentage difference are rounded to 1 digit instead of the kpi's
rounding, as the KPI rounding does not make sense in this case
* [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

8
mis_builder/models/mis_report.py

@ -575,7 +575,7 @@ class MisReportKpi(models.Model):
return unicode(value)
def compare_and_render(self, lang, value, base_value,
average_value, average_base_value):
average_value=1, average_base_value=1):
""" render the comparison of two KPI values, ready for display
Returns a tuple, with the numeric comparison and its string rendering.
@ -589,7 +589,7 @@ class MisReportKpi(models.Model):
base_value = AccountingNone
if self.type == 'pct':
delta = value - base_value
if delta and round(delta, self.dp) != 0:
if delta and round(delta, self.dp + 2) != 0:
return delta, self._render_num(
lang,
delta,
@ -615,8 +615,10 @@ class MisReportKpi(models.Model):
return delta, self._render_num(
lang,
delta,
0.01, self.dp, '', '%',
0.01, 1, '', '%',
sign='+')
else:
return AccountingNone, ''
return 0, ''
def _render_num(self, lang, value, divider,

61
mis_builder/tests/test_render.py

@ -79,3 +79,64 @@ class TestRendering(common.TransactionCase):
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é'))
def test_compare_num_pct(self):
self.assertEquals('pct', self.kpi.compare_method)
self.assertEquals((1.0, u'+100.0\xa0%'),
self.kpi.compare_and_render(self.lang, 100, 50))
self.assertEquals((0.5, u'+50.0\xa0%'),
self.kpi.compare_and_render(self.lang, 75, 50))
self.assertEquals((0.5, u'+50.0\xa0%'),
self.kpi.compare_and_render(self.lang, -25, -50))
self.assertEquals((1.0, u'+100.0\xa0%'),
self.kpi.compare_and_render(self.lang, 0, -50))
self.assertEquals((2.0, u'+200.0\xa0%'),
self.kpi.compare_and_render(self.lang, 50, -50))
self.assertEquals((-0.5, u'\u201150.0\xa0%'),
self.kpi.compare_and_render(self.lang, 25, 50))
self.assertEquals((-1.0, u'\u2011100.0\xa0%'),
self.kpi.compare_and_render(self.lang, 0, 50))
self.assertEquals((-2.0, u'\u2011200.0\xa0%'),
self.kpi.compare_and_render(self.lang, -50, 50))
self.assertEquals((-0.5, u'\u201150.0\xa0%'),
self.kpi.compare_and_render(self.lang, -75, -50))
self.assertEquals((AccountingNone, u''),
self.kpi.compare_and_render(
self.lang, 50, AccountingNone))
self.assertEquals((AccountingNone, u''),
self.kpi.compare_and_render(
self.lang, 50, None))
self.assertEquals((-1.0, u'\u2011100.0\xa0%'),
self.kpi.compare_and_render(
self.lang, AccountingNone, 50))
self.assertEquals((-1.0, u'\u2011100.0\xa0%'),
self.kpi.compare_and_render(
self.lang, None, 50))
def test_compare_num_diff(self):
self.kpi.compare_method = 'diff'
self.assertEquals((25, u'+25'),
self.kpi.compare_and_render(self.lang, 75, 50))
self.assertEquals((-25, u'\u201125'),
self.kpi.compare_and_render(self.lang, 25, 50))
self.kpi.suffix = u''
self.assertEquals((-25, u'\u201125\xa0'),
self.kpi.compare_and_render(self.lang, 25, 50))
self.kpi.suffix = u''
self.assertEquals((50.0, u'+50'),
self.kpi.compare_and_render(
self.lang, 50, AccountingNone))
self.assertEquals((50.0, u'+50'),
self.kpi.compare_and_render(
self.lang, 50, None))
self.assertEquals((-50.0, u'\u201150'),
self.kpi.compare_and_render(
self.lang, AccountingNone, 50))
self.assertEquals((-50.0, u'\u201150'),
self.kpi.compare_and_render(
self.lang, None, 50))
def test_compare_pct(self):
self.kpi.type = 'pct'
self.assertEquals((0.25, u'+25\xa0pp'),
self.kpi.compare_and_render(self.lang, 0.75, 0.50))
Loading…
Cancel
Save