Beispiel #1
0
 def testBusiness(self):
     self.__repoBook = RepoBook()
     self.__business = ServiceBook(self.__repoBook, self.__validBook, self.__undoList)
     self.assertEquals(len(self.__business),0)
     
     self.__business.addNewBook(self.__bookId, self.__title, self.__author, self.__description)
     self.assertEquals(len(self.__business), 1)
     self.__business.addNewBook(15, "GoT", "George RR Martin", "abcd")
     self.assertEquals(len(self.__business), 2)
     
     self.__repoClient = RepoClient()
     self.__business = ServiceClient(self.__repoClient, self.__validClient, self.__undoList)
     self.__business.addNewClient(self.__clientId, self.__name)
     self.assertEquals(len(self.__business), 1)
     self.__business.addNewClient(5, "Nicolae Ceausescu")
     self.assertEquals(len(self.__business), 2)
     element = Client(5, "Traian Basescu")
     self.__repoClient.updateElement(element, 0)
     search = self.__repoClient.searchElement(5, 2)
     self.assertEquals(search.get_name(), "Traian Basescu")
     self.__business.removeClient(5, "Nicolae Ceausescu")
     self.assertEquals(len(self.__business), 1)
     
     self.__repoBook = RepoBook()
     self.__business = ServiceBook(self.__repoBook, self.__validBook, self.__undoList)
     self.__business.addNewBook(23, "De veghe in lanul de secara", "JD Salinger", "Roman")
     self.__business.addNewBook(15, "Martianul", "Andy Weir", "I-a placut lui Leo")
     self.__business.removeBook(15, 0)
     self.assertEquals(len(self.__business), 1)
     element = Book(15, "1984", "George Orwell", "Tot lui Leo i-a placut")
     self.__repoBook.addElement(element, 0)
     element = Book(15, "1984", "Pam pam", "Tot lui Leo i-a placut")
     self.__repoBook.updateElement(element, 0)
     search = self.__repoBook.searchElement(15, 1)
     self.assertEquals(search.get_author(), "Pam pam")
Beispiel #2
0
    def searchBooks(self, command, characteristic):
        '''
        Function that searches for a book after a given characteristic
        Input: command - the category in which the search is made(id, title, author, description)
        Precondition: command - integer
                      characteristic - string
        Output: -
        Postcondition: - 
        '''
        book = Book()
        books = []
        cnt = 0
        i = 0
        while i < len(self.__repoBooks):
            search = self.__repoBooks.searchBook(characteristic, command, i)
            book = search[0]
            if book.get_book_id() > -1:
                cnt += 1
                i = search[1]
                books.append(book)
            i += 1
        if cnt == 0:
            raise ValueError("This book couldn't be found")

        return books
Beispiel #3
0
 def setUp(self):
     self.__bookId = 5
     self.__title = "Fratii Karamazov"
     self.__author = "Dostoievski"
     self.__description = "pam pam"
     self.__clientId = 7
     self.__name = "Ion Iliescu"
     self.__rentalId = 16
     self.__rentedDate = 20
     self.__dueDate = 13
     self.__returnedDate = 30
     self.__book = Book(self.__bookId, self.__title, self.__description,
                        self.__author)
     self.__client = Client(self.__clientId, self.__name)
     self.__rental = Rental(self.__rentalId, self.__bookId, self.__clientId,
                            self.__rentedDate, self.__dueDate,
                            self.__returnedDate)
     self.__repoBook = RepoBook()
     self.__repoClient = RepoClient()
     self.__undoList = UndoRedo()
     self.__repoRental = RepoRental(self.__repoBook, self.__repoClient)
     self.__validBook = BookValidator()
     self.__validClient = ClientValidator()
     self.__validRental = RepoValidator(self.__repoBook, self.__repoClient)
     self.__bookService = ServiceBook(self.__repoBook, self.__validBook,
                                      self.__undoList)
     self.__clientService = ServiceClient(self.__repoClient,
                                          self.__validClient,
                                          self.__undoList)
     self.__rentalService = ServiceRental(self.__repoBook,
                                          self.__repoClient,
                                          self.__validRental,
                                          self.__repoRental,
                                          self.__undoList)
