Skip to content

Commit 3039044

Browse files
committed
Доработаны тесты
1 parent 7322fc6 commit 3039044

File tree

7 files changed

+156
-122
lines changed

7 files changed

+156
-122
lines changed

graph/contour_graph.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
from mpl_toolkits.mplot3d import Axes3D
32
import matplotlib.pyplot as plt
43

54
from graph.mpl_canvas import MlpCanvas

gui/mainwindow.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,19 +339,12 @@ def save_min_max(self):
339339
def draw_graph(self):
340340
self.parameters = self.read_parameters_function()
341341
# TODO: добавить комментарии
342-
# constraints_high, er = parser_field.parse_number_list(self.display_error_message,
343-
# self.ui.constraints_high.text(),
344-
# self.ui.constraints_x1_label.text())
345-
# constraints_down, er = parser_field.parse_number_list(self.display_error_message,
346-
# self.ui.constraints_down.text(),
347-
# self.ui.constraints_x2_label.text())
348342

349343
expr_x1 = self.ui.slice_expr_x1.text()
350344
expr_x2 = self.ui.slice_expr_x2.text()
351345

352346
# TODO: написать функцию validation для ограничений, != [], x[0]<x[1]
353347
if (self.parameters is not None) and (self.parameters.constraints_high != []) and (self.parameters.constraints_down != []):
354-
# if (constraints_high != []) and (constraints_down != []) and (self.parameters is not None):
355348
self.graph_3d = None
356349
self.contour_graph = None
357350
self.slice_graph_1 = None

tests/draw_graph_test.py

Lines changed: 25 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,16 @@
99

1010
# тестируемый метод draw_graph
1111
from gui.mainwindow import MainWindow
12+
from parameters import Parameters
1213

1314

1415
class Closeable:
1516
def close(self):
1617
print('closed')
1718

1819

19-
type_f = ["", "method_min", "hyperbolic_potential", "exponential_potential"]
20-
constraints = [[], [-6, 6]]
21-
params = [True, None]
22-
# constraints = [[], [-6], [-6, 6], [6, -6], [0, 0], [-1, 0, 1]]
23-
24-
25-
@pytest.fixture(params=type_f)
26-
def param_type_f(request):
27-
return request.param
28-
29-
30-
@pytest.fixture(params=constraints)
31-
def param_constraints(request):
32-
return request.param
33-
34-
35-
@pytest.fixture(params=params)
36-
def param_loop(request):
37-
return request.param
38-
39-
4020
class TestDrawGraph:
41-
# @patch('gui.mainwindow.MainWindow.create_layout_with_graph')
42-
# @patch('gui.mainwindow.MainWindow.delete_widget')
43-
# @patch('gui.mainwindow.MainWindow.display_error_message')
44-
def setup(self): # no_print_er, no_dw, no_create_graph
45-
# self.no_print_er = no_print_er
46-
# self.no_dw = no_dw
47-
# self.no_create_graph = no_create_graph
48-
21+
def setup(self):
4922
self.app = QApplication(sys.argv)
5023
self.mw = MainWindow()
5124
self.mw.show()
@@ -54,78 +27,46 @@ def teardown(self):
5427
with pytest.raises(SystemExit):
5528
with contextlib.closing(Closeable()):
5629
sys.exit()
57-
# sys.exit(self.app.exec_())
5830

5931
@patch('graph.graph_3d.Canvas3dGraph')
6032
@patch('graph.contour_graph.CanvasContourGraph')
6133
@patch('graph.slice_graph.CanvasSliceGraph')
6234
@patch('gui.mainwindow.MainWindow.create_layout_with_graph')
6335
@patch('gui.mainwindow.MainWindow.delete_widget')
64-
@patch('gui.mainwindow.MainWindow.display_error_message')
6536
@patch('gui.mainwindow.MainWindow.read_type')
66-
@patch('parser_field.parse_number_list')
6737
@patch('gui.mainwindow.MainWindow.read_parameters_function')
68-
@patch('gui.mainwindow.MainWindow.get_func')
69-
def test_draw_graph(self, no_func, no_rpf, no_parser_field, no_rt, no_print_er, no_dw, no_create_graph,
70-
no_slice, no_contour, no_3d,
71-
param_type_f, param_constraints, param_loop):
72-
print(param_type_f)
73-
print(param_constraints)
74-
75-
# замена возвращаемого функцией parser_field.parse_number_list значения
76-
no_parser_field.return_value = param_constraints
38+
def test_draw_graph(self, read_parameters_function, no_rt, no_dw, no_create_graph,
39+
no_slice, no_contour, no_3d):
7740

