mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-25 01:20:35 +00:00
Updates tests
This commit is contained in:
parent
ed71b791c9
commit
b4cfda0587
2 changed files with 32 additions and 9 deletions
|
@ -17,7 +17,7 @@ from bookwyrm.tests.validate_html import validate_html
|
||||||
class Views(TestCase):
|
class Views(TestCase):
|
||||||
"""tag views"""
|
"""tag views"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self): # pylint: disable=invalid-name
|
||||||
"""we need basic test data and mocks"""
|
"""we need basic test data and mocks"""
|
||||||
self.factory = RequestFactory()
|
self.factory = RequestFactory()
|
||||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||||
|
@ -90,13 +90,29 @@ class Views(TestCase):
|
||||||
|
|
||||||
self.assertIsInstance(response, TemplateResponse)
|
self.assertIsInstance(response, TemplateResponse)
|
||||||
validate_html(response.render())
|
validate_html(response.render())
|
||||||
connector_results = response.context_data["results"]
|
|
||||||
self.assertEqual(len(connector_results), 2)
|
|
||||||
self.assertEqual(connector_results[0]["results"][0].title, "Test Book")
|
|
||||||
self.assertEqual(connector_results[1]["results"][0].title, "Mock Book")
|
|
||||||
|
|
||||||
# don't search remote
|
local_results = response.context_data["results"]
|
||||||
|
self.assertEqual(local_results[0].title, "Test Book")
|
||||||
|
|
||||||
|
connector_results = response.context_data["remote_results"]
|
||||||
|
self.assertEqual(connector_results[0]["results"][0].title, "Mock Book")
|
||||||
|
|
||||||
|
def test_search_book_anonymous(self):
|
||||||
|
"""Don't search remote for logged out user"""
|
||||||
|
view = views.Search.as_view()
|
||||||
|
|
||||||
|
connector = models.Connector.objects.create(
|
||||||
|
identifier="example.com",
|
||||||
|
connector_file="openlibrary",
|
||||||
|
base_url="https://example.com",
|
||||||
|
books_url="https://example.com/books",
|
||||||
|
covers_url="https://example.com/covers",
|
||||||
|
search_url="https://example.com/search?q=",
|
||||||
|
)
|
||||||
|
mock_result = SearchResult(title="Mock Book", connector=connector, key="hello")
|
||||||
|
|
||||||
request = self.factory.get("", {"q": "Test Book", "remote": True})
|
request = self.factory.get("", {"q": "Test Book", "remote": True})
|
||||||
|
|
||||||
anonymous_user = AnonymousUser
|
anonymous_user = AnonymousUser
|
||||||
anonymous_user.is_authenticated = False
|
anonymous_user.is_authenticated = False
|
||||||
request.user = anonymous_user
|
request.user = anonymous_user
|
||||||
|
@ -107,11 +123,15 @@ class Views(TestCase):
|
||||||
{"results": [mock_result], "connector": connector}
|
{"results": [mock_result], "connector": connector}
|
||||||
]
|
]
|
||||||
response = view(request)
|
response = view(request)
|
||||||
|
|
||||||
self.assertIsInstance(response, TemplateResponse)
|
self.assertIsInstance(response, TemplateResponse)
|
||||||
validate_html(response.render())
|
validate_html(response.render())
|
||||||
connector_results = response.context_data["results"]
|
|
||||||
self.assertEqual(len(connector_results), 1)
|
local_results = response.context_data["results"]
|
||||||
self.assertEqual(connector_results[0]["results"][0].title, "Test Book")
|
self.assertEqual(local_results[0].title, "Test Book")
|
||||||
|
|
||||||
|
connector_results = response.context_data.get("remote_results")
|
||||||
|
self.assertIsNone(connector_results)
|
||||||
|
|
||||||
def test_search_users(self):
|
def test_search_users(self):
|
||||||
"""searches remote connectors"""
|
"""searches remote connectors"""
|
||||||
|
|
|
@ -27,6 +27,9 @@ class Search(View):
|
||||||
return api_book_search(request)
|
return api_book_search(request)
|
||||||
|
|
||||||
query = request.GET.get("q")
|
query = request.GET.get("q")
|
||||||
|
if not query:
|
||||||
|
return TemplateResponse(request, "search/book.html")
|
||||||
|
|
||||||
search_type = request.GET.get("type")
|
search_type = request.GET.get("type")
|
||||||
if query and not search_type:
|
if query and not search_type:
|
||||||
search_type = "user" if "@" in query else "book"
|
search_type = "user" if "@" in query else "book"
|
||||||
|
|
Loading…
Reference in a new issue