def test_correct_set_value(self):
        model = PrimeNumberViewModel()
        model.set_start_value(1)
        model.set_end_value(10)

        self.assertEqual(1, model.get_start_value())
        self.assertEqual(10, model.get_end_value())
 def test_model_perform_logging(self):
     model = PrimeNumberViewModel()
     model.set_start_value(2)
     model.set_end_value(10)
     model.perform()
     self.assertEqual('Result = [2, 3, 5, 7]',
                      model.logger.get_last_message())
    def test_input_wrong_data(self):
        model = PrimeNumberViewModel()
        model.set_start_value(10)
        model.set_end_value(2)
        model.perform()

        wrong_message = 'Что-то пошло не так.\nВозможно первое число оказалось больше второго'
        self.assertEqual(wrong_message, model.logger.get_last_message())
    def test_model_perform_all_message_logging(self):
        model = PrimeNumberViewModel()
        model.set_start_value(2)
        model.set_end_value(10)
        model.perform()

        log_message = [
            'Welcome!', 'Setting start value - 2', 'Setting end value - 10',
            'Button clicked', 'Interval = range(2, 10)',
            'Result = [2, 3, 5, 7]'
        ]
        self.assertEqual(log_message, model.logger.get_log_messages()[-6:])
Exemplo n.º 5
0
class GUIView:
    view_model = PrimeNumberViewModel()

    def __init__(self):
        self.root = tk.Tk()
        self.root['bg'] = '#fafafa'
        self.root.title('Печать всех простых чисел')
        self.root.geometry('800x400')
        self.root.resizable(width=True, height=True)

        self.frame = tk.Frame(self.root, bg='#2b4a54')
        self.title = tk.Label(self.frame,
                              text='Введите начало и конец промежутка',
                              bg='#619eb3',
                              font=40)
        self.input_start_value = tk.Entry(self.frame,
                                          bg='white',
                                          state='normal')
        self.input_end_value = tk.Entry(self.frame, bg='white')
        self.btn = tk.Button(self.frame,
                             text='Вывод простых чисел',
                             bg='#6dcae8')
        self.res = tk.Label(self.frame,
                            text='Здесь будет результат',
                            bg='#619eb3',
                            font=40)
        self.error_field = tk.Label(self.frame,
                                    text='',
                                    fg='red',
                                    bg='#619eb3',
                                    font=('', 12, 'bold'))
        self.logger_field = tk.Label(self.frame,
                                     text='Log:',
                                     bg='#619eb3',
                                     font=6)

        self.set_weight_to_grid()
        self.bind_events()

        self.mvvm_bind()
        self.mvvm_back_bind()
        self.root.mainloop()

    def set_weight_to_grid(self):
        self.root.grid_rowconfigure(2, minsize=30)
        self.root.grid_rowconfigure(5, minsize=30, weight=60)
        self.frame.place(relx=0.05, rely=0.05, relwidth=0.9, relheight=0.9)
        self.title.grid(row=0, column=0, columnspan=3, stick='we', padx=5)
        self.input_start_value.grid(row=1,
                                    column=0,
                                    stick='wens',
                                    padx=5,
                                    pady=5)
        self.input_end_value.grid(row=1,
                                  column=2,
                                  stick='wens',
                                  padx=5,
                                  pady=5)
        self.btn.grid(row=2, column=1, stick='wens')
        self.res.grid(row=4, column=0, columnspan=3, stick='we', padx=5)
        self.error_field.grid(row=5,
                              column=0,
                              columnspan=3,
                              stick='we',
                              padx=5)
        self.logger_field.grid(row=0, column=3, stick='ns', rowspan=30)

    def bind_events(self):
        self.input_start_value.bind('<KeyRelease>', self.first_input_changed)
        self.input_end_value.bind('<KeyRelease>', self.second_input_changed)
        self.btn.bind('<Button-1>', self.btn_clicked)

    def btn_clicked(self, event):
        self.mvvm_bind()
        self.view_model.perform()
        self.mvvm_back_bind()

    def first_input_changed(self, event):
        self.mvvm_bind()
        self.mvvm_back_bind()

    def second_input_changed(self, event):
        self.mvvm_bind()
        self.mvvm_back_bind()

    def mvvm_bind(self):
        self.view_model.set_start_value(self.input_start_value.get())
        self.view_model.set_end_value(self.input_end_value.get())

    def mvvm_back_bind(self):
        self.input_start_value.delete(0, tk.END)
        self.input_start_value.insert(tk.END,
                                      self.view_model.get_start_value())

        self.input_end_value.delete(0, tk.END)
        self.input_end_value.insert(tk.END, self.view_model.get_end_value())

        self.btn.config(state=self.view_model.is_button_enable())

        self.res.config(text='%s\n' %
                        (get_result_text(self.view_model.get_result())))
        self.error_field.config(
            text='%s\n' %
            (get_error_text(self.view_model.get_error_message())))
        self.logger_field.config(
            text='%s\n' %
            (get_log_messages(self.view_model.logger.get_log_messages())))
 def test_by_default_button_is_disable(self):
     model = PrimeNumberViewModel()
     self.assertEqual('disabled', model.is_button_enable())
    def test_get_interval_if_no_values(self):
        model = PrimeNumberViewModel()
        model.perform()

        self.assertEqual('', model.get_interval_label())
    def test_get_interval(self):
        model = PrimeNumberViewModel(1, 10)
        model.perform()

        self.assertEqual('range(1, 10)', model.get_interval_label())
 def test_when_first_value_is_bigger_than_second(self):
     model = PrimeNumberViewModel(10, 1)
     model.perform()
     error_message = 'Что-то пошло не так.\nВозможно первое число оказалось больше второго'
     self.assertEqual(error_message, model.get_error_message())
     self.assertIsNone(model.get_result())
 def test_when_enter_incorrect_values(self):
     model = PrimeNumberViewModel('asd', '11a')
     self.assertEqual('disabled', model.is_button_enable())
    def test_when_enter_values_display_corrected_list(self):
        model = PrimeNumberViewModel(1, 10)
        model.perform()
        answer = [2, 3, 5, 7]

        self.assertEqual(answer, model.get_result())
    def test_when_clear_second_number_button_disabled(self):
        model = PrimeNumberViewModel(1, 10)
        model.set_end_value(None)

        self.assertEqual('disabled', model.is_button_enable())
 def test_when_enter_both_number_button_enabled(self):
     model = PrimeNumberViewModel(1, 10)
     self.assertTrue(model.is_button_enable())
 def test_set_end_value_logging(self):
     model = PrimeNumberViewModel()
     model.set_end_value(10)
     self.assertEqual('Setting end value - 10',
                      model.logger.get_last_message())
 def test_set_start_value_logging(self):
     model = PrimeNumberViewModel()
     model.set_start_value(2)
     self.assertEqual('Setting start value - 2',
                      model.logger.get_last_message())
 def test_init_logging(self):
     model = PrimeNumberViewModel(1, 10)
     self.assertEqual('Welcome!', model.logger.get_last_message())