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.

432 lines
15 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. # Copyright 2017-2019 Onestein (<https://www.onestein.eu>)
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. import json
  4. import odoo
  5. from odoo.exceptions import UserError, ValidationError
  6. from odoo.tests.common import TransactionCase
  7. from odoo.tools import mute_logger
  8. from ..hooks import post_load, uninstall_hook
  9. class TestBiViewEditor(TransactionCase):
  10. def setUp(self):
  11. def _get_models(model_name_list):
  12. return (
  13. self.env["ir.model"].search([("model", "=", name)])
  14. for name in model_name_list
  15. )
  16. def _get_fields(model_field_list):
  17. return (
  18. self.env["ir.model.fields"].search(
  19. [("model", "=", model_field[0]), ("name", "=", model_field[1])],
  20. limit=1,
  21. )
  22. for model_field in model_field_list
  23. )
  24. def get_new_field(self):
  25. return {
  26. "model_id": self.partner_model.id,
  27. "name": self.partner_field_name,
  28. "id": self.partner_field.id,
  29. "model": self.partner_model_name,
  30. "type": self.partner_field.ttype,
  31. "model_name": self.partner_model.name,
  32. "description": self.partner_field.field_description,
  33. }
  34. super().setUp()
  35. self.partner_model_name = "res.partner"
  36. self.partner_field_name = "name"
  37. self.partner_company_field_name = "company_id"
  38. self.company_model_name = "res.company"
  39. self.company_field_name = "name"
  40. self.bi_view1 = None
  41. self.partner_model, self.company_model = _get_models(
  42. [self.partner_model_name, self.company_model_name]
  43. )
  44. (
  45. self.partner_field,
  46. self.partner_company_field,
  47. self.company_field,
  48. ) = _get_fields(
  49. [
  50. (self.partner_model_name, self.partner_field_name),
  51. (self.partner_model_name, self.partner_company_field_name),
  52. (self.company_model_name, self.company_field_name),
  53. ]
  54. )
  55. self.data = [
  56. {
  57. "model_id": self.partner_model.id,
  58. "model_name": self.partner_model.name,
  59. "model": self.partner_model_name,
  60. "type": self.partner_field.ttype,
  61. "id": self.partner_field.id,
  62. "description": self.partner_field.field_description,
  63. "table_alias": "t0",
  64. "row": 0,
  65. "column": 1,
  66. "list": 1,
  67. "measure": 0,
  68. },
  69. {
  70. "model_id": self.partner_model.id,
  71. "table_alias": "t0",
  72. "relation": self.company_model_name,
  73. "model": self.partner_model_name,
  74. "model_name": self.partner_model.name,
  75. "type": self.partner_company_field.ttype,
  76. "id": self.partner_company_field.id,
  77. "join_node": "t1",
  78. "description": self.partner_company_field.field_description,
  79. "row": 0,
  80. "column": 0,
  81. "list": 1,
  82. "measure": 0,
  83. },
  84. {
  85. "model_id": self.company_model.id,
  86. "model_name": self.company_model.name,
  87. "model": self.company_model_name,
  88. "type": self.company_field.ttype,
  89. "id": self.company_field.id,
  90. "description": self.company_field.field_description,
  91. "table_alias": "t1",
  92. "row": 1,
  93. "column": 0,
  94. "list": 0,
  95. "measure": 0,
  96. },
  97. ]
  98. self.bi_view1_vals = {"state": "draft", "data": json.dumps(self.data)}
  99. self.new_field = get_new_field(self)
  100. def test_01_get_fields(self):
  101. fields = self.env["ir.model"].get_fields(self.partner_model.id)
  102. self.assertIsInstance(fields, list)
  103. self.assertGreater(len(fields), 0)
  104. def test_02_get_join_nodes(self):
  105. field_res_users = self.env["ir.model.fields"].search(
  106. [("name", "=", "login"), ("model", "=", "res.users")], limit=1
  107. )
  108. field_data = [
  109. {
  110. "model_id": field_res_users.model_id.id,
  111. "name": "login",
  112. "column": False,
  113. "table_alias": "t0",
  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. ]
  123. new_field = self.new_field
  124. nodes = self.env["ir.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. nodes = self.env["ir.model"].get_join_nodes([], new_field)
  130. self.assertIsInstance(nodes, list)
  131. self.assertEqual(len(nodes), 0)
  132. def test_04_get_related_models(self):
  133. all_models = self.env["ir.model"].get_models()
  134. self.assertIsInstance(all_models, list)
  135. self.assertGreater(len(all_models), 0)
  136. related_models = self.env["ir.model"].get_models(
  137. {"t0": self.partner_model.id, "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", "group_ids": [(6, 0, [employees_group.id])],}
  157. )
  158. bi_view2 = self.env["bve.view"].create(vals)
  159. self.assertEqual(len(bi_view2.user_ids), len(employees_group.users))
  160. def test_07_check_empty_data(self):
  161. vals = {"name": "Test View Empty", "state": "draft", "data": ""}
  162. bi_view4 = self.env["bve.view"].create(vals)
  163. self.assertEqual(len(bi_view4), 1)
  164. self.assertTrue(bi_view4.er_diagram_image)
  165. # create sql view
  166. with self.assertRaises(ValidationError):
  167. bi_view4.action_create()
  168. def test_08_get_models(self):
  169. models = self.env["ir.model"].get_models()
  170. self.assertIsInstance(models, list)
  171. self.assertGreater(len(models), 0)
  172. @odoo.tests.tagged("post_install", "-at_install")
  173. def test_09_create_open_bve_object(self):
  174. vals = self.bi_view1_vals
  175. employees_group = self.env.ref("base.group_user")
  176. vals.update(
  177. {"name": "Test View4", "group_ids": [(6, 0, [employees_group.id])],}
  178. )
  179. bi_view = self.env["bve.view"].create(vals)
  180. self.assertEqual(len(bi_view), 1)
  181. self.assertEqual(len(bi_view.line_ids), 3)
  182. self.assertTrue(bi_view.er_diagram_image)
  183. # check lines
  184. line1 = bi_view.line_ids[0]
  185. line2 = bi_view.line_ids[1]
  186. line3 = bi_view.line_ids[2]
  187. self.assertTrue(line1.in_list)
  188. self.assertTrue(line2.in_list)
  189. self.assertFalse(line3.in_list)
  190. self.assertFalse(line1.row)
  191. self.assertTrue(line1.column)
  192. self.assertFalse(line1.measure)
  193. self.assertFalse(line2.row)
  194. self.assertFalse(line2.column)
  195. self.assertFalse(line2.measure)
  196. self.assertTrue(line3.row)
  197. self.assertFalse(line3.column)
  198. self.assertFalse(line3.measure)
  199. # create bve object
  200. bi_view.action_create()
  201. model = self.env["ir.model"].search(
  202. [("model", "=", "x_bve.testview4"), ("name", "=", "Test View4")]
  203. )
  204. self.assertEqual(len(model), 1)
  205. # open view
  206. open_action = bi_view.open_view()
  207. self.assertEqual(isinstance(open_action, dict), True)
  208. self.assertEqual(bi_view.state, "created")
  209. # try to remove view
  210. with self.assertRaises(UserError):
  211. bi_view.unlink()
  212. # reset to draft
  213. bi_view.action_reset()
  214. self.assertEqual(bi_view.state, "draft")
  215. # remove view
  216. bi_view.unlink()
  217. @odoo.tests.tagged("post_install", "-at_install")
  218. def test_10_create_open_bve_object_apostrophe(self):
  219. vals = self.bi_view1_vals
  220. vals.update(
  221. {"name": "Test View5",}
  222. )
  223. data_list = list()
  224. for r in json.loads(vals["data"]):
  225. r["model_name"] = "model'name"
  226. data_list.append(r)
  227. new_format_data = json.dumps(data_list)
  228. vals.update({"data": new_format_data})
  229. bi_view = self.env["bve.view"].create(vals)
  230. self.assertEqual(len(bi_view), 1)
  231. # create bve object
  232. bi_view.action_create()
  233. def test_11_clean_nodes(self):
  234. data_dict1 = {
  235. "sequence": 1,
  236. "model_id": 74,
  237. "id": 858,
  238. "name": "name",
  239. "model_name": "Contact",
  240. "model": "res.partner",
  241. "type": "char",
  242. "table_alias": "t74",
  243. "description": "Name",
  244. "row": False,
  245. "column": False,
  246. "measure": False,
  247. "list": True,
  248. }
  249. data_dict2 = {
  250. "sequence": 2,
  251. "model_id": 74,
  252. "id": 896,
  253. "name": "company_id",
  254. "model_name": "Contact",
  255. "model": "res.partner",
  256. "type": "many2one",
  257. "table_alias": "t74",
  258. "description": "Company",
  259. "row": False,
  260. "column": False,
  261. "measure": False,
  262. "list": True,
  263. "join_node": "t83",
  264. "relation": "res.company",
  265. "join_left": False,
  266. }
  267. old_data = json.dumps([data_dict1, data_dict2])
  268. new_data = self.env["bve.view"].get_clean_list(old_data)
  269. new_data_dict = json.loads(new_data)
  270. self.assertEqual(len(new_data_dict), 1)
  271. for key in data_dict1.keys():
  272. self.assertEqual(new_data_dict[0][key], data_dict1[key])
  273. def test_12_check_groups(self):
  274. vals = self.bi_view1_vals
  275. group_system = self.env.ref("base.group_system")
  276. vals.update(
  277. {"name": "Test View1", "group_ids": [(6, 0, [group_system.id])],}
  278. )
  279. bi_view1 = self.env["bve.view"].create(vals)
  280. with self.assertRaises(UserError):
  281. bi_view1.action_create()
  282. def test_13_check_lines_missing_model(self):
  283. vals = self.bi_view1_vals
  284. group_user = self.env.ref("base.group_user")
  285. vals.update(
  286. {"name": "Test View1", "group_ids": [(6, 0, [group_user.id])],}
  287. )
  288. bi_view1 = self.env["bve.view"].create(vals)
  289. for line in bi_view1.line_ids:
  290. self.assertTrue(line.model_id)
  291. self.assertTrue(line.model_name)
  292. self.env.cr.execute("UPDATE bve_view_line SET model_id = null")
  293. bi_view1.invalidate_cache()
  294. for line in bi_view1.line_ids:
  295. self.assertFalse(line.model_id)
  296. self.assertTrue(line.model_name)
  297. with self.assertRaises(ValidationError):
  298. bi_view1.action_create()
  299. def test_14_check_lines_missing_fieldl(self):
  300. vals = self.bi_view1_vals
  301. group_user = self.env.ref("base.group_user")
  302. vals.update(
  303. {"name": "Test View1", "group_ids": [(6, 0, [group_user.id])],}
  304. )
  305. bi_view1 = self.env["bve.view"].create(vals)
  306. for line in bi_view1.line_ids:
  307. self.assertTrue(line.field_id)
  308. self.assertTrue(line.field_name)
  309. self.env.cr.execute("UPDATE bve_view_line SET field_id = null")
  310. bi_view1.invalidate_cache()
  311. for line in bi_view1.line_ids:
  312. self.assertFalse(line.field_id)
  313. self.assertTrue(line.field_name)
  314. with self.assertRaises(ValidationError):
  315. bi_view1.action_create()
  316. def test_15_create_lines(self):
  317. vals = self.bi_view1_vals
  318. vals.update({"name": "Test View1"})
  319. bi_view1 = self.env["bve.view"].create(vals)
  320. bi_view1._compute_serialized_data()
  321. data = json.loads(bi_view1.data)
  322. self.assertTrue(data)
  323. self.assertTrue(isinstance(data, list))
  324. def test_16_post_load(self):
  325. post_load()
  326. def test_17_uninstall_hook(self):
  327. uninstall_hook(self.cr, self.env)
  328. def test_18_action_translations(self):
  329. self.env["res.lang"].load_lang("it_IT")
  330. vals = self.bi_view1_vals
  331. vals.update({"name": "Test View1"})
  332. bi_view1 = self.env["bve.view"].create(vals)
  333. res = bi_view1.action_translations()
  334. self.assertFalse(res)
  335. bi_view1.action_create()
  336. res = bi_view1.action_translations()
  337. self.assertTrue(res)
  338. @odoo.tests.tagged("post_install", "-at_install")
  339. def test_19_field_selection(self):
  340. field = self.env["ir.model.fields"].search(
  341. [
  342. ("model", "=", self.company_model_name),
  343. ("name", "=", "base_onboarding_company_state"),
  344. ],
  345. limit=1,
  346. )
  347. selection_data = [
  348. {
  349. "model_id": self.company_model.id,
  350. "model_name": self.company_model.name,
  351. "model": self.company_model_name,
  352. "type": field.ttype,
  353. "id": field.id,
  354. "description": "State of the onboarding company step",
  355. "table_alias": "t1",
  356. "row": 0,
  357. "column": 0,
  358. "list": 1,
  359. "measure": 0,
  360. }
  361. ]
  362. vals = {"state": "draft", "data": json.dumps(self.data + selection_data)}
  363. vals.update({"name": "Test View6"})
  364. bi_view1 = self.env["bve.view"].create(vals)
  365. bi_view1.action_create()
  366. self.assertEqual(len(bi_view1.line_ids), 4)
  367. @mute_logger("odoo.sql_db")
  368. def test_20_broken_view(self):
  369. """
  370. Create a broken query, a nice UserError should be raised.
  371. odoo.sql_db logger is muted to avoid the
  372. ERROR: bad_query line in the logs.
  373. """
  374. vals = self.bi_view1_vals
  375. vals.update(
  376. {"name": "Test View broken", "over_condition": "bad SQL code",}
  377. )
  378. bi_view = self.env["bve.view"].create(vals)
  379. with self.assertRaises(UserError) as ue:
  380. bi_view.action_create()
  381. self.assertEqual(bi_view.state, "draft")
  382. self.assertIn(bi_view.over_condition, str(ue.exception))
  383. # remove view
  384. bi_view.unlink()