def test_table_1(self, mock_fdf): ''' _make_table(): - "count frequency" is True - file extension on "pathname" with period ''' test_wm = WorkflowManager([]) test_wm.settings(None, 'count frequency', True) # just to be 100% clear mock_fdf.return_value = mock.MagicMock(spec_set=pandas.DataFrame) test_wm._result = mock_fdf.return_value # to avoid a RuntimeError test_wm._previous_exp = 'intervals' # to get the proper "exp_name" top_x = None threshold = None exp_name = 'Interval Frequency' pathname = 'test_path' test_wm._make_table('CSV', pathname + '.csv', top_x, threshold) # pylint: disable=protected-access test_wm._make_table('Excel', pathname + '.xlsx', top_x, threshold) # pylint: disable=protected-access test_wm._make_table('Stata', pathname + '.dta', top_x, threshold) # pylint: disable=protected-access test_wm._make_table('HTML', pathname + '.html', top_x, threshold) # pylint: disable=protected-access mock_fdf.return_value.to_csv.assert_called_once_with('test_path.csv') mock_fdf.return_value.to_stata.assert_called_once_with('test_path.dta') mock_fdf.return_value.to_excel.assert_called_once_with('test_path.xlsx') mock_fdf.return_value.to_html.assert_called_once_with('test_path.html') self.assertSequenceEqual([mock.call(top_x=top_x, threshold=threshold, name=exp_name) for _ in range(4)], mock_fdf.call_args_list)
def test_histogram_1(self, mock_call, mock_gdf, mock_join): # with specified pathname; last experiment was intervals with 20 pieces; self._result is DF test_wc = WorkflowManager([]) test_wc._previous_exp = u'intervals' test_wc._data = [1 for _ in xrange(20)] test_wc._result = MagicMock(spec=pandas.DataFrame) path = u'pathname!' actual = test_wc._make_histogram(path) self.assertEqual(0, mock_gdf.call_count) expected_args = [u'Rscript', u'--vanilla', mock_join.return_value, path + u'.dta', path + u'.png', u'int', u'20'] mock_call.assert_called_once_with(expected_args) self.assertEqual(path + u'.png', actual) self.assertEqual(1, mock_join.call_count)
def test_histogram_3(self, mock_call, mock_gdf, mock_join): # test_ouput_6, plus top_x and threshold test_wc = WorkflowManager([]) test_wc._previous_exp = u'n-grams' test_wc._data = [1] test_wc._shared_settings[u'n'] = 14 test_wc._result = MagicMock(spec=pandas.Series) path = u'test_output/output_result' actual = test_wc._make_histogram(top_x=420, threshold=1987) mock_gdf.assert_called_once_with(u'freq', 420, 1987) expected_args = [u'Rscript', u'--vanilla', mock_join.return_value, path + u'.dta', path + u'.png', u'14', u'1'] mock_call.assert_called_once_with(expected_args) self.assertEqual(path + u'.png', actual) self.assertEqual(1, mock_join.call_count)
def test_histogram_2(self, mock_call, mock_gdf, mock_join): # with unspecified pathname; last experiment was 14-grams with 1 piece; self._result is S test_wc = WorkflowManager([]) test_wc._previous_exp = u'n-grams' test_wc._data = [1] test_wc._shared_settings[u'n'] = 14 test_wc._result = MagicMock(spec=pandas.Series) path = u'test_output/output_result' actual = test_wc._make_histogram() mock_gdf.assert_called_once_with(u'freq', None, None) expected_args = [u'Rscript', u'--vanilla', mock_join.return_value, path + u'.dta', path + u'.png', u'14', u'1'] mock_call.assert_called_once_with(expected_args) self.assertEqual(path + u'.png', actual) self.assertEqual(1, mock_join.call_count)
def test_output_2(self, mock_lily): # ensure output() calls _make_lilypond() as required # 1: prepare lily_path = u'the_path' mock_lily.return_value = lily_path test_wc = WorkflowManager([]) test_wc._previous_exp = u'intervals' test_wc._data = [1 for _ in xrange(20)] test_wc._result = MagicMock(spec=pandas.DataFrame) path = u'pathname!' expected_args = [path] # 2: run actual = test_wc.output('LilyPond', path) # 3: check self.assertEqual(lily_path, actual) mock_lily.assert_called_once_with(*expected_args)
def test_output_5(self, mock_table): """ensure output() calls export() as required""" # 1: prepare export_path = 'the_path' mock_table.return_value = export_path test_wc = WorkflowManager([]) test_wc._previous_exp = 'intervals' test_wc._data = [1 for _ in range(20)] test_wc._result = MagicMock(spec=pandas.DataFrame) path = 'pathname!' expected_args = ['Excel', path, None, None] # 2: run actual = test_wc.output('Excel', path) # 3: check self.assertEqual(export_path, actual) mock_table.assert_called_once_with(*expected_args)
def test_output_1b(self, mock_histo): # ensure output() calls _make_histogram() as required (with 'R histogram' instruction) # 1: prepare histo_path = u'the_path.svg' mock_histo.return_value = histo_path test_wc = WorkflowManager([]) test_wc._previous_exp = u'intervals' test_wc._data = [1 for _ in xrange(20)] test_wc._result = MagicMock(spec=pandas.DataFrame) path = u'pathname!' top_x = 20 threshold = 10 expected_args = [path, top_x, threshold] # 2: run actual = test_wc.output('R histogram', path, top_x, threshold) # 3: check self.assertEqual(histo_path, actual) mock_histo.assert_called_once_with(*expected_args)
def test_histogram_4(self, mock_call, mock_gdf, mock_join): # test_output_4() but the subprocess thing fails def raiser(*args): raise CalledProcessError(u'Bach!', 42, u'CPE') mock_call.side_effect = raiser test_wc = WorkflowManager([]) test_wc._previous_exp = u'intervals' test_wc._data = [1 for _ in xrange(20)] test_wc._result = MagicMock(spec=pandas.DataFrame) path = u'pathname!' expected_msg = u'Error during call to R: CPE (return code: Bach!)' actual = None try: test_wc._make_histogram(path) except RuntimeError as run_e: actual = run_e self.assertEqual(expected_msg, actual.message) self.assertEqual(0, mock_gdf.call_count) expected_args = [u'Rscript', u'--vanilla', mock_join.return_value, path + u'.dta', path + u'.png', u'int', u'20'] mock_call.assert_called_once_with(expected_args) self.assertEqual(1, mock_join.call_count)
def test_histogram_3(self, mock_bar, mock_fdf): """ That _make_histogram() works properly. - pathname: None - top_x: None - threshold: None - test_wc._previous_exp: 'cheese' """ test_wc = WorkflowManager([]) test_wc._previous_exp = 'cheese' mock_fdf.return_value = 'filtered DataFrame' exp_setts = {'pathname': 'test_output/output_result', 'token': 'objects', 'type': 'png', 'nr_pieces': 0} exp_png_path = 'your png path' mock_experimenter = mock.MagicMock() mock_experimenter.run = mock.MagicMock(return_value=exp_png_path) mock_bar.RBarChart = mock.MagicMock(return_value=mock_experimenter) actual = test_wc._make_histogram() self.assertEqual(exp_png_path, actual) mock_fdf.assert_called_once_with(top_x=None, threshold=None, name='freq') mock_bar.RBarChart.assert_called_once_with(mock_fdf.return_value, exp_setts)
def test_histogram_2(self, mock_bar, mock_fdf): """ That _make_histogram() works properly. - pathname: given - top_x: given - threshold: given - test_wc._previous_exp: 'interval n-grams' """ test_wc = WorkflowManager([]) test_wc._previous_exp = 'interval n-grams' test_wc.settings(None, 'n', 42) mock_fdf.return_value = 'filtered DataFrame' exp_setts = {'pathname': 'some_path', 'token': '42-gram', 'type': 'png', 'nr_pieces': 0} exp_png_path = 'your png path' mock_experimenter = mock.MagicMock() mock_experimenter.run = mock.MagicMock(return_value=exp_png_path) mock_bar.RBarChart = mock.MagicMock(return_value=mock_experimenter) actual = test_wc._make_histogram('some_path', 10, 100) self.assertEqual(exp_png_path, actual) mock_fdf.assert_called_once_with(top_x=10, threshold=100, name='freq') mock_bar.RBarChart.assert_called_once_with(mock_fdf.return_value, exp_setts)