def test_table_3(self): ''' _make_table(): - "count frequency" is False - file extension not on "pathname" - there are several IndexedPiece objects ''' test_wm = WorkflowManager([]) test_wm.settings(None, 'count frequency', False) test_wm._result = [mock.MagicMock(spec_set=pandas.DataFrame) for _ in range(5)] test_wm._data = ['boop' for _ in range(len(test_wm._result))] top_x = None threshold = None pathname = 'test_path' test_wm._make_table('CSV', pathname, top_x, threshold) # pylint: disable=protected-access test_wm._make_table('Excel', pathname, top_x, threshold) # pylint: disable=protected-access test_wm._make_table('Stata', pathname, top_x, threshold) # pylint: disable=protected-access test_wm._make_table('HTML', pathname, top_x, threshold) # pylint: disable=protected-access for i in range(len(test_wm._result)): test_wm._result[i].to_csv.assert_called_once_with('{}-{}{}'.format(pathname, i, '.csv')) test_wm._result[i].to_excel.assert_called_once_with('{}-{}{}'.format(pathname, i, '.xlsx')) test_wm._result[i].to_stata.assert_called_once_with('{}-{}{}'.format(pathname, i, '.dta')) test_wm._result[i].to_html.assert_called_once_with('{}-{}{}'.format(pathname, i, '.html'))
def test_load_1(self): # that "get_data" is called correctly on each thing test_wc = WorkflowManager([]) test_wc._data = [mock.MagicMock(spec=IndexedPiece) for _ in xrange(5)] test_wc.load(u'pieces') for mock_piece in test_wc._data: mock_piece.get_data.assert_called_once_with([noterest.NoteRestIndexer]) self.assertTrue(test_wc._loaded)
def test_lilypond_1b(self): """error conditions: if the lengths are different, (but 'count frequency' is okay)""" test_wm = WorkflowManager(['fake piece']) test_wm._data = ['fake IndexedPiece'] test_wm._result = ['fake results', 'more fake results', 'so many fake results'] test_wm.settings(None, 'count frequency', False) with self.assertRaises(RuntimeError) as run_err: test_wm._make_lilypond(['paths']) self.assertEqual(WorkflowManager._COUNT_FREQUENCY_MESSAGE, run_err.exception.args[0])
def test_lilypond_1a(self): """error conditions: if 'count frequency' is True (but the lengths are okay)""" test_wm = WorkflowManager(['fake piece']) test_wm._data = ['fake IndexedPiece'] test_wm._result = ['fake results'] # test twice like this to make sure (1) the try/except will definitely catch something, and # (2) we're not getting hit by another RuntimeError, of which there could be many with self.assertRaises(RuntimeError) as run_err: test_wm._make_lilypond(['paths']) self.assertEqual(WorkflowManager._COUNT_FREQUENCY_MESSAGE, run_err.exception.args[0])
def test_lilypond_1b(self): # error conditions: if the lengths are different, (but 'count frequency' is okay) test_wm = WorkflowManager(['fake piece']) test_wm._data = ['fake IndexedPiece'] test_wm._result = ['fake results', 'more fake results', 'so many fake results'] test_wm.settings(None, 'count frequency', False) self.assertRaises(RuntimeError, test_wm._make_lilypond, ['paths']) try: test_wm._make_lilypond(['paths']) except RuntimeError as the_err: self.assertEqual(WorkflowManager._COUNT_FREQUENCY_MESSAGE, the_err.message)
def test_run_off_rep_5(self, mock_off, mock_rep): # run neither indexer; input a dict # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm.settings(1, 'offset interval', 0) workm.settings(1, 'filter repeats', False) in_val = {'b': 43, 'a': 42, 'c': 44} # run actual = workm._run_off_rep(1, in_val) # test self.assertSequenceEqual(in_val, actual) self.assertEqual(0, workm._data[1].get_data.call_count)
def test_run_off_rep_1(self): """run neither indexer""" # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm.settings(1, 'offset interval', 0) workm.settings(1, 'filter repeats', False) in_val = 42 # run actual = workm._run_off_rep(1, in_val) # test self.assertEqual(in_val, actual) self.assertEqual(0, workm._data[1].get_data.call_count)
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_run_off_rep_3(self, mock_off, mock_rep): # run repeat indexer # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm._data[1].get_data.return_value = 24 workm.settings(1, 'offset interval', 0) workm.settings(1, 'filter repeats', True) in_val = 42 # run actual = workm._run_off_rep(1, in_val) # test self.assertEqual(workm._data[1].get_data.return_value, actual) workm._data[1].get_data.assert_called_once_with([mock_rep], {}, in_val)
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_run_off_rep_3(self): """run repeat indexer""" # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm._data[1].get_data.return_value = 24 workm.settings(1, 'offset interval', 0) workm.settings(1, 'filter repeats', True) in_val = 42 exp_analyzers = [repeat.FilterByRepeatIndexer] # run actual = workm._run_off_rep(1, in_val) # test self.assertEqual(workm._data[1].get_data.return_value, actual) workm._data[1].get_data.assert_called_once_with(exp_analyzers, {}, in_val)
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_run_off_rep_6(self, mock_off, mock_rep): # run offset indexer with is_horizontal set to True # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm._data[1].get_data.return_value = 24 workm.settings(1, 'offset interval', 0.5) workm.settings(1, 'filter repeats', False) in_val = 42 # run actual = workm._run_off_rep(1, in_val, True) # test self.assertEqual(workm._data[1].get_data.return_value, actual) workm._data[1].get_data.assert_called_once_with([mock_off], {'quarterLength': 0.5, 'method': None}, in_val)
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_run_off_rep_4(self, mock_off, mock_rep): # run offset and repeat indexer # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm._data[1].get_data.return_value = 24 workm.settings(1, 'offset interval', 0.5) workm.settings(1, 'filter repeats', True) in_val = 42 # run actual = workm._run_off_rep(1, in_val) # test self.assertEqual(workm._data[1].get_data.return_value, actual) self.assertEqual(2, workm._data[1].get_data.call_count) workm._data[1].get_data.assert_any_call([mock_off], {'quarterLength': 0.5}, in_val) workm._data[1].get_data.assert_any_call([mock_rep], {}, workm._data[1].get_data.return_value)
def test_run_off_rep_4(self): """run offset and repeat indexer""" # setup workm = WorkflowManager(['', '', '']) workm._data = [None, MagicMock(spec=IndexedPiece), None] workm._data[1].get_data.return_value = 24 workm.settings(1, 'offset interval', 0.5) workm.settings(1, 'filter repeats', True) in_val = 42 exp_an_off = [offset.FilterByOffsetIndexer] exp_an_rep = [repeat.FilterByRepeatIndexer] # run actual = workm._run_off_rep(1, in_val) # test self.assertEqual(workm._data[1].get_data.return_value, actual) self.assertEqual(2, workm._data[1].get_data.call_count) workm._data[1].get_data.assert_any_call(exp_an_off, {'quarterLength': 0.5}, in_val) workm._data[1].get_data.assert_any_call(exp_an_rep, {}, workm._data[1].get_data.return_value)
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)