Esempio n. 1
0
class Program:
    def __init__(self):
        self.__ui_manager = CommandUI()
        self.__DEBUGGING_MODE = True
        self.__function = Function()

    def change_ui(self):
        if not self.__ui_manager.switch():
            return
        if self.__ui_manager.TYPE == constants.TYPE_COMMAND:
            self.__ui_manager = MenuUI()
            print("Changed to menu based UI")
        else:
            self.__ui_manager = CommandUI()
            print("Changed to command based UI")

    def start(self):
        if self.__DEBUGGING_MODE:
            run_tests()

        self.__function.preload_list()
        self.change_ui()
        self.__ui_manager.show_help()

        while True:
            action = self.__ui_manager.get_action()

            try:
                if action.type == constants.ACTION_ADD:
                    result = self.__function.add(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_CHANGE_UI:
                    self.change_ui()

                elif action.type == constants.ACTION_ERROR:
                    self.__ui_manager.on_invalid_command()

                elif action.type == constants.ACTION_EXIT:
                    return

                elif action.type == constants.ACTION_FILTER_CATEGORY:
                    result = self.__function.filter_category(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_FILTER_CATEGORY_CONDITION:
                    result = self.__function.filter_category_condition(
                        action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_HELP:
                    self.__ui_manager.show_help()

                elif action.type == constants.ACTION_INSERT:
                    result = self.__function.insert(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_LIST_ALL:
                    result = self.__function.list_all()
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_LIST_CATEGORY:
                    result = self.__function.list_category(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_LIST_CATEGORY_CONDITION:
                    result = self.__function.list_category_condition(
                        action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_MAX_DAY:
                    result = self.__function.max_day(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_REMOVE_CATEGORY:
                    result = self.__function.remove_category(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_REMOVE_DAY:
                    result = self.__function.remove_day(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_REMOVE_RANGE:
                    result = self.__function.remove_range(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_SORT_CATEGORY:
                    result = self.__function.sort_category(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_SORT_DAY:
                    result = self.__function.sort_day(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_SUM_CATEGORY:
                    result = self.__function.sum_category(action.params)
                    self.__ui_manager.print_result(result)

                elif action.type == constants.ACTION_UNDO:
                    result = self.__function.undo_last_action()
                    self.__ui_manager.print_result(result)
            except ValidationError as error:
                self.__ui_manager.handle_error(error)
Esempio n. 2
0
def run_max_day_test():
    expenses = []
    function = Function(expenses)

    try:
        function.max_day([""])
        assert False
    except ValidationError:
        assert True

    try:
        function.max_day(["abc"])
        assert False
    except ValidationError:
        assert True

    try:
        function.max_day(["-1"])
        assert False
    except ValidationError:
        assert True

    try:
        function.max_day(["0"])
        assert False
    except ValidationError:
        assert True

    try:
        function.max_day(["32"])
        assert False
    except ValidationError:
        assert True

    try:
        function.max_day(["19"])
        assert False
    except ValidationError:
        assert True

    function.insert(["18", "32", constants.CATEGORY_HOUSEKEEPING])
    assert len(expenses) == 1
    assert "32" in function.max_day(["18"])

    function.insert(["17", "64", constants.CATEGORY_INTERNET])
    assert len(expenses) == 2
    assert "32" in function.max_day(["18"])

    function.insert(["18", "128", constants.CATEGORY_OTHERS])
    assert len(expenses) == 3
    assert "128" in function.max_day(["18"])