Ejemplo n.º 1
0
class BookRepositoryTest(unittest.TestCase):
    def setUp(self):
        self.book_repository = BookRepository()
        book = Book(1, "Title1", "Author1")
        self.book_repository.add_book(book)
        book = Book(2, "Title2", "Author2")
        self.book_repository.add_book(book)

    def test_book_repository_add_book(self):
        book = Book(3, "Title3", "Author3")
        self.book_repository.add_book(book)
        books_list = self.book_repository.get_all_books()
        book = books_list[-1]
        assert (book.id == 3)
        assert (book.title == "Title3")
        assert (book.author == "Author3")

    def test_book_repository_remove_book(self):
        books_list = self.book_repository.get_all_books()
        book = books_list[-1]
        assert (book.id == 2)
        self.book_repository.remove_book_by_index(1)
        book = books_list[-1]
        assert (book.id == 1)

    def test_book_repository_update_book(self):
        books_list = self.book_repository.get_all_books()
        book = books_list[-1]
        assert (book.id == 2)
        new_book = Book(10, "New Title", "New Author")
        self.book_repository.update_book(1, new_book)
        book = books_list[-1]
        assert (book.id == 10)
        assert (book.title == "New Title")
        assert (book.author == "New Author")

    def test_book_repository_get_next_book_id(self):
        assert (self.book_repository.get_next_book_id() == 1)
        self.book_repository.increment_last_book_id()
        assert (self.book_repository.get_next_book_id() == 3)

    def test_book_repository_get_all_books(self):
        books_list = self.book_repository.get_all_books()
        assert len(books_list) == 2

    def tearDown(self):
        del self.book_repository
Ejemplo n.º 2
0
 def remove_book_by_index(self, index):
     self.__read_all_from_file()
     BookRepository.remove_book_by_index(self, index)
     self.__write_all_to_file()
 def remove_book_by_index(self, index):
     self.__read_all_from_database()
     book = self.get_all_books()[index]
     BookRepository.remove_book_by_index(self, index)
     self.__remove_from_database(book.id)