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.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2016 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from lxml import etree
  5. from openerp.tests.common import TransactionCase
  6. class ExtractorCase(TransactionCase):
  7. def setUp(self):
  8. super(ExtractorCase, self).setUp()
  9. # Shortcut
  10. self.imgs_from_html = self.env["ir.fields.converter"].imgs_from_html
  11. def test_mixed_images_found(self):
  12. """Images correctly found in <img> elements and backgrounds."""
  13. content = u"""
  14. <div>
  15. <!-- src-less img -->
  16. <img/>
  17. <p/>
  18. <img src="/path/0"/>
  19. <img src="/path/1"/>
  20. <img src="/path/2"/>
  21. <img src="/path/3"/>
  22. <section style="background : URL('/path/4');;background;ö;">
  23. <div style='BACKGROUND-IMAGE:url(/path/5)'>
  24. <p style="background:uRl(&quot;/path/6&quot;)">
  25. <img src="/path/7"/>
  26. </p>
  27. </div>
  28. </section>
  29. </div>
  30. """
  31. # Read all images
  32. for n, url in enumerate(self.imgs_from_html(content)):
  33. self.assertEqual("/path/%d" % n, url)
  34. self.assertEqual(n, 7)
  35. # Read only first image
  36. for n, url in enumerate(self.imgs_from_html(content, 1)):
  37. self.assertEqual("/path/%d" % n, url)
  38. self.assertEqual(n, 0)
  39. def test_empty_html(self):
  40. """Empty HTML handled correctly."""
  41. for laps, text in self.imgs_from_html(""):
  42. self.assertTrue(False) # You should never get here
  43. with self.assertRaises(etree.XMLSyntaxError):
  44. list(self.imgs_from_html("", fail=True))
  45. def test_false_html(self):
  46. """``False`` HTML handled correctly."""
  47. for laps, text in self.imgs_from_html(False):
  48. self.assertTrue(False) # You should never get here
  49. with self.assertRaises(TypeError):
  50. list(self.imgs_from_html(False, fail=True))
  51. def test_bad_html(self):
  52. """Bad HTML handled correctly."""
  53. for laps, text in self.imgs_from_html("<<bad>"):
  54. self.assertTrue(False) # You should never get here
  55. with self.assertRaises(etree.ParserError):
  56. list(self.imgs_from_html("<<bad>", fail=True))