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.

76 lines
2.7 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. from openerp.tools import mute_logger
  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("openerp.addons.html_image_url_extractor.models"
  41. ".ir_fields_converter")
  42. def test_empty_html(self):
  43. """Empty HTML handled correctly."""
  44. for laps, text in self.imgs_from_html(""):
  45. self.assertTrue(False) # You should never get here
  46. with self.assertRaises(etree.XMLSyntaxError):
  47. list(self.imgs_from_html("", fail=True))
  48. @mute_logger("openerp.addons.html_image_url_extractor.models"
  49. ".ir_fields_converter")
  50. def test_false_html(self):
  51. """``False`` HTML handled correctly."""
  52. for laps, text in self.imgs_from_html(False):
  53. self.assertTrue(False) # You should never get here
  54. with self.assertRaises(TypeError):
  55. list(self.imgs_from_html(False, fail=True))
  56. @mute_logger("openerp.addons.html_image_url_extractor.models"
  57. ".ir_fields_converter")
  58. def test_bad_html(self):
  59. """Bad HTML handled correctly."""
  60. for laps, text in self.imgs_from_html("<<bad>"):
  61. self.assertTrue(False) # You should never get here
  62. with self.assertRaises(etree.ParserError):
  63. list(self.imgs_from_html("<<bad>", fail=True))