Beispiel #4
0
 def __removeBooks(self):
     try:
         book = self.serviceRental.removeAparition(self.idBooktf.get(),
                                                   "book")
         book = Book(self.idBooktf.get(), self.titletf.get(),
                     self.authortf.get(), self.descriptiontf.get())
         messagebox.showinfo("Removed",
                             "Element %s was removed" % book.get_title())
         print
     except Exception as e:
         messagebox.showinfo("Error", "Error removing element - " + str(e))
 def searchBook(self, element, command, index):
     '''
     Function that searches for a given book
     Input: element - the id of the book after which the search is made
            command - the category in which the search is made(id, title, author, description)
            index - the position from which the search should start
     PreconditionL element, command, index - integers
     Output: (the element, position)
     Precondition: (the element, position) - a tuple 
      '''
     book = Book(-1)
     index = int(index)
     if command == 1:
         for i in range(index, len(self.__list)):
             if self.partialString(self.__list[i].get_book_id(), element):
                 return (self.__list[i], i)
         return (book, -1)
     elif command == 2:
         for i in range(index, len(self.__list)):
             if self.partialString(self.__list[i].get_title(), element):
                 return (self.__list[i], i)
         return (book, -1)
     else:
         if command == 3:
             for i in range(index, len(self.__list)):
                 if self.partialString(self.__list[i].get_description(),
                                       element):
                     return (self.__list[i], i)
             return (book, -1)
         elif command == 4:
             for i in range(index, len(self.__list)):
                 if self.partialString(self.__list[i].get_author(),
                                       element):
                     return (self.__list[i], i)
             return (book, -1)
Beispiel #6
0
    def updateBook(self,
                   bookId,
                   title,
                   author,
                   description,
                   numberOfRentals,
                   numberOfDays,
                   undo=True):
        '''
        Function that updates a given book
        Input: bookId, title, author, description, numberOfRentals, numberOfDays
        Precondition: bookId - integer
                      title, author, description - strings
                      numberOfRentals, numberOfDays - integers
        Output: -
        Postcondition: -
        '''
        bookId = int(bookId)
        old_book = self.__repoBooks.searchElement(bookId, 1)
        book = Book(bookId, title, author, description, numberOfRentals,
                    numberOfDays)
        self.__valid.validBook(book)
        self.__repoBooks.updateElement(book, 0)

        index = 1

        if undo == True:
            self.__undoService.addNewOperation(self.__repoBooks.updateElement,
                                               self.__repoBooks.updateElement,
                                               [bookId, old_book], index)
Beispiel #7
0
 def removeElement(self, element, command):
     '''
     Function that removes a given element from the list if it existed in the list before
     Input: element
     Precondition: -
     Output: -
     Postcondition: -
     '''
     #if element not in self.__list:
     #raise ValueError("inexisting element")
     # i = 0
     element = int(element)
     if command == "1":
         element = Book(element)
         index = self.__list.searchElement1(element)
         del self.__list[index]
         #while i < len(self.__list):
         #   if self.__list[i].get_book_id() == element:
         #      del self.__list[i]
         #     i -= 1
         # i += 1
     elif command == "2":
         element = Client(element)
         index = self.__list.searchElement1(element)
         del self.__list[index]
Beispiel #8
0
 def __storePressedBooks(self):
     """
       Handler method for store button pressed
       Store the book
       Show error messages on exceptions
     """
     try:
         st = self.serviceBooks.addNewBook(int(self.idBooktf.get()),
                                           self.titletf.get(),
                                           self.authortf.get(),
                                           self.descriptiontf.get())
         st = Book(self.idBooktf.get(), self.titletf.get(),
                   self.authortf.get(), self.descriptiontf.get())
         messagebox.showinfo("Stored",
                             "Element %s saved.." % st.get_title())
         print
     except Exception as e:
         messagebox.showinfo("Error", "Error saving element - " + str(e))
Beispiel #9
0
 def removeBook(self, bookId, param):
     '''
     Function that removes a book from the library
     Input: bookId
     Precondition: bookId - integer
     Output: -
     Postcondition: -
     '''
     bookId = int(bookId)
     old_book = Book(bookId)
     self.__repoBooks.removeElement(bookId, 0)
