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.

69 lines
2.6 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 lxml import etree
  4. from odoo.tools import mute_logger
  5. from odoo.tests.common import TransactionCase
  6. from ..models import ir_fields_converter
  7. class ExtractorCase(TransactionCase):
  8. def setUp(self):
  9. super(ExtractorCase, self).setUp()
  10. # Shortcut
  11. self.imgs_from_html = self.env["ir.fields.converter"].imgs_from_html
  12. def test_mixed_images_found(self):
  13. """Images correctly found in <img> elements and backgrounds."""
  14. content = u"""
  15. <div>
  16. <!-- src-less img -->
  17. <img/>
  18. <p/>
  19. <img src="/path/0"/>
  20. <img src="/path/1"/>
  21. <img src="/path/2"/>
  22. <img src="/path/3"/>
  23. <section style="background : URL('/path/4');;background;ö;">
  24. <div style='BACKGROUND-IMAGE:url(/path/5)'>
  25. <p style="background:uRl(&quot;/path/6&quot;)">
  26. <img src="/path/7"/>
  27. </p>
  28. </div>
  29. </section>
  30. </div>
  31. """
  32. # Read all images
  33. for n, url in enumerate(self.imgs_from_html(content)):
  34. self.assertEqual("/path/%d" % n, url)
  35. self.assertEqual(n, 7)
  36. # Read only first image
  37. for n, url in enumerate(self.imgs_from_html(content, 1)):
  38. self.assertEqual("/path/%d" % n, url)
  39. self.assertEqual(n, 0)
  40. @mute_logger(ir_fields_converter.__name__)
  41. def test_empty_html(self):
  42. """Empty HTML handled correctly."""
  43. for laps, text in self.imgs_from_html(""):
  44. self.assertTrue(False) # You should never get here
  45. with self.assertRaises(etree.XMLSyntaxError):
  46. list(self.imgs_from_html("", fail=True))
  47. @mute_logger(ir_fields_converter.__name__)
  48. def test_false_html(self):
  49. """``False`` HTML handled correctly."""
  50. for laps, text in self.imgs_from_html(False):
  51. self.assertTrue(False) # You should never get here
  52. with self.assertRaises(TypeError):
  53. list(self.imgs_from_html(False, fail=True))
  54. @mute_logger(ir_fields_converter.__name__)
  55. def test_bad_html(self):
  56. """Bad HTML handled correctly."""
  57. for laps, text in self.imgs_from_html("<<bad>"):
  58. self.assertTrue(False) # You should never get here
  59. with self.assertRaises(etree.ParserError):
  60. list(self.imgs_from_html("<<bad>", fail=True))