Skip to content

Commit

Permalink
extend tests to check return of vars
Browse files Browse the repository at this point in the history
  • Loading branch information
MialLewis committed Sep 20, 2023
1 parent cd094bd commit 2737647
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/mslice/scripting/helperfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ def add_cut_lines(script_lines, plot_handler, ax):
cuts = plot_handler._cut_plotter_presenter._cut_cache_dict[ax]
errorbars = plot_handler._canvas.figure.gca().containers
intensity_correction = plot_handler.intensity_type
return_cut_vars = add_cut_lines_with_width(errorbars, script_lines, cuts, intensity_correction)
return_ws_vars = add_cut_lines_with_width(errorbars, script_lines, cuts, intensity_correction)
hide_lines(script_lines, plot_handler, ax)
return return_cut_vars
return return_ws_vars


def hide_lines(script_lines, plot_handler, ax):
Expand Down
22 changes: 17 additions & 5 deletions tests/scripting_helperfunctions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ def test_that_add_cut_plot_statements_works_as_expected(self, gfm, add_cut_lines
fig = mock.MagicMock()
ax = fig.add_subplot(111, projection='mslice')

add_cut_plot_statements(script_lines, plot_handler, ax)
add_cut_lines.return_value = ['test_ws_var']

ret_val = add_cut_plot_statements(script_lines, plot_handler, ax)

add_cut_lines.assert_called_once_with(script_lines, plot_handler, ax)
add_plot_options.assert_called_once_with(script_lines, plot_handler)
Expand All @@ -242,6 +244,7 @@ def test_that_add_cut_plot_statements_works_as_expected(self, gfm, add_cut_lines
plot_handler.x_axis_min), script_lines)
self.assertIn("ax.set_yscale('symlog', linthresh=pow(10, np.floor(np.log10({}))))\n".format(
plot_handler.y_axis_min), script_lines)
self.assertEqual(['test_ws_var'], ret_val)

@mock.patch('mslice.scripting.helperfunctions.add_cut_lines_with_width')
@mock.patch('mslice.cli._mslice_commands.GlobalFigureManager')
Expand All @@ -253,14 +256,17 @@ def test_that_add_cut_lines_works_as_expected(self, gfm, add_cut_lines_with_widt
plot_handler.add_mock_spec(CutPlot)
plot_handler.intensity_type = IntensityType.SCATTERING_FUNCTION

add_cut_lines_with_width.return_value = ['test_ws_var']

script_lines = []

add_cut_lines(script_lines, plot_handler, ax)
ret_val = add_cut_lines(script_lines, plot_handler, ax)

cuts = plot_handler._cut_plotter_presenter._cut_cache_dict[ax]
errorbars = plot_handler._canvas.figure.gca().containers

add_cut_lines_with_width.assert_called_once_with(errorbars, script_lines, cuts, plot_handler.intensity_type)
self.assertEqual(['test_ws_var'], ret_val)

@mock.patch('mslice.cli._mslice_commands.GlobalFigureManager')
def test_that_add_plot_options_works_as_expected(self, gfm):
Expand Down Expand Up @@ -307,7 +313,8 @@ def test_that_add_cut_lines_with_width_works_as_expected_without_intensity_range
cuts = [cut]
script_lines = []

add_cut_lines_with_width(errorbars, script_lines, cuts, IntensityType.SCATTERING_FUNCTION)
ret_val = add_cut_lines_with_width(errorbars, script_lines, cuts, IntensityType.SCATTERING_FUNCTION)
pass

self.assertIn(
'cut_ws_{} = mc.Cut(ws_{}, CutAxis="{}", IntegrationAxis="{}", NormToOne={}, IntensityCorrection={}, '
Expand All @@ -318,6 +325,8 @@ def test_that_add_cut_lines_with_width_works_as_expected_without_intensity_range
'ax.errorbar(cut_ws_{}, label="{}", color="{}", marker="{}", ls="{}", lw={}, plot_over={})\n\n'.format(
0, 'errorbar_label', 'blue', None, '-', 1.5, False), script_lines)

self.assertEqual(['cut_ws_0'], ret_val)

def test_that_add_cut_lines_with_width_works_as_expected_with_intensity_range(self):
x_data, y_data = np.arange(0, 10), np.arange(0, 10)
plt.errorbar(x_data, y_data, linestyle='-', linewidth=1.5, color='blue', label='errorbar_label')
Expand All @@ -328,12 +337,13 @@ def test_that_add_cut_lines_with_width_works_as_expected_with_intensity_range(se
cuts = [cut]
script_lines = []

add_cut_lines_with_width(errorbars, script_lines, cuts, IntensityType.SCATTERING_FUNCTION)
ret_val = add_cut_lines_with_width(errorbars, script_lines, cuts, IntensityType.SCATTERING_FUNCTION)

self.assertIn(
'ax.errorbar(cut_ws_{}, label="{}", color="{}", marker="{}", ls="{}", lw={}, '
'intensity_range={}, plot_over={})\n\n'.format(0, 'errorbar_label', 'blue', None, '-', 1.5, (1.0, 2.0), False),
script_lines)
self.assertEqual(['cut_ws_0'], ret_val)

def test_that_add_cut_lines_with_width_works_as_expected_with_multiple_cuts(self):
x_data, y_data = np.arange(0, 10), np.arange(0, 10)
Expand All @@ -349,7 +359,7 @@ def test_that_add_cut_lines_with_width_works_as_expected_with_multiple_cuts(self
cuts = [cut_0, cut_2]
script_lines = []

add_cut_lines_with_width(errorbars, script_lines, cuts, IntensityType.SCATTERING_FUNCTION)
ret_val = add_cut_lines_with_width(errorbars, script_lines, cuts, IntensityType.SCATTERING_FUNCTION)

self.assertIn(
'cut_ws_{} = mc.Cut(ws_{}, CutAxis="{}", IntegrationAxis="{}", NormToOne={}, IntensityCorrection={}, '
Expand Down Expand Up @@ -381,6 +391,8 @@ def test_that_add_cut_lines_with_width_works_as_expected_with_multiple_cuts(self
'intensity_range={}, plot_over={})\n\n'.format(2, 'error_label_2', 'blue', None, '-', 1.5, (1.0, 2.0), True),
script_lines)

self.assertEqual(['cut_ws_0', 'cut_ws_1', 'cut_ws_2'], ret_val)

def test_show_or_hide_containers_in_script(self):
fig, ax = plt.subplots()
ax.errorbar([1], [2], [0.3], label="label1")
Expand Down

0 comments on commit 2737647

Please sign in to comment.