def test_run_dijkstra_with_incorrect_weights_return_error(self): model = DAViewModel() model.set_num_vertex('3') model.set_start_vertex('2') matrix = [['0', '5', '1'], ['2', '0', '3'], ['1', '3', '0']] model.run_dijkstra(matrix) self.assertEqual(model.get_error(), "Matrix is not symmetric")
def test_run_dijkstra_with_incorrect_start_vertex_return_error(self): model = DAViewModel() model.set_num_vertex('3') model.set_start_vertex('-2') matrix = [['0', '2', '1'], ['2', '0', '3'], ['1', '3', '0']] model.run_dijkstra(matrix) self.assertEqual(model.get_error(), "Wrong value of source vertex")
def test_run_dijkstra_return_correct_result(self): model = DAViewModel() model.set_num_vertex('3') model.set_start_vertex('2') matrix = [['0', '2', '1'], ['2', '0', '3'], ['1', '3', '0']] self.assertEqual(model.run_dijkstra(matrix), [1, 3, 0])
class GUIView: view_model = DAViewModel() def __init__(self): self.root = tk.Tk() self.root['bg'] = 'snow' self.root.title('Алгоритм Дейкстры') self.root.geometry('630x400') self.frame = tk.Frame(self.root, bg='snow') self.lbl_num_vertex = tk.Label(self.root, text="vertex count:") self.input_num_vertex = tk.Entry(self.root, width=3) self.input_graph = tk.Button(self.root, text="create graph", state='disabled') self.errors = tk.Label(self.root, text="", bg='snow') self.lbl_start_vertex = tk.Label(self.root, text="start vertex:") self.input_start_vertex = tk.Entry(self.root, width=3) self.run = tk.Button(self.root, text="run dijksta") self.entries = [] self.set_weight_to_grid() self.bind_events() self.root.mainloop() def set_weight_to_grid(self): self.lbl_num_vertex.grid(row=0, column=0) self.input_num_vertex.grid(row=0, column=1) self.input_graph.grid(row=1, column=0) self.lbl_start_vertex.grid(row=0, column=2) self.input_start_vertex.grid(row=0, column=3) self.errors.grid(row=1, column=2, columnspan=3) self.run.grid(row=1, column=1) self.frame.place(relx=0.2, rely=0.2, relwidth=0.6, relheight=0.6) def bind_events(self): self.input_num_vertex.bind('<KeyRelease>', self.num_vertex_changed) self.input_start_vertex.bind('<KeyRelease>', self.start_vertex_changes) self.input_graph.bind('<Button-1>', self.input_graph_clicked) self.run.bind('<Button-1>', self.run_clicked) def num_vertex_changed(self, event): self.mvvm_bind() self.mvvm_back_bind() def start_vertex_changes(self, event): self.mvvm_bind() self.mvvm_back_bind() def input_graph_clicked(self, event): self.mvvm_bind() self.draw_table() self.mvvm_back_bind() def run_clicked(self, event): self.mvvm_bind() self.draw_result(self.view_model.run_dijkstra(self.read_weights())) self.mvvm_back_bind() def draw_table(self): if self.view_model.get_btn_create_graph_state() == 'disabled': return self.frame.destroy() self.frame = self.frame = tk.Frame(self.root, bg='snow') self.frame.place(relx=0.2, rely=0.2, relwidth=0.6, relheight=0.6) self.entries = [] rows = int(self.input_num_vertex.get()) cols = int(self.input_num_vertex.get()) header = tk.Label(self.frame, text="Вершины", bg='snow') header.grid(row=0, column=0) for column_id in range(cols): header = tk.Label(self.frame, text=str(column_id), bg='snow') header.grid(row=0, column=column_id + 1) for row_id in range(rows): header = tk.Label(self.frame, text=str(row_id), bg='snow') header.grid(row=row_id + 1, column=0) self.entries.append([]) for column_id in range(cols): self.entries[row_id].append(tk.Entry(self.frame, width=3)) self.entries[row_id][column_id].grid(row=row_id + 1, column=column_id + 1) def draw_result(self, result): rows = int(self.input_num_vertex.get()) lbl_result = tk.Label(self.frame, text="dists", bg='snow') lbl_result.grid(row=rows + 1, column=0) for column_id, dist in enumerate(result): dist = tk.Label(self.frame, text=str(dist), bg='snow') dist.grid(row=rows + 1, column=column_id + 1) def read_weights(self): rows = int(self.input_num_vertex.get()) cols = int(self.input_num_vertex.get()) matrix = [] for i in range(rows): matrix.append([]) for j in range(cols): matrix[i].append(self.entries[i][j].get()) return matrix def mvvm_bind(self): self.view_model.set_num_vertex(self.input_num_vertex.get()) self.view_model.set_start_vertex(self.input_start_vertex.get()) def mvvm_back_bind(self): self.input_num_vertex.delete(0, tk.END) self.input_num_vertex.insert(tk.END, self.view_model.get_num_vertex()) self.input_graph.config(state=self.view_model.get_btn_create_graph_state()) self.input_start_vertex.delete(0, tk.END) self.input_start_vertex.insert(tk.END, self.view_model.get_start_vertex()) self.errors.config(text='{}\n'.format(self.view_model.get_error()))
def test_by_default_button_create_graph_disable(self): model = DAViewModel() self.assertEqual('disabled', model.get_btn_create_graph_state())
def test_can_get_vertex_number(self): model = DAViewModel() model.set_num_vertex('5') self.assertEqual('5', model.get_num_vertex())
def test_can_set_get_start_vertex(self): model = DAViewModel() model.set_start_vertex('0') self.assertEqual('0', model.get_start_vertex())
def test_when_enter_less_zero_number_button_create_graph_disabled(self): model = DAViewModel() model.set_num_vertex('-1') self.assertEqual('disabled', model.get_btn_create_graph_state())
def test_when_enter_incorrect_number_button_create_graph_keeps_disabled( self): model = DAViewModel() model.set_num_vertex('some incorrect input') self.assertEqual('disabled', model.get_btn_create_graph_state())
def test_when_clear_number_button_create_graph_disabled(self): model = DAViewModel() model.set_num_vertex(None) self.assertEqual('disabled', model.get_btn_create_graph_state())
def test_when_enter_correct_number_button_create_graph_enabled(self): model = DAViewModel() model.set_num_vertex('5') self.assertEqual('normal', model.get_btn_create_graph_state())
def setUp(self): self.view_model = DAViewModel(FakeLogger())
class TestViewModelFakeLogging(unittest.TestCase): def setUp(self): self.view_model = DAViewModel(FakeLogger()) def test_logging_init(self): self.assertEqual('Welcome!', self.view_model.logger.get_last_message()) def test_logging_enable_set_graph_button(self): self.view_model.set_btn_create_graph_enabled() self.assertEqual('Can create graph', self.view_model.logger.get_last_message()) def test_logging_disable_set_graph_button(self): self.view_model.set_btn_create_graph_disabled() self.assertEqual('Can\'t create graph', self.view_model.logger.get_last_message()) def test_logging_set_start_vertex(self): self.view_model.set_start_vertex('322') self.assertEqual('Result from setting start vertex: 322', self.view_model.logger.get_last_message()) def test_logging_set_correct_vertex_number(self): self.view_model.set_num_vertex('5') self.assertEqual('Result from setting number of vertex: 5', self.view_model.logger.get_last_message()) def test_logging_set_incorrect_vertex_number(self): self.view_model.set_num_vertex('Z') self.assertEqual('Entered value isn\'t correct!', self.view_model.logger.get_last_message()) def test_logging_run_dijkstra_return_correct_result(self): self.view_model.set_num_vertex('3') self.view_model.set_start_vertex('2') matrix = [['0', '2', '1'], ['2', '0', '3'], ['1', '3', '0']] self.view_model.run_dijkstra(matrix) self.assertEqual('Dijkstra algorithm successfully completed', self.view_model.logger.get_last_message()) def test_logging_run_dijkstra_return_error(self): self.view_model.set_num_vertex('2') self.view_model.set_start_vertex('0') matrix = [['A', 'B'], ['C', 'D']] self.view_model.run_dijkstra(matrix) self.assertEqual('Error: invalid literal for int() with base 10: \'A\'', self.view_model.logger.get_last_message())
def setUp(self): self.view_model = DAViewModel(RealLogger())