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.

53 lines
1.9 KiB

  1. # Copyright 2016-2017 Jairo Llopis <jairo.llopis@tecnativa.com>
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from odoo.tools import mute_logger
  4. from odoo.tests.common import TransactionCase
  5. class ExtractorCase(TransactionCase):
  6. def setUp(self):
  7. super(ExtractorCase, self).setUp()
  8. # Shortcut
  9. self.text_from_html = self.env["ir.fields.converter"].text_from_html
  10. def test_excerpts(self):
  11. """Text gets correctly extracted."""
  12. html = u"""
  13. <html>
  14. <body>
  15. <div class="this should not appear">
  16. <h1>I'm a title</h1>
  17. <p>I'm a paragraph</p>
  18. <small>¡Pues yo soy español!</small>
  19. </div>
  20. </body>
  21. </html>
  22. """
  23. self.assertEqual(
  24. self.text_from_html(html),
  25. u"I'm a title I'm a paragraph ¡Pues yo soy español!")
  26. self.assertEqual(
  27. self.text_from_html(html, 8),
  28. u"I'm a title I'm a paragraph ¡Pues yo…")
  29. self.assertEqual(
  30. self.text_from_html(html, 8, 31),
  31. u"I'm a title I'm a paragraph ¡P…")
  32. self.assertEqual(
  33. self.text_from_html(html, 7, ellipsis=""),
  34. u"I'm a title I'm a paragraph ¡Pues")
  35. @mute_logger("odoo.addons.html_text.models.ir_fields_converter")
  36. def test_empty_html(self):
  37. """Empty HTML handled correctly."""
  38. self.assertEqual(self.text_from_html(""), "")
  39. with self.assertRaises(Exception):
  40. self.text_from_html("", fail=True)
  41. @mute_logger("odoo.addons.html_text.models.ir_fields_converter")
  42. def test_false_html(self):
  43. """``False`` HTML handled correctly."""
  44. self.assertEqual(self.text_from_html(False), "")
  45. with self.assertRaises(Exception):
  46. self.text_from_html(False, fail=True)