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.

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