2020-05-12 20:03:46 +00:00
|
|
|
''' testing book data connectors '''
|
|
|
|
import datetime
|
|
|
|
from django.test import TestCase
|
2020-11-28 00:24:53 +00:00
|
|
|
from django.utils import timezone
|
2020-05-12 20:03:46 +00:00
|
|
|
|
2020-09-21 15:10:37 +00:00
|
|
|
from bookwyrm import models
|
|
|
|
from bookwyrm.connectors.self_connector import Connector
|
|
|
|
from bookwyrm.settings import DOMAIN
|
2020-05-12 20:03:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SelfConnector(TestCase):
|
2021-01-02 22:20:39 +00:00
|
|
|
''' just uses local data '''
|
2020-05-12 20:03:46 +00:00
|
|
|
def setUp(self):
|
2021-01-02 22:20:39 +00:00
|
|
|
''' creating the connector '''
|
2020-05-12 20:03:46 +00:00
|
|
|
models.Connector.objects.create(
|
|
|
|
identifier=DOMAIN,
|
|
|
|
name='Local',
|
|
|
|
local=True,
|
|
|
|
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):
|
|
|
|
''' create a SearchResult '''
|
|
|
|
author = models.Author.objects.create(name='Anonymous')
|
|
|
|
edition = models.Edition.objects.create(
|
|
|
|
title='Edition of Example Work',
|
|
|
|
published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc),
|
2020-05-12 20:03:46 +00:00
|
|
|
)
|
2021-01-02 22:20:39 +00:00
|
|
|
edition.authors.add(author)
|
|
|
|
result = self.connector.search('Edition of Example')[0]
|
|
|
|
self.assertEqual(result.title, 'Edition of Example Work')
|
|
|
|
self.assertEqual(result.key, edition.remote_id)
|
|
|
|
self.assertEqual(result.author, 'Anonymous')
|
|
|
|
self.assertEqual(result.year, 1980)
|
|
|
|
self.assertEqual(result.connector, self.connector)
|
|
|
|
|
|
|
|
|
|
|
|
def test_search_rank(self):
|
|
|
|
''' prioritize certain results '''
|
2020-12-21 21:03:48 +00:00
|
|
|
author = models.Author.objects.create(name='Anonymous')
|
2021-01-02 22:20:39 +00:00
|
|
|
edition = models.Edition.objects.create(
|
2020-05-12 20:03:46 +00:00
|
|
|
title='Edition of Example Work',
|
2020-11-28 00:24:53 +00:00
|
|
|
published_date=datetime.datetime(1980, 5, 10, tzinfo=timezone.utc),
|
2021-01-03 00:09:54 +00:00
|
|
|
parent_work=models.Work.objects.create(title='')
|
2020-05-12 20:03:46 +00:00
|
|
|
)
|
2021-01-02 22:20:39 +00:00
|
|
|
# author text is rank C
|
|
|
|
edition.authors.add(author)
|
|
|
|
|
|
|
|
# series is rank D
|
2020-12-21 21:03:48 +00:00
|
|
|
models.Edition.objects.create(
|
2020-05-12 20:03:46 +00:00
|
|
|
title='Another Edition',
|
2021-01-03 00:09:54 +00:00
|
|
|
series='Anonymous',
|
|
|
|
parent_work=models.Work.objects.create(title='')
|
2020-05-12 20:03:46 +00:00
|
|
|
)
|
2021-01-02 22:20:39 +00:00
|
|
|
# subtitle is rank B
|
2020-12-21 21:03:48 +00:00
|
|
|
models.Edition.objects.create(
|
2020-05-12 20:03:46 +00:00
|
|
|
title='More Editions',
|
|
|
|
subtitle='The Anonymous Edition',
|
2021-01-03 00:09:54 +00:00
|
|
|
parent_work=models.Work.objects.create(title='')
|
2020-05-12 20:03:46 +00:00
|
|
|
)
|
2021-01-02 22:20:39 +00:00
|
|
|
# title is rank A
|
|
|
|
models.Edition.objects.create(title='Anonymous')
|
|
|
|
# doesn't rank in this search
|
2020-12-21 19:57:45 +00:00
|
|
|
edition = models.Edition.objects.create(
|
2020-05-12 20:03:46 +00:00
|
|
|
title='An Edition',
|
2021-01-03 00:09:54 +00:00
|
|
|
parent_work=models.Work.objects.create(title='')
|
2020-05-12 20:03:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
results = self.connector.search('Anonymous')
|
2021-01-02 22:20:39 +00:00
|
|
|
self.assertEqual(len(results), 3)
|
|
|
|
self.assertEqual(results[0].title, 'Anonymous')
|
|
|
|
self.assertEqual(results[1].title, 'More Editions')
|
|
|
|
self.assertEqual(results[2].title, 'Edition of Example Work')
|
2020-05-12 20:03:46 +00:00
|
|
|
|
|
|
|
|
2021-01-02 23:15:25 +00:00
|
|
|
def test_search_multiple_editions(self):
|
2020-11-08 01:48:50 +00:00
|
|
|
''' it should get rid of duplicate editions for the same work '''
|
2021-01-02 22:20:39 +00:00
|
|
|
work = models.Work.objects.create(title='Work Title')
|
2021-01-02 23:15:25 +00:00
|
|
|
edition_1 = models.Edition.objects.create(
|
2021-01-02 22:20:39 +00:00
|
|
|
title='Edition 1 Title', parent_work=work)
|
|
|
|
edition_2 = models.Edition.objects.create(
|
|
|
|
title='Edition 2 Title', parent_work=work)
|
|
|
|
edition_3 = models.Edition.objects.create(
|
|
|
|
title='Fish', parent_work=work)
|
|
|
|
work.default_edition = edition_2
|
|
|
|
work.save()
|
2020-11-08 01:48:50 +00:00
|
|
|
|
2021-01-02 23:15:25 +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)
|
|
|
|
|
|
|
|
# pick the default edition when no match is best
|
2021-01-02 22:20:39 +00:00
|
|
|
results = self.connector.search('Edition Title')
|
2020-05-12 20:03:46 +00:00
|
|
|
self.assertEqual(len(results), 1)
|
2021-01-02 22:20:39 +00:00
|
|
|
self.assertEqual(results[0].key, edition_2.remote_id)
|
2020-05-12 20:03:46 +00:00
|
|
|
|
2021-01-02 23:15:25 +00:00
|
|
|
# only matches one edition, so no deduplication takes place
|
2020-05-12 20:03:46 +00:00
|
|
|
results = self.connector.search('Fish')
|
|
|
|
self.assertEqual(len(results), 1)
|
2021-01-02 22:20:39 +00:00
|
|
|
self.assertEqual(results[0].key, edition_3.remote_id)
|