1
0

test_preview.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014-2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from synapse.rest.media.v1.preview_url_resource import (
  16. decode_and_calc_og,
  17. summarize_paragraphs,
  18. )
  19. from . import unittest
  20. class PreviewTestCase(unittest.TestCase):
  21. def test_long_summarize(self):
  22. example_paras = [
  23. """Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:
  24. Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in
  25. Troms county, Norway. The administrative centre of the municipality is
  26. the city of Tromsø. Outside of Norway, Tromso and Tromsö are
  27. alternative spellings of the city.Tromsø is considered the northernmost
  28. city in the world with a population above 50,000. The most populous town
  29. north of it is Alta, Norway, with a population of 14,272 (2013).""",
  30. """Tromsø lies in Northern Norway. The municipality has a population of
  31. (2015) 72,066, but with an annual influx of students it has over 75,000
  32. most of the year. It is the largest urban area in Northern Norway and the
  33. third largest north of the Arctic Circle (following Murmansk and Norilsk).
  34. Most of Tromsø, including the city centre, is located on the island of
  35. Tromsøya, 350 kilometres (217 mi) north of the Arctic Circle. In 2012,
  36. Tromsøya had a population of 36,088. Substantial parts of the urban area
  37. are also situated on the mainland to the east, and on parts of Kvaløya—a
  38. large island to the west. Tromsøya is connected to the mainland by the Tromsø
  39. Bridge and the Tromsøysund Tunnel, and to the island of Kvaløya by the
  40. Sandnessund Bridge. Tromsø Airport connects the city to many destinations
  41. in Europe. The city is warmer than most other places located on the same
  42. latitude, due to the warming effect of the Gulf Stream.""",
  43. """The city centre of Tromsø contains the highest number of old wooden
  44. houses in Northern Norway, the oldest house dating from 1789. The Arctic
  45. Cathedral, a modern church from 1965, is probably the most famous landmark
  46. in Tromsø. The city is a cultural centre for its region, with several
  47. festivals taking place in the summer. Some of Norway's best-known
  48. musicians, Torbjørn Brundtland and Svein Berge of the electronica duo
  49. Röyksopp and Lene Marlin grew up and started their careers in Tromsø.
  50. Noted electronic musician Geir Jenssen also hails from Tromsø.""",
  51. ]
  52. desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
  53. self.assertEquals(
  54. desc,
  55. "Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
  56. " Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
  57. " Troms county, Norway. The administrative centre of the municipality is"
  58. " the city of Tromsø. Outside of Norway, Tromso and Tromsö are"
  59. " alternative spellings of the city.Tromsø is considered the northernmost"
  60. " city in the world with a population above 50,000. The most populous town"
  61. " north of it is Alta, Norway, with a population of 14,272 (2013).",
  62. )
  63. desc = summarize_paragraphs(example_paras[1:], min_size=200, max_size=500)
  64. self.assertEquals(
  65. desc,
  66. "Tromsø lies in Northern Norway. The municipality has a population of"
  67. " (2015) 72,066, but with an annual influx of students it has over 75,000"
  68. " most of the year. It is the largest urban area in Northern Norway and the"
  69. " third largest north of the Arctic Circle (following Murmansk and Norilsk)."
  70. " Most of Tromsø, including the city centre, is located on the island of"
  71. " Tromsøya, 350 kilometres (217 mi) north of the Arctic Circle. In 2012,"
  72. " Tromsøya had a population of 36,088. Substantial parts of the urban…",
  73. )
  74. def test_short_summarize(self):
  75. example_paras = [
  76. "Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
  77. " Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
  78. " Troms county, Norway.",
  79. "Tromsø lies in Northern Norway. The municipality has a population of"
  80. " (2015) 72,066, but with an annual influx of students it has over 75,000"
  81. " most of the year.",
  82. "The city centre of Tromsø contains the highest number of old wooden"
  83. " houses in Northern Norway, the oldest house dating from 1789. The Arctic"
  84. " Cathedral, a modern church from 1965, is probably the most famous landmark"
  85. " in Tromsø.",
  86. ]
  87. desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
  88. self.assertEquals(
  89. desc,
  90. "Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
  91. " Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
  92. " Troms county, Norway.\n"
  93. "\n"
  94. "Tromsø lies in Northern Norway. The municipality has a population of"
  95. " (2015) 72,066, but with an annual influx of students it has over 75,000"
  96. " most of the year.",
  97. )
  98. def test_small_then_large_summarize(self):
  99. example_paras = [
  100. "Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
  101. " Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
  102. " Troms county, Norway.",
  103. "Tromsø lies in Northern Norway. The municipality has a population of"
  104. " (2015) 72,066, but with an annual influx of students it has over 75,000"
  105. " most of the year."
  106. " The city centre of Tromsø contains the highest number of old wooden"
  107. " houses in Northern Norway, the oldest house dating from 1789. The Arctic"
  108. " Cathedral, a modern church from 1965, is probably the most famous landmark"
  109. " in Tromsø.",
  110. ]
  111. desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
  112. self.assertEquals(
  113. desc,
  114. "Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
  115. " Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
  116. " Troms county, Norway.\n"
  117. "\n"
  118. "Tromsø lies in Northern Norway. The municipality has a population of"
  119. " (2015) 72,066, but with an annual influx of students it has over 75,000"
  120. " most of the year. The city centre of Tromsø contains the highest number"
  121. " of old wooden houses in Northern Norway, the oldest house dating from"
  122. " 1789. The Arctic Cathedral, a modern church from…",
  123. )
  124. class PreviewUrlTestCase(unittest.TestCase):
  125. def test_simple(self):
  126. html = """
  127. <html>
  128. <head><title>Foo</title></head>
  129. <body>
  130. Some text.
  131. </body>
  132. </html>
  133. """
  134. og = decode_and_calc_og(html, "http://example.com/test.html")
  135. self.assertEquals(og, {"og:title": "Foo", "og:description": "Some text."})
  136. def test_comment(self):
  137. html = """
  138. <html>
  139. <head><title>Foo</title></head>
  140. <body>
  141. <!-- HTML comment -->
  142. Some text.
  143. </body>
  144. </html>
  145. """
  146. og = decode_and_calc_og(html, "http://example.com/test.html")
  147. self.assertEquals(og, {"og:title": "Foo", "og:description": "Some text."})
  148. def test_comment2(self):
  149. html = """
  150. <html>
  151. <head><title>Foo</title></head>
  152. <body>
  153. Some text.
  154. <!-- HTML comment -->
  155. Some more text.
  156. <p>Text</p>
  157. More text
  158. </body>
  159. </html>
  160. """
  161. og = decode_and_calc_og(html, "http://example.com/test.html")
  162. self.assertEquals(
  163. og,
  164. {
  165. "og:title": "Foo",
  166. "og:description": "Some text.\n\nSome more text.\n\nText\n\nMore text",
  167. },
  168. )
  169. def test_script(self):
  170. html = """
  171. <html>
  172. <head><title>Foo</title></head>
  173. <body>
  174. <script> (function() {})() </script>
  175. Some text.
  176. </body>
  177. </html>
  178. """
  179. og = decode_and_calc_og(html, "http://example.com/test.html")
  180. self.assertEquals(og, {"og:title": "Foo", "og:description": "Some text."})
  181. def test_missing_title(self):
  182. html = """
  183. <html>
  184. <body>
  185. Some text.
  186. </body>
  187. </html>
  188. """
  189. og = decode_and_calc_og(html, "http://example.com/test.html")
  190. self.assertEquals(og, {"og:title": None, "og:description": "Some text."})
  191. def test_h1_as_title(self):
  192. html = """
  193. <html>
  194. <meta property="og:description" content="Some text."/>
  195. <body>
  196. <h1>Title</h1>
  197. </body>
  198. </html>
  199. """
  200. og = decode_and_calc_og(html, "http://example.com/test.html")
  201. self.assertEquals(og, {"og:title": "Title", "og:description": "Some text."})
  202. def test_missing_title_and_broken_h1(self):
  203. html = """
  204. <html>
  205. <body>
  206. <h1><a href="foo"/></h1>
  207. Some text.
  208. </body>
  209. </html>
  210. """
  211. og = decode_and_calc_og(html, "http://example.com/test.html")
  212. self.assertEquals(og, {"og:title": None, "og:description": "Some text."})