116 lines
3.4 KiB
Python
116 lines
3.4 KiB
Python
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from books.models import Author, Book
|
|
from scrobbles.tasks import enrich_book_metadata
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@patch("books.models.lookup_book_from_ol")
|
|
def test_enrich_book_metadata_tags_success(mock_lookup):
|
|
mock_lookup.return_value = {
|
|
"title": "Test Book",
|
|
"openlibrary_id": "OL12345",
|
|
"summary": "A test summary",
|
|
"authors": ["Test Author"],
|
|
}
|
|
book = Book.objects.create(title="Test Book", pages=100)
|
|
|
|
enrich_book_metadata(book.id)
|
|
|
|
book.refresh_from_db()
|
|
assert book.openlibrary_id == "OL12345"
|
|
assert book.summary == "A test summary"
|
|
assert book.authors.filter(name="Test Author").exists()
|
|
assert "book-enriched" in book.tags.names()
|
|
assert "enrichment-failed" not in book.tags.names()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@patch("books.models.lookup_book_from_ol", return_value={})
|
|
def test_enrich_book_metadata_tags_failure_when_no_match(mock_lookup):
|
|
book = Book.objects.create(title="Unknown Book", pages=100)
|
|
|
|
enrich_book_metadata(book.id)
|
|
|
|
book.refresh_from_db()
|
|
assert "book-enriched" not in book.tags.names()
|
|
assert "enrichment-failed" in book.tags.names()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
@patch("books.models.lookup_book_from_ol", side_effect=Exception("boom"))
|
|
def test_enrich_book_metadata_tags_failure_on_exception(mock_lookup):
|
|
book = Book.objects.create(title="Test Book", pages=100)
|
|
|
|
enrich_book_metadata(book.id)
|
|
|
|
book.refresh_from_db()
|
|
assert "book-enriched" not in book.tags.names()
|
|
assert "enrichment-failed" in book.tags.names()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_enrich_book_metadata_skips_missing_book():
|
|
enrich_book_metadata(99999)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_enrich_book_metadata_preserves_koreader_title_on_mismatch():
|
|
from unittest.mock import Mock
|
|
|
|
with patch("books.models.lookup_book_from_ol") as mock_lookup:
|
|
mock_lookup.return_value = {
|
|
"title": "Completely Different Title",
|
|
"openlibrary_id": "OL999",
|
|
}
|
|
book = Book.objects.create(title="Test Book", pages=100)
|
|
|
|
enrich_book_metadata(book.id)
|
|
|
|
book.refresh_from_db()
|
|
assert book.title == "Test Book"
|
|
assert book.openlibrary_id == "OL999"
|
|
assert "book-enriched" in book.tags.names()
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_create_book_from_row_dispatches_enrichment():
|
|
from books.koreader import create_book_from_row
|
|
|
|
row = [
|
|
1,
|
|
"Test Book - Author Name",
|
|
"Test Author",
|
|
0,
|
|
0,
|
|
0,
|
|
300,
|
|
"",
|
|
"",
|
|
"abc123def456",
|
|
3600,
|
|
120,
|
|
]
|
|
with patch("scrobbles.tasks.enrich_book_metadata.delay") as mock_delay:
|
|
book = create_book_from_row(row)
|
|
mock_delay.assert_called_once_with(book.id)
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_fix_metadata_does_not_crash_on_locg_data_with_isbn():
|
|
from unittest.mock import Mock
|
|
|
|
with patch("books.models.lookup_book_from_ol", return_value={}):
|
|
with patch("books.models.lookup_comic_from_locg") as mock_locg:
|
|
mock_locg.return_value = {
|
|
"title": "Test Book",
|
|
"summary": "A comic summary",
|
|
"isbn": "1234567890",
|
|
}
|
|
book = Book.objects.create(title="Test Book", pages=100)
|
|
enriched = book.fix_metadata()
|
|
assert enriched is True
|
|
book.refresh_from_db()
|
|
assert book.summary == "A comic summary"
|