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.

230 lines
7.7 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017 Onestein (<http://www.onestein.eu>)
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp.tests.common import TransactionCase, at_install, post_install
  5. from openerp.exceptions import Warning as UserError
  6. class TestBiViewEditor(TransactionCase):
  7. def setUp(self):
  8. def _get_models(model_name_list):
  9. Model = self.env['ir.model']
  10. return (Model.search(
  11. [('model', '=', name)]) for name in model_name_list)
  12. def _get_fields(model_field_list):
  13. ModelFields = self.env['ir.model.fields']
  14. return (ModelFields.search(
  15. [('model', '=', model_field[0]),
  16. ('name', '=', model_field[1])],
  17. limit=1) for model_field in model_field_list)
  18. def get_new_field(self):
  19. new_field = {
  20. 'model_id': self.partner_model.id,
  21. 'name': self.partner_field_name,
  22. 'custom': False,
  23. 'id': self.partner_field.id,
  24. 'model': self.partner_model_name,
  25. 'type': self.partner_field.ttype,
  26. 'model_name': self.partner_model.name,
  27. 'description': self.partner_field.field_description
  28. }
  29. return new_field
  30. super(TestBiViewEditor, self).setUp()
  31. self.partner_model_name = 'res.partner'
  32. self.partner_field_name = 'name'
  33. self.partner_company_field_name = 'company_id'
  34. self.company_model_name = 'res.company'
  35. self.company_field_name = 'name'
  36. self.bi_view1 = None
  37. self.partner_model, self.company_model = _get_models(
  38. [self.partner_model_name, self.company_model_name])
  39. (self.partner_field,
  40. self.partner_company_field,
  41. self.company_field) = _get_fields([
  42. (self.partner_model_name, self.partner_field_name),
  43. (self.partner_model_name, self.partner_company_field_name),
  44. (self.company_model_name, self.company_field_name)])
  45. data = [
  46. {'model_id': self.partner_model.id,
  47. 'name': self.partner_field_name,
  48. 'model_name': self.partner_model.name,
  49. 'model': self.partner_model_name,
  50. 'custom': 0,
  51. 'type': self.partner_field.ttype,
  52. 'id': self.partner_field.id,
  53. 'description': self.partner_field.field_description,
  54. 'table_alias': 't0',
  55. 'row': 0,
  56. 'column': 1,
  57. 'list': 1,
  58. 'measure': 0
  59. },
  60. {'model_id': self.partner_model.id,
  61. 'name': self.partner_company_field_name,
  62. 'table_alias': 't0',
  63. 'custom': 0,
  64. 'relation': self.company_model_name,
  65. 'model': self.partner_model_name,
  66. 'model_name': self.partner_model.name,
  67. 'type': self.partner_company_field.ttype,
  68. 'id': self.partner_company_field.id,
  69. 'join_node': 't1',
  70. 'description': self.partner_company_field.field_description,
  71. 'row': 0,
  72. 'column': 0,
  73. 'list': 1,
  74. 'measure': 0
  75. },
  76. {'model_id': self.company_model.id,
  77. 'name': 'name_1',
  78. 'model_name': self.company_model.name,
  79. 'model': self.company_model_name,
  80. 'custom': 0,
  81. 'type': self.company_field.ttype,
  82. 'id': self.company_field.id,
  83. 'description': self.company_field.field_description,
  84. 'table_alias': 't1',
  85. 'row': 1,
  86. 'column': 0,
  87. 'list': 0,
  88. 'measure': 0
  89. }
  90. ]
  91. format_data = self.env['bve.view']._get_format_data(str(data))
  92. self.bi_view1_vals = {
  93. 'state': 'draft',
  94. 'data': format_data
  95. }
  96. self.new_field = get_new_field(self)
  97. def test_01_get_fields(self):
  98. Model = self.env['ir.model']
  99. fields = Model.get_fields(self.partner_model.id)
  100. self.assertIsInstance(fields, list)
  101. self.assertGreater(len(fields), 0)
  102. def test_02_get_join_nodes(self):
  103. Fields = self.env['ir.model.fields']
  104. field_res_users = Fields.search([
  105. ('name', '=', 'login'),
  106. ('model', '=', 'res.users')
  107. ], limit=1)
  108. field_data = [{
  109. 'model_id': field_res_users.model_id.id,
  110. 'name': 'login',
  111. 'column': False,
  112. 'table_alias': 't0',
  113. 'custom': False,
  114. 'measure': False,
  115. 'id': field_res_users.id,
  116. 'model': 'res.users',
  117. 'row': False,
  118. 'type': 'char',
  119. 'model_name': 'Users',
  120. 'description': 'Login'
  121. }]
  122. new_field = self.new_field
  123. Model = self.env['ir.model']
  124. nodes = Model.get_join_nodes(field_data, new_field)
  125. self.assertIsInstance(nodes, list)
  126. self.assertGreater(len(nodes), 0)
  127. def test_03_get_join_nodes(self):
  128. new_field = self.new_field
  129. Model = self.env['ir.model']
  130. nodes = Model.get_join_nodes([], new_field)
  131. self.assertIsInstance(nodes, list)
  132. self.assertEqual(len(nodes), 0)
  133. def test_04_get_related_models(self):
  134. Model = self.env['ir.model']
  135. related_models = Model.get_related_models({
  136. 't0': self.partner_model.id,
  137. 't1': self.company_model.id
  138. })
  139. self.assertIsInstance(related_models, list)
  140. self.assertGreater(len(related_models), 0)
  141. def test_05_create_copy_view(self):
  142. vals = self.bi_view1_vals
  143. vals.update({'name': 'Test View1'})
  144. # create
  145. bi_view1 = self.env['bve.view'].create(vals)
  146. self.assertIsNotNone(bi_view1)
  147. self.assertEqual(len(bi_view1), 1)
  148. self.assertEqual(bi_view1.state, 'draft')
  149. # copy
  150. bi_view2 = bi_view1.copy()
  151. self.assertEqual(bi_view2.name, 'Test View1 (copy)')
  152. def test_06_create_group_bve_object(self):
  153. vals = self.bi_view1_vals
  154. employees_group = self.env.ref('base.group_user')
  155. vals.update({
  156. 'name': 'Test View2',
  157. 'group_ids': [(6, 0, [employees_group.id])],
  158. })
  159. bi_view2 = self.env['bve.view'].create(vals)
  160. self.assertEqual(len(bi_view2.user_ids), len(employees_group.users))
  161. def test_07_check_empty_data(self):
  162. vals = {
  163. 'name': 'Test View Empty',
  164. 'state': 'draft',
  165. 'data': ''
  166. }
  167. bi_view4 = self.env['bve.view'].create(vals)
  168. self.assertEqual(len(bi_view4), 1)
  169. # create sql view
  170. with self.assertRaises(UserError):
  171. bi_view4.action_create()
  172. def test_08_get_models(self):
  173. Model = self.env['ir.model']
  174. models = Model.get_models()
  175. self.assertIsInstance(models, list)
  176. self.assertGreater(len(models), 0)
  177. @at_install(False)
  178. @post_install(True)
  179. def test_09_create_open_bve_object(self):
  180. vals = self.bi_view1_vals
  181. employees_group = self.env.ref('base.group_user')
  182. vals.update({
  183. 'name': 'Test View4',
  184. 'group_ids': [(6, 0, [employees_group.id])],
  185. })
  186. bi_view = self.env['bve.view'].create(vals)
  187. self.assertEqual(len(bi_view), 1)
  188. # create bve object
  189. bi_view.action_create()
  190. model = self.env['ir.model'].search([
  191. ('model', '=', 'x_bve.testview4'),
  192. ('name', '=', 'Test View4')
  193. ])
  194. self.assertEqual(len(model), 1)
  195. # open view
  196. open_action = bi_view.open_view()
  197. self.assertEqual(isinstance(open_action, dict), True)
  198. # try to remove view
  199. with self.assertRaises(UserError):
  200. bi_view.unlink()