Beispiel #10
0
 def validRepo(self, element):
     errors = ""
     book = element.get_book_id()
     client = element.get_client_id()
     search = self.__bookRepo.searchElement1(Book(book))
     if search == -1:
         errors += "this book doesn't exist\n"
     search = self.__clientRepo.searchElement1(Client(client))
     if search == -1:
         errors += "this client doesn't exist\n"
     if len(errors) > 0:
         raise RepoError(errors)
Beispiel #11
0
    def testRepository(self):
        self.assertEquals(len(self.__repoBook), 0)
        self.__repoBook.addElement(self.__book, 0)
        self.assertEquals(len(self.__repoBook), 1)
        new_book = Book(3, "Ion", "glasul pamantului/glasul iubirii",
                        "Liviu Rebreanu")
        self.__repoBook.addElement(new_book, 0)
        self.assertEquals(len(self.__repoBook), 2)
        try:
            self.__repoBook.addElement(self.__book, 0)
            self.assertEquals(False)
        except ValueError as re:
            self.assertEquals(str(re), "existing element")

        self.assertEquals(len(self.__repoClient), 0)
        self.__repoClient.addElement(self.__client, 0)
        self.assertEquals(len(self.__repoClient), 1)
        new_client = Client(2, "Justin Trudeau")
        self.__repoClient.addElement(new_client, 0)
        self.assertEquals(len(self.__repoClient), 2)
        try:
            self.__repoClient.addElement(self.__client, 0)
            self.assertEquals(False)
        except ValueError as re:
            self.assertEquals(str(re), "existing element")
        self.__repoClient.removeElement(new_client.get_client_id())
        self.assertEquals(len(self.__repoClient), 1)
        try:
            self.__repoClient.removeElement(new_client.get_client_id())
            self.assertEquals(False)
        except ValueError as re:
            self.assertEquals(str(re), "inexisting element")

        #self.__repo.printBooks()
        new_book = Book(3, "HP", "JK Rowling", "magic")
        self.__repoBook.updateElement(new_book, 0)
        cnt = self.__repoBook.searchElement1(new_book)
        cnt = int(cnt)
 def searchElement(self, element, command):
     '''
     Function that searches for an element in the given list
     Input: element - the id of the element after which the search is made
            command - the type of the element
     Precondition: element, command - integers
     Output: the element if it was found or -1 otherwise
     Postcondition:-
     '''
     element = int(element)
     if command == 1:
         element = Book(element)
     else:
         element = Client(element)
     for i in range(0, len(self.__list)):
         if self.__list[i] == element:
             return self.__list[i]
     return -1
 def removeElement(self, element, param):
     '''
     Function that removes a given element from the list if it existed in the list before
     Input: element
     Precondition: -
     Output: -
     Postcondition: -
     '''
     element = int(element)
     element = Book(element)
     if element not in self.__list:
         raise ValueError("inexisting element")
     i = 0
     while i < len(self.__list):
         if self.__list[i] == element:
             del self.__list[i]
             i -= 1
         i += 1
Beispiel #14
0
    def addNewBook(self, bookId, title, author, description, undo=True):
        '''
        Function that adds a new book in the library
        Input: bookId, title, author, description
        Precondition: bookId - integer
                      title - string
                      author - string
                      description - string
        Output: -
        Postcondition: -
        '''
        bookId = int(bookId)
        new_book = Book(bookId, title, author, description)
        self.__valid.validBook(new_book)
        self.__repoBooks.addElement(new_book, 0)
        index = 1

        if undo == True:
            self.__undoService.addNewOperation(self.__repoBooks.addElement,
                                               self.__repoBooks.removeElement,
                                               [new_book, bookId], index)
