moviewyrm/bookwyrm/tests/connectors/test_self_connector.py

108 lines
4.1 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing book data connectors """
import datetime
from django.test import TestCase
from django.utils import timezone
from bookwyrm import models
from bookwyrm.connectors.self_connector import Connector
from bookwyrm.settings import DOMAIN
class SelfConnector(TestCase):
2021-04-26 16:15:42 +00:00
"""just uses local data"""
2021-03-08 16:49:10 +00:00
def setUp(self):
2021-04-26 16:15:42 +00:00
"""creating the connector"""
models.Connector.objects.create(
identifier=DOMAIN,
2021-03-08 16:49:10 +00:00
name="Local",
local=True,
2021-03-08 16:49:10 +00:00
connector_file="self_connector",
base_url="https://%s" % DOMAIN,
books_url="https://%s/book" % DOMAIN,
covers_url="https://%s/images/covers" % DOMAIN,
search_url="https://%s/search?q=" % DOMAIN,
priority=1,
)
self.connector = Connector(DOMAIN)
2021-01-02 22:20:39 +00:00
def test_format_search_result(self):
2021-04-26 16:15:42 +00:00
"""create a SearchResult"""
2021-03-08 16:49:10 +00:00
author = models.Author.objects.create(name="Anonymous")
2021-08-02 23:05:40 +00:00
edition = models.Edition.objects.create(
title="Edition of Example Work",
published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc),
)
edition.authors.add(author)
result = self.connector.search("Edition of Example")[0]
2021-03-08 16:49:10 +00:00
self.assertEqual(result.title, "Edition of Example Work")
2021-01-02 22:20:39 +00:00
self.assertEqual(result.key, edition.remote_id)
2021-03-08 16:49:10 +00:00
self.assertEqual(result.author, "Anonymous")
2021-01-02 22:20:39 +00:00
self.assertEqual(result.year, 1980)
self.assertEqual(result.connector, self.connector)
2021-08-02 23:05:40 +00:00
def test_search_rank(self):
2021-04-26 16:15:42 +00:00
"""prioritize certain results"""
2021-03-08 16:49:10 +00:00
author = models.Author.objects.create(name="Anonymous")
2021-06-26 16:57:56 +00:00
edition = models.Edition.objects.create(
title="Edition of Example Work",
published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc),
parent_work=models.Work.objects.create(title=""),
)
# author text is rank B
edition.authors.add(author)
2021-01-02 22:20:39 +00:00
2021-06-26 16:57:56 +00:00
# series is rank D
models.Edition.objects.create(
title="Another Edition",
series="Anonymous",
parent_work=models.Work.objects.create(title=""),
)
# subtitle is rank B
models.Edition.objects.create(
title="More Editions",
subtitle="The Anonymous Edition",
parent_work=models.Work.objects.create(title=""),
)
# title is rank A
models.Edition.objects.create(title="Anonymous")
# doesn't rank in this search
models.Edition.objects.create(
2021-06-26 16:57:56 +00:00
title="An Edition", parent_work=models.Work.objects.create(title="")
)
2021-06-26 16:57:56 +00:00
results = self.connector.search("Anonymous")
self.assertEqual(len(results), 4)
2021-03-08 16:49:10 +00:00
self.assertEqual(results[0].title, "Anonymous")
self.assertEqual(results[1].title, "More Editions")
self.assertEqual(results[2].title, "Edition of Example Work")
self.assertEqual(results[3].title, "Another Edition")
2021-08-02 23:05:40 +00:00
def test_search_multiple_editions(self):
2021-04-26 16:15:42 +00:00
"""it should get rid of duplicate editions for the same work"""
2021-06-26 16:57:56 +00:00
work = models.Work.objects.create(title="Work Title")
edition_1 = models.Edition.objects.create(
title="Edition 1 Title", parent_work=work
)
edition_2 = models.Edition.objects.create(
title="Edition 2 Title",
parent_work=work,
isbn_13="123456789", # this is now the defualt edition
)
edition_3 = models.Edition.objects.create(title="Fish", parent_work=work)
2020-11-08 01:48:50 +00:00
2021-06-26 16:57:56 +00:00
# pick the best edition
results = self.connector.search("Edition 1 Title")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_1.remote_id)
2021-06-26 16:57:56 +00:00
# pick the default edition when no match is best
results = self.connector.search("Edition Title")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_2.remote_id)
2021-06-26 16:57:56 +00:00
# only matches one edition, so no deduplication takes place
results = self.connector.search("Fish")
self.assertEqual(len(results), 1)
self.assertEqual(results[0].key, edition_3.remote_id)