7841
# замена возвращаемого методом MainWindow.read_parameters_function значения
79-
no_rpf.return_value = param_loop
42+
coord = [[-2, 4], [0, 0], [4, 4], [4, 0], [-2, 0],[0, -2], [-4, 2], [2, -4], [2, 2], [-4, -2]]
43+
func_value = [0, 3, 5, 6, 7, 8, 9, 10, 11, 12]
44+
p = Parameters(1, "feldbaum_function", 10, coord, func_value,
45+
[[0.6, 1.6], [1.6, 2], [0.6, 0.6], [1.1, 1.8], [0.5, 0.5], [1.3, 1.3], [0.8, 1.2], [0.9, 0.3], [1.1, 1.7], [1.2, 0.5]],
46+
[[6, 6], [6, 7], [6, 7], [5, 5], [5, 5], [5, 5], [4, 3], [2, 4], [6, 4], [3, 3]],
47+
[6, 6], [-6, -6], [-2, 4], [6, 6], min_f=0, max_f=23)
48+
read_parameters_function.return_value = p
8049

8150
def f(): pass
82-
# def log(s): print(s)
8351

8452
no_dw.return_value = f
8553

86-
no_rt.return_value = param_type_f
87-
88-
no_func.return_value = f
54+
no_rt.return_value = "feldbaum_function"
8955

9056
no_create_graph.return_value = f
9157

9258
self.mw.draw_graph()
9359

94-
assert no_rpf.called, "метод read_parameters_function не был вызван"
95-
assert no_parser_field.called, "функция parser_field.parse_number_list не была вызвана"
96-
97-
if (not param_constraints) or (param_loop is None): # param_constraints == []
98-
no_print_er.assert_called_once_with("Что-то пошло не так")
99-
else:
100-
calls = [call(self.mw.ui.v_box_3d_graph), call(self.mw.ui.v_box_contour_graph),
101-
call(self.mw.ui.v_box_slice_graph1), call(self.mw.ui.v_box_slice_graph2)]
102-
no_dw.assert_has_calls(calls)
103-
104-
assert no_rt.called, "метод read_type не вызван"
105-
if param_type_f: # param_type_f != ""
106-
no_func.assert_called_once_with(param_type_f)
107-
no_create_graph.assert_called()
108-
# assert no_3d in no_create_graph.call_args_list, "некорректный вызов метода create_layout_with_graph"
109-
no_create_graph.assert_called()
110-
# assert no_contour in no_create_graph.call_args_list, "некорректный вызов метода create_layout_with_graph"
111-
112-
# self.no_create_graph.assert_called()
113-
# assert no_slice in self.no_create_graph.call_args_list, "некорректный вызов метода create_layout_with_graph"
114-
# self.no_create_graph.assert_called()
115-
# assert no_slice in self.no_create_graph.call_args_list, "некорректный вызов метода create_layout_with_graph"
116-
else:
117-
no_print_er.assert_called_once_with("Выберите метод конструирования тестовой функции")
118-
119-
120-
# data = [('', {'uid': ['happy times']})]
121-
# search_s = Mock(return_value=data)
122-
# no_ldap.return_value = Mock(search_s=search_s)
123-
# count = 0
124-
# for i in find_users('', '', '', ''):
125-
# count += 1
126-
# assert i=='happy times'
127-
# assert count == 1
128-
129-
130-
# if __name__ == '__main__':
131-
# unittest.main(verbosity=2)
60+
assert read_parameters_function.called, "метод read_parameters_function не был вызван"
61+
62+
calls = [call(self.mw.ui.v_box_3d_graph), call(self.mw.ui.v_box_contour_graph),
63+
call(self.mw.ui.v_box_slice_graph1), call(self.mw.ui.v_box_slice_graph2)]
64+
no_dw.assert_has_calls(calls)
65+
66+
assert no_rt.called, "метод read_type не вызван"
67+
68+
for i in range(len(func_value)):
69+
assert self.mw.func(coord[i]) == func_value[i]
70+
71+
no_create_graph.assert_called()
72+
no_create_graph.assert_called()

