/module_prototyper/tests/test_prototype.py

https://gitlab.com/Haritiana/server-tools · Python · 113 lines · 75 code · 14 blank · 24 comment · 6 complexity · 0686f79a06d73c8d52d43e392ea250d3 MD5 · raw file

  1. # -*- encoding: utf-8 -*- #
  2. # OpenERP, Open Source Management Solution
  3. # This module copyright (C) 2013 Savoir-faire Linux
  4. # (<http://www.savoirfairelinux.com>).
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import ast
  20. import lxml.etree
  21. try:
  22. import pep8
  23. except ImportError:
  24. pep8 = None
  25. from jinja2 import Environment
  26. from jinja2.exceptions import TemplateNotFound
  27. from openerp.tests import common
  28. class TestModulePrototyper(common.TransactionCase):
  29. def setUp(self):
  30. super(TestModulePrototyper, self).setUp()
  31. self.main_model = self.env['module_prototyper']
  32. self.module_category_model = self.env['ir.module.category']
  33. self.module_module_model = self.env['ir.module.module']
  34. self.prototype = self.main_model.create({
  35. 'name': 't_name',
  36. 'category_id': self.module_category_model.browse(1).id,
  37. 'human_name': 't_human_name',
  38. 'summary': 't_summary',
  39. 'description': 't_description',
  40. 'author': 't_author',
  41. 'maintainer': 't_maintainer',
  42. 'website': 't_website',
  43. 'dependencies': [(6, 0, [1, 2, 3, 4])],
  44. })
  45. self.api_version = '8.0'
  46. def test_generate_files_assert_if_no_env(self):
  47. self.assertRaises(
  48. AssertionError,
  49. self.prototype.generate_files
  50. )
  51. def test_generate_files(self):
  52. """Test generate_files returns a tuple."""
  53. self.prototype.set_jinja_env(self.api_version)
  54. details = self.prototype.generate_files()
  55. self.assertIsInstance(details, list)
  56. # namedtuples in tuple
  57. for file_details in details:
  58. self.assertIsInstance(file_details, tuple)
  59. self.assertIsInstance(file_details.filename, basestring)
  60. self.assertIsInstance(file_details.filecontent, basestring)
  61. name, contents = file_details
  62. if name.endswith(".py"):
  63. # We have a "coding utf-8" line in there, we need to encode
  64. contents = contents.encode("utf-8")
  65. ast.parse(contents)
  66. if pep8:
  67. checker = pep8.Checker(
  68. name,
  69. contents.splitlines(True))
  70. res = checker.check_all()
  71. self.assertFalse(
  72. res,
  73. "Python file {0} has pep8 errors:\n"
  74. "{1}\n{2}".format(name, checker.report.messages,
  75. repr(contents))
  76. )
  77. elif name.endswith(".xml"):
  78. # TODO validate valid odoo xml
  79. lxml.etree.fromstring(contents)
  80. def test_generate_files_raise_templatenotfound_if_not_found(self):
  81. self.prototype.set_jinja_env('t_api_version')
  82. self.assertRaises(
  83. TemplateNotFound,
  84. self.prototype.generate_files
  85. )
  86. def test_set_env(self):
  87. """test the jinja2 environment is set."""
  88. self.assertIsNone(self.prototype._env)
  89. self.prototype.set_jinja_env(self.api_version)
  90. self.assertIsInstance(
  91. self.prototype._env, Environment
  92. )
  93. def test_friendly_name_return(self):
  94. """Test if the returns match the pattern."""
  95. name = 'res.partner'
  96. self.assertEqual(
  97. self.prototype.friendly_name(name),
  98. name.replace('.', '_')
  99. )