Beispiel #15
0
    def removeAparition(self, bookId, command, undo=True):
        if command == "book":
            bookId = int(bookId)
            book = self.__repoBooks.searchElement(bookId, 1)
            new_book_Id = book.get_book_id()
            new_book_title = book.get_title()
            new_book_author = book.get_author()
            new_book_description = book.get_description()
            new_book_number_of_rentals = book.get_number_of_rentals()
            new_book_number_of_days = book.get_number_of_days()
            new_book = Book(new_book_Id, new_book_title, new_book_author,
                            new_book_description, new_book_number_of_rentals,
                            new_book_number_of_days)
            i = 2
            rental = self.__repo.findRentalId(bookId, "book")
            self.__repoBooks.removeElement(bookId)
            self.__undoService.addNewOperation(self.__repoBooks.removeElement,
                                               self.__repoBooks.addElement,
                                               [bookId, new_book], i)
            if type(rental) != int:
                index = int(self.__repo.searchElement1(rental))
                while index != -1:
                    old_rental_id = rental.get_rental_id()
                    old_rental_book_id = rental.get_book_id()
                    old_rental_client_id = rental.get_client_id()
                    old_rental_rented_date = rental.get_rented_date()
                    old_rental_due_date = rental.get_due_date()
                    old_rental_returned_date = rental.get_returned_date()
                    self.__repo.removeElementInCascade(index)
                    old_rental = Rental(old_rental_id, old_rental_book_id,
                                        old_rental_client_id,
                                        old_rental_rented_date,
                                        old_rental_due_date,
                                        old_rental_returned_date)
                    self.__undoService.addNewOperation(self.removeAparition,
                                                       self.__repo.addRental,
                                                       [0, old_rental], i)
                    i += 1
                    rental = self.__repo.findRentalId(bookId, "book")

                    if type(rental) != int:
                        index = int(self.__repo.searchElement1(rental))
                    else:
                        index = -1
        elif command == "client":
            i = 2
            bookId = int(bookId)
            client = self.__repoClients.searchElement(bookId, 2)
            new_client_Id = client.get_client_id()
            new_client_name = client.get_name()
            new_client_number_of_days = client.get_number_of_days()
            new_client = Client(new_client_Id, new_client_name,
                                new_client_number_of_days)
            rental = self.__repo.findRentalId(bookId, "client")
            self.__repoClients.removeElement(bookId)
            self.__undoService.addNewOperation(
                self.__repoClients.removeElement,
                self.__repoClients.addElement, [bookId, new_client], i)
            if type(rental) != int:
                index = int(self.__repo.searchElement1(rental))
                while index != -1:
                    old_rental_id = rental.get_rental_id()
                    old_rental_book_id = rental.get_book_id()
                    old_rental_client_id = rental.get_client_id()
                    old_rental_rented_date = rental.get_rented_date()
                    old_rental_due_date = rental.get_due_date()
                    old_rental_returned_date = rental.get_returned_date()
                    self.__repo.removeElementInCascade(index)
                    rental = Rental(old_rental_id, old_rental_book_id,
                                    old_rental_client_id,
                                    old_rental_rented_date,
                                    old_rental_due_date,
                                    old_rental_returned_date)
                    self.__undoService.addNewOperation(self.removeAparition,
                                                       self.addNewRental,
                                                       [0, rental], i)
                    i += 1
                    rental = self.__repo.findRentalId(bookId, "client")
                    if type(rental) != int:
                        index = int(self.__repo.searchElement1(rental))
                    else:
                        index = -1
