mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-01-04 22:38:43 +00:00
updates search tests
This commit is contained in:
parent
8c4cafed79
commit
d9284ede9b
1 changed files with 18 additions and 35 deletions
|
@ -1,5 +1,6 @@
|
||||||
""" test for app action functionality """
|
""" test for app action functionality """
|
||||||
import json
|
import json
|
||||||
|
import pathlib
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from django.contrib.auth.models import AnonymousUser
|
from django.contrib.auth.models import AnonymousUser
|
||||||
|
@ -7,9 +8,9 @@ from django.http import JsonResponse
|
||||||
from django.template.response import TemplateResponse
|
from django.template.response import TemplateResponse
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
from django.test.client import RequestFactory
|
from django.test.client import RequestFactory
|
||||||
|
import responses
|
||||||
|
|
||||||
from bookwyrm import models, views
|
from bookwyrm import models, views
|
||||||
from bookwyrm.connectors import abstract_connector
|
|
||||||
from bookwyrm.settings import DOMAIN
|
from bookwyrm.settings import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
@ -36,15 +37,11 @@ class Views(TestCase):
|
||||||
remote_id="https://example.com/book/1",
|
remote_id="https://example.com/book/1",
|
||||||
parent_work=self.work,
|
parent_work=self.work,
|
||||||
)
|
)
|
||||||
models.Connector.objects.create(
|
|
||||||
identifier="self", connector_file="self_connector"
|
|
||||||
)
|
|
||||||
models.SiteSettings.objects.create()
|
models.SiteSettings.objects.create()
|
||||||
|
|
||||||
def test_search_json_response(self):
|
def test_search_json_response(self):
|
||||||
"""searches local data only and returns book data in json format"""
|
"""searches local data only and returns book data in json format"""
|
||||||
view = views.Search.as_view()
|
view = views.Search.as_view()
|
||||||
# we need a connector for this, sorry
|
|
||||||
request = self.factory.get("", {"q": "Test Book"})
|
request = self.factory.get("", {"q": "Test Book"})
|
||||||
with patch("bookwyrm.views.search.is_api_request") as is_api:
|
with patch("bookwyrm.views.search.is_api_request") as is_api:
|
||||||
is_api.return_value = True
|
is_api.return_value = True
|
||||||
|
@ -67,28 +64,11 @@ class Views(TestCase):
|
||||||
self.assertIsInstance(response, TemplateResponse)
|
self.assertIsInstance(response, TemplateResponse)
|
||||||
response.render()
|
response.render()
|
||||||
|
|
||||||
|
@responses.activate
|
||||||
def test_search_books(self):
|
def test_search_books(self):
|
||||||
"""searches remote connectors"""
|
"""searches remote connectors"""
|
||||||
view = views.Search.as_view()
|
view = views.Search.as_view()
|
||||||
|
|
||||||
class TestConnector(abstract_connector.AbstractMinimalConnector):
|
|
||||||
"""nothing added here"""
|
|
||||||
|
|
||||||
def format_search_result(self, search_result):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def get_or_create_book(self, remote_id):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def parse_search_data(self, data):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def format_isbn_search_result(self, search_result):
|
|
||||||
return search_result
|
|
||||||
|
|
||||||
def parse_isbn_search_data(self, data):
|
|
||||||
return data
|
|
||||||
|
|
||||||
models.Connector.objects.create(
|
models.Connector.objects.create(
|
||||||
identifier="example.com",
|
identifier="example.com",
|
||||||
connector_file="openlibrary",
|
connector_file="openlibrary",
|
||||||
|
@ -97,26 +77,29 @@ class Views(TestCase):
|
||||||
covers_url="https://example.com/covers",
|
covers_url="https://example.com/covers",
|
||||||
search_url="https://example.com/search?q=",
|
search_url="https://example.com/search?q=",
|
||||||
)
|
)
|
||||||
connector = TestConnector("example.com")
|
datafile = pathlib.Path(__file__).parent.joinpath("../data/ol_search.json")
|
||||||
|
search_data = json.loads(datafile.read_bytes())
|
||||||
search_result = abstract_connector.SearchResult(
|
responses.add(
|
||||||
key="http://www.example.com/book/1",
|
responses.GET,
|
||||||
title="Gideon the Ninth",
|
"https://example.com/search?q=Test%20Book",
|
||||||
author="Tamsyn Muir",
|
json=search_data
|
||||||
year="2019",
|
|
||||||
connector=connector,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
request = self.factory.get("", {"q": "Test Book", "remote": True})
|
request = self.factory.get("", {"q": "Test Book", "remote": True})
|
||||||
request.user = self.local_user
|
request.user = self.local_user
|
||||||
with patch("bookwyrm.views.search.is_api_request") as is_api:
|
with patch("bookwyrm.views.search.is_api_request") as is_api:
|
||||||
is_api.return_value = False
|
is_api.return_value = False
|
||||||
with patch("bookwyrm.connectors.connector_manager.search") as manager:
|
response = view(request)
|
||||||
manager.return_value = [search_result]
|
|
||||||
response = view(request)
|
|
||||||
self.assertIsInstance(response, TemplateResponse)
|
self.assertIsInstance(response, TemplateResponse)
|
||||||
response.render()
|
response.render()
|
||||||
self.assertEqual(response.context_data["results"][0].title, "Gideon the Ninth")
|
connector_results = response.context_data["results"]
|
||||||
|
self.assertEqual(
|
||||||
|
connector_results[0]["results"][0].title, "Test Book"
|
||||||
|
)
|
||||||
|
self.assertEqual(
|
||||||
|
connector_results[1]["results"][0].title,
|
||||||
|
"This Is How You Lose the Time War"
|
||||||
|
)
|
||||||
|
|
||||||
def test_search_users(self):
|
def test_search_users(self):
|
||||||
"""searches remote connectors"""
|
"""searches remote connectors"""
|
||||||
|
|
Loading…
Reference in a new issue