123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import mock
  2. from collections import defaultdict
  3. from searx.engines import pdbe
  4. from searx.testing import SearxTestCase
  5. class TestPdbeEngine(SearxTestCase):
  6. def test_request(self):
  7. query = 'test_query'
  8. dicto = defaultdict(dict)
  9. params = pdbe.request(query, dicto)
  10. self.assertTrue('url' in params)
  11. self.assertTrue('ebi.ac.uk' in params['url'])
  12. self.assertTrue('data' in params)
  13. self.assertTrue('q' in params['data'])
  14. self.assertTrue(query in params['data']['q'])
  15. self.assertTrue('wt' in params['data'])
  16. self.assertTrue('json' in params['data']['wt'])
  17. self.assertTrue('method' in params)
  18. self.assertTrue(params['method'] == 'POST')
  19. def test_response(self):
  20. self.assertRaises(AttributeError, pdbe.response, None)
  21. self.assertRaises(AttributeError, pdbe.response, [])
  22. self.assertRaises(AttributeError, pdbe.response, '')
  23. self.assertRaises(AttributeError, pdbe.response, '[]')
  24. json = """
  25. {
  26. "response": {
  27. "docs": [
  28. {
  29. "citation_title": "X-ray crystal structure of ferric Aplysia limacina myoglobin in different liganded states.",
  30. "citation_year": 1993,
  31. "entry_author_list": [
  32. "Conti E, Moser C, Rizzi M, Mattevi A, Lionetti C, Coda A, Ascenzi P, Brunori M, Bolognesi M"
  33. ],
  34. "journal": "J. Mol. Biol.",
  35. "journal_page": "498-508",
  36. "journal_volume": "233",
  37. "pdb_id": "2fal",
  38. "status": "REL",
  39. "title": "X-RAY CRYSTAL STRUCTURE OF FERRIC APLYSIA LIMACINA MYOGLOBIN IN DIFFERENT LIGANDED STATES"
  40. }
  41. ],
  42. "numFound": 1,
  43. "start": 0
  44. },
  45. "responseHeader": {
  46. "QTime": 0,
  47. "params": {
  48. "q": "2fal",
  49. "wt": "json"
  50. },
  51. "status": 0
  52. }
  53. }
  54. """
  55. response = mock.Mock(text=json)
  56. results = pdbe.response(response)
  57. self.assertEqual(type(results), list)
  58. self.assertEqual(len(results), 1)
  59. self.assertEqual(results[0]['title'],
  60. 'X-RAY CRYSTAL STRUCTURE OF FERRIC APLYSIA LIMACINA MYOGLOBIN IN DIFFERENT LIGANDED STATES')
  61. self.assertEqual(results[0]['url'], pdbe.pdbe_entry_url.format(pdb_id='2fal'))
  62. self.assertEqual(results[0]['img_src'], pdbe.pdbe_preview_url.format(pdb_id='2fal'))
  63. self.assertTrue('Conti E' in results[0]['content'])
  64. self.assertTrue('X-ray crystal structure of ferric Aplysia limacina myoglobin in different liganded states.' in
  65. results[0]['content'])
  66. self.assertTrue('1993' in results[0]['content'])
  67. # Testing proper handling of PDB entries marked as obsolete
  68. json = """
  69. {
  70. "response": {
  71. "docs": [
  72. {
  73. "citation_title": "Obsolete entry test",
  74. "citation_year": 2016,
  75. "entry_author_list": ["Doe J"],
  76. "journal": "J. Obs.",
  77. "journal_page": "1-2",
  78. "journal_volume": "1",
  79. "pdb_id": "xxxx",
  80. "status": "OBS",
  81. "title": "OBSOLETE ENTRY TEST",
  82. "superseded_by": "yyyy"
  83. }
  84. ],
  85. "numFound": 1,
  86. "start": 0
  87. },
  88. "responseHeader": {
  89. "QTime": 0,
  90. "params": {
  91. "q": "xxxx",
  92. "wt": "json"
  93. },
  94. "status": 0
  95. }
  96. }
  97. """
  98. response = mock.Mock(text=json)
  99. results = pdbe.response(response)
  100. self.assertEqual(type(results), list)
  101. self.assertEqual(len(results), 1)
  102. self.assertEqual(results[0]['title'], 'OBSOLETE ENTRY TEST (OBSOLETE)')
  103. self.assertTrue(results[0]['content'].startswith('This entry has been superseded by'))