moviewyrm/bookwyrm/tests/models/test_book_model.py

101 lines
3.3 KiB
Python
Raw Normal View History

2020-05-10 00:10:02 +00:00
''' testing models '''
2020-12-17 20:30:49 +00:00
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
2020-10-29 19:32:37 +00:00
from bookwyrm.models.book import isbn_10_to_13, isbn_13_to_10
2020-05-10 00:10:02 +00:00
class Book(TestCase):
''' not too much going on in the books model but here we are '''
def setUp(self):
2020-12-14 18:18:14 +00:00
''' we'll need some books '''
2020-05-14 18:28:45 +00:00
self.work = models.Work.objects.create(
title='Example Work',
remote_id='https://example.com/book/1'
)
self.first_edition = models.Edition.objects.create(
title='Example Edition',
parent_work=self.work,
)
self.second_edition = models.Edition.objects.create(
title='Another Example Edition',
parent_work=self.work,
)
2020-05-10 00:10:02 +00:00
def test_remote_id(self):
2020-12-14 18:18:14 +00:00
''' fanciness with remote/origin ids '''
2020-11-13 17:47:35 +00:00
remote_id = 'https://%s/book/%d' % (settings.DOMAIN, self.work.id)
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)
2020-05-10 00:10:02 +00:00
def test_create_book(self):
''' you shouldn't be able to create Books (only editions and works) '''
self.assertRaises(
ValueError,
models.Book.objects.create,
title='Invalid Book'
)
2020-10-29 19:32:37 +00:00
def test_isbn_10_to_13(self):
2020-11-08 01:48:50 +00:00
''' checksums and so on '''
2020-10-29 19:32:37 +00:00
isbn_10 = '178816167X'
isbn_13 = isbn_10_to_13(isbn_10)
self.assertEqual(isbn_13, '9781788161671')
2020-10-30 19:43:02 +00:00
isbn_10 = '1-788-16167-X'
isbn_13 = isbn_10_to_13(isbn_10)
self.assertEqual(isbn_13, '9781788161671')
2020-10-29 19:32:37 +00:00
def test_isbn_13_to_10(self):
2020-11-08 01:48:50 +00:00
''' checksums and so on '''
2020-10-29 19:32:37 +00:00
isbn_13 = '9781788161671'
isbn_10 = isbn_13_to_10(isbn_13)
self.assertEqual(isbn_10, '178816167X')
2020-10-30 19:43:02 +00:00
isbn_13 = '978-1788-16167-1'
isbn_10 = isbn_13_to_10(isbn_13)
self.assertEqual(isbn_10, '178816167X')
2020-12-17 20:30:49 +00:00
def test_get_edition_info(self):
''' text slug about an edition '''
2020-12-17 21:01:08 +00:00
book = models.Edition.objects.create(title='Test Edition')
2020-12-17 20:30:49 +00:00
self.assertEqual(book.edition_info, '')
book.physical_format = 'worm'
book.save()
self.assertEqual(book.edition_info, 'worm')
book.languages = ['English']
book.save()
self.assertEqual(book.edition_info, 'worm')
book.languages = ['Glorbish', 'English']
book.save()
self.assertEqual(book.edition_info, 'worm, Glorbish language')
book.published_date = timezone.make_aware(parse('2020'))
book.save()
self.assertEqual(book.edition_info, 'worm, Glorbish language, 2020')
self.assertEqual(
book.alt_text, 'Test Edition cover (worm, Glorbish language, 2020)')
def test_get_rank(self):
''' sets the data quality index for the book '''
# basic rank
self.assertEqual(self.first_edition.edition_rank, 0)
self.first_edition.description = 'hi'
self.first_edition.save()
self.assertEqual(self.first_edition.edition_rank, 1)
# default edition
self.work.default_edition = self.first_edition
self.work.save()
self.first_edition.refresh_from_db()
self.assertEqual(self.first_edition.edition_rank, 20)