Skip to content

Commit b7e5b8a

Browse files
committed
feat: add list unread books command
- Add get_unread_books() method to BookCollection in books.py - Add 'unread' command handler in book_app.py - Update help text with new unread command - Add 4 test cases covering empty, all unread, some read, and all read scenarios
1 parent b139530 commit b7e5b8a

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

samples/book-app-project/book_app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def handle_remove():
5050
print("\nBook removed if it existed.\n")
5151

5252

53+
def handle_list_unread():
54+
books = collection.get_unread_books()
55+
show_books(books)
56+
57+
5358
def handle_find():
5459
print("\nFind Books by Author\n")
5560

@@ -65,6 +70,7 @@ def show_help():
6570
6671
Commands:
6772
list - Show all books
73+
unread - Show unread books
6874
add - Add a new book
6975
remove - Remove a book by title
7076
find - Find books by author
@@ -79,6 +85,7 @@ def main():
7985

8086
commands = {
8187
"list": handle_list,
88+
"unread": handle_list_unread,
8289
"add": handle_add,
8390
"remove": handle_remove,
8491
"find": handle_find,

samples/book-app-project/books.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ def add_book(self, title: str, author: str, year: int) -> Book:
4444
def list_books(self) -> List[Book]:
4545
return self.books
4646

47+
def get_unread_books(self) -> List[Book]:
48+
"""Return all books that haven't been read yet."""
49+
return [b for b in self.books if not b.read]
50+
4751
def find_book_by_title(self, title: str) -> Optional[Book]:
4852
for book in self.books:
4953
if book.title.lower() == title.lower():

samples/book-app-project/tests/test_books.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,37 @@ def test_remove_book_invalid():
5151
collection = BookCollection()
5252
result = collection.remove_book("Nonexistent Book")
5353
assert result is False
54+
55+
56+
def test_get_unread_books_empty_collection():
57+
collection = BookCollection()
58+
unread = collection.get_unread_books()
59+
assert unread == []
60+
61+
62+
def test_get_unread_books_all_unread():
63+
collection = BookCollection()
64+
collection.add_book("1984", "George Orwell", 1949)
65+
collection.add_book("Dune", "Frank Herbert", 1965)
66+
unread = collection.get_unread_books()
67+
assert len(unread) == 2
68+
69+
70+
def test_get_unread_books_some_read():
71+
collection = BookCollection()
72+
collection.add_book("1984", "George Orwell", 1949)
73+
collection.add_book("Dune", "Frank Herbert", 1965)
74+
collection.mark_as_read("Dune")
75+
unread = collection.get_unread_books()
76+
assert len(unread) == 1
77+
assert unread[0].title == "1984"
78+
79+
80+
def test_get_unread_books_all_read():
81+
collection = BookCollection()
82+
collection.add_book("1984", "George Orwell", 1949)
83+
collection.add_book("Dune", "Frank Herbert", 1965)
84+
collection.mark_as_read("1984")
85+
collection.mark_as_read("Dune")
86+
unread = collection.get_unread_books()
87+
assert len(unread) == 0

0 commit comments

Comments
 (0)