tests/func_test.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import contextlib
2+
3+
import pytest
4+
5+
import sys
6+
7+
from PyQt5.QtWidgets import QApplication
8+
from mock import patch, call
9+
10+
# тестируемый метод draw_graph
11+
from gui.mainwindow import MainWindow
12+
13+
14+
class Closeable:
15+
def close(self):
16+
print('closed')
17+
18+
19+
type_f = ["", "method_min", "hyperbolic_potential", "exponential_potential"]
20+
constraints = [[], [-6, -6]]
21+
params = [True, None]
22+
# constraints = [[], [-6], [-6, 6], [6, -6], [0, 0], [-1, 0, 1]]
23+
24+
25+
@pytest.fixture(params=type_f)
26+
def param_type_f(request):
27+
return request.param
28+
29+
30+
@pytest.fixture(params=constraints)
31+
def param_constraints(request):
32+
return request.param
33+
34+
35+
@pytest.fixture(params=params)
36+
def param_loop(request):
37+
return request.param
38+
39+
40+
@pytest.mark.skip("Не доделан")
41+
class TestDrawGraph:
42+
def setup(self):
43+
self.app = QApplication(sys.argv)
44+
self.mw = MainWindow()
45+
self.mw.show()
46+
47+
def teardown(self):
48+
with pytest.raises(SystemExit):
49+
with contextlib.closing(Closeable()):
50+
sys.exit()
51+
52+
@patch('graph.graph_3d.Canvas3dGraph')
53+
@patch('graph.contour_graph.CanvasContourGraph')
54+
@patch('graph.slice_graph.CanvasSliceGraph')
55+
@patch('gui.mainwindow.MainWindow.create_layout_with_graph')
56+
@patch('gui.mainwindow.MainWindow.delete_widget')
57+
@patch('gui.mainwindow.MainWindow.display_error_message')
58+
@patch('gui.mainwindow.MainWindow.read_type')
59+
@patch('gui.mainwindow.MainWindow.read_parameters_function')
60+
@patch('gui.mainwindow.MainWindow.get_func')
61+
def test_draw_graph(self, no_func, no_rpf, no_rt, no_print_er, no_dw, no_create_graph,
62+
no_slice, no_contour, no_3d,
63+
param_type_f, param_constraints, param_loop):
64+
print(param_type_f)
65+
print(param_constraints)
66+
67+
# замена возвращаемого методом MainWindow.read_parameters_function значения
68+
no_rpf.return_value = param_loop
69+
70+
def f(): pass
71+
72+
no_dw.return_value = f
73+
74+
no_rt.return_value = param_type_f
75+
76+
no_func.return_value = f
77+
78+
no_create_graph.return_value = f
79+
80+
self.mw.draw_graph()
81+
82+
assert no_rpf.called, "метод read_parameters_function не был вызван"
83+
84+
if (not param_constraints) or (param_loop is None): # param_constraints == []
85+
no_print_er.assert_called_once_with("Что-то пошло не так")
86+
else:
87+
calls = [call(self.mw.ui.v_box_3d_graph), call(self.mw.ui.v_box_contour_graph),
88+
call(self.mw.ui.v_box_slice_graph1), call(self.mw.ui.v_box_slice_graph2)]
89+
no_dw.assert_has_calls(calls)
90+
91+
assert no_rt.called, "метод read_type не вызван"
92+
if param_type_f: # param_type_f != ""
93+
no_func.assert_called_once_with(param_type_f)
94+
no_create_graph.assert_called()
95+
no_create_graph.assert_called()
96+
else:
97+
no_print_er.assert_called_once_with("Выберите метод конструирования тестовой функции")
98+
99+
100+

tests/make_data_slice_graph_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def teardown(self):
3030
pass
3131

3232
def test_make_data(self, expression_loop):
33-
x, y = self.obj.make_data(self.constraints, self.constraints, mock_func, expression_loop)
33+
x, y = self.obj.make_data([6, 6], [-6, -6], mock_func, expression_loop)
3434
print(x)
3535
print(y)
3636
if expression_loop in ["x1", "x2"]:

