diff --git a/openlibrary/plugins/openlibrary/home.py b/openlibrary/plugins/openlibrary/home.py index 04507c76a7a..1bd833adbec 100644 --- a/openlibrary/plugins/openlibrary/home.py +++ b/openlibrary/plugins/openlibrary/home.py @@ -171,7 +171,8 @@ def get_ia_carousel_books(query=None, subject=None, sorts=None, limit=None, safe query=query, safe_mode=safe_mode, ) - formatted_books = [format_book_data(book, False) for book in books if book != "error"] + formatted_books = [format_book_data(book, False) for book in books if book and book != "error"] + return formatted_books diff --git a/openlibrary/plugins/openlibrary/tests/test_home.py b/openlibrary/plugins/openlibrary/tests/test_home.py index bb55a6727cd..1928e38ca9e 100644 --- a/openlibrary/plugins/openlibrary/tests/test_home.py +++ b/openlibrary/plugins/openlibrary/tests/test_home.py @@ -137,6 +137,15 @@ def spoofed_generic_carousel(*args, **kwargs): assert "About the Project" in html +class Test_get_ia_carousel_books: + def test_skips_missing_books(self, monkeypatch): + book = web.storage(key="/books/OL1M") + monkeypatch.setattr(home.lending, "get_available", lambda **kwargs: [book, None, "error"]) + monkeypatch.setattr(home, "format_book_data", lambda book, fetch_availability: {"key": book.key}) + + assert home.get_ia_carousel_books() == [{"key": "/books/OL1M"}] + + class Test_format_book_data: def test_all(self, mock_site, mock_ia): mock_site.quicksave("/books/OL1M", "/type/edition", title="Foo")