Ejemplo n.º 1
0
    def test_remove_student(self):
        services = Services()
        repo = services.repository

        services.add(["discipline", "0", "discipline test"])
        services.add(["student", "1", "student 1 test"])
        services.add(["student", "2", "student 2 test"])
        services.grade(["1", "0", "10"])
        services.grade(["1", "0", "6"])
        services.grade(["2", "0", "9"])
        services.grade(["2", "0", "7"])
        services.grade(["2", "0", "8"])

        self.assertEqual(repo.get_students_count(), 2)
        self.assertEqual(repo.get_disciplines_count(), 1)
        self.assertEqual(repo.get_grades_count(), 5)

        self.assertRaises(Exception, lambda: services.remove([]))
        self.assertRaises(Exception, lambda: services.remove(["", ""]))
        self.assertRaises(Exception, lambda: services.remove(["abc", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.remove(["student", "abc"]))
        self.assertRaises(Exception, lambda: services.remove(["student", "0"]))
        services.remove(["student", "2"])

        self.assertEqual(repo.get_students_count(), 1)
        self.assertEqual(repo.get_disciplines_count(), 1)
        self.assertEqual(repo.get_grades_count(), 2)
        self.assertRaises(Exception, lambda: services.remove(["student", "2"]))
Ejemplo n.º 2
0
    def test_add_discipline(self):
        services = Services()
        repo = services.repository

        self.assertRaises(Exception, lambda: services.add([]))
        self.assertRaises(Exception, lambda: services.add(["", "", ""]))
        self.assertRaises(Exception,
                          lambda: services.add(["abc", "abc", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.add(["discipline", "abc", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.add(["discipline", "0", "abc"]))
        services.add(["discipline", "0", "test"])

        discipline1 = repo.get_discipline(0)
        self.assertEqual(discipline1.id, 0)
        self.assertEqual(discipline1.name, "test")
        self.assertEqual(repo.get_disciplines_count(), 1)

        self.assertRaises(Exception,
                          lambda: services.add(["discipline", "0", "testing"]))
        services.add(["discipline", "1", "test"])

        discipline2 = repo.get_discipline(1)
        self.assertEqual(discipline2.id, 1)
        self.assertEqual(discipline2.name, "test")
        self.assertEqual(repo.get_disciplines_count(), 2)

        self.assertNotEqual(discipline1, discipline2)
Ejemplo n.º 3
0
    def test_update_discipline(self):
        services = Services()
        repo = services.repository

        services.add(["discipline", "1", "discipline test"])
        self.assertEqual(repo.get_disciplines_count(), 1)
        discipline = repo.get_discipline(1)
        self.assertEqual(discipline.id, 1)
        self.assertEqual(discipline.name, "discipline test")

        self.assertRaises(Exception, lambda: services.update([]))
        self.assertRaises(Exception, lambda: services.update(["", "", ""]))
        self.assertRaises(Exception,
                          lambda: services.update(["abc", "abc", "abc"]))
        self.assertRaises(
            Exception, lambda: services.update(["discipline", "abc", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.update(["discipline", "0", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.update(["discipline", "1", "abc"]))
        services.update(["discipline", "1", "test"])

        self.assertEqual(discipline.id, 1)
        self.assertEqual(discipline.name, "test")
        self.assertEqual(repo.get_disciplines_count(), 1)
Ejemplo n.º 4
0
    def test_grade(self):
        services = Services()
        repo = services.repository

        services.add(["student", "1", "stud 1"])
        services.add(["discipline", "1", "discipline 1"])

        self.assertEqual(repo.get_students_count(), 1)
        self.assertEqual(repo.get_disciplines_count(), 1)

        self.assertRaises(Exception, lambda: services.grade([]))
        self.assertRaises(Exception, lambda: services.grade(["", "", ""]))
        self.assertRaises(Exception,
                          lambda: services.grade(["abc", "abc", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.grade(["0", "abc", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.grade(["1", "abc", "abc"]))
        self.assertRaises(Exception, lambda: services.grade(["1", "0", "abc"]))
        self.assertRaises(Exception, lambda: services.grade(["1", "1", "abc"]))
        self.assertRaises(Exception, lambda: services.grade(["1", "1", "0"]))
        self.assertRaises(Exception, lambda: services.grade(["1", "1", "11"]))

        services.grade(["1", "1", "1"])
        services.grade(["1", "1", "10"])
        services.grade(["1", "1", "5"])

        self.assertEqual(repo.get_grades_count(), 3)
Ejemplo n.º 5
0
    def test_add_student(self):
        services = Services()
        repo = services.repository

        self.assertRaises(Exception, lambda: services.add([]))
        self.assertRaises(Exception, lambda: services.add(["", "", ""]))
        self.assertRaises(Exception,
                          lambda: services.add(["abc", "abc", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.add(["student", "abc", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.add(["student", "0", "abc"]))
        services.add(["student", "0", "test"])

        student1 = repo.get_student(0)
        self.assertEqual(student1.id, 0)
        self.assertEqual(student1.name, "test")
        self.assertEqual(repo.get_students_count(), 1)

        self.assertRaises(Exception,
                          lambda: services.add(["student", "0", "testing"]))
        services.add(["student", "1", "test"])

        student2 = repo.get_student(1)
        self.assertEqual(student2.id, 1)
        self.assertEqual(student2.name, "test")
        self.assertEqual(repo.get_students_count(), 2)

        self.assertNotEqual(student1, student2)
Ejemplo n.º 6
0
    def test_update_student(self):
        services = Services()
        repo = services.repository

        services.add(["student", "1", "student test"])
        self.assertEqual(repo.get_students_count(), 1)
        student = repo.get_student(1)
        self.assertEqual(student.id, 1)
        self.assertEqual(student.name, "student test")

        self.assertRaises(Exception, lambda: services.update([]))
        self.assertRaises(Exception, lambda: services.update(["", "", ""]))
        self.assertRaises(Exception,
                          lambda: services.update(["abc", "abc", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.update(["student", "abc", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.update(["student", "0", "abc"]))
        self.assertRaises(Exception,
                          lambda: services.update(["student", "1", "abc"]))
        services.update(["student", "1", "test"])

        self.assertEqual(student.id, 1)
        self.assertEqual(student.name, "test")
        self.assertEqual(repo.get_students_count(), 1)
Ejemplo n.º 7
0
def run_filter_above_test():
    expenses = []
    services = Services(expenses)

    try:
        services.filter_above([""])
        assert False
    except:
        assert True

    try:
        services.filter_above(["abc"])
        assert False
    except:
        assert True

    services.filter_above(["0"])

    services.add(["8", "4", "transport"])
    assert len(expenses) == 1

    services.add(["7", "2", "transport"])
    assert len(expenses) == 2

    services.add(["6", "1", "transport"])
    assert len(expenses) == 3

    services.add(["5", "2", "cloathing"])
    assert len(expenses) == 4

    services.filter_above(["1"])
    assert len(expenses) == 3
Ejemplo n.º 8
0
    def test_search(self):
        services = Services()
        repo = services.repository

        services.add(["student", "1", "stud 1"])
        services.add(["student", "2", "stud 2"])
        services.add(["discipline", "1", "discipline"])

        self.assertEqual(repo.get_students_count(), 2)
        self.assertEqual(repo.get_disciplines_count(), 1)

        self.assertRaises(Exception, lambda: services.search([]))
        self.assertRaises(Exception, lambda: services.search([""]))
        self.assertRaises(Exception, lambda: services.search(["magic"]))

        self.assertNotIn('|', services.search(["stud"]))
        self.assertIn('|', services.search(["1"]))
Ejemplo n.º 9
0
    def test_disciplines_with_grades(self):
        services = Services()
        repo = services.repository

        services = Services()
        repo = services.repository

        services.add(["student", "1", "stud 1"])
        services.add(["student", "2", "stud 2"])
        services.add(["student", "3", "stud 3"])
        services.add(["discipline", "1", "discipline 1"])
        services.add(["discipline", "2", "discipline 2"])

        self.assertEqual(repo.get_students_count(), 3)
        self.assertEqual(repo.get_disciplines_count(), 2)

        self.assertRaises(Exception, lambda: services.see_grades())

        services.grade(["1", "1", "10"])
        services.grade(["2", "1", "10"])
        services.grade(["2", "1", "1"])
        services.grade(["2", "1", "4"])
        services.grade(["2", "2", "5"])
        services.grade(["3", "1", "1"])
        services.grade(["3", "1", "2"])
        services.grade(["3", "1", "3"])
        services.grade(["3", "1", "4"])
        services.grade(["3", "2", "5"])
        self.assertEqual(repo.get_grades_count(), 10)

        self.assertIn("\n", services.see_grades())
Ejemplo n.º 10
0
class Program:
    def __init__(self):
        self.__ui_manager = MenuUI()
        self.__DEBUGGING_MODE = True
        self.__services = Services()

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

        self.__services.preload_list()
        self.__ui_manager.show_help()

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

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

                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_ABOVE:
                    result = self.__services.filter_above(action.params)
                    self.__ui_manager.print_result(result)

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

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

                elif action.type == constants.ACTION_UNDO:
                    result = self.__services.undo_last_action()
                    self.__ui_manager.print_result(result)
            except ValidationError as error:
                self.__ui_manager.handle_error(error)
Ejemplo n.º 11
0
def run_undo_test():
    expenses = []
    services = Services(expenses)

    services.add(["4", "4", "food"])
    assert len(expenses) == 1

    services.add(["4", "8", "housekeeping"])
    assert len(expenses) == 2

    services.add(["3", "16", "housekeeping"])
    assert len(expenses) == 3

    services.add(["2", "32", "internet"])
    assert len(expenses) == 4

    services.filter_above(["10"])
    assert len(expenses) == 2

    services.undo_last_action()
    assert len(expenses) == 4

    services.undo_last_action()
    assert len(expenses) == 3

    services.undo_last_action()
    assert len(expenses) == 2

    services.undo_last_action()
    assert len(expenses) == 1

    services.undo_last_action()
    assert len(expenses) == 0

    try:
        services.undo_last_action()
        assert False
    except:
        assert True
Ejemplo n.º 12
0
    def test_list_all(self):
        services = Services()
        repo = services.repository

        self.assertRaises(Exception, lambda: services.list_all())

        services.add(["student", "0", "stud 1"])
        self.assertEqual(repo.get_students_count(), 1)

        self.assertNotIn('|', services.list_all())

        repo.clear()
        self.assertEqual(repo.get_students_count(), 0)
        services.add(["discipline", "1", "discipline 1"])
        self.assertEqual(repo.get_disciplines_count(), 1)

        self.assertNotIn('|', services.list_all())

        services.add(["student", "2", "stud 1"])
        self.assertEqual(repo.get_students_count(), 1)
        services.add(["student", "3", "stud 2"])
        self.assertEqual(repo.get_students_count(), 2)

        self.assertIn('|', services.list_all())
Ejemplo n.º 13
0
def run_add_test():
    expenses = []
    services = Services(expenses)

    try:
        services.add(["", "", ""])
        assert False
    except:
        assert True
    assert len(expenses) == 0
    
    try:
        services.add(["abc", "abc", "abc"])
        assert False
    except:
        assert True
    assert len(expenses) == 0

    try:
        services.add(["0", "0", "abc"])
        assert False
    except:
        assert True
    assert len(expenses) == 0

    try:
        services.add(["32", "abc", "abc"])
        assert False
    except:
        assert True
    assert len(expenses) == 0

    try:
        services.add(["30", "abc", "abc"])
        assert False
    except:
        assert True
    assert len(expenses) == 0

    services.add(["28", "64", "food"])
    assert len(expenses) == 1

    expense = expenses[0]
    assert expense.cost == 64
    assert expense.category == "food"
    assert expense.day == 28
Ejemplo n.º 14
0
    def test_redo(self):
        services = Services()
        repo = services.repository

        self.assertRaises(Exception, lambda: services.redo())

        services.add(["student", "1", "stud 1"])
        services.add(["student", "2", "stud 2"])
        services.add(["student", "3", "stud 3"])
        services.add(["discipline", "1", "discipline 1"])
        services.add(["discipline", "2", "discipline 2"])

        self.assertEqual(repo.get_students_count(), 3)
        self.assertEqual(repo.get_disciplines_count(), 2)
        self.assertEqual(repo.get_grades_count(), 0)

        self.assertRaises(Exception, lambda: services.redo())

        services.undo()
        services.undo()

        self.assertEqual(repo.get_students_count(), 3)
        self.assertEqual(repo.get_disciplines_count(), 0)
        self.assertEqual(repo.get_grades_count(), 0)

        services.redo()
        services.redo()

        self.assertEqual(repo.get_students_count(), 3)
        self.assertEqual(repo.get_disciplines_count(), 2)
        self.assertEqual(repo.get_grades_count(), 0)

        services.grade(["1", "1", "10"])
        services.grade(["2", "1", "10"])
        services.grade(["2", "1", "1"])
        services.grade(["2", "1", "4"])
        services.grade(["2", "2", "5"])
        services.grade(["3", "1", "1"])
        services.grade(["3", "1", "2"])
        services.grade(["3", "1", "3"])
        services.grade(["3", "2", "4"])
        services.grade(["3", "2", "5"])

        self.assertEqual(repo.get_students_count(), 3)
        self.assertEqual(repo.get_disciplines_count(), 2)
        self.assertEqual(repo.get_grades_count(), 10)

        services.remove(["discipline", "2"])

        self.assertEqual(repo.get_students_count(), 3)
        self.assertEqual(repo.get_disciplines_count(), 1)
        self.assertEqual(repo.get_grades_count(), 7)

        services.undo()

        self.assertEqual(repo.get_students_count(), 3)
        self.assertEqual(repo.get_disciplines_count(), 2)
        self.assertEqual(repo.get_grades_count(), 10)

        services.redo()

        self.assertEqual(repo.get_students_count(), 3)
        self.assertEqual(repo.get_disciplines_count(), 1)
        self.assertEqual(repo.get_grades_count(), 7)

        services.remove(["student", "3"])

        self.assertEqual(repo.get_students_count(), 2)
        self.assertEqual(repo.get_disciplines_count(), 1)
        self.assertEqual(repo.get_grades_count(), 4)

        for _ in range(13):
            services.undo()

        self.assertEqual(repo.get_students_count(), 3)
        self.assertEqual(repo.get_disciplines_count(), 1)
        self.assertEqual(repo.get_grades_count(), 0)

        services.redo()
        services.redo()

        self.assertEqual(repo.get_students_count(), 3)
        self.assertEqual(repo.get_disciplines_count(), 2)
        self.assertEqual(repo.get_grades_count(), 1)

        services.update(["discipline", "1", "test"])
        self.assertEqual(repo.get_disciplines_count(), 2)

        services.undo()
        self.assertEqual(repo.get_disciplines_count(), 2)

        discipline = repo.get_discipline(1)
        self.assertEqual(discipline.name, "discipline 1")

        services.redo()
        self.assertEqual(repo.get_disciplines_count(), 2)

        discipline = repo.get_discipline(1)
        self.assertEqual(discipline.name, "test")

        for _ in range(7):
            services.undo()

        self.assertTrue(repo.is_empty())

        services.preload_list()
        self.assertFalse(repo.is_empty())

        services.undo()
        self.assertTrue(repo.is_empty())

        services.redo()
        self.assertFalse(repo.is_empty())

        self.assertRaises(Exception, lambda: services.redo())
Ejemplo n.º 15
0
class Program:

    def __init__(self):
        self.__services = Services()
        self.__ui_manager = MenuUI()

    def change_ui(self):
         if self.__ui_manager.switch_ui_prefernece():
            if self.__ui_manager.TYPE == MenuUI.TYPE:
                self.__ui_manager = GraphicalUI()
            else:
                self.__ui_manager = MenuUI()

    def start(self):
        self.__services.preload_list()
        self.change_ui()

        while True:
            result = ""

            try:
                action = self.__ui_manager.get_action()

                if action.type == Action.ADD:
                    self.__services.add(action.params)

                elif action.type == Action.REMOVE:
                    self.__services.remove(action.params)

                elif action.type == Action.UPDATE:
                    self.__services.update(action.params)

                elif action.type == Action.LIST:
                    result = self.__services.list_all()

                elif action.type == Action.GRADE:
                    self.__services.grade(action.params)

                elif action.type == Action.SEARCH:
                    result = self.__services.search(action.params)

                elif action.type == Action.SEE_FAILING_STUDENTS:
                    result = self.__services.see_failing_students()

                elif action.type == Action.SEE_BEST_STUDENTS:
                    result = self.__services.see_best_students()

                elif action.type == Action.SEE_GRADES:
                    result = self.__services.see_grades()

                elif action.type == Action.UNDO:
                    self.__services.undo()

                elif action.type == Action.REDO:
                    self.__services.redo()

                elif action.type == Action.HELP:
                    self.__ui_manager.show_help()
                    continue

                elif action.type == Action.UI:
                    self.change_ui()
                    continue

                elif action.type == Action.TEST:
                    unittest.main(module = "services_test", exit = False) 
                    continue

                elif action.type == Action.EXIT:
                    return
                
                self.__ui_manager.print_result(result)
            except Exception as error:
                self.__ui_manager.handle_error(error)