示例#1
0
    def extract_command(self, payload):
        """ Extract a command object from the payload object that gets sent by
            Slack. Returns a Command object if it was successful, None if it 
            wasn't.
        """
        try:

            # If it is not the correct command trigger, return None
            command = payload["command"]
            if command != "/{}".format(self.COMMAND):
                return None

            # Throws a KeyError if the Enum doesn't contain the given argument
            commandArg = CommandArgument[payload["text"].upper()]

            return Command(command, commandArg)

        except KeyError as e:
            self.logger.info("Invalid command argument: {}".format(e))
            return None
        except Exception as e:
            self.logger.info("Couldn't extract command: {}".format(e))
            return None
示例#2
0
    def testCommand(self):
        '''
        Method to test the getter and the setter of the Command class
        '''
        self.assertRaises(LibraryException, Command, "addBook|||")

        try:
            Command("addBook|Title||")
            assert False
        except LibraryException:
            pass

        try:
            Command("addBook||Description|")
            assert False
        except LibraryException:
            pass

        try:
            Command("addBook|||Author")
            assert False
        except LibraryException:
            pass

        try:
            Command("addBook|Title|Description|")
            assert False
        except LibraryException:
            pass

        try:
            Command("addBook|Title||Author")
            assert False
        except LibraryException:
            pass

        try:
            Command("addBook||Description|Author")
            assert False
        except LibraryException:
            pass

        command = Command("addBook|Title|Description|Author")
        assert command.toAddBook(1) == Book(1, "Title", "Description",
                                            "Author")

        try:
            Command("Cosmin")
            assert False
        except LibraryException:
            pass

        try:
            Command("addClient|1960715060015")
            assert False
        except LibraryException:
            pass

        try:
            command = Command("addClient|numarul25|Rusu Cosmin")
            command.toAddClient()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("removeBook|numarul25")
            command.toRemoveBook()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("removeBook|1|Title")
            command.toRemoveBook()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("removeClient|numarul25")
            command.toRemoveClient()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("removeClient")
            command.toRemoveClient()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("removeClient|1960715060|Rusu Cosmin")
            command.toRemoveClient()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateCnp|numarul25|newValue")
            command.toUpdateCnp()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateCnp|1960715060015|newValue")
            command.toUpdateCnp()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateCnp|numarul25|1960715060015")
            command.toUpdateCnp()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateCnp||")
            command.toUpdateCnp()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateCnp||1960715060015")
            command.toUpdateCnp()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateCnp|1960715060015|")
            command.toUpdateCnp()
            assert False
        except LibraryException:
            pass

        Command("updateCnp|2960715060015|1960715060015")

        try:
            command = Command("updateName|numarul25|newValue")
            command.toUpdateName()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateName||")
            command.toUpdateName()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateName||1960715060015")
            command.toUpdateName()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateName|1960715060015|")
            command.toUpdateName()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateTitle|numarul25|NewValue")
            command.toUpdateTitle()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateTitle||")
            command.toUpdateTitle()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateTitle||1960715060015")
            command.toUpdateTitle()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateTitle|1960715060015|")
            command.toUpdateTitle()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateTitle|numarul25|NewValue")
            command.toUpdateTitle()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateAuthor|numarul25|NewValue")
            command.toUpdateAuthor()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateAuthor||")
            command.toUpdateAuthor()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateAuthor||1960715060015")
            command.toUpdateAuthor()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateAuthor|1960715060015|")
            command.toUpdateAuthor()
            assert False
        except LibraryException:
            pass

        try:
            command = Command("updateAuthor|numarul25|NewValue")
            command.toUpdateAuthor()
            assert False
        except LibraryException:
            pass

        command = Command("addClient|1960715060015|Rusu Cosmin")
        assert command.toAddClient() == Client(1960715060015, "Rusu Cosmin")
