Skip to content

Commit 03795b2

Browse files
Added logging, exception on tests
Co-Authored-By: Carrington <215159144+carrington-cs@users.noreply.github.com>
1 parent 0ee751d commit 03795b2

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

examples/with-unittest/library_management_system/main.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
import logging
2+
3+
logging.basicConfig(
4+
format='%(asctime)s - %(levelname)s - %(message)s',
5+
datefmt='%Y-%m-%d %H:%M:%S',
6+
level=logging.DEBUG,
7+
# add streamHandler or fileHandler if needed
8+
handlers = [
9+
logging.StreamHandler(),
10+
logging.FileHandler('logs/lms.log') # Uncomment to log to a file
11+
]
12+
)
13+
14+
logger = logging.getLogger(__name__)
15+
116
class Library:
217
"""
318
Docstring for Library
@@ -8,6 +23,20 @@ def __init__(self, name):
823
self.name = name
924
self.reading_units = {}
1025
self.librarians = {}
26+
self.cart = {}
27+
28+
def get_reading_materials(self):
29+
return self.reading_units
30+
31+
def get_librarians(self):
32+
return self.librarians
33+
34+
def book_reading_material_unit(self, reading_material: ReadingMaterialUnit):
35+
if reading_material.isbn not in self.cart:
36+
self.cart[reading_material.isbn] = True
37+
else:
38+
logger.info(f"{reading_material.isbn} is already reserved for this user")
39+
raise Exception(f"{reading_material.isbn} is already reserved for this user")
1140

1241
class ReadingMaterialUnit:
1342
"""

examples/with-unittest/library_management_system/tests/test_library.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_library_has_librarians(self):
3535
self.assertNotEqual(self.library.librarians, None)
3636
self.assertTrue(type(self.library.librarians), dict)
3737

38-
def test_library_booking_checkout(self, reading_material):
38+
def test_library_booking_checkout(self):
3939
"""
4040
Docstring for test_library_booking_checkout
4141
@@ -46,6 +46,20 @@ def test_library_booking_checkout(self, reading_material):
4646
self.library.book_reading_material_unit(reading_material)
4747
self.assertTrue(1, len(self.library.get_reading_materials()))
4848

49+
def test_library_booking_checkout_collision(self):
50+
"""
51+
Docstring for test_library_booking_checkout
52+
53+
:param self: Description
54+
:param reading_material: reading material
55+
"""
56+
reading_material = ReadingMaterialUnit("New Maths", "author", "isbn", "year_published")
57+
reading_material2 = ReadingMaterialUnit("New Maths", "author", "isbn", "year_published")
58+
self.library.book_reading_material_unit(reading_material)
59+
with self.assertRaises(Exception):
60+
self.library.book_reading_material_unit(reading_material2)
61+
# self.assertTrue(1, len(self.library.get_reading_materials()))
62+
4963
def test_library_booking_system_cancellelation(self):
5064
"""
5165
Docstring for test_library_has_librarians

0 commit comments

Comments
 (0)