moviewyrm/bookwyrm/tests/views/test_search.py

120 lines
4.3 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" test for app action functionality """
2021-01-13 20:03:27 +00:00
import json
from unittest.mock import patch
from django.http import JsonResponse
from django.template.response import TemplateResponse
from django.test import TestCase
from django.test.client import RequestFactory
from bookwyrm import models, views
from bookwyrm.connectors import abstract_connector
from bookwyrm.settings import DOMAIN
class ShelfViews(TestCase):
2021-03-08 16:49:10 +00:00
""" tag views"""
2021-01-13 20:03:27 +00:00
def setUp(self):
2021-03-08 16:49:10 +00:00
""" we need basic test data and mocks """
2021-01-13 20:03:27 +00:00
self.factory = RequestFactory()
self.local_user = models.User.objects.create_user(
2021-03-08 16:49:10 +00:00
"mouse@local.com",
"mouse@mouse.com",
"mouseword",
local=True,
localname="mouse",
remote_id="https://example.com/users/mouse",
2021-01-13 20:03:27 +00:00
)
2021-03-08 16:49:10 +00:00
self.work = models.Work.objects.create(title="Test Work")
2021-01-13 20:03:27 +00:00
self.book = models.Edition.objects.create(
2021-03-08 16:49:10 +00:00
title="Test Book",
remote_id="https://example.com/book/1",
parent_work=self.work,
2021-01-13 20:03:27 +00:00
)
models.Connector.objects.create(
2021-03-08 16:49:10 +00:00
identifier="self", connector_file="self_connector", local=True
2021-01-13 20:03:27 +00:00
)
models.SiteSettings.objects.create()
2021-01-13 20:03:27 +00:00
def test_search_json_response(self):
2021-03-08 16:49:10 +00:00
""" searches local data only and returns book data in json format """
2021-01-13 20:03:27 +00:00
view = views.Search.as_view()
# we need a connector for this, sorry
2021-03-08 16:49:10 +00:00
request = self.factory.get("", {"q": "Test Book"})
with patch("bookwyrm.views.search.is_api_request") as is_api:
2021-01-13 20:03:27 +00:00
is_api.return_value = True
response = view(request)
self.assertIsInstance(response, JsonResponse)
data = json.loads(response.content)
self.assertEqual(len(data), 1)
2021-03-08 16:49:10 +00:00
self.assertEqual(data[0]["title"], "Test Book")
self.assertEqual(data[0]["key"], "https://%s/book/%d" % (DOMAIN, self.book.id))
2021-01-13 20:03:27 +00:00
def test_search_html_response(self):
2021-03-08 16:49:10 +00:00
""" searches remote connectors """
2021-01-13 20:03:27 +00:00
view = views.Search.as_view()
2021-03-08 16:49:10 +00:00
2021-01-13 20:03:27 +00:00
class TestConnector(abstract_connector.AbstractMinimalConnector):
2021-03-08 16:49:10 +00:00
""" nothing added here """
2021-01-13 20:03:27 +00:00
def format_search_result(self, search_result):
pass
2021-03-08 16:49:10 +00:00
2021-01-13 20:03:27 +00:00
def get_or_create_book(self, remote_id):
pass
2021-03-08 16:49:10 +00:00
2021-01-13 20:03:27 +00:00
def parse_search_data(self, data):
pass
2021-03-08 16:49:10 +00:00
2021-03-01 20:09:21 +00:00
def format_isbn_search_result(self, search_result):
return search_result
2021-03-08 16:49:10 +00:00
2021-03-01 20:09:21 +00:00
def parse_isbn_search_data(self, data):
return data
2021-03-08 16:49:10 +00:00
2021-01-13 20:03:27 +00:00
models.Connector.objects.create(
2021-03-08 16:49:10 +00:00
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=",
2021-01-13 20:03:27 +00:00
)
2021-03-08 16:49:10 +00:00
connector = TestConnector("example.com")
2021-01-13 20:03:27 +00:00
search_result = abstract_connector.SearchResult(
2021-03-08 16:49:10 +00:00
key="http://www.example.com/book/1",
title="Gideon the Ninth",
author="Tamsyn Muir",
year="2019",
connector=connector,
2021-01-13 20:03:27 +00:00
)
2021-03-08 16:49:10 +00:00
request = self.factory.get("", {"q": "Test Book"})
2021-02-01 21:06:06 +00:00
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.search.is_api_request") as is_api:
2021-01-13 20:03:27 +00:00
is_api.return_value = False
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.connectors.connector_manager.search") as manager:
2021-01-13 20:03:27 +00:00
manager.return_value = [search_result]
response = view(request)
self.assertIsInstance(response, TemplateResponse)
response.render()
2021-01-13 20:03:27 +00:00
self.assertEqual(
2021-03-08 16:49:10 +00:00
response.context_data["book_results"][0].title, "Gideon the Ninth"
)
2021-01-13 20:03:27 +00:00
def test_search_html_response_users(self):
2021-03-08 16:49:10 +00:00
""" searches remote connectors """
2021-01-13 20:03:27 +00:00
view = views.Search.as_view()
2021-03-08 16:49:10 +00:00
request = self.factory.get("", {"q": "mouse"})
2021-02-01 21:06:06 +00:00
request.user = self.local_user
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.views.search.is_api_request") as is_api:
2021-01-13 20:03:27 +00:00
is_api.return_value = False
2021-03-08 16:49:10 +00:00
with patch("bookwyrm.connectors.connector_manager.search"):
2021-01-13 20:03:27 +00:00
response = view(request)
self.assertIsInstance(response, TemplateResponse)
response.render()
2021-03-08 16:49:10 +00:00
self.assertEqual(response.context_data["user_results"][0], self.local_user)