test_preview.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. u"""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. u"""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. u"""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. u"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
  56. u" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
  57. u" Troms county, Norway. The administrative centre of the municipality is"
  58. u" the city of Tromsø. Outside of Norway, Tromso and Tromsö are"
  59. u" alternative spellings of the city.Tromsø is considered the northernmost"
  60. u" city in the world with a population above 50,000. The most populous town"
  61. u" 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. u"Tromsø lies in Northern Norway. The municipality has a population of"
  67. u" (2015) 72,066, but with an annual influx of students it has over 75,000"
  68. u" most of the year. It is the largest urban area in Northern Norway and the"
  69. u" third largest north of the Arctic Circle (following Murmansk and Norilsk)."
  70. u" Most of Tromsø, including the city centre, is located on the island of"
  71. u" Tromsøya, 350 kilometres (217 mi) north of the Arctic Circle. In 2012,"
  72. u" Tromsøya had a population of 36,088. Substantial parts of the urban…"
  73. )
  74. def test_short_summarize(self):
  75. example_paras = [
  76. u"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
  77. u" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
  78. u" Troms county, Norway.",
  79. u"Tromsø lies in Northern Norway. The municipality has a population of"
  80. u" (2015) 72,066, but with an annual influx of students it has over 75,000"
  81. u" most of the year.",
  82. u"The city centre of Tromsø contains the highest number of old wooden"
  83. u" houses in Northern Norway, the oldest house dating from 1789. The Arctic"
  84. u" Cathedral, a modern church from 1965, is probably the most famous landmark"
  85. u" in Tromsø.",
  86. ]
  87. desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
  88. self.assertEquals(
  89. desc,
  90. u"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
  91. u" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
  92. u" Troms county, Norway.\n"
  93. u"\n"
  94. u"Tromsø lies in Northern Norway. The municipality has a population of"
  95. u" (2015) 72,066, but with an annual influx of students it has over 75,000"
  96. u" most of the year."
  97. )
  98. def test_small_then_large_summarize(self):
  99. example_paras = [
  100. u"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
  101. u" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
  102. u" Troms county, Norway.",
  103. u"Tromsø lies in Northern Norway. The municipality has a population of"
  104. u" (2015) 72,066, but with an annual influx of students it has over 75,000"
  105. u" most of the year."
  106. u" The city centre of Tromsø contains the highest number of old wooden"
  107. u" houses in Northern Norway, the oldest house dating from 1789. The Arctic"
  108. u" Cathedral, a modern church from 1965, is probably the most famous landmark"
  109. u" in Tromsø.",
  110. ]
  111. desc = summarize_paragraphs(example_paras, min_size=200, max_size=500)
  112. self.assertEquals(
  113. desc,
  114. u"Tromsø (Norwegian pronunciation: [ˈtrʊmsœ] ( listen); Northern Sami:"
  115. u" Romsa; Finnish: Tromssa[2] Kven: Tromssa) is a city and municipality in"
  116. u" Troms county, Norway.\n"
  117. u"\n"
  118. u"Tromsø lies in Northern Norway. The municipality has a population of"
  119. u" (2015) 72,066, but with an annual influx of students it has over 75,000"
  120. u" most of the year. The city centre of Tromsø contains the highest number"
  121. u" of old wooden houses in Northern Norway, the oldest house dating from"
  122. u" 1789. The Arctic Cathedral, a modern church from…"
  123. )
  124. class PreviewUrlTestCase(unittest.TestCase):
  125. def test_simple(self):
  126. html = u"""
  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, {
  136. u"og:title": u"Foo",
  137. u"og:description": u"Some text."
  138. })
  139. def test_comment(self):
  140. html = u"""
  141. <html>
  142. <head><title>Foo</title></head>
  143. <body>
  144. <!-- HTML comment -->
  145. Some text.
  146. </body>
  147. </html>
  148. """
  149. og = decode_and_calc_og(html, "http://example.com/test.html")
  150. self.assertEquals(og, {
  151. u"og:title": u"Foo",
  152. u"og:description": u"Some text."
  153. })
  154. def test_comment2(self):
  155. html = u"""
  156. <html>
  157. <head><title>Foo</title></head>
  158. <body>
  159. Some text.
  160. <!-- HTML comment -->
  161. Some more text.
  162. <p>Text</p>
  163. More text
  164. </body>
  165. </html>
  166. """
  167. og = decode_and_calc_og(html, "http://example.com/test.html")
  168. self.assertEquals(og, {
  169. u"og:title": u"Foo",
  170. u"og:description": u"Some text.\n\nSome more text.\n\nText\n\nMore text"
  171. })
  172. def test_script(self):
  173. html = u"""
  174. <html>
  175. <head><title>Foo</title></head>
  176. <body>
  177. <script> (function() {})() </script>
  178. Some text.
  179. </body>
  180. </html>
  181. """
  182. og = decode_and_calc_og(html, "http://example.com/test.html")
  183. self.assertEquals(og, {
  184. u"og:title": u"Foo",
  185. u"og:description": u"Some text."
  186. })
  187. def test_missing_title(self):
  188. html = u"""
  189. <html>
  190. <body>
  191. Some text.
  192. </body>
  193. </html>
  194. """
  195. og = decode_and_calc_og(html, "http://example.com/test.html")
  196. self.assertEquals(og, {
  197. u"og:title": None,
  198. u"og:description": u"Some text."
  199. })
  200. def test_h1_as_title(self):
  201. html = u"""
  202. <html>
  203. <meta property="og:description" content="Some text."/>
  204. <body>
  205. <h1>Title</h1>
  206. </body>
  207. </html>
  208. """
  209. og = decode_and_calc_og(html, "http://example.com/test.html")
  210. self.assertEquals(og, {
  211. u"og:title": u"Title",
  212. u"og:description": u"Some text."
  213. })
  214. def test_missing_title_and_broken_h1(self):
  215. html = u"""
  216. <html>
  217. <body>
  218. <h1><a href="foo"/></h1>
  219. Some text.
  220. </body>
  221. </html>
  222. """
  223. og = decode_and_calc_og(html, "http://example.com/test.html")
  224. self.assertEquals(og, {
  225. u"og:title": None,
  226. u"og:description": u"Some text."
  227. })