def test_add_book_method_books_already_in_books_by_authors(self): self.library = Library("Alex") self.library.add_book("King", "It") self.library.add_book("King", "It") self.library.add_book("Jones", "It") self.assertEqual(self.library.books_by_authors["King"], ["It"]) self.assertEqual(self.library.books_by_authors["Jones"], ["It"])
def test_change_username_method_with_user_id_not_included_in_library_records_should_return_message( self): v = User(12, 'Valentina') p = User(13, 'Peter') library = Library() library.add_user(v) result = library.change_username(13, 'George') self.assertEqual(result, 'There is no user with id = 13!')
def test_add_user_already_registered_in_the_library_should_return_message( self): user = User(12, 'Valentina') library = Library() library.add_user(user) result = library.add_user(user) self.assertEqual( result, 'User with id = 12 already registered in the library!')
def test_method_rent_book_happy_case(self): self.library = Library("Alex") self.library.add_reader("Miro") self.library.add_book("King", "It") # book_index = self.library.books_by_authors["King"].index("It") self.library.rent_book("Miro", "King", "It") self.assertEqual(self.library.readers, {"Miro": [{"King": "It"}]}) self.assertEqual(self.library.books_by_authors, {"King": []})
def test_remove_user_method_with_user_not_registered_should_return_message( self): v = User(12, 'Valentina') p = User(13, 'Peter') library = Library() library.add_user(v) result = library.remove_user(p) self.assertEqual(result, 'We could not find such user to remove!')
def test_change_username_method_with_valid_data_should_return_message_and_update_library_records( self): v = User(12, 'Valentina') p = User(13, 'Peter') library = Library() library.add_user(v) result = library.change_username(12, 'Violeta') self.assertEqual( result, 'Username successfully changed to: Violeta for userid: 12') self.assertEqual(library.user_records[0].__str__(), '12, Violeta, []')
def test_change_username_method_with_user_id_included_in_library_records_but_provided_new_username_is_the_same_should_return_message( self): v = User(12, 'Valentina') p = User(13, 'Peter') library = Library() library.add_user(v) result = library.change_username(12, 'Valentina') self.assertEqual( result, 'Please check again the provided username - it should be different than the username used so far!' )
def test_remove_user_method_with_valid_data_should_update_library_records_properly( self): user = User(12, 'Valentina') library = Library() library.add_user(user) library.add_user(User(13, 'Peter')) library.remove_user(user) self.assertEqual(library.user_records[0].__str__(), '13, Peter, []')
def get_book(self, author: str, book_name: str, days_to_return: int, library: Library) -> Union[str, None]: # TODO: check if book is not in library if book_name in library.books_available[author]: library.books_available[author].remove(book_name) if self.username not in library.rented_books: library.rented_books[self.username] = {} library.rented_books[self.username][book_name] = days_to_return library.book_return_days[book_name] = days_to_return self.books.append(book_name) return f"{book_name} successfully rented for the next {days_to_return} days!" searched_days = library.book_return_days[book_name] return f'The book "{book_name}" is already rented and will be available in {searched_days} days!'
class TestLibrary(unittest.TestCase): def test__init__(self): self.library = Library("Alex") self.assertEqual(self.library.name, "Alex") self.assertEqual(self.library.books_by_authors, {}) self.assertEqual(self.library.readers, {}) def test__init__name_empty_string(self): with self.assertRaises(ValueError) as err: self.library = Library("") msg = "Name cannot be empty string!" self.assertEqual(str(err.exception), msg) def test_method_add_book_happy_case(self): self.library = Library("Alex") self.library.add_book("King", "It") self.assertEqual(self.library.books_by_authors["King"], ["It"]) def test_method_add_book_author_already_in_books_by_author(self): self.library = Library("Alex") self.library.add_book("King", "It") self.library.add_book("King", "Shining") self.assertEqual(self.library.books_by_authors["King"], ["It", "Shining"]) def test_add_book_method_books_already_in_books_by_authors(self): self.library = Library("Alex") self.library.add_book("King", "It") self.library.add_book("King", "It") self.library.add_book("Jones", "It") self.assertEqual(self.library.books_by_authors["King"], ["It"]) self.assertEqual(self.library.books_by_authors["Jones"], ["It"]) def test_method_add_reader_happy_case(self): self.library = Library("Alex") self.library.add_reader("Miro") self.assertEqual(self.library.readers, {"Miro": []}) def test_method_add_reader_username_already_registered(self): self.library = Library("Alex") self.library.add_reader("Miro") self.assertEqual(self.library.add_reader("Miro"), "Miro is already registered in the Alex library.") def test_method_rent_book_happy_case(self): self.library = Library("Alex") self.library.add_reader("Miro") self.library.add_book("King", "It") # book_index = self.library.books_by_authors["King"].index("It") self.library.rent_book("Miro", "King", "It") self.assertEqual(self.library.readers, {"Miro": [{"King": "It"}]}) self.assertEqual(self.library.books_by_authors, {"King": []}) def test_method_rent_book_reader_not_in_readers(self): self.library = Library("Alex") self.assertEqual(self.library.rent_book("Miro", "King", "It"), "Miro is not registered in the Alex Library.") def test_method_rent_book_author_not_in_books_by_author(self): self.library = Library("Alex") self.library.add_reader("Miro") self.assertEqual(self.library.rent_book("Miro", "King", "It"), "Alex Library does not have any King's books.") def test_method_rent_book_book_title_not_in_books_by_author(self): self.library = Library("Alex") self.library.add_reader("Miro") self.library.add_book("King", "It") self.assertEqual(self.library.rent_book("Miro", "King", "Shining"), """Alex Library does not have King's "Shining".""")
def test_method_add_reader_username_already_registered(self): self.library = Library("Alex") self.library.add_reader("Miro") self.assertEqual(self.library.add_reader("Miro"), "Miro is already registered in the Alex library.")
def test_method_add_reader_happy_case(self): self.library = Library("Alex") self.library.add_reader("Miro") self.assertEqual(self.library.readers, {"Miro": []})
class LibraryTest(TestCase): def setUp(self) -> None: self.library = Library('AI') def test_initialization(self): self.assertEqual('AI', self.library.name) self.assertEqual({}, self.library.books_by_authors) self.assertEqual({}, self.library.readers) def test_name_property(self): self.library.name = 'Are' self.assertEqual('Are', self.library.name) def test_name_property_raise(self): expected = "Name cannot be empty string!" with self.assertRaises(ValueError) as ex: self.library.name = '' self.assertEqual(expected, str(ex.exception)) def test_add_book(self): self.assertEqual({}, self.library.books_by_authors) self.library.add_book('AZ', 'Kniga') self.assertEqual({'AZ': ['Kniga']}, self.library.books_by_authors) def test_add_reader(self): self.assertEqual({}, self.library.readers) self.library.add_reader('Pesho') self.assertEqual({'Pesho': []}, self.library.readers) def test_add_reader_msg(self): self.assertEqual({}, self.library.readers) self.library.add_reader('Pesho') self.assertEqual({'Pesho': []}, self.library.readers) expected = "Pesho is already registered in the AI library." result = self.library.add_reader('Pesho') self.assertEqual(expected, result) def test_rent_book_reader_not_there(self): expected = "Pesho is not registered in the AI Library." result = self.library.rent_book('Pesho', 'AZ', 'Kniga') self.assertEqual(expected, result) def test_rent_book_author_not_there(self): self.library.add_reader('Pesho') expected = "AI Library does not have any AZ's books." result = self.library.rent_book('Pesho', 'AZ', 'Kniga') self.assertEqual(expected, result) def test_rent_book_book_not_there(self): self.library.add_reader('Pesho') self.library.add_book('AZ', 'Kniga') expected = """AI Library does not have AZ's "Kniga2".""" result = self.library.rent_book('Pesho', 'AZ', 'Kniga2') self.assertEqual(expected, result) def test_rent_book_working(self): self.library.add_reader('Pesho') self.library.add_book('AZ', 'Kniga') self.assertEqual({'Pesho': []}, self.library.readers) self.assertEqual({'AZ': ['Kniga']}, self.library.books_by_authors) self.library.rent_book('Pesho', 'AZ', 'Kniga') self.assertEqual({'Pesho': [{'AZ': 'Kniga'}]}, self.library.readers) def test_rent_book_removing_book(self): self.library.add_reader('Pesho') self.library.add_book('AZ', 'Kniga') self.assertEqual({'Pesho': []}, self.library.readers) self.assertEqual({'AZ': ['Kniga']}, self.library.books_by_authors) self.library.rent_book('Pesho', 'AZ', 'Kniga') self.assertEqual({'Pesho': [{'AZ': 'Kniga'}]}, self.library.readers) self.assertEqual({'AZ': []}, self.library.books_by_authors)
def setUp(self): self.lib = Library("Test")
def test_method_rent_book_book_title_not_in_books_by_author(self): self.library = Library("Alex") self.library.add_reader("Miro") self.library.add_book("King", "It") self.assertEqual(self.library.rent_book("Miro", "King", "Shining"), """Alex Library does not have King's "Shining".""")
def test__init__(self): self.library = Library("Alex") self.assertEqual(self.library.name, "Alex") self.assertEqual(self.library.books_by_authors, {}) self.assertEqual(self.library.readers, {})
def setUp(self) -> None: self.library = Library('AI')
def test_method_rent_book_reader_not_in_readers(self): self.library = Library("Alex") self.assertEqual(self.library.rent_book("Miro", "King", "It"), "Miro is not registered in the Alex Library.")
def setUp(self): self.user = User(12, 'Valentina') self.library = Library()
def test_method_rent_book_author_not_in_books_by_author(self): self.library = Library("Alex") self.library.add_reader("Miro") self.assertEqual(self.library.rent_book("Miro", "King", "It"), "Alex Library does not have any King's books.")
def test__init__name_empty_string(self): with self.assertRaises(ValueError) as err: self.library = Library("") msg = "Name cannot be empty string!" self.assertEqual(str(err.exception), msg)
class TestLibrary(TestCase): def setUp(self): self.library = Library("Test") def test_initializing_all_attributes(self): self.assertEqual("Test", self.library.name) self.assertEqual({}, self.library.books_by_authors) self.assertEqual({}, self.library.readers) with self.assertRaises(ValueError) as ex: self.library.name = "" self.assertEqual("Name cannot be empty string!", str(ex.exception)) def test_add_book(self): self.assertDictEqual({}, self.library.books_by_authors) self.library.add_book("Ivan", "TestTitle") self.assertDictEqual({"Ivan": ["TestTitle"]}, self.library.books_by_authors) self.library.add_book("Ivan", "TestTitle2") self.assertDictEqual({"Ivan": ["TestTitle", "TestTitle2"]}, self.library.books_by_authors) def test_add_reader(self): self.assertDictEqual({}, self.library.readers) self.library.add_reader("Ivan") self.assertDictEqual({"Ivan": []}, self.library.readers) result = self.library.add_reader("Ivan") self.assertEqual("Ivan is already registered in the Test library.", result) def test_rent_book_reader_not_exist(self): self.library.add_book("Ivan", "TestTitle") result = self.library.rent_book("TestReader", "Ivan", "TestTitle") self.assertEqual("TestReader is not registered in the Test Library.", result) def test_rent_book_author_not_exist(self): self.library.add_reader("TestReader") result = self.library.rent_book("TestReader", "Ivan", "TestTitle") self.assertEqual("Test Library does not have any Ivan's books.", result) def test_rent_book_title_not_exist(self): self.library.add_book("Ivan", "TestTitle") self.library.add_reader("TestReader") result = self.library.rent_book("TestReader", "Ivan", "TitleNotExist") self.assertEqual(f"""Test Library does not have Ivan's "TitleNotExist".""", result) def test_rent_book(self): self.library.add_book("Ivan", "TestTitle") self.library.add_reader("TestReader") self.library.rent_book("TestReader", "Ivan", "TestTitle") self.assertDictEqual({"TestReader": [{"Ivan": "TestTitle"}]}, self.library.readers) self.assertDictEqual({"Ivan": []}, self.library.books_by_authors)
def test_method_add_book_happy_case(self): self.library = Library("Alex") self.library.add_book("King", "It") self.assertEqual(self.library.books_by_authors["King"], ["It"])
class TestLibrary(TestCase): def setUp(self): self.lib = Library("Test") def test_init(self): self.assertEqual("Test", self.lib.name) self.assertEqual({}, self.lib.books_by_authors) self.assertEqual({}, self.lib.readers) def test_add_invalid_name(self): with self.assertRaises(ValueError) as ex: self.lib.name("") self.assertEqual("Name cannot be empty string!", str(ex.exception)) def test_add_book_to_dict(self): self.assertEqual({}, self.books_by_authors) self.lib.add_book("Stephen King", ["It"]) self.assertEqual({"Stephen King": ["It"]}, self.lib.books_by_authors) self.lib.add_book("Stephen King", ["The Green Mile"]) self.assertEqual({"Stephen King": ["It", "The Green Mile"]}, self.lib.books_by_authors) def test_add_reader(self): self.assertEqual({}, self.readers) self.lib.add_reader("Vako") self.assertEqual({"Vako": []}, self.lib.readers) def test_add_reader_already_registered(self): self.assertEqual({}, self.readers) self.lib.add_reader("Vako") self.assertEqual({"Vako": []}, self.lib.readers) result = self.lib.add_reader("Vako") self.assertEqual("Vako is already registered in the 'Test' library.", result) def test_rent_a_book_reader_not_in_library(self): self.assertEqual({}, self.readers) result = self.lib.rent_book("Test_reader_name", "test_book_author", "test_book_title") self.assertEqual("Test_reader_name is not registered in the Test Library.", result) def test_rent_a_book_author_not_in_library(self): self.assertEqual({}, self.books_by_authors) result = self.lib.rent_book("Test_reader_name", "test_book_author", "test_book_title") self.assertEqual("Test Library does not have any test_book_author's books.", result) def test_rent_a_book_title_not_in_library(self): self.assertEqual({"Vako": []}, self.readers) self.assertEqual({"Stephen King": ["It"]}, self.books_by_authors) result = self.lib.rent_book("Vako", "Stephen King", "The Green Mile") self.assertEqual("""Test Library does not have Stephen King's "The Green Mile".""", result) def test_rent_a_book_successes(self): self.assertEqual({"Vako": []}, self.readers) self.assertEqual({"Stephen King": ["It"]}, self.books_by_authors) self.lib.rent_book("Vako", "Stephen King", "It") self.assertEqual({"Vako": [{"Stephen King": "It"}]}, self.readers) self.assertEqual({}, self.books_by_authors)
def test_method_add_book_author_already_in_books_by_author(self): self.library = Library("Alex") self.library.add_book("King", "It") self.library.add_book("King", "Shining") self.assertEqual(self.library.books_by_authors["King"], ["It", "Shining"])
from project.library import Library from project.user import User user = User(12, 'Peter') library = Library() library.add_user(user) print(library.add_user(user)) library.remove_user(user) print(library.remove_user(user)) library.add_user(user) print(library.change_username(2, 'Igor')) print(library.change_username(12, 'Peter')) print(library.change_username(12, 'George')) [ print( f'{user_record.user_id}, {user_record.username}, {user_record.books}') for user_record in library.user_records ] library.books_available.update({ 'J.K.Rowling': [ 'The Chamber of Secrets', 'The Prisoner of Azkaban', 'The Goblet of Fire', 'The Order of the Phoenix', 'The Half-Blood Prince', 'The Deathly Hallows' ] }) user.get_book('J.K.Rowling', 'The Deathly Hallows', 17, library) print(library.books_available) print(library.rented_books) print(user.books)
from project.library import Library from project.user import User # TODO: Fix if needed- Users not added and removed from the library can access it user = User(12, 'Peter') user2 = User(13, 'Simo') user3 = User(14, 'Boris') library = Library() library.books_available.update({ 'J.K.Rowling': [ 'The Chamber of Secrets', 'The Prisoner of Azkaban', 'The Goblet of Fire', 'The Order of the Phoenix', 'The Half-Blood Prince', 'The Deathly Hallows' ] }) library.add_user(user) print(library.remove_user(user)) print(library.remove_user(user)) # print(library.change_username(12, "Asen")) # print(user.get_book("J.K.Rowling", 'The Chamber of Secrets', 10, library)) # print(user.get_book("J.K.Rowling", 'The Deathly Hallows', 12, library)) # print(user2.get_book("J.K.Rowling", 'The Order of the Phoenix', 14, library)) # print(library.rented_books) print(user.info())