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.

73 lines
2.7 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Author: Florian da Costa
  5. # Copyright 2015 Akretion
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distnaributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. import base64
  22. from openerp.tests.common import TransactionCase
  23. from openerp import exceptions
  24. class TestExportSqlQuery(TransactionCase):
  25. def setUp(self):
  26. super(TestExportSqlQuery, self).setUp()
  27. query_vals = {
  28. 'name': 'test',
  29. 'query': "SELECT name, street FROM res_partner;"
  30. }
  31. self.sql_model = self.registry('sql.export')
  32. self.query_id = self.sql_model.create(
  33. self.cr,
  34. self.uid,
  35. query_vals)
  36. def test_sql_query(self):
  37. test = self.sql_model.export_sql_query(
  38. self.cr, self.uid, [self.query_id])
  39. self.registry('sql.file.wizard').export_sql(
  40. self.cr, self.uid, test['res_id'])
  41. wizard = self.registry('sql.file.wizard').browse(
  42. self.cr, self.uid, test['res_id'])
  43. export = base64.b64decode(wizard.binary_file)
  44. self.assertEqual(export.split(';')[0], 'name')
  45. self.assertTrue(len(export.split(';')) > 6)
  46. def test_prohibited_queries_creation(self):
  47. prohibited_queries = [
  48. "upDaTe res_partner SET name = 'test' WHERE id = 1",
  49. "DELETE FROM sql_export WHERE name = 'test';",
  50. " DELETE FROM sql_export WHERE name = 'test' ;",
  51. """DELETE
  52. FROM
  53. sql_export
  54. WHERE name = 'test'
  55. """,
  56. ]
  57. for query in prohibited_queries:
  58. with self.assertRaises(exceptions.ValidationError):
  59. self.sql_model.create(
  60. self.cr, self.uid,
  61. {'name': 'test_prohibited',
  62. 'query': query})
  63. ok_query = {
  64. 'name': 'test ok',
  65. 'query': "SELECT create_date FROM res_partner"
  66. }
  67. query_id = self.sql_model.create(self.cr, self.uid, ok_query)
  68. self.assertIsNotNone(query_id)