Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Python CI

on:
push:
branches: [ "**" ]
pull_request:
branches: [ "main" ]

jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.9]

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

- name: Run tests with pytest
run: |
pytest
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/LibrarySystem1.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added __pycache__/book.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/library.cpython-311.pyc
Binary file not shown.
14 changes: 14 additions & 0 deletions book.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
self.available = True

def borrow(self):
if not self.available:
return False
self.available = False
return True

def return_book(self):
self.available = True
27 changes: 27 additions & 0 deletions library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from book import Book

class Library:
def __init__(self):
self.books = []

def add_book(self, title, author):
self.books.append(Book(title, author))

def is_available(self, title):
for book in self.books:
if book.title == title:
return book.available
return False

def borrow_book(self, title):
for book in self.books:
if book.title == title:
return book.borrow()
return False

def return_book(self, title):
for book in self.books:
if book.title == title:
book.return_book()
return True
return False
49 changes: 49 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@


from library import Library

def main():
lib = Library()

print("\n📚 Welcome to the Library System")

# Add books to the library
lib.add_book("1984", "George Orwell")
lib.add_book("The Hobbit", "J.R.R. Tolkien")

print("\n📖 Books Added:")
for book in lib.books:
print(f"- {book.title} by {book.author} (Available: {book.available})")

# Check availability
print(f"\n🔍 Is '1984' available? {lib.is_available('1984')}")

# Borrow a book
print("\n📥 Borrowing '1984'...")
success = lib.borrow_book("1984")
print(f"Borrowed: {success}")
print(f"Available now? {lib.is_available('1984')}")

# Try borrowing it again
print("\n📥 Trying to borrow '1984' again...")
success = lib.borrow_book("1984")
print(f"Borrowed: {success}")

# Return the book
print("\n📤 Returning '1984'...")
returned = lib.return_book("1984")
print(f"Returned: {returned}")
print(f"Available now? {lib.is_available('1984')}")

# Try returning again (already available)
print("\n📤 Returning '1984' again...")
returned = lib.return_book("1984")
print(f"Returned: {returned}")

# Borrow a non-existent book
print("\n📥 Borrowing 'Unknown Book'...")
success = lib.borrow_book("Unknown Book")
print(f"Borrowed: {success}")

if __name__ == "__main__":
main()

3 changes: 3 additions & 0 deletions requirments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pytest
requests
flask
File renamed without changes.
Binary file added tests/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
30 changes: 30 additions & 0 deletions tests/test_book.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from book import Book

def test_initialization():
"""Test 1: Initialization — title, author, and availability set correctly"""
book = Book("Clean Code", "Robert Cecil Martin")
assert book.title == "Clean Code"
assert book.author == "Robert Cecil Martin"
assert book.available is True

def test_borrow_when_available():
"""Test 2: Borrow when available — borrow() returns True, sets available to False"""
book = Book("Clean Code", "Robert Cecil Martin")
result = book.borrow()
assert result is True
assert book.available is False

def test_borrow_when_not_available():
"""Test 3: Borrow when not available — borrow() returns False, available remains False"""
book = Book("Clean Code", "Robert Cecil Martin")
book.borrow() # First borrow
result = book.borrow() # Try borrowing again
assert result is False
assert book.available is False

def test_return_book():
"""Test 4: Return book — return_book() sets available to True"""
book = Book("Clean Code", "Robert Cecil Martin")
book.borrow()
book.return_book()
assert book.available is True
87 changes: 87 additions & 0 deletions tests/test_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from library import Library

# 1. Library Initialization
def test_library_initialization():
"""
Test: Library should initialize with an empty book list.
"""
lib = Library()
assert isinstance(lib.books, list)
assert len(lib.books) == 0

# 2. Add book
def test_add_book():
"""
Test: Add book to the library.
- Book is added to the list with correct details.
- Book is initially available.
"""
lib = Library()
lib.add_book("1984", "George Orwell")
assert len(lib.books) == 1
book = lib.books[0]
assert book.title == "1984"
assert book.author == "George Orwell"
assert book.available is True

# 3. Check availability - exists
def test_is_available_true():
lib = Library()
lib.add_book("Dune", "Frank Herbert")
assert lib.is_available("Dune") is True

# 4. Check availability - doesn’t exist
def test_is_available_false_for_nonexistent_book():
lib = Library()
assert lib.is_available("Nonexistent") is False

# 5. Borrow book - available
def test_borrow_book_available():
lib = Library()
lib.add_book("Clean Code", "Robert Martin")
result = lib.borrow_book("Clean Code")
assert result is True
assert lib.books[0].available is False

# 6. Borrow book - already borrowed
def test_borrow_book_already_borrowed():
lib = Library()
lib.add_book("Clean Code", "Robert Martin")
lib.borrow_book("Clean Code")
result = lib.borrow_book("Clean Code")
assert result is False
assert lib.books[0].available is False

# 7. Borrow book - not in library
def test_borrow_book_not_in_library():
lib = Library()
result = lib.borrow_book("Unknown Book")
assert result is False

# 8. Return book - valid
def test_return_book_valid():
lib = Library()
lib.add_book("Refactoring", "Martin Fowler")
lib.borrow_book("Refactoring")
result = lib.return_book("Refactoring")
assert result is True
assert lib.books[0].available is True

# 9. Return book - doesn’t exist
def test_return_book_not_in_library():
lib = Library()
result = lib.return_book("Nonexistent")
assert result is False

# 10. Return book - already available
def test_return_book_already_available():
"""
Returning a book that's already available:
- Should return True.
- Should not break logic.
"""
lib = Library()
lib.add_book("Domain-Driven Design", "Eric Evans")
result = lib.return_book("Domain-Driven Design")
assert result is True
assert lib.books[0].available is True
Loading