def test_get_known_words(self): words_list_comparator = ContainerComparator( elem_equality_comparator=lambda lhs, rhs: lhs == rhs, sort_key=lambda w: w) database = Database(self.db_handle) self.assertEqual(len(database.get_known_words()), 0) book1 = Book.from_path(f"{base_dir}/test/data/book.epub") for word in book1.words: word.mark_if_known(True) book2 = Book.from_path(f"{base_dir}/test/data/book2.epub") for word in book2.words: word.mark_if_known(True) database.store_book(book1) database.store_book(book2) book1_raw_known_words = set(w.stored_word for w in book1.known_words) book2_raw_known_words = set(w.stored_word for w in book2.known_words) both_books_words = book1_raw_known_words.union(book2_raw_known_words) self.assertTrue( words_list_comparator(database.get_known_words(), both_books_words))
def setUp(self): self.transition = [[0, 2, 3], [3, 0, 4], [3, 4, 0]] self.books = [ Book(0, 1, 5), Book(1, 0, 3), Book(2, 1, 10), Book(3, 1, 2), Book(4, 2, 8), ] self.readers = [ Reader({ 0: 3, 1: 2 }, 0, 10), Reader({ 1: 1, 2: 10, 3: 3 }, 0, 10), Reader({ 0: 1, 1: 1, 2: 1, 3: 1, 4: 1 }, 1, 10), Reader({ 2: 5, 4: 5 }, 2, 10), ] self.state = State(self.transition, self.books, self.readers)
def test_exercise(): book = Book("J.R.R. Tolkein", "Lord of the Rings", "1000") assert book.get_author() == "J.R.R. Tolkein" assert book.get_name() == "Lord of the Rings" assert book.get_pages() == "1000" assert str(book) == "J.R.R. Tolkein, Lord of the Rings, 1000 pages"
def check_for_books(self): '''check the current hand for any possible books''' #a dictionary of books possible_books = dict() #while the player still has cards while len(self.hand) > 0: #remove one from the top card = self.hand.pop() try: #check if the card's value already has a book #in the dict book = possible_books[card.value] #if that worked, we can add the card book.add(card) except KeyError: #if the dict didn't have a matching index #create a new book new_book = Book() #add the card to it new_book.add(card) #set the new book to the card's value in the dict possible_books[card.value] = new_book #create a list of the completed books complete_books = filter(lambda book: book.is_complete(),\ list(possible_books.values())) #add any new books to the player's books self.books.extend(complete_books) #create a lit of the incomplete books incomplete_books = filter(lambda book: not book.is_complete(), \ list(possible_books.values())) #add each of the book's cards back to the player's hand for book in incomplete_books: self.hand.extend(book.cards) #sort the player's hand smallest to largest self.hand.sort(key=lambda card: card.value)
def setUp(self): self.book_data = {0: 5, 1: 2, 3: 3} self.reader = Reader(books=self.book_data, location=1, max_weeks=6) self.books = [ Book(0, 3, 1), Book(1, 5, 0), Book(2, 4, 1), Book(3, 2, 1) ]
def setUp(self): self.book_data = {0: 5, 1: 2, 3: 3} self.reader = Reader(books=self.book_data, location=1, max_weeks=6) self.books = [ Book(0, 1, 3), Book(1, 0, 5), Book(2, 1, 4), Book(3, 1, 2) ]
def create_new_book(): if request.method == 'GET': return render_template('new_book.html') else: book_name = request.form['book_name'] user = User.get_by_email(session['email']) new_book = Book(user.email, book_name, user._id) new_book.save_to_mongo() return make_response(user_books(user._id))
def setUp(self): self.room_1 = Room("Bongo", 3, 20) self.room_2 = Room("Studio 24", 5, 30) self.guest_1 = Guest("Pierre", 50, "Gotta Go") self.guest_2 = Guest("Alexander", 40, "On My Radio") self.guest_3 = Guest("Pepe", 30, "Divorce a I'ltalienne") self.guest_4 = Guest("Edi", 5, "Gotta Go") self.book = Book("For taxation purposes") self.hiden_book = Book("Real income")
def create_book(command, counter): Path.reset_path() Debug.logger.info(u"开始制作第 {} 本电子书".format(counter)) Debug.logger.info(u"对记录 {} 进行分析".format(command)) task_package = ReadListParser.get_task(command) # 分析命令 if not task_package.is_work_list_empty(): worker_factory(task_package.work_list) # 执行抓取程序 Debug.logger.info(u"网页信息抓取完毕") if not task_package.is_book_list_empty(): Debug.logger.info(u"开始从数据库中生成电子书") book = Book(task_package.book_list) book.create() return
def test_addBookWithSameTitle_RaiseException(self): title = "Python to ML" book1 = Book(title, self.abstract, self.summary, self.num_pages, self.isbn, self.author, self.category, self.edition, self.price) book2 = Book(title, self.abstract, self.summary, self.num_pages, self.isbn, self.author, self.category, self.edition, self.price) collection = CollectionBooks() collection.add(book1) self.assertRaises(Exception, collection.add, book2)
def test_storing_and_restoring_book(self): database = Database(self.db_handle) book = Book.from_path(f"{base_dir}/test/data/book.epub") # small data manipulation to make sure that it was retained when saving to db for word in book.words: word.mark_if_known(True) database.store_book(book) restored_book = database.restore_book(book.name) words_list_comparator = ContainerComparator( elem_equality_comparator=self._are_words_equal, sort_key=lambda w: w.stored_word) self.assertEqual(book.are_all_words_processed(), restored_book.are_all_words_processed()) self.assertTrue( words_list_comparator(book.known_words, restored_book.known_words)) self.assertTrue( words_list_comparator(book.unknown_words, restored_book.unknown_words)) self.assertEqual(book.name, restored_book.name) self.assertTrue(words_list_comparator(book.words, restored_book.words)) self.assertEqual(book.flashcards, restored_book.flashcards)
def test_something(self): feature = MakeFlashcards() interface = Interface() book = Book.from_path(f"{base_dir}\\test\\data\\short_book.epub") for word in book.words: word.mark_if_known(False) feature.run(interface, book)
def run(self, interface, **kwargs): book_path = interface.get_input("Enter path to ebook", input_processor=FilePathProcessor()) book = Book.from_path(book_path) db = Database() db.store_book(book) return book
def setUp(self) -> None: self.price = 20.00 self.title = "Python to ML" self.book1 = Book(self.title, "*" * 500, "introduction to python to ML", 50, "978–85–33302-27–3", "Guilherme Silveira", "Data Science", 11, self.price) self.collection = CollectionBooks() self.collection.add(self.book1)
def test_input_parser(self): ip = InputParser() for line in self.input_str.split("\n"): ip.parse_line(line) st = ip.get_state() trans = [[0, 2, 3], [3, 0, 4], [3, 4, 0]] books = [ Book(0, 5, 0), Book(1, 3, 0), Book(2, 10, 1), Book(3, 2, 2), Book(4, 8, 2), ] readers = [ Reader({ 0: 10, 1: 2 }, 0, 32), Reader({ 1: 1, 2: 10, 3: 3 }, 0, 32), Reader({ 0: 1, 1: 1, 2: 1, 3: 1, 4: 1 }, 1, 32), Reader({ 2: 5, 4: 5 }, 2, 32), ] expected = State(trans, books, readers) self.assertEqual(trans, st._transition) self.assertEqual(books, st._books) self.assertEqual(readers, st._readers) self.assertEqual(0, st._score) self.assertEqual(expected, st)
def test_searchBookByTitle_return_MatchedBooks(self): title1 = "Python to ML" title2 = "Python to DS" text_search = "Py" book1 = Book(title1, self.abstract, self.summary, self.num_pages, self.isbn, self.author, self.category, self.edition, self.price) book2 = Book(title2, self.abstract, self.summary, self.num_pages, self.isbn, self.author, self.category, self.edition, self.price) collection = CollectionBooks() collection.add(book1) collection.add(book2) results = collection.search(text_search) self.assertEqual(2, len(results))
def test_are_all_words_processed(self): data_dir = f"{base_dir}\\test\\data\\" file_name = "book.epub" book = Book.from_path(data_dir + file_name) self.assertFalse(book.are_all_words_processed()) for word in book.words: word.mark_if_known(True) self.assertTrue(book.are_all_words_processed())
def test_searchBookWithTitleShorterThan2chars_raiseException(self): title = "Python to ML" text_search = "P" book = Book(title, self.abstract, self.summary, self.num_pages, self.isbn, self.author, self.category, self.edition, self.price) collection = CollectionBooks() collection.add(book) self.assertRaises(Exception, collection.search, text_search)
class TestBook(unittest.TestCase): def setUp(self): self.room_1 = Room("Bongo", 3, 20) self.room_2 = Room("Studio 24", 5, 30) self.guest_1 = Guest("Pierre", 50, "Gotta Go") self.guest_2 = Guest("Alexander", 40, "On My Radio") self.guest_3 = Guest("Pepe", 30, "Divorce a I'ltalienne") self.guest_4 = Guest("Edi", 5, "Gotta Go") self.book = Book("For taxation purposes") self.hiden_book = Book("Real income") def test_book_has_name(self): self.assertEqual("Real income", self.hiden_book.name) def test_if_book_is_empty(self): self.assertEqual({}, self.book.page) def test_record_spending_in_book(self): self.book.record_guest_spending(self.guest_1, self.room_1) self.book.record_guest_spending(self.guest_1, self.room_2) self.book.record_guest_spending(self.guest_2, self.room_2) self.book.record_guest_spending(self.guest_3, self.room_2) self.book.record_guest_spending(self.guest_2, self.room_1) self.assertEqual({ "Pierre": 50, "Alexander": 50, "Pepe": 30 }, self.book.page)
def create_book(command, counter): Path.reset_path() Debug.logger.info(u"Ready to make No.{} e-book".format(counter)) Debug.logger.info(u"Analysis {} ".format(command)) task_package = UrlParser.get_task(command) # 分析命令 Debug.logger.debug(u"#Debug:#task_package是:" + str(task_package)) if not task_package.is_work_list_empty(): worker_factory(task_package.work_list) # 执行抓取程序 Debug.logger.info(u"Complete fetching from web") file_name_set = None if not task_package.is_book_list_empty(): Debug.logger.info(u"Start generating e-book from the database") book = Book(task_package.book_list) file_name_set = book.create() if file_name_set is not None: file_name_set2list = list(file_name_set) file_name = '-'.join(file_name_set2list[0:3]) return file_name return u"Oops! no epub file produced"
def test_creation_from_file(self): data_dir = f"{base_dir}\\test\\data\\" file_name = "book.epub" book = Book.from_path(data_dir + file_name) self.assertEqual(len(book.known_words), 0) self.assertEqual(len(book.unknown_words), 0) self.assertEqual(len(book.flashcards), 0) self.assertEqual(len(book.words), 6349) self.assertEqual(book.name, file_name) self.assertEqual(book.path, data_dir+file_name)
def test_searchBookByTitle_return_BooksInfo(self): title = "Python to ML" text_search = "Py" book = Book(title, self.abstract, self.summary, self.num_pages, self.isbn, self.author, self.category, self.edition, self.price) collection = CollectionBooks() collection.add(book) results = collection.search(text_search) first_result = results[0] self.assertEqual(first_result.title, title)
def main(): bookList = [] while (True): bookTitle = input("Title: ") bookPages = int(input("Pages: ")) bookPubYear = int(input("Publication year: ")) if not bookTitle: break bookList.append(Book(bookTitle, bookPages, bookPubYear)) whatToPrint = input("What information will be printed?") for book in bookList: if (whatToPrint == "everything"): print(book) elif (whatToPrint == "name"): print(book.name)
def test_known_and_unknown_words_getters(self): data_dir = f"{base_dir}\\test\\data\\" file_name = "book.epub" book = Book.from_path(data_dir + file_name) self.assertEqual(len(book.known_words), 0) self.assertEqual(len(book.unknown_words), 0) known_words_to_mark = {"the", "it", "on"} for word in book.words: if word.stored_word in known_words_to_mark: word.mark_if_known(True) self.assertEqual({w.stored_word for w in book.known_words}, known_words_to_mark) unknown_words_to_mark = {"she", "he"} for word in book.words: if word.stored_word in unknown_words_to_mark: word.mark_if_known(False) self.assertEqual({w.stored_word for w in book.unknown_words}, unknown_words_to_mark)
def test_has_book(self): database = Database(self.db_handle) book_dir = f"{base_dir}/test/data/book.epub" book = Book.from_path(book_dir) database.store_book(book) self.assertTrue(database.has_book(book.name))
def get_books(self): return Book.find_by_author_id(self._id)
def new_book(self, book_name): book = Book(author=self.email, book_name=book_name, author_id=self._id) book.save_to_mongo()
def new_review(book_id, title, content): book = Book.from_mongo(book_id) book.new_review(title=title, content=content)
def test_create(self): """ Test creates, which is a util to parse """ book = create_book(self.line.split(" ")[1:]) self.assertEqual(Book(0, 0, 5), book)
class BookTest(unittest.TestCase): """ Book test """ def setUp(self): self.book = Book(1, 3, 0) self.book2 = Book(0, 3, 1) def test_book_creation(self): """ Tests book creation """ self.assertEqual(0, self.book._location) self.assertEqual(1, self.book.id_book) self.assertEqual(0, self.book._internal_time) self.assertEqual(1, self.book2._location) self.assertEqual(0, self.book2.id_book) self.assertEqual(0, self.book2._internal_time) def test_move(self): """ Tests move action """ self.assertEqual(0, self.book._location) self.book.move(1, 5) self.assertEqual(1, self.book._location) self.assertEqual(5, self.book._internal_time) self.book.move(0, 3) self.assertEqual(0, self.book._location) self.assertEqual(8, self.book._internal_time) def test_read(self): """ Test read action """ self.assertEqual(0, self.book._internal_time) self.book.read(3, 0) self.assertEqual(3, self.book._internal_time) self.book.read(5, 1) self.assertEqual(5, self.book._internal_time) try: self.book.read(1, 2) self.fail() except AssertionError: pass def test_move_then_read(self): """ Tests move and then read """ self.assertEqual(0, self.book._internal_time) self.book.move(1, 5) self.assertEqual(1, self.book._location) self.assertEqual(5, self.book._internal_time) self.book.read(8, 3) self.assertEqual(8, self.book._internal_time)
def setUp(self): self.book = Book(1, 3, 0) self.book2 = Book(0, 3, 1)