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.

63 lines
2.2 KiB

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