123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. # -*- coding: utf-8 -*-
  2. from lxml.html import fromstring
  3. from collections import defaultdict
  4. import mock
  5. from searx.engines import wikidata
  6. from searx.testing import SearxTestCase
  7. class TestWikidataEngine(SearxTestCase):
  8. def test_request(self):
  9. wikidata.supported_languages = ['en', 'es']
  10. query = 'test_query'
  11. dicto = defaultdict(dict)
  12. dicto['language'] = 'en-US'
  13. params = wikidata.request(query, dicto)
  14. self.assertIn('url', params)
  15. self.assertIn(query, params['url'])
  16. self.assertIn('wikidata.org', params['url'])
  17. self.assertIn('en', params['url'])
  18. dicto['language'] = 'es-ES'
  19. params = wikidata.request(query, dicto)
  20. self.assertIn(query, params['url'])
  21. self.assertIn('es', params['url'])
  22. # successful cases are not tested here to avoid sending additional requests
  23. def test_response(self):
  24. self.assertRaises(AttributeError, wikidata.response, None)
  25. self.assertRaises(AttributeError, wikidata.response, [])
  26. self.assertRaises(AttributeError, wikidata.response, '')
  27. self.assertRaises(AttributeError, wikidata.response, '[]')
  28. response = mock.Mock(text='<html></html>', search_params={"language": "en"})
  29. self.assertEqual(wikidata.response(response), [])
  30. def test_getDetail(self):
  31. response = {}
  32. results = wikidata.getDetail(response, "Q123", "en", "en-US")
  33. self.assertEqual(results, [])
  34. title_html = '<div><div class="wikibase-title-label">Test</div></div>'
  35. html = """
  36. <div>
  37. <div class="wikibase-entitytermsview-heading-description">
  38. </div>
  39. <div>
  40. <ul class="wikibase-sitelinklistview-listview">
  41. <li data-wb-siteid="enwiki"><a href="http://en.wikipedia.org/wiki/Test">Test</a></li>
  42. </ul>
  43. </div>
  44. </div>
  45. """
  46. response = {"parse": {"displaytitle": title_html, "text": html}}
  47. results = wikidata.getDetail(response, "Q123", "en", "en-US")
  48. self.assertEqual(len(results), 1)
  49. self.assertEqual(results[0]['url'], 'https://en.wikipedia.org/wiki/Test')
  50. title_html = """
  51. <div>
  52. <div class="wikibase-title-label">
  53. <span lang="en">Test</span>
  54. <sup class="wb-language-fallback-indicator">English</sup>
  55. </div>
  56. </div>
  57. """
  58. html = """
  59. <div>
  60. <div class="wikibase-entitytermsview-heading-description">
  61. <span lang="en">Description</span>
  62. <sup class="wb-language-fallback-indicator">English</sup>
  63. </div>
  64. <div id="P856">
  65. <div class="wikibase-statementgroupview-property-label">
  66. <a href="/wiki/Property:P856">
  67. <span lang="en">official website</span>
  68. <sup class="wb-language-fallback-indicator">English</sup>
  69. </a>
  70. </div>
  71. <div class="wikibase-statementview-mainsnak">
  72. <a class="external free" href="https://officialsite.com">
  73. https://officialsite.com
  74. </a>
  75. </div>
  76. </div>
  77. <div>
  78. <ul class="wikibase-sitelinklistview-listview">
  79. <li data-wb-siteid="enwiki"><a href="http://en.wikipedia.org/wiki/Test">Test</a></li>
  80. </ul>
  81. </div>
  82. </div>
  83. """
  84. response = {"parse": {"displaytitle": title_html, "text": html}}
  85. results = wikidata.getDetail(response, "Q123", "yua", "yua_MX")
  86. self.assertEqual(len(results), 2)
  87. self.assertEqual(results[0]['title'], 'Official website')
  88. self.assertEqual(results[0]['url'], 'https://officialsite.com')
  89. self.assertEqual(results[1]['infobox'], 'Test')
  90. self.assertEqual(results[1]['id'], None)
  91. self.assertEqual(results[1]['content'], 'Description')
  92. self.assertEqual(results[1]['attributes'], [])
  93. self.assertEqual(results[1]['urls'][0]['title'], 'Official website')
  94. self.assertEqual(results[1]['urls'][0]['url'], 'https://officialsite.com')
  95. self.assertEqual(results[1]['urls'][1]['title'], 'Wikipedia (en)')
  96. self.assertEqual(results[1]['urls'][1]['url'], 'https://en.wikipedia.org/wiki/Test')
  97. def test_add_image(self):
  98. image_src = wikidata.add_image(fromstring("<div></div>"))
  99. self.assertEqual(image_src, None)
  100. html = u"""
  101. <div>
  102. <div id="P18">
  103. <div class="wikibase-statementgroupview-property-label">
  104. <a href="/wiki/Property:P18">
  105. image
  106. </a>
  107. </div>
  108. <div class="wikibase-statementlistview">
  109. <div class="wikibase-statementview listview-item">
  110. <div class="wikibase-statementview-rankselector">
  111. <span class="wikibase-rankselector-normal"></span>
  112. </div>
  113. <div class="wikibase-statementview-mainsnak">
  114. <div>
  115. <div class="wikibase-snakview-value">
  116. <a href="https://commons.wikimedia.org/wiki/File:image.png">
  117. image.png
  118. </a>
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. </div>
  124. </div>
  125. </div>
  126. """
  127. html_etree = fromstring(html)
  128. image_src = wikidata.add_image(html_etree)
  129. self.assertEqual(image_src,
  130. "https://commons.wikimedia.org/wiki/Special:FilePath/image.png?width=500&height=400")
  131. html = u"""
  132. <div>
  133. <div id="P2910">
  134. <div class="wikibase-statementgroupview-property-label">
  135. <a href="/wiki/Property:P2910">
  136. icon
  137. </a>
  138. </div>
  139. <div class="wikibase-statementlistview">
  140. <div class="wikibase-statementview listview-item">
  141. <div class="wikibase-statementview-rankselector">
  142. <span class="wikibase-rankselector-normal"></span>
  143. </div>
  144. <div class="wikibase-statementview-mainsnak">
  145. <div>
  146. <div class="wikibase-snakview-value">
  147. <a href="https://commons.wikimedia.org/wiki/File:icon.png">
  148. icon.png
  149. </a>
  150. </div>
  151. </div>
  152. </div>
  153. </div>
  154. </div>
  155. </div>
  156. <div id="P154">
  157. <div class="wikibase-statementgroupview-property-label">
  158. <a href="/wiki/Property:P154">
  159. logo
  160. </a>
  161. </div>
  162. <div class="wikibase-statementlistview">
  163. <div class="wikibase-statementview listview-item">
  164. <div class="wikibase-statementview-rankselector">
  165. <span class="wikibase-rankselector-normal"></span>
  166. </div>
  167. <div class="wikibase-statementview-mainsnak">
  168. <div>
  169. <div class="wikibase-snakview-value">
  170. <a href="https://commons.wikimedia.org/wiki/File:logo.png">
  171. logo.png
  172. </a>
  173. </div>
  174. </div>
  175. </div>
  176. </div>
  177. </div>
  178. </div>
  179. </div>
  180. """
  181. html_etree = fromstring(html)
  182. image_src = wikidata.add_image(html_etree)
  183. self.assertEqual(image_src,
  184. "https://commons.wikimedia.org/wiki/Special:FilePath/logo.png?width=500&height=400")
  185. def test_add_attribute(self):
  186. html = u"""
  187. <div>
  188. <div id="P27">
  189. <div class="wikibase-statementgroupview-property-label">
  190. <a href="/wiki/Property:P27">
  191. country of citizenship
  192. </a>
  193. </div>
  194. <div class="wikibase-statementlistview">
  195. <div class="wikibase-statementview listview-item">
  196. <div class="wikibase-statementview-rankselector">
  197. <span class="wikibase-rankselector-normal"></span>
  198. </div>
  199. <div class="wikibase-statementview-mainsnak">
  200. <div>
  201. <div class="wikibase-snakview-value">
  202. <a href="/wiki/Q145">
  203. United Kingdom
  204. </a>
  205. </div>
  206. </div>
  207. </div>
  208. </div>
  209. </div>
  210. </div>
  211. </div>
  212. """
  213. attributes = []
  214. html_etree = fromstring(html)
  215. wikidata.add_attribute(attributes, html_etree, "Fail")
  216. self.assertEqual(attributes, [])
  217. wikidata.add_attribute(attributes, html_etree, "P27")
  218. self.assertEqual(len(attributes), 1)
  219. self.assertEqual(attributes[0]["label"], "Country of citizenship")
  220. self.assertEqual(attributes[0]["value"], "United Kingdom")
  221. html = u"""
  222. <div>
  223. <div id="P569">
  224. <div class="wikibase-statementgroupview-property-label">
  225. <a href="/wiki/Property:P569">
  226. date of birth
  227. </a>
  228. </div>
  229. <div class="wikibase-statementlistview">
  230. <div class="wikibase-statementview listview-item">
  231. <div class="wikibase-statementview-rankselector">
  232. <span class="wikibase-rankselector-normal"></span>
  233. </div>
  234. <div class="wikibase-statementview-mainsnak">
  235. <div>
  236. <div class="wikibase-snakview-value">
  237. 27 January 1832
  238. <sup class="wb-calendar-name">
  239. Gregorian
  240. </sup>
  241. </div>
  242. </div>
  243. </div>
  244. </div>
  245. </div>
  246. </div>
  247. </div>
  248. """
  249. attributes = []
  250. html_etree = fromstring(html)
  251. wikidata.add_attribute(attributes, html_etree, "P569", date=True)
  252. self.assertEqual(len(attributes), 1)
  253. self.assertEqual(attributes[0]["label"], "Date of birth")
  254. self.assertEqual(attributes[0]["value"], "27 January 1832")
  255. html = u"""
  256. <div>
  257. <div id="P6">
  258. <div class="wikibase-statementgroupview-property-label">
  259. <a href="/wiki/Property:P27">
  260. head of government
  261. </a>
  262. </div>
  263. <div class="wikibase-statementlistview">
  264. <div class="wikibase-statementview listview-item">
  265. <div class="wikibase-statementview-rankselector">
  266. <span class="wikibase-rankselector-normal"></span>
  267. </div>
  268. <div class="wikibase-statementview-mainsnak">
  269. <div>
  270. <div class="wikibase-snakview-value">
  271. <a href="/wiki/Q206">
  272. Old Prime Minister
  273. </a>
  274. </div>
  275. </div>
  276. </div>
  277. </div>
  278. <div class="wikibase-statementview listview-item">
  279. <div class="wikibase-statementview-rankselector">
  280. <span class="wikibase-rankselector-preferred"></span>
  281. </div>
  282. <div class="wikibase-statementview-mainsnak">
  283. <div>
  284. <div class="wikibase-snakview-value">
  285. <a href="/wiki/Q3099714">
  286. Actual Prime Minister
  287. </a>
  288. </div>
  289. </div>
  290. </div>
  291. </div>
  292. </div>
  293. </div>
  294. </div>
  295. """
  296. attributes = []
  297. html_etree = fromstring(html)
  298. wikidata.add_attribute(attributes, html_etree, "P6")
  299. self.assertEqual(len(attributes), 1)
  300. self.assertEqual(attributes[0]["label"], "Head of government")
  301. self.assertEqual(attributes[0]["value"], "Old Prime Minister, Actual Prime Minister")
  302. attributes = []
  303. html_etree = fromstring(html)
  304. wikidata.add_attribute(attributes, html_etree, "P6", trim=True)
  305. self.assertEqual(len(attributes), 1)
  306. self.assertEqual(attributes[0]["value"], "Actual Prime Minister")
  307. def test_add_url(self):
  308. html = u"""
  309. <div>
  310. <div id="P856">
  311. <div class="wikibase-statementgroupview-property-label">
  312. <a href="/wiki/Property:P856">
  313. official website
  314. </a>
  315. </div>
  316. <div class="wikibase-statementlistview">
  317. <div class="wikibase-statementview listview-item">
  318. <div class="wikibase-statementview-mainsnak">
  319. <div>
  320. <div class="wikibase-snakview-value">
  321. <a class="external free" href="https://searx.me">
  322. https://searx.me/
  323. </a>
  324. </div>
  325. </div>
  326. </div>
  327. </div>
  328. </div>
  329. </div>
  330. </div>
  331. """
  332. urls = []
  333. html_etree = fromstring(html)
  334. wikidata.add_url(urls, html_etree, 'P856')
  335. self.assertEquals(len(urls), 1)
  336. self.assertIn({'title': 'Official website', 'url': 'https://searx.me/'}, urls)
  337. urls = []
  338. results = []
  339. wikidata.add_url(urls, html_etree, 'P856', 'custom label', results=results)
  340. self.assertEquals(len(urls), 1)
  341. self.assertEquals(len(results), 1)
  342. self.assertIn({'title': 'custom label', 'url': 'https://searx.me/'}, urls)
  343. self.assertIn({'title': 'custom label', 'url': 'https://searx.me/'}, results)
  344. html = u"""
  345. <div>
  346. <div id="P856">
  347. <div class="wikibase-statementgroupview-property-label">
  348. <a href="/wiki/Property:P856">
  349. official website
  350. </a>
  351. </div>
  352. <div class="wikibase-statementlistview">
  353. <div class="wikibase-statementview listview-item">
  354. <div class="wikibase-statementview-mainsnak">
  355. <div>
  356. <div class="wikibase-snakview-value">
  357. <a class="external free" href="http://www.worldofwarcraft.com">
  358. http://www.worldofwarcraft.com
  359. </a>
  360. </div>
  361. </div>
  362. </div>
  363. </div>
  364. <div class="wikibase-statementview listview-item">
  365. <div class="wikibase-statementview-mainsnak">
  366. <div>
  367. <div class="wikibase-snakview-value">
  368. <a class="external free" href="http://eu.battle.net/wow/en/">
  369. http://eu.battle.net/wow/en/
  370. </a>
  371. </div>
  372. </div>
  373. </div>
  374. </div>
  375. </div>
  376. </div>
  377. </div>
  378. """
  379. urls = []
  380. html_etree = fromstring(html)
  381. wikidata.add_url(urls, html_etree, 'P856')
  382. self.assertEquals(len(urls), 2)
  383. self.assertIn({'title': 'Official website', 'url': 'http://www.worldofwarcraft.com'}, urls)
  384. self.assertIn({'title': 'Official website', 'url': 'http://eu.battle.net/wow/en/'}, urls)
  385. def test_get_imdblink(self):
  386. html = u"""
  387. <div>
  388. <div class="wikibase-statementview-mainsnak">
  389. <div>
  390. <div class="wikibase-snakview-value">
  391. <a class="wb-external-id" href="http://www.imdb.com/tt0433664">
  392. tt0433664
  393. </a>
  394. </div>
  395. </div>
  396. </div>
  397. </div>
  398. """
  399. html_etree = fromstring(html)
  400. imdblink = wikidata.get_imdblink(html_etree, 'https://www.imdb.com/')
  401. html = u"""
  402. <div>
  403. <div class="wikibase-statementview-mainsnak">
  404. <div>
  405. <div class="wikibase-snakview-value">
  406. <a class="wb-external-id"
  407. href="href="http://tools.wmflabs.org/...http://www.imdb.com/&id=nm4915994"">
  408. nm4915994
  409. </a>
  410. </div>
  411. </div>
  412. </div>
  413. </div>
  414. """
  415. html_etree = fromstring(html)
  416. imdblink = wikidata.get_imdblink(html_etree, 'https://www.imdb.com/')
  417. self.assertIn('https://www.imdb.com/name/nm4915994', imdblink)
  418. def test_get_geolink(self):
  419. html = u"""
  420. <div>
  421. <div class="wikibase-statementview-mainsnak">
  422. <div>
  423. <div class="wikibase-snakview-value">
  424. 60°N, 40°E
  425. </div>
  426. </div>
  427. </div>
  428. </div>
  429. """
  430. html_etree = fromstring(html)
  431. geolink = wikidata.get_geolink(html_etree)
  432. self.assertIn('https://www.openstreetmap.org/', geolink)
  433. self.assertIn('lat=60&lon=40', geolink)
  434. html = u"""
  435. <div>
  436. <div class="wikibase-statementview-mainsnak">
  437. <div>
  438. <div class="wikibase-snakview-value">
  439. 34°35'59"S, 58°22'55"W
  440. </div>
  441. </div>
  442. </div>
  443. </div>
  444. """
  445. html_etree = fromstring(html)
  446. geolink = wikidata.get_geolink(html_etree)
  447. self.assertIn('https://www.openstreetmap.org/', geolink)
  448. self.assertIn('lat=-34.59', geolink)
  449. self.assertIn('lon=-58.38', geolink)
  450. def test_get_wikilink(self):
  451. html = """
  452. <div>
  453. <div>
  454. <ul class="wikibase-sitelinklistview-listview">
  455. <li data-wb-siteid="arwiki"><a href="http://ar.wikipedia.org/wiki/Test">Test</a></li>
  456. <li data-wb-siteid="enwiki"><a href="http://en.wikipedia.org/wiki/Test">Test</a></li>
  457. </ul>
  458. </div>
  459. <div>
  460. <ul class="wikibase-sitelinklistview-listview">
  461. <li data-wb-siteid="enwikiquote"><a href="https://en.wikiquote.org/wiki/Test">Test</a></li>
  462. </ul>
  463. </div>
  464. </div>
  465. """
  466. html_etree = fromstring(html)
  467. wikilink = wikidata.get_wikilink(html_etree, 'nowiki')
  468. self.assertEqual(wikilink, None)
  469. wikilink = wikidata.get_wikilink(html_etree, 'enwiki')
  470. self.assertEqual(wikilink, 'https://en.wikipedia.org/wiki/Test')
  471. wikilink = wikidata.get_wikilink(html_etree, 'arwiki')
  472. self.assertEqual(wikilink, 'https://ar.wikipedia.org/wiki/Test')
  473. wikilink = wikidata.get_wikilink(html_etree, 'enwikiquote')
  474. self.assertEqual(wikilink, 'https://en.wikiquote.org/wiki/Test')