示例#3
0
    def testRunningScenarios(self):
        #Running scenario 1
        books = []
        clients = []
        testrepo = LibraryRepository(False)
        controller = LibraryController(testrepo)
        cmd = Command(
            "addBook|Introduction to Algorithms|The Bible for every computer scientist|Thomas H Cormen"
        )
        controller.addBook(cmd.toAddBook(0))
        books.append(cmd.toAddBook(0))
        cmd = Command(
            "addBook|Learning Python - 3rd Edition|A very nice book for learning Python from scratch|Mark Lutz"
        )
        controller.addBook(cmd.toAddBook(1))
        books.append(cmd.toAddBook(1))
        cmd = Command("addClient|1960715060015|Rusu Cosmin")
        controller.addClient(cmd.toAddClient())
        clients.append(cmd.toAddClient())
        cmd = Command("addClient|2960715060015|Rusu Raluca")
        controller.addClient(cmd.toAddClient())
        clients.append(cmd.toAddClient())
        assert controller.getBooks() == books
        assert controller.getClients() == clients
        cmd = Command("updateName|1960715060015|Rusu Cosmin-Ionut")
        controller.updateClientName(int(cmd.getArg(1)), cmd.getArg(2))
        clients[0].setName("Rusu Cosmin-Ionut")
        assert controller.getClients() == clients
        cmd = Command("updateCnp|2960715060015|2020715060015")
        controller.updateClientCnp(int(cmd.getArg(1)), int(cmd.getArg(2)))
        clients[1].setCnp(2020715060015)
        assert controller.getClients() == clients
        cmd = Command("removeClient|2020715060015")
        controller.removeClient(int(cmd.getArg(1)))
        clients = clients[:-1]
        assert controller.getClients() == clients
        cmd = Command("removeBook|1")
        controller.removeBook(int(cmd.getArg(1)))
        books = books[:-1]
        cmd = Command(
            "updateDescription|0|The best book on algorithms and data structures"
        )
        controller.updateDescription(int(cmd.getArg(1)), cmd.getArg(2))
        books[0].setDescription(
            "The best book on algorithms and data structures")
        assert controller.getBooks() == books
        controller.undo()
        controller.undo()
        assert controller.getBooks() == [
            Book(0, "Introduction to Algorithms",
                 "The Bible for every computer scientist", "Thomas H Cormen"),
            Book(1, "Learning Python - 3rd Edition",
                 "A very nice book for learning Python from scratch",
                 "Mark Lutz")
        ]
        assert controller.getClients() == [
            Client(1960715060015, "Rusu Cosmin-Ionut")
        ]
        controller.redo()
        assert controller.getBooks() == [
            Book(0, "Introduction to Algorithms",
                 "The Bible for every computer scientist", "Thomas H Cormen")
        ]
        assert controller.getClients() == [
            Client(1960715060015, "Rusu Cosmin-Ionut")
        ]

        #tests exactly like the running scenario here: https://github.com/rusucosmin/courses/tree/master/fop/lab05-07

        books = []
        clients = []
        testrepo = LibraryRepository(False)
        controller = LibraryController(testrepo)
        cmd = Command(
            "addBook|Introduction to Algorithms|The Bible for every computer scientist|Thomas H Cormen"
        )
        controller.addBook(cmd.toAddBook(0))
        books.append(cmd.toAddBook(0))
        cmd = Command(
            "addBook|Learning Python - 3rd Edition|A very nice book for learning Python from scratch|Mark Lutz"
        )
        controller.addBook(cmd.toAddBook(1))
        books.append(cmd.toAddBook(1))
        cmd = Command("addClient|1960715060015|Rusu Cosmin")
        controller.addClient(cmd.toAddClient())
        clients.append(cmd.toAddClient())
        cmd = Command("addClient|2960715060015|Rusu Raluca")
        controller.addClient(cmd.toAddClient())
        clients.append(cmd.toAddClient())
        assert controller.getBooks() == books
        assert controller.getClients() == clients
        cmd = Command("updateName|1960715060015|Rusu Cosmin-Ionut")
        controller.updateClientName(int(cmd.getArg(1)), cmd.getArg(2))
        clients[0].setName("Rusu Cosmin-Ionut")
        assert controller.getClients() == clients
        cmd = Command("updateCnp|2960715060015|2020715060015")
        controller.updateClientCnp(int(cmd.getArg(1)), int(cmd.getArg(2)))
        clients[1].setCnp(2020715060015)
        assert controller.getClients() == clients
        cmd = Command("removeClient|2020715060015")
        controller.removeClient(int(cmd.getArg(1)))
        clients = clients[:-1]
        assert controller.getClients() == clients
        cmd = Command("removeBook|1")
        controller.removeBook(int(cmd.getArg(1)))
        books = books[:-1]
        cmd = Command(
            "updateDescription|0|The best book on algorithms and data structures"
        )
        controller.updateDescription(int(cmd.getArg(1)), cmd.getArg(2))
        books[0].setDescription(
            "The best book on algorithms and data structures")
        assert controller.getBooks() == books
        controller.undo()
        controller.undo()
        assert controller.getBooks() == [
            Book(0, "Introduction to Algorithms",
                 "The Bible for every computer scientist", "Thomas H Cormen"),
            Book(1, "Learning Python - 3rd Edition",
                 "A very nice book for learning Python from scratch",
                 "Mark Lutz")
        ]
        assert controller.getClients() == [
            Client(1960715060015, "Rusu Cosmin-Ionut")
        ]
        controller.redo()
        assert controller.getBooks() == [
            Book(0, "Introduction to Algorithms",
                 "The Bible for every computer scientist", "Thomas H Cormen")
        ]
        assert controller.getClients() == [
            Client(1960715060015, "Rusu Cosmin-Ionut")
        ]

        cmd = Command("addClient|2960715060015|Rusu Raluca-Rusu")
        controller.addClient(cmd.toAddClient())
        cmd = Command("addBook|Dorian Gray's Portret|Modern novel|Oscar Wilde")
        controller.addBook(cmd.toAddBook(1))
        cmd = Command(
            "addBook|The basis of Math|A nice book on Math for students|Dorin Andrica"
        )
        controller.addBook(cmd.toAddBook(2))
        print(controller.getClients())
        print(controller.getLoans())
        cmd = Command("rentBook|1960715060015|0")
        controller.rentBook(int(cmd.getArg(1)), int(cmd.getArg(2)))
        cmd = Command("rentBook|1960715060015|1")
        controller.rentBook(int(cmd.getArg(1)), int(cmd.getArg(2)))
        cmd = Command("returnBook|2960715060015|1")
        controller.returnBook(int(cmd.getArg(1)), int(cmd.getArg(2)))
        assert controller.getBooks() == [
            Book(0, "Introduction to Algorithms",
                 "The Bible for every computer scientist", "Thomas H Cormen"),
            Book(1, "Dorian Gray's Portret", "Modern novel", "Oscar Wilde"),
            Book(2, "The basis of Math", "A nice book on Math for students",
                 "Dorin Andrica")
        ]
        assert controller.getClients() == [
            Client(1960715060015, "Rusu Cosmin-Ionut"),
            Client(2960715060015, "Rusu Raluca-Rusu")
        ]
        assert controller.getLoans() == [
            Loan(
                Client(1960715060015, "Rusu Cosmin-Ionut"),
                Book(0, "Introduction to Algorithms",
                     "The Bible for every computer scientist",
                     "Thomas H Cormen"))
        ]
        '''
示例#4
0
 def run(self):
     '''
     Main function which handles the ui menu:
         - handles the user inputted commands: it's the bridge between the ui and the backend application
     '''
     answer = input("Would you like to continue?")
     while answer != "yes" and answer != "no":
         answer = input("yes / no")
     if answer == "no":
         self._controller.recreate()
     while True:
         try:
             opt = Command(
                 input(
                     "Type a command. Press 'help' for the available commands: "
                 ))
             arg = opt.getArg(0)
             if arg == "help":
                 self.showMenu()
             elif arg == "addBook".lower():
                 self._controller.addBook(
                     opt.toAddBook(len(self._controller.getBooks())))
             elif arg == "addClient".lower():
                 self._controller.addClient(opt.toAddClient())
             elif arg == "removeBook".lower():
                 self._controller.removeBook(int(opt.getArg(1)))
             elif arg == "removeClient".lower():
                 self._controller.removeClient(int(opt.getArg(1)))
             elif arg == "updateCnp".lower():
                 self._controller.updateClientCnp(int(opt.getArg(1)),
                                                  int(opt.getArg(2)))
             elif arg == "updateName".lower():
                 self._controller.updateClientName(int(opt.getArg(1)),
                                                   opt.getArg(2))
             elif arg == "updateTitle".lower():
                 self._controller.updateTitle(int(opt.getArg(1)),
                                              opt.getArg(2))
             elif arg == "updateDescription".lower():
                 self._controller.updateDescription(int(opt.getArg(1)),
                                                    opt.getArg(2))
             elif arg == "updateAuthor".lower():
                 self._controller.updateAuthor(int(opt.getArg(1)),
                                               opt.getArg(2))
             elif arg == "listBooks".lower():
                 print('\n\n'.join(
                     str(book) for book in self._controller.getBooks()))
             elif arg == "listClients".lower():
                 print('\n\n'.join(
                     str(client)
                     for client in self._controller.getClients()))
             elif arg == "listLoans".lower():
                 print('\n\n'.join(
                     str(loan) for loan in self._controller.getLoans()))
             elif arg == "list":
                 print(self._controller.getLibrary())
             elif arg == "rentBook".lower():
                 self._controller.rentBook(int(opt.getArg(1)),
                                           int(opt.getArg(2)))
             elif arg == "returnBook".lower():
                 self._controller.returnBook(int(opt.getArg(1)),
                                             int(opt.getArg(2)))
             elif arg == "rentedBooksSorted".lower():
                 print("List of rented books ordered alphabetically\n\n" +
                       "\n\n".join(
                           str(book) for book in
                           self._controller.getRentedBooksSorted()))
             elif arg == "mostActiveUsers".lower():
                 many = self._controller.getMostActiveUsers()
                 print("The most active users are:\n\n")
                 for x in many:
                     print(
                         str(x[0]) + " has " + str(x[1]) +
                         " rented books.\n")
             elif arg == "filterBooks".lower():
                 x = self._controller.filterBooks(
                     self._controller.getBooks())
             elif arg == "undo":
                 self._controller.undo()
             elif arg == "redo":
                 self._controller.redo()
             elif arg == "save":
                 self._controller.save()
             elif arg == "exit":
                 print("Exiting...")
                 break
             elif arg == 'delete':
                 self._controller.recreate()
             else:
                 print("Command not recognized!")
         except ValueError as ve:
             print("ValueError - Argument should be integer!")
         except LibraryException as le:
             print(str(le))
示例#5
0
文件: tester.py 项目: crssic/courses
 def testController(self):
     books = []
     clients = []
     testrepo = LibraryRepository(False)
     controller = LibraryController(testrepo)
     cmd = Command(
         "addBook|Introduction to Algorithms|The Bible for every computer scientist|Thomas H Cormen"
     )
     controller.addBook(cmd.toAddBook(0))
     books.append(cmd.toAddBook(0))
     cmd = Command(
         "addBook|Learning Python - 3rd Edition|A very nice book for learning Python from scratch|Mark Lutz"
     )
     controller.addBook(cmd.toAddBook(1))
     books.append(cmd.toAddBook(1))
     cmd = Command("addClient|1960715060015|Rusu Cosmin")
     controller.addClient(cmd.toAddClient())
     clients.append(cmd.toAddClient())
     cmd = Command("addClient|2960715060015|Rusu Raluca")
     controller.addClient(cmd.toAddClient())
     clients.append(cmd.toAddClient())
     assert controller.getBooks() == books
     assert controller.getClients() == clients
     cmd = Command("updateName|1960715060015|Rusu Cosmin-Ionut")
     controller.updateClientName(int(cmd.getArg(1)), cmd.getArg(2))
     clients[0].setName("Rusu Cosmin-Ionut")
     assert controller.getClients() == clients
     cmd = Command("updateCnp|2960715060015|2020715060015")
     controller.updateClientCnp(int(cmd.getArg(1)), int(cmd.getArg(2)))
     clients[1].setCnp(2020715060015)
     assert controller.getClients() == clients
     cmd = Command("removeClient|2020715060015")
     controller.removeClient(int(cmd.getArg(1)))
     clients = clients[:-1]
     assert controller.getClients() == clients
     cmd = Command("removeBook|1")
     controller.removeBook(int(cmd.getArg(1)))
     books = books[:-1]
     cmd = Command(
         "updateDescription|0|The best book on algorithms and data structures"
     )
     controller.updateDescription(int(cmd.getArg(1)), cmd.getArg(2))
     books[0].setDescription(
         "The best book on algorithms and data structures")
     assert controller.getBooks() == books
     controller.undo()
     controller.undo()
     assert controller.getBooks() == [
         Book(0, "Introduction to Algorithms",
              "The Bible for every computer scientist", "Thomas H Cormen"),
         Book(1, "Learning Python - 3rd Edition",
              "A very nice book for learning Python from scratch",
              "Mark Lutz")
     ]
     assert controller.getClients() == [
         Client(1960715060015, "Rusu Cosmin-Ionut")
     ]
     controller.redo()
     assert controller.getBooks() == [
         Book(0, "Introduction to Algorithms",
              "The Bible for every computer scientist", "Thomas H Cormen")
     ]
     assert controller.getClients() == [
         Client(1960715060015, "Rusu Cosmin-Ionut")
     ]