OCA reporting engine fork for dev and update.
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.

116 lines
3.7 KiB

7 years ago
7 years ago
  1. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  2. from odoo.tests.common import TransactionCase
  3. class TestKPI(TransactionCase):
  4. def setUp(self):
  5. super(TestKPI, self).setUp()
  6. def test_invalid_threshold_range(self):
  7. range1 = self.env['kpi.threshold.range'].create({
  8. 'name': 'Range1',
  9. 'min_type': 'static',
  10. 'max_type': 'static',
  11. 'min_fixed_value': 3,
  12. 'max_fixed_value': 1,
  13. })
  14. range2 = self.env['kpi.threshold.range'].create({
  15. 'name': 'Range2',
  16. 'min_type': 'static',
  17. 'max_type': 'static',
  18. 'min_fixed_value': 4,
  19. 'max_fixed_value': 10,
  20. })
  21. self.assertFalse(range1.valid)
  22. self.assertTrue(range2.valid)
  23. def test_invalid_threshold(self):
  24. range1 = self.env['kpi.threshold.range'].create({
  25. 'name': 'Range1',
  26. 'min_type': 'static',
  27. 'max_type': 'static',
  28. 'min_fixed_value': 1,
  29. 'max_fixed_value': 4,
  30. })
  31. range2 = self.env['kpi.threshold.range'].create({
  32. 'name': 'Range2',
  33. 'min_type': 'static',
  34. 'max_type': 'static',
  35. 'min_fixed_value': 4,
  36. 'max_fixed_value': 10,
  37. })
  38. range3 = self.env['kpi.threshold.range'].create({
  39. 'name': 'Range3',
  40. 'min_type': 'static',
  41. 'max_type': 'static',
  42. 'min_fixed_value': 1,
  43. 'max_fixed_value': 3,
  44. })
  45. range_invalid = self.env['kpi.threshold.range'].create({
  46. 'name': 'RangeInvalid',
  47. 'min_type': 'static',
  48. 'max_type': 'static',
  49. 'min_fixed_value': 3,
  50. 'max_fixed_value': 1,
  51. })
  52. threshold1 = self.env['kpi.threshold'].create({
  53. 'name': 'Threshold1',
  54. 'range_ids': [(6, 0, [range1.id, range2.id])],
  55. })
  56. threshold2 = self.env['kpi.threshold'].create({
  57. 'name': 'Threshold1',
  58. 'range_ids': [(6, 0, [range3.id, range2.id])],
  59. })
  60. threshold3 = self.env['kpi.threshold'].create({
  61. 'name': 'Threshold1',
  62. 'range_ids': [(6, 0, [range_invalid.id, range2.id])],
  63. })
  64. self.assertFalse(threshold1.valid)
  65. self.assertTrue(threshold2.valid)
  66. self.assertFalse(threshold3.valid)
  67. def test_invalid_threshold_range_exception(self):
  68. range_error = self.env['kpi.threshold.range'].create({
  69. 'name': 'RangeError',
  70. 'min_type': 'python',
  71. 'min_code': '<Not a valid python expression>',
  72. 'max_type': 'static',
  73. 'max_fixed_value': 1,
  74. })
  75. self.assertFalse(range_error.valid)
  76. def test_kpi_python(self):
  77. kpi_category = self.env['kpi.category'].create({
  78. 'name': 'Dynamic KPIs'
  79. })
  80. kpi_threshold = self.env['kpi.threshold'].create({
  81. 'name': 'KPI Threshold for dynamic KPIs'
  82. })
  83. kpi_code = """
  84. {
  85. 'value': 1.0,
  86. 'color': '#00FF00'
  87. }
  88. """
  89. kpi = self.env['kpi'].create({
  90. 'name': 'Dynamic python kpi',
  91. 'description': 'Dynamic python kpi',
  92. 'category_id': kpi_category.id,
  93. 'threshold_id': kpi_threshold.id,
  94. 'periodicity': 1,
  95. 'periodicity_uom': 'day',
  96. 'kpi_type': 'python',
  97. 'kpi_code': kpi_code,
  98. })
  99. kpi.update_kpi_value()
  100. kpi_history = self.env['kpi.history'].search(
  101. [('kpi_id', '=', kpi.id)])
  102. self.assertEqual(len(kpi_history), 1)
  103. self.assertEqual(kpi_history.color, '#00FF00')
  104. self.assertEqual(kpi_history.value, 1.0)