bookwyrm/bookwyrm/tests/models/test_book_model.py

173 lines
6.3 KiB
Python
Raw Normal View History

2021-03-08 16:49:10 +00:00
""" testing models """
import pathlib
import pytest
from dateutil.parser import parse
2020-05-10 00:10:02 +00:00
from django.test import TestCase
2020-12-17 20:30:49 +00:00
from django.utils import timezone
2020-05-10 00:10:02 +00:00
from bookwyrm import models, settings
from bookwyrm.models.book import isbn_10_to_13, isbn_13_to_10, normalize_isbn
from bookwyrm.settings import ENABLE_THUMBNAIL_GENERATION
2020-05-10 00:10:02 +00:00
2022-12-30 17:44:28 +00:00
2020-05-10 00:10:02 +00:00
class Book(TestCase):
2021-04-26 16:15:42 +00:00
"""not too much going on in the books model but here we are"""
2021-03-08 16:49:10 +00:00
@classmethod
def setUpTestData(cls):
2021-04-26 16:15:42 +00:00
"""we'll need some books"""
cls.work = models.Work.objects.create(
2021-08-02 23:05:40 +00:00
title="Example Work", remote_id="https://example.com/book/1"
)
cls.first_edition = models.Edition.objects.create(
title="Example Edition", parent_work=cls.work
2021-08-02 23:05:40 +00:00
)
cls.second_edition = models.Edition.objects.create(
2021-08-02 23:05:40 +00:00
title="Another Example Edition",
parent_work=cls.work,
2021-08-02 23:05:40 +00:00
)
2020-05-10 00:10:02 +00:00
def test_remote_id(self):
2021-04-26 16:15:42 +00:00
"""fanciness with remote/origin ids"""
remote_id = f"https://{settings.DOMAIN}/book/{self.work.id}"
2020-11-13 17:47:35 +00:00
self.assertEqual(self.work.get_remote_id(), remote_id)
2020-12-04 01:18:23 +00:00
self.assertEqual(self.work.remote_id, remote_id)
def test_generated_links(self):
"""links produced from identifiers"""
book = models.Edition.objects.create(
title="ExEd",
parent_work=self.work,
openlibrary_key="OL123M",
inventaire_id="isbn:123",
)
2021-12-10 21:39:41 +00:00
self.assertEqual(book.openlibrary_link, "https://openlibrary.org/books/OL123M")
self.assertEqual(book.inventaire_link, "https://inventaire.io/entity/isbn:123")
def test_create_book_invalid(self):
2021-04-26 16:15:42 +00:00
"""you shouldn't be able to create Books (only editions and works)"""
2021-03-08 16:49:10 +00:00
self.assertRaises(ValueError, models.Book.objects.create, title="Invalid Book")
2020-05-10 00:10:02 +00:00
2020-10-29 19:32:37 +00:00
def test_isbn_10_to_13(self):
2021-04-26 16:15:42 +00:00
"""checksums and so on"""
2021-03-08 16:49:10 +00:00
isbn_10 = "178816167X"
2020-10-29 19:32:37 +00:00
isbn_13 = isbn_10_to_13(isbn_10)
2021-03-08 16:49:10 +00:00
self.assertEqual(isbn_13, "9781788161671")
2020-10-29 19:32:37 +00:00
2021-03-08 16:49:10 +00:00
isbn_10 = "1-788-16167-X"
2020-10-30 19:43:02 +00:00
isbn_13 = isbn_10_to_13(isbn_10)
2021-03-08 16:49:10 +00:00
self.assertEqual(isbn_13, "9781788161671")
2020-10-30 19:43:02 +00:00
2020-10-29 19:32:37 +00:00
def test_isbn_13_to_10(self):
2021-04-26 16:15:42 +00:00
"""checksums and so on"""
2021-03-08 16:49:10 +00:00
isbn_13 = "9781788161671"
2020-10-29 19:32:37 +00:00
isbn_10 = isbn_13_to_10(isbn_13)
2021-03-08 16:49:10 +00:00
self.assertEqual(isbn_10, "178816167X")
2020-10-29 19:32:37 +00:00
2021-03-08 16:49:10 +00:00
isbn_13 = "978-1788-16167-1"
2020-10-30 19:43:02 +00:00
isbn_10 = isbn_13_to_10(isbn_13)
2021-03-08 16:49:10 +00:00
self.assertEqual(isbn_10, "178816167X")
2020-12-17 20:30:49 +00:00
def test_normalize_isbn(self):
"""Remove misc characters from ISBNs"""
self.assertEqual(normalize_isbn("978-0-4633461-1-2"), "9780463346112")
2020-12-17 20:30:49 +00:00
def test_get_edition_info(self):
2021-04-26 16:15:42 +00:00
"""text slug about an edition"""
2021-08-02 23:05:40 +00:00
book = models.Edition.objects.create(title="Test Edition")
2021-03-08 16:49:10 +00:00
self.assertEqual(book.edition_info, "")
2020-12-17 20:30:49 +00:00
2021-03-08 16:49:10 +00:00
book.physical_format = "worm"
2020-12-17 20:30:49 +00:00
book.save()
2021-03-08 16:49:10 +00:00
self.assertEqual(book.edition_info, "worm")
2020-12-17 20:30:49 +00:00
2021-03-08 16:49:10 +00:00
book.languages = ["English"]
2020-12-17 20:30:49 +00:00
book.save()
2021-03-08 16:49:10 +00:00
self.assertEqual(book.edition_info, "worm")
2020-12-17 20:30:49 +00:00
2021-03-08 16:49:10 +00:00
book.languages = ["Glorbish", "English"]
2020-12-17 20:30:49 +00:00
book.save()
2021-03-08 16:49:10 +00:00
self.assertEqual(book.edition_info, "worm, Glorbish language")
2020-12-17 20:30:49 +00:00
2021-03-08 16:49:10 +00:00
book.published_date = timezone.make_aware(parse("2020"))
2020-12-17 20:30:49 +00:00
book.save()
2021-03-08 16:49:10 +00:00
self.assertEqual(book.edition_info, "worm, Glorbish language, 2020")
2023-09-25 18:28:24 +00:00
def test_alt_text(self):
"""text slug used for cover images"""
book = models.Edition.objects.create(title="Test Edition")
author = models.Author.objects.create(name="Author Name")
self.assertEqual(book.alt_text, "Test Edition")
book.authors.set([author])
book.save()
self.assertEqual(book.alt_text, "Author Name: Test Edition")
book.physical_format = "worm"
book.published_date = timezone.make_aware(parse("2022"))
self.assertEqual(book.alt_text, "Author Name: Test Edition (worm, 2022)")
def test_get_rank(self):
2021-04-26 16:15:42 +00:00
"""sets the data quality index for the book"""
# basic rank
self.assertEqual(self.first_edition.edition_rank, 0)
2021-03-08 16:49:10 +00:00
self.first_edition.description = "hi"
self.first_edition.save()
self.assertEqual(self.first_edition.edition_rank, 1)
@pytest.mark.skipif(
not ENABLE_THUMBNAIL_GENERATION,
2022-12-30 17:44:28 +00:00
reason="Thumbnail generation disabled in settings",
)
def test_thumbnail_fields(self):
"""Just hit them"""
image_path = pathlib.Path(__file__).parent.joinpath(
"../../static/images/default_avi.jpg"
)
2022-07-07 18:37:34 +00:00
book = models.Edition.objects.create(title="hello")
with open(image_path, "rb") as image_file:
book.cover.save("test.jpg", image_file)
2022-07-07 18:37:34 +00:00
self.assertIsNotNone(book.cover_bw_book_xsmall_webp.url)
self.assertIsNotNone(book.cover_bw_book_xsmall_jpg.url)
self.assertIsNotNone(book.cover_bw_book_small_webp.url)
self.assertIsNotNone(book.cover_bw_book_small_jpg.url)
self.assertIsNotNone(book.cover_bw_book_medium_webp.url)
self.assertIsNotNone(book.cover_bw_book_medium_jpg.url)
self.assertIsNotNone(book.cover_bw_book_large_webp.url)
self.assertIsNotNone(book.cover_bw_book_large_jpg.url)
self.assertIsNotNone(book.cover_bw_book_xlarge_webp.url)
self.assertIsNotNone(book.cover_bw_book_xlarge_jpg.url)
self.assertIsNotNone(book.cover_bw_book_xxlarge_webp.url)
self.assertIsNotNone(book.cover_bw_book_xxlarge_jpg.url)
2023-04-25 12:17:23 +00:00
def test_populate_sort_title(self):
"""The sort title should remove the initial article on save"""
books = (
models.Edition.objects.create(
title=f"{article} Test Edition", languages=[langauge]
)
for langauge, articles in settings.LANGUAGE_ARTICLES.items()
2023-04-26 00:46:38 +00:00
for article in articles
2023-04-25 12:17:23 +00:00
)
2023-04-26 02:00:16 +00:00
self.assertTrue(all(book.sort_title == "test edition" for book in books))
2023-07-18 03:00:45 +00:00
def test_repair_edition(self):
"""Fix editions with no works"""
edition = models.Edition.objects.create(title="test")
edition.authors.set([models.Author.objects.create(name="Author Name")])
2023-07-18 03:00:45 +00:00
self.assertIsNone(edition.parent_work)
edition.repair()
edition.refresh_from_db()
self.assertEqual(edition.parent_work.title, "test")
self.assertEqual(edition.parent_work.authors.count(), 1)