test_search.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. from django.urls import reverse
  2. from misago.categories.models import Category
  3. from misago.users.testutils import AuthenticatedUserTestCase
  4. from .. import testutils
  5. class SearchApiTests(AuthenticatedUserTestCase):
  6. def setUp(self):
  7. super(SearchApiTests, self).setUp()
  8. self.category = Category.objects.get(slug='first-category')
  9. self.api_link = reverse('misago:api:search')
  10. def test_no_query(self):
  11. """api handles no search query"""
  12. response = self.client.get(self.api_link)
  13. self.assertEqual(response.status_code, 200)
  14. reponse_json = response.json()
  15. self.assertIn('threads', [p['id'] for p in reponse_json])
  16. for provider in reponse_json:
  17. if provider['id'] == 'threads':
  18. self.assertEqual(provider['results']['results'], [])
  19. def test_empty_query(self):
  20. """api handles empty search query"""
  21. response = self.client.get('%s?q=' % self.api_link)
  22. self.assertEqual(response.status_code, 200)
  23. reponse_json = response.json()
  24. self.assertIn('threads', [p['id'] for p in reponse_json])
  25. for provider in reponse_json:
  26. if provider['id'] == 'threads':
  27. self.assertEqual(provider['results']['results'], [])
  28. def test_short_query(self):
  29. """api handles short search query"""
  30. thread = testutils.post_thread(self.category)
  31. post = testutils.reply_thread(thread, message="Lorem ipsum dolor.")
  32. response = self.client.get('%s?q=ip' % self.api_link)
  33. self.assertEqual(response.status_code, 200)
  34. reponse_json = response.json()
  35. self.assertIn('threads', [p['id'] for p in reponse_json])
  36. for provider in reponse_json:
  37. if provider['id'] == 'threads':
  38. self.assertEqual(provider['results']['results'], [])
  39. def test_wrong_query(self):
  40. """api handles query miss"""
  41. thread = testutils.post_thread(self.category)
  42. post = testutils.reply_thread(thread, message="Lorem ipsum dolor.")
  43. response = self.client.get('%s?q=elit' % self.api_link)
  44. self.assertEqual(response.status_code, 200)
  45. reponse_json = response.json()
  46. self.assertIn('threads', [p['id'] for p in reponse_json])
  47. for provider in reponse_json:
  48. if provider['id'] == 'threads':
  49. self.assertEqual(provider['results']['results'], [])
  50. def test_hidden_post(self):
  51. """hidden posts are extempt from search"""
  52. thread = testutils.post_thread(self.category)
  53. post = testutils.reply_thread(
  54. thread, message="Lorem ipsum dolor.", is_hidden=True)
  55. response = self.client.get('%s?q=ipsum' % self.api_link)
  56. self.assertEqual(response.status_code, 200)
  57. reponse_json = response.json()
  58. self.assertIn('threads', [p['id'] for p in reponse_json])
  59. for provider in reponse_json:
  60. if provider['id'] == 'threads':
  61. self.assertEqual(provider['results']['results'], [])
  62. def test_unapproved_post(self):
  63. """unapproves posts are extempt from search"""
  64. thread = testutils.post_thread(self.category)
  65. post = testutils.reply_thread(
  66. thread, message="Lorem ipsum dolor.", is_unapproved=True)
  67. response = self.client.get('%s?q=ipsum' % self.api_link)
  68. self.assertEqual(response.status_code, 200)
  69. reponse_json = response.json()
  70. self.assertIn('threads', [p['id'] for p in reponse_json])
  71. for provider in reponse_json:
  72. if provider['id'] == 'threads':
  73. self.assertEqual(provider['results']['results'], [])
  74. def test_query(self):
  75. """api handles search query"""
  76. thread = testutils.post_thread(self.category)
  77. post = testutils.reply_thread(thread, message="Lorem ipsum dolor.")
  78. response = self.client.get('%s?q=ipsum' % self.api_link)
  79. self.assertEqual(response.status_code, 200)
  80. reponse_json = response.json()
  81. self.assertIn('threads', [p['id'] for p in reponse_json])
  82. for provider in reponse_json:
  83. if provider['id'] == 'threads':
  84. results = provider['results']['results']
  85. self.assertEqual(len(results), 1)
  86. self.assertEqual(results[0]['id'], post.id)
  87. def test_thread_title_search(self):
  88. """api searches threads by title"""
  89. thread = testutils.post_thread(self.category, title="Atmosphere of mars")
  90. testutils.reply_thread(thread, message="Lorem ipsum dolor.")
  91. response = self.client.get('%s?q=mars atmosphere' % self.api_link)
  92. self.assertEqual(response.status_code, 200)
  93. reponse_json = response.json()
  94. self.assertIn('threads', [p['id'] for p in reponse_json])
  95. for provider in reponse_json:
  96. if provider['id'] == 'threads':
  97. results = provider['results']['results']
  98. self.assertEqual(len(results), 1)
  99. self.assertEqual(results[0]['id'], thread.first_post_id)
  100. def test_complex_query(self):
  101. """api handles complex query that uses fulltext search facilities"""
  102. thread = testutils.post_thread(self.category)
  103. post = testutils.reply_thread(thread, message="Atmosphere of Mars")
  104. response = self.client.get('%s?q=Mars atmosphere' % self.api_link)
  105. self.assertEqual(response.status_code, 200)
  106. reponse_json = response.json()
  107. self.assertIn('threads', [p['id'] for p in reponse_json])
  108. for provider in reponse_json:
  109. if provider['id'] == 'threads':
  110. results = provider['results']['results']
  111. self.assertEqual(len(results), 1)
  112. self.assertEqual(results[0]['id'], post.id)
  113. class SearchProviderApiTests(SearchApiTests):
  114. def setUp(self):
  115. super(SearchProviderApiTests, self).setUp()
  116. self.api_link = reverse('misago:api:search', kwargs={
  117. 'search_provider': 'threads'
  118. })