Skip to content

Commit 4b38860

Browse files
Practice testing
1 parent b817956 commit 4b38860

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

Library_management.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import pandas as pd
2-
import datetime
2+
3+
class BookNotAvailableError(Exception):
4+
pass
5+
36
class Book:
47
def __init__(self, isbn, title, author, publication_year):
58
self.isbn = isbn
@@ -19,14 +22,14 @@ def __init__(self):
1922
self.books_df = pd.DataFrame(columns=["ISBN", "Title", "Author", "Publication Year", "Is Borrowed"])
2023

2124
def add_book(self, title, author, publication_year):
22-
current_year = datetime.datetime.now().year
23-
if publication_year > current_year:
24-
raise ValueError("The publication year cannot be in the future.")
25-
25+
# Increment the ISBN counter by 1 for each new book
2626
Library_Management.isbn_counter += 1
2727
isbn = Library_Management.isbn_counter
28+
29+
# Create a new Book object
2830
book = Book(isbn, title, author, publication_year)
2931

32+
# Add the book details to the DataFrame using concat
3033
new_book_df = pd.DataFrame([{
3134
"ISBN": book.isbn,
3235
"Title": book.title,
@@ -35,6 +38,8 @@ def add_book(self, title, author, publication_year):
3538
"Is Borrowed": False
3639
}])
3740
self.books_df = pd.concat([self.books_df, new_book_df], ignore_index=True)
41+
print(f"Book '{book.title}' added to the library with ISBN '{book.isbn}'.")
42+
# print(self.books_df)
3843

3944
def borrow_book(self, book_title, isbn):
4045
# Check if the book is available for borrowing

Test_cases.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@ def test_add_books(self):
1010

1111
# Add a book to the library
1212
library.add_book("The Great Gatsby", "F. Scott Fitzgerald", 1925)
13-
14-
with self.assertRaises(ValueError) as context:
15-
library.add_book("The Great Gatsby", "F. Scott Fitzgerald", 2225)
16-
17-
# Verify that the correct error message is raised
18-
self.assertEqual(str(context.exception), "The publication year cannot be in the future.")
13+
1914
# Check if the book was added successfully
2015
# added_book = library.books_df.iloc[0]
2116
# self.assertEqual(added_book["Title"], "The Great Gatsby")
2217
# self.assertEqual(added_book["Author"], "F. Scott Fitzgerald")
2318
# self.assertEqual(added_book["Publication Year"], 1925)
24-
19+
20+
# library.add_book("To Kill a Mockingbird", "Harper Lee", 1960)
21+
# added_book = library.books_df.iloc[1]
22+
# self.assertEqual(added_book["Title"], "To Kill a Mockingbird")
23+
# self.assertEqual(added_book["Author"], "Harper Lee")
24+
# self.assertEqual(added_book["Publication Year"], 1960)
25+
2526
def test_borrow_book(self):
2627
"""
2728
Test that it can borrow a book from the library if it's available
@@ -90,7 +91,6 @@ def test_view_available_books(self):
9091
library.borrow_book("The Twilight Saga",2)
9192
library.return_book("The Great Gatsby",4)
9293
library.view_available_books()
93-
9494

9595
if __name__ == '__main__':
9696
unittest.main()
-61 Bytes
Binary file not shown.
-787 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)