示例#1
0
    def test_remove_student(self):
        controller = Controller()
        services = controller.services

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

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

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

        self.assertEqual(services.get_students_count(), 1)
        self.assertEqual(services.get_disciplines_count(), 1)
        self.assertEqual(services.get_grades_count(), 2)
        self.assertRaises(Exception,
                          lambda: controller.remove(["student", "2"]))
class TestController(TestCase):
    def setUp(self):
        super().setUp()
        self.repo = Repo(
            SentenceValidator,
            "/Users/Stefan/PycharmProjects/hangman/sentence_test.txt")
        self.controller = Controller(self.repo)

    def test_add(self):
        self.assertEqual(len(self.repo.get_all()), 3, "Error at loading")
        self.controller.add("planes are quick")
        self.assertEqual(len(self.repo.get_all()), 4, "Error at adding")
        self.assertRaises(ControllerException, self.controller.add,
                          "planes are quick")

    def test_get_random_sentence(self):
        random_sentence = self.controller.get_random_sentence()
        self.assertEqual(type(random_sentence), Sentence,
                         "Fail at getting random")

    def test_get_available_letters(self):
        sentence = Sentence("anna has apples")
        char_list = self.controller.get_available_letters(sentence)
        self.assertEqual(True, 'a' in char_list, "fail at getting letter")
        self.assertEqual(True, 'h' in char_list, "fail at getting letter")
        self.assertEqual(True, 's' in char_list, "fail at getting letter")
示例#3
0
    def test_add_discipline(self):
        controller = Controller()
        services = controller.services

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

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

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

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

        self.assertNotEqual(discipline1, discipline2)
示例#4
0
    def test_update_discipline(self):
        controller = Controller()
        services = controller.services

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

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

        self.assertEqual(discipline.id, 1)
        self.assertEqual(discipline.name, "test")
        self.assertEqual(services.get_disciplines_count(), 1)
示例#5
0
    def test_grade(self):
        controller = Controller()
        services = controller.services

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

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

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

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

        self.assertEqual(services.get_grades_count(), 3)
示例#6
0
    def test_add_student(self):
        controller = Controller()
        services = controller.services

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

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

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

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

        self.assertNotEqual(student1, student2)
示例#7
0
    def test_update_student(self):
        controller = Controller()
        services = controller.services

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

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

        self.assertEqual(student.id, 1)
        self.assertEqual(student.name, "test")
        self.assertEqual(services.get_students_count(), 1)
示例#8
0
    def test_search(self):
        controller = Controller()
        services = controller.services

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

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

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

        self.assertNotIn('|', controller.search(["stud"]))
        self.assertIn('|', controller.search(["1"]))
示例#9
0
    def test_disciplines_with_grades(self):
        controller = Controller()
        services = controller.services

        controller = Controller()
        services = controller.services

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

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

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

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

        self.assertIn("\n", controller.see_grades())
示例#10
0
文件: t.py 项目: fans656/edi
    def newDocument(self):
        document = Document()
        document.text = text

        view = View()
        view.font = self.font()
        view.cursorBlinkingTime = 1000
        view.position = QPoint(0, 0)
        view.size = self.size()

        controller = Controller()
        controller.add(view)

        # if a view is relink with another document
        # then it can finds its old controller from old document
        # and new controller from new document
        controller.document = document
        document.controller = controller

        self.documents.append(document)
        self.views.append(view)
        self.controllers.append(controller)
示例#11
0
    def test_list_all(self):
        controller = Controller()
        services = controller.services

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

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

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

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

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

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

        self.assertIn('|', controller.list_all())
示例#12
0
 def pushButtonAddHandler(self):
     if not (self.textEditIn.toPlainText() is ''):
         contrl = Controller()
         res = contrl.add(self.textEditIn.toPlainText())
         self.textEditIn.clear()
         self.textEditIn.setPlainText(str(res))
示例#13
0
class LEDStrip:
    def __init__(self, r, g, b):
        self.ctr = Controller()
        self.pi = pigpio.pi()
        self.r = Pin(self.pi, RED_PIN, self.ctr, 'r', r)
        self.g = Pin(self.pi, GREEN_PIN, self.ctr, 'g', g)
        self.b = Pin(self.pi, BLUE_PIN, self.ctr, 'b', b)
        self.set_color(r, g, b)

    def set_color(self, r, g, b):
        self.ctr.stop_all()

        self.r.set_intensity(r)
        self.g.set_intensity(g)
        self.b.set_intensity(b)

    def get_color(self):
        return {
            'r': self.r.intensity,
            'g': self.g.intensity,
            'b': self.b.intensity,
        }

    def off(self, fade_out=False):
        c = self.get_color()
        if fade_out:
            self.ctr.stop_all()
            self.shift((c['r'], c['g'], c['b']), (0, 0, 0), 2)
        else:
            self.set_color(0, 0, 0)
            self.ctr.stop_all()

    def on(self, r, g, b, fade_in=False):
        if fade_in:
            self.ctr.stop_all()
            self.shift((0, 0, 0), (r, g, b), 2)
        else:
            self.set_color(r, g, b)

    # min/max: e.g. Tuple(255, 255, 255)
    def shift(self, min, max, duration, cont=False):
        print(f"Shifting between {min} and {max}, period={duration}s")
        min_r = min[0]
        max_r = max[0]
        min_g = min[1]
        max_g = max[1]
        min_b = min[2]
        max_b = max[2]

        self.ctr.add('r', self.r.shift, min_r, max_r, duration, cont)
        self.ctr.add('g', self.g.shift, min_g, max_g, duration, cont)
        self.ctr.add('b', self.b.shift, min_b, max_b, duration, cont)

        self.ctr.start_all()

    def _print_colors(self, verbose=False):
        # FIXME: mess
        while not (
            self.ctr.get_thread('r').stopped() and
            self.ctr.get_thread('g').stopped() and
            self.ctr.get_thread('b').stopped()):

            log_str = '#'
            end_str = ''
            if verbose:
                log_str = f"R: {self.r.intensity} G: {self.g.intensity} B: {self.b.intensity}"
                end_str = '\r'

            print(Colr().rgb(self.r.intensity, self.g.intensity, self.b.intensity, log_str), end=end_str, flush=True)
        self.ctr.get_thread('log').stop()
示例#14
0
    def test_redo(self):
        controller = Controller()
        services = controller.services

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

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

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

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

        controller.undo()
        controller.undo()

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

        controller.redo()
        controller.redo()

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

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

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

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

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

        controller.undo()

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

        controller.redo()

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

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

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

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

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

        controller.redo()
        controller.redo()

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

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

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

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

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

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

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

        self.assertTrue(services.is_empty())

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

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

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

        self.assertRaises(Exception, lambda: controller.redo())
示例#15
0
class Program:
    def __init__(self):
        self.__controller = Controller()
        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.__controller.preload_list()
        self.change_ui()

        while True:
            result = ""

            try:
                action = self.__ui_manager.get_action()

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

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

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

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

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

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

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

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

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

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

                elif action.type == Action.REDO:
                    self.__controller.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="controller_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)