tests/unut_tests/parser_test.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,35 @@ def setUp(self):
2525
"""
2626
def test_parse_number_list_empty_str(self):
2727
errors = []
28-
res = pf.parse_number_list(lambda x: errors.append(x), "", self.field_name)
29-
self.assertEqual(errors[0], "Поле \"" + self.field_name + "\" не заполнено")
28+
res, er = pf.parse_number_list("", self.field_name)
29+
self.assertEqual(er, "Поле \"" + self.field_name + "\" не заполнено")
3030
self.assertEqual(res, [])
3131

3232
def test_parse_number_list_int(self):
33-
res = pf.parse_number_list(None, "-1, 2, -3, 4, -5", self.field_name)
33+
res, er = pf.parse_number_list("-1, 2, -3, 4, -5", self.field_name)
3434
# self.assertEqual(errors[0], "Поле \"" + self.field_name + "\" не заполнено")
3535
self.assertEqual(res, [-1, 2, -3, 4, -5])
3636

3737
def test_parse_number_list_float(self):
38-
res = pf.parse_number_list(None, "1.185628563, 2.3, -3.0, 4.617, -5.0234820", self.field_name)
38+
res, er = pf.parse_number_list("1.185628563, 2.3, -3.0, 4.617, -5.0234820", self.field_name)
3939
# self.assertEqual(errors[0], "Поле \"" + self.field_name + "\" не заполнено")
4040
self.assertEqual(res, [1.185628563, 2.3, -3.0, 4.617, -5.0234820])
4141

4242
def test_parse_number_list_str(self):
43-
res = pf.parse_number_list(None, "п, 2.3, -3.0, 4.6f7, -5.02sdf20", self.field_name)
43+
res, er = pf.parse_number_list("п, 2.3, -3.0, 4.6f7, -5.02sdf20", self.field_name)
4444
# self.assertEqual(errors[0], "Поле \"" + self.field_name + "\" не заполнено")
45-
self.assertEqual(res, "Поле \"" + self.field_name + "\" заполнено некорректно")
45+
self.assertEqual(er, "Поле \"" + self.field_name + "\" заполнено некорректно")
4646

4747
# parse_number(number, field_name: str, print_error) -> int
4848
def test_parse_number_ok(self):
49-
res = pf.parse_number(1, self.field_name, None)
49+
res, er = pf.parse_number(1, self.field_name)
5050
# self.assertEqual(errors[0], "Поле \"" + self.field_name + "\" не заполнено")
5151
self.assertEqual(res, 1)
5252

5353
def test_parse_number_fail(self):
5454
errors = []
55-
res = pf.parse_number(0, self.field_name, lambda x: errors.append(x))
56-
self.assertEqual(errors[0], "Поле \"" + self.field_name + "\" заполнено некорректно")
55+
res, er = pf.parse_number(0, self.field_name)
56+
self.assertEqual(er, "Поле \"" + self.field_name + "\" заполнено некорректно")
5757
self.assertEqual(res, 0)
5858

5959
"""
@@ -67,18 +67,18 @@ def test_parse_number_fail(self):
6767
"""
6868
def test_parse_coordinates_empty_str(self):
6969
errors = []
70-
res = pf.parse_coordinates("", lambda x: errors.append(x))
71-
self.assertEqual(errors[0], "Поле \"Координаты экстремумов\" не заполнено")
70+
res, er = pf.parse_coordinates("")
71+
self.assertEqual(er, "Поле \"Координаты экстремумов\" не заполнено")
7272
self.assertEqual(res, [])
7373

7474
def test_parse_coordinates_str(self):
7575
errors = []
76-
res = pf.parse_coordinates("[1, 2], [d, g], [9, 0o]", lambda x: errors.append(x))
77-
self.assertEqual(errors[0], "Поле \"Координаты экстремумов\" заполнено некорректно")
76+
res, er = pf.parse_coordinates("[1, 2], [d, g], [9, 0o]")
77+
self.assertEqual(er, "Поле \"Координаты экстремумов\" заполнено некорректно")
7878
self.assertEqual(res, [])
7979

8080
def test_parse_coordinates_ok(self):
81-
res = pf.parse_coordinates("[1, 2], [8, 3], [9, 0]", None)
81+
res, er = pf.parse_coordinates("[1, 2], [8, 3], [9, 0]")
8282
self.assertEqual(res, [[1, 2], [8, 3], [9, 0]])
8383

8484

0 commit comments

Comments
 (0)