Beispiel #16
0
class Tests(unittest.TestCase):
    def setUp(self):
        self.__bookId = 5
        self.__title = "Fratii Karamazov"
        self.__author = "Dostoievski"
        self.__description = "pam pam"
        self.__clientId = 7
        self.__name = "Ion Iliescu"
        self.__rentalId = 16
        self.__rentedDate = 20
        self.__dueDate = 13
        self.__returnedDate = 30
        self.__book = Book(self.__bookId, self.__title, self.__description,
                           self.__author)
        self.__client = Client(self.__clientId, self.__name)
        self.__rental = Rental(self.__rentalId, self.__bookId, self.__clientId,
                               self.__rentedDate, self.__dueDate,
                               self.__returnedDate)
        self.__repoBook = RepoBook()
        self.__repoClient = RepoClient()
        self.__undoList = UndoRedo()
        self.__repoRental = RepoRental(self.__repoBook, self.__repoClient)
        self.__validBook = BookValidator()
        self.__validClient = ClientValidator()
        self.__validRental = RepoValidator(self.__repoBook, self.__repoClient)
        self.__bookService = ServiceBook(self.__repoBook, self.__validBook,
                                         self.__undoList)
        self.__clientService = ServiceClient(self.__repoClient,
                                             self.__validClient,
                                             self.__undoList)
        self.__rentalService = ServiceRental(self.__repoBook,
                                             self.__repoClient,
                                             self.__validRental,
                                             self.__repoRental,
                                             self.__undoList)

    def testModels(self):
        self.assertEquals(self.__book.get_book_id(), self.__bookId)
        self.assertEquals(self.__client.get_name(), self.__name)
        self.assertEquals(self.__rental.get_rental_id(), self.__rentalId)
        #self.assertEquals self.__rental.get_number_of_rentals() == self.__numberOfRentals

    def testRepository(self):
        self.assertEquals(len(self.__repoBook), 0)
        self.__repoBook.addElement(self.__book, 0)
        self.assertEquals(len(self.__repoBook), 1)
        new_book = Book(3, "Ion", "glasul pamantului/glasul iubirii",
                        "Liviu Rebreanu")
        self.__repoBook.addElement(new_book, 0)
        self.assertEquals(len(self.__repoBook), 2)
        try:
            self.__repoBook.addElement(self.__book, 0)
            self.assertEquals(False)
        except ValueError as re:
            self.assertEquals(str(re), "existing element")

        self.assertEquals(len(self.__repoClient), 0)
        self.__repoClient.addElement(self.__client, 0)
        self.assertEquals(len(self.__repoClient), 1)
        new_client = Client(2, "Justin Trudeau")
        self.__repoClient.addElement(new_client, 0)
        self.assertEquals(len(self.__repoClient), 2)
        try:
            self.__repoClient.addElement(self.__client, 0)
            self.assertEquals(False)
        except ValueError as re:
            self.assertEquals(str(re), "existing element")
        self.__repoClient.removeElement(new_client.get_client_id())
        self.assertEquals(len(self.__repoClient), 1)
        try:
            self.__repoClient.removeElement(new_client.get_client_id())
            self.assertEquals(False)
        except ValueError as re:
            self.assertEquals(str(re), "inexisting element")

        #self.__repo.printBooks()
        new_book = Book(3, "HP", "JK Rowling", "magic")
        self.__repoBook.updateElement(new_book, 0)
        cnt = self.__repoBook.searchElement1(new_book)
        cnt = int(cnt)

    def testBusiness(self):
        self.__repoBook = RepoBook()
        self.__business = ServiceBook(self.__repoBook, self.__validBook,
                                      self.__undoList)
        self.assertEquals(len(self.__business), 0)

        self.__business.addNewBook(self.__bookId, self.__title, self.__author,
                                   self.__description)
        self.assertEquals(len(self.__business), 1)
        self.__business.addNewBook(15, "GoT", "George RR Martin", "abcd")
        self.assertEquals(len(self.__business), 2)

        self.__repoClient = RepoClient()
        self.__business = ServiceClient(self.__repoClient, self.__validClient,
                                        self.__undoList)
        self.__business.addNewClient(self.__clientId, self.__name)
        self.assertEquals(len(self.__business), 1)
        self.__business.addNewClient(5, "Nicolae Ceausescu")
        self.assertEquals(len(self.__business), 2)
        element = Client(5, "Traian Basescu")
        self.__repoClient.updateElement(element, 0)
        search = self.__repoClient.searchElement(5, 2)
        self.assertEquals(search.get_name(), "Traian Basescu")
        self.__business.removeClient(5, "Nicolae Ceausescu")
        self.assertEquals(len(self.__business), 1)

        self.__repoBook = RepoBook()
        self.__business = ServiceBook(self.__repoBook, self.__validBook,
                                      self.__undoList)
        self.__business.addNewBook(23, "De veghe in lanul de secara",
                                   "JD Salinger", "Roman")
        self.__business.addNewBook(15, "Martianul", "Andy Weir",
                                   "I-a placut lui Leo")
        self.__business.removeBook(15, 0)
        self.assertEquals(len(self.__business), 1)
        element = Book(15, "1984", "George Orwell", "Tot lui Leo i-a placut")
        self.__repoBook.addElement(element, 0)
        element = Book(15, "1984", "Pam pam", "Tot lui Leo i-a placut")
        self.__repoBook.updateElement(element, 0)
        search = self.__repoBook.searchElement(15, 1)
        self.assertEquals(search.get_author(), "Pam pam")

    def testRent(self):
        pass