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.

123 lines
4.2 KiB

  1. # Copyright 2020 Creu Blanca
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo.tests.common import TransactionCase
  4. from odoo.exceptions import ValidationError
  5. from odoo.tests.common import Form
  6. class TestKpiDashboard(TransactionCase):
  7. def setUp(self):
  8. super(TestKpiDashboard, self).setUp()
  9. self.kpi_01 = self.env['kpi.kpi'].create({
  10. 'name': 'KPI 01',
  11. 'computation_method': 'function',
  12. 'widget': 'number',
  13. 'function': 'test_demo_number'
  14. })
  15. self.kpi_02 = self.env['kpi.kpi'].create({
  16. 'name': 'KPI 02',
  17. 'computation_method': 'function',
  18. 'widget': 'number',
  19. 'function': 'test_demo_number'
  20. })
  21. self.dashboard = self.env['kpi.dashboard'].create({
  22. 'name': 'Dashboard',
  23. 'number_of_columns': 4,
  24. 'widget_dimension_x': 250,
  25. 'widget_dimension_y': 250,
  26. })
  27. self.env['kpi.dashboard.item'].create({
  28. 'dashboard_id': self.dashboard.id,
  29. 'kpi_id': self.kpi_01.id,
  30. 'name': self.kpi_01.name,
  31. 'row': 1,
  32. 'column': 1,
  33. })
  34. self.env['kpi.dashboard.item'].create({
  35. 'dashboard_id': self.dashboard.id,
  36. 'name': self.kpi_02.name,
  37. 'kpi_id': self.kpi_02.id,
  38. 'row': 1,
  39. 'column': 2,
  40. })
  41. self.env['kpi.dashboard.item'].create({
  42. 'dashboard_id': self.dashboard.id,
  43. 'name': 'TITLE',
  44. 'row': 2,
  45. 'column': 1,
  46. })
  47. def test_constrains_01(self):
  48. with self.assertRaises(ValidationError):
  49. self.kpi_01.dashboard_item_ids.write({'size_x': 2})
  50. def test_constrains_02(self):
  51. with self.assertRaises(ValidationError):
  52. self.kpi_02.dashboard_item_ids.write({'size_x': 4})
  53. def test_constrains_03(self):
  54. with self.assertRaises(ValidationError):
  55. self.kpi_01.dashboard_item_ids.write({'size_y': 11})
  56. def test_menu(self):
  57. self.assertFalse(self.dashboard.menu_id)
  58. wzd = self.env['kpi.dashboard.menu'].create({
  59. 'dashboard_id': self.dashboard.id,
  60. 'menu_id': self.env['ir.ui.menu'].search([], limit=1).id,
  61. })
  62. wzd.generate_menu()
  63. self.assertTrue(self.dashboard.menu_id)
  64. self.assertFalse(self.dashboard.menu_id.groups_id)
  65. self.dashboard.write({
  66. 'group_ids': [
  67. (6, 0, self.env['res.groups'].search([], limit=1).ids)]
  68. })
  69. self.assertTrue(self.dashboard.menu_id.groups_id)
  70. def test_onchange(self):
  71. with Form(self.env['kpi.dashboard']) as dashboard:
  72. dashboard.name = 'New Dashboard'
  73. with dashboard.item_ids.new() as item:
  74. item.kpi_id = self.kpi_01
  75. self.assertTrue(item.name)
  76. def test_read_dashboard(self):
  77. data = self.dashboard.read_dashboard()
  78. title_found = False
  79. actions = 0
  80. for item in data['item_ids']:
  81. if not item.get('kpi_id'):
  82. title_found = True
  83. if item.get('actions', False):
  84. actions += len(item['actions'])
  85. self.assertTrue(title_found)
  86. self.assertEqual(0, actions)
  87. act01 = self.env['ir.actions.act_window'].search(
  88. [], limit=1)
  89. self.env['kpi.kpi.action'].create({
  90. 'kpi_id': self.kpi_01.id,
  91. 'action': '%s,%s' % (act01._name, act01.id)
  92. })
  93. act02 = self.env['ir.actions.act_url'].search(
  94. [], limit=1)
  95. self.env['kpi.kpi.action'].create({
  96. 'kpi_id': self.kpi_01.id,
  97. 'action': '%s,%s' % (act02._name, act02.id)
  98. })
  99. data = self.dashboard.read_dashboard()
  100. title_found = False
  101. actions = 0
  102. for item in data['item_ids']:
  103. if not item.get('kpi_id'):
  104. title_found = True
  105. if item.get('actions', False):
  106. actions += len(item['actions'])
  107. self.assertTrue(title_found)
  108. self.assertEqual(2, actions)
  109. def test_compute(self):
  110. self.assertFalse(self.kpi_01.value_last_update)
  111. self.kpi_01.compute()
  112. self.assertTrue(self.kpi_01.value_last_update)