def __init__(self, filename): """ Constructor method Opens the file and loads the entries in memory filename is a string, the path to the file used for storage O(n) """ BookList.__init__(self) self.__filename = filename try: self.__bookFile = open(self.__filename, "r") except IOError: # File does not exist. Create it. self.__bookFile = open(self.__filename, "w") self.__bookFile.close() self.__bookFile = open(self.__filename, "r") line = "Nonempty String" while (line != ""): line = self.__bookFile.readline() if (line == "" or line == "\n"): break details = line.split("|") book = Book(details[0].strip(), details[1].strip(), details[2].strip()) if details[3] == "True": book.setBorrowed(True) book.setPopularity(int(details[4])) self.bookList.append(book) self.__bookFile.close()
def test_Book(): book = Book("title", "author", "description") assert book.getTitle() == "title" assert book.getAuthor() == "author" assert book.getDescription() == "description" book.setBorrowed(True) assert book.isBorrowed() == True book.setAuthor("new author") assert book.getAuthor() == "new author" book.setDescription(None) book.setTitle("new title") assert book.getTitle() == "new title" book.setPopularity(1) assert book.getPopularity() == 1 assert book.getDescription() == None book.setId(0)