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.

72 lines
2.6 KiB

  1. # Copyright (C) 2015 Akretion (<http://www.akretion.com>)
  2. # @author: Florian da Costa
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import base64
  5. from odoo.tests.common import TransactionCase, post_install
  6. from odoo.exceptions import UserError
  7. from odoo import fields
  8. @post_install(True)
  9. class TestExportSqlQuery(TransactionCase):
  10. def setUp(self):
  11. super(TestExportSqlQuery, self).setUp()
  12. self.sql_export_obj = self.env['sql.export']
  13. self.wizard_obj = self.env['sql.file.wizard']
  14. self.sql_report_demo = self.env.ref('sql_export.sql_export_partner')
  15. def test_sql_query(self):
  16. wizard = self.wizard_obj.create({
  17. 'sql_export_id': self.sql_report_demo.id,
  18. })
  19. wizard.export_sql()
  20. export = base64.b64decode(wizard.binary_file).decode('utf-8')
  21. self.assertEqual(export.split(';')[0], 'name')
  22. self.assertTrue(len(export.split(';')) > 6)
  23. def test_prohibited_queries(self):
  24. prohibited_queries = [
  25. "upDaTe res_partner SET name = 'test' WHERE id = 1",
  26. "DELETE FROM sql_export WHERE name = 'test';",
  27. " DELETE FROM sql_export WHERE name = 'test' ;",
  28. """DELETE
  29. FROM
  30. sql_export
  31. WHERE name = 'test'
  32. """,
  33. ]
  34. for query in prohibited_queries:
  35. with self.assertRaises(UserError):
  36. sql_export = self.sql_export_obj.create({
  37. 'name': 'test_prohibited',
  38. 'query': query})
  39. sql_export.button_validate_sql_expression()
  40. def test_authorized_queries(self):
  41. authorized_queries = [
  42. "SELECT create_date FROM res_partner",
  43. ]
  44. for query in authorized_queries:
  45. sql_export = self.sql_export_obj.create({
  46. 'name': 'test_authorized',
  47. 'query': query})
  48. sql_export.button_validate_sql_expression()
  49. self.assertEqual(
  50. sql_export.state, 'sql_valid',
  51. "%s is a valid request" % (query))
  52. def test_sql_query_with_params(self):
  53. query = self.env.ref('sql_export.sql_export_partner_with_variables')
  54. categ_id = self.env.ref('base.res_partner_category_0').id
  55. wizard = self.wizard_obj.create({
  56. 'sql_export_id': query.id,
  57. 'x_date': fields.Date.today(),
  58. 'x_id': 1,
  59. 'x_partner_categ_ids': [(6, 0, [categ_id])]
  60. })
  61. wizard.export_sql()
  62. export = base64.b64decode(wizard.binary_file)
  63. self.assertTrue(export)