def test_note_beat_strength_indexer_1(self): # When the parts are empty expected = pandas.DataFrame({'0': pandas.Series(), '1': pandas.Series()}) test_parts = [stream.Part(), stream.Part()] ip = IndexedPiece() ip.metadata('parts', expected.columns) ip._analyses['part_streams'] = test_parts # supply part_streams. actual = ip._get_beat_strength()['meter.NoteBeatStrengthIndexer'] self.assertTrue(actual.equals(expected))
def test_measure_indexer_1(self): # When the parts are empty expected = pandas.DataFrame({'0': pandas.Series(), '1': pandas.Series()}) test_parts = [stream.Part(), stream.Part()] ip = IndexedPiece('phony_file_location') # it doesn't matter what the string is becuase we supply part_streams ip.metadata('parts', expected.columns) ip._analyses['part_streams'] = test_parts # supply part_streams. actual = ip._get_measure()['meter.MeasureIndexer'] self.assertTrue(actual.equals(expected))
def test_note_beat_strength_indexer_1(self): # When the parts are empty expected = pandas.DataFrame({ '0': pandas.Series(), '1': pandas.Series() }) test_parts = [stream.Part(), stream.Part()] ip = IndexedPiece() ip.metadata('parts', expected.columns) ip._analyses['part_streams'] = test_parts # supply part_streams. actual = ip._get_beat_strength()['meter.NoteBeatStrengthIndexer'] self.assertTrue(actual.equals(expected))
def test_measure_indexer_2(self): # When the part has no Measure objects in it but a bunch of notes. expected = pandas.DataFrame({'0': pandas.Series()}) test_part = stream.Part() # add stuff to the test_part for i in range(0, 20, 2): add_me = note.Note('C#5', quarterLength=2.0) add_me.offset = i test_part.append(add_me) test_part = [test_part] # finished adding stuff to the test_part ip = IndexedPiece('phony_file_location') # it doesn't matter what the string is becuase we supply part_streams ip.metadata('parts', expected.columns) ip._analyses['part_streams'] = test_part # supply part_streams. actual = ip._get_measure()['meter.MeasureIndexer'] self.assertTrue(actual.equals(expected))
def test_duration_indexer_3(self): # When there are a bunch of notes expected = TestDurationIndexer.make_series([(float(x), 1.0) for x in range(10)]) expected.name = 'Part_1' test_part = stream.Part() # add stuff to the test_part for i in range(10): add_me = note.Note('C4', quarterLength=1.0) add_me.offset = i test_part.append(add_me) ip = IndexedPiece() test_part = [test_part] ip._analyses['part_streams'] = test_part ip.metadata('parts', [expected.name]) actual = ip.get_data('duration').iloc[:, 0] self.assertTrue(actual.equals(expected))
def test_meta(self): meta = os.path.join(VIS_PATH, 'tests', 'corpus', 'meta') piece = os.path.join( VIS_PATH, 'tests', 'corpus', 'Missa-Fortuna-desperata_Kyrie_Josquin-Des-Prez_file6.xml') ind = IndexedPiece(piece, metafile=meta) self.assertEqual('Sacred', ind.metadata('religiosity'))
def test_note_beat_strength_indexer_2(self): # When the part has no Note or Rest objects in it expected = pandas.DataFrame({'0': pandas.Series()}) test_part = stream.Part() # add stuff to the test_part for i in range(1000): add_me = clef.BassClef() add_me.offset = i test_part.append(add_me) add_me = bar.Barline() add_me.offset = i test_part.append(add_me) # finished adding stuff to the test_part ip = IndexedPiece() ip.metadata('parts', expected.columns) ip._analyses['part_streams'] = [test_part] # supply part_streams. actual = ip._get_beat_strength()['meter.NoteBeatStrengthIndexer'] self.assertTrue(actual.equals(expected))
def test_measure_indexer_3(self): # When there are a bunch of measures with a note in each one. expected = pandas.DataFrame({'0': pandas.Series(range(1, 11), index=[float(x) for x in range(10)])}) test_part = stream.Part() # add stuff to the test_part for i in range(1, 11): add_me = stream.Measure() add_me.number = i add_me.insert(note.Note('C#5', quarterLength=1.0)) add_me.offset = i test_part.append(add_me) test_part = [test_part] # finished adding stuff to the test_part ip = IndexedPiece('phony_file_location') # it doesn't matter what the string is becuase we supply part_streams ip.metadata('parts', expected.columns) ip._analyses['part_streams'] = test_part # supply part_streams. actual = ip._get_measure()['meter.MeasureIndexer'] self.assertTrue(actual.equals(expected))
def test_note_rest_indexer_2(self): # When the part has no Note or Rest objects in it. Really this is a test for the methods between # _get_part_streams() and _get_noterest(). expected = pandas.DataFrame({'Part 1': pandas.Series()}) test_part = stream.Part() # add stuff to the test_part for i in range(1000): add_me = clef.BassClef() add_me.offset = i test_part.append(add_me) add_me = bar.Barline() add_me.offset = i test_part.append(add_me) ip = IndexedPiece() ip._analyses['part_streams'] = [test_part] ip.metadata('parts', _find_part_names(ip._analyses['part_streams'])) actual = ip.get_data('noterest')['noterest.NoteRestIndexer'] self.assertTrue(actual.equals(expected))
def test_note_beat_strength_indexer_3(self): # When there are a few notes expected = pandas.DataFrame({'0': pandas.Series([1.0, 0.5, 0.5, 1.0, 0.5, 0.5])}) test_part = stream.Part() # add stuff to the test_part measure = stream.Measure() # In music21 beginning time signatures are preferably inserted in the first measure and a # timeSignature is needed to be able to calculate beatStrength measure.insert(0, m21_meter.TimeSignature('3/4')) for i in range(6): add_me = note.Note(u'C4', quarterLength=1.0) add_me.offset = i measure.append(add_me) test_part.insert(0, measure) # finished adding stuff to the test_part ip = IndexedPiece() ip.metadata('parts', expected.columns) ip._analyses['part_streams'] = [test_part] # supply part_streams. actual = ip._get_beat_strength()['meter.NoteBeatStrengthIndexer'] self.assertTrue(actual.equals(expected))
def test_note_beat_strength_indexer_3(self): # When there are a few notes expected = pandas.DataFrame( {'0': pandas.Series([1.0, 0.5, 0.5, 1.0, 0.5, 0.5])}) test_part = stream.Part() # add stuff to the test_part measure = stream.Measure() # In music21 beginning time signatures are preferably inserted in the first measure and a # timeSignature is needed to be able to calculate beatStrength measure.insert(0, m21_meter.TimeSignature('3/4')) for i in range(6): add_me = note.Note(u'C4', quarterLength=1.0) add_me.offset = i measure.append(add_me) test_part.insert(0, measure) # finished adding stuff to the test_part ip = IndexedPiece() ip.metadata('parts', expected.columns) ip._analyses['part_streams'] = [test_part] # supply part_streams. actual = ip._get_beat_strength()['meter.NoteBeatStrengthIndexer'] self.assertTrue(actual.equals(expected))
class TestIndexedPieceA(TestCase): def setUp(self): self._pathname = u'test_path' self.ind_piece = IndexedPiece(self._pathname) def test_metadata(self): # access fields which are set by default pathname = self.ind_piece.metadata('pathname') self.assertEquals(self._pathname, pathname, "pathname has changed!") # assign a value to a valid field self.ind_piece.metadata('date', 2) value = self.ind_piece.metadata('date') self.assertEquals(2, value, "extracted metadata field doesn't match assigned value") # assign a value to an invalid field self.assertRaises(AttributeError, self.ind_piece.metadata, 'field', 2) # access an invalid value self.assertRaises(AttributeError, self.ind_piece.metadata, 'invalid_field') # try accessing keys with invalid types self.assertRaises(TypeError, self.ind_piece.metadata, 2) self.assertRaises(TypeError, self.ind_piece.metadata, []) self.assertRaises(TypeError, self.ind_piece.metadata, {}) def test_get_data_0(self): # try getting data for a non-Indexer, non-Experimenter class non_analyzer = Mock() self.assertRaises(TypeError, self.ind_piece.get_data, non_analyzer) def test_get_data_1(self): # get data for an Indexer requiring a Score mock_indexer_cls = type('MockIndexer', (Indexer,), {}) mock_indexer_cls.required_score_type = music21.stream.Part mock_indexer_cls.run = MagicMock() mock_indexer_cls.run.return_value = u'ahhh!' self.assertEquals(u'ahhh!', self.ind_piece.get_data([mock_indexer_cls])) mock_indexer_cls.run.assert_called_once_with() def test_get_data_2(self): # get data for an Indexer requiring other data mock_indexer_cls = type('MockIndexer', (Indexer,), {}) mock_init = MagicMock() mock_init.return_value = None mock_indexer_cls.__init__ = mock_init mock_indexer_cls.run = MagicMock() mock_indexer_cls.run.return_value = u'ahhh!' mock_indexer_cls.required_score_type = int self.assertEqual(u'ahhh!', self.ind_piece.get_data([mock_indexer_cls], data=[14])) mock_indexer_cls.run.assert_called_once_with() mock_init.assert_called_once_with([14], None) def test_get_data_3(self): # get data from a chained Indexer first_indexer_cls = type('MockIndexer', (Indexer,), {}) first_init = MagicMock() first_init.return_value = None first_indexer_cls.__init__ = first_init first_indexer_cls.run = MagicMock() first_indexer_cls.run.return_value = [14] first_indexer_cls.required_score_type = music21.stream.Part second_indexer_cls = type('MockIndexer', (Indexer,), {}) second_init = MagicMock() second_init.return_value = None second_indexer_cls.__init__ = second_init second_indexer_cls.run = MagicMock() second_indexer_cls.run.return_value = u'ahhh!' second_indexer_cls.required_score_type = int self.assertEqual(u'ahhh!', self.ind_piece.get_data([first_indexer_cls, second_indexer_cls])) first_init.assert_called_once_with([], None) second_init.assert_called_once_with(first_indexer_cls.run.return_value, None) first_indexer_cls.run.assert_called_once_with() second_indexer_cls.run.assert_called_once_with() def test_get_data_4(self): # chained Indexer plus settings first_indexer_cls = type('MockIndexer', (Indexer,), {}) first_init = MagicMock() first_init.return_value = None first_indexer_cls.__init__ = first_init first_indexer_cls.run = MagicMock() first_indexer_cls.run.return_value = [14] first_indexer_cls.required_score_type = music21.stream.Part second_indexer_cls = type('MockIndexer', (Indexer,), {}) second_init = MagicMock() second_init.return_value = None second_indexer_cls.__init__ = second_init second_indexer_cls.run = MagicMock() second_indexer_cls.run.return_value = u'ahhh!' second_indexer_cls.required_score_type = int settings_dict = {u'fake setting': u'so good!'} self.assertEqual(u'ahhh!', self.ind_piece.get_data([first_indexer_cls, second_indexer_cls], settings_dict)) first_init.assert_called_once_with([], settings_dict) second_init.assert_called_once_with(first_indexer_cls.run.return_value, settings_dict) first_indexer_cls.run.assert_called_once_with() second_indexer_cls.run.assert_called_once_with() def test_get_data_5(self): # get data from an Experimenter that requires an Indexer # (same as test 3, but second_indexer_cls is an Experimenter subclass) mock_indexer_cls = type('MockIndexer', (Indexer,), {}) mock_init = MagicMock() mock_init.return_value = None mock_indexer_cls.__init__ = mock_init mock_indexer_cls.run = MagicMock() mock_indexer_cls.run.return_value = [14] mock_indexer_cls.required_score_type = music21.stream.Part mock_experimenter_cls = type('MockIndexer', (Experimenter,), {}) exp_init = MagicMock() exp_init.return_value = None mock_experimenter_cls.__init__ = exp_init mock_experimenter_cls.run = MagicMock() mock_experimenter_cls.run.return_value = u'ahhh!' self.assertEqual(u'ahhh!', self.ind_piece.get_data([mock_indexer_cls, mock_experimenter_cls])) mock_init.assert_called_once_with([], None) exp_init.assert_called_once_with(mock_indexer_cls.run.return_value, None) mock_indexer_cls.run.assert_called_once_with() mock_experimenter_cls.run.assert_called_once_with() def test_get_data_6(self): # That get_data() complains when an Indexer expects the results of another Indexer but # doesn't get them. mock_indexer_cls = type('MockIndexer', (Indexer,), {}) mock_indexer_cls.required_score_type = pandas.Series self.assertRaises(RuntimeError, self.ind_piece.get_data, [mock_indexer_cls]) def test_get_data_7(self): # That get_data() complains when you call it with something that isn't either an Indexer # or Experimenter. self.assertRaises(TypeError, self.ind_piece.get_data, [TestIndexedPieceB]) def test_get_data_8(self): # That get_data() calls _get_note_rest_index() if asked for NoteRestIndexer. with patch.object(IndexedPiece, u'_get_note_rest_index') as mock_gnri: self.ind_piece.get_data([noterest.NoteRestIndexer]) mock_gnri.assert_called_once_with(known_opus=False) def test_get_data_9(self): # That get_data() calls _get_note_rest_index() if asked for NoteRestIndexer, and another # test Indexer is also called. This is a regression test to monitor a bug found after # implementing caching of NoteRestIndexer results. Also ensure that NoteRestIndexer is only # instantiated once with patch.object(IndexedPiece, u'_get_note_rest_index') as mock_gnri: mock_gnri.return_value = 42 with patch.object(IndexedPiece, u'_type_verifier') as mock_tv: # we'll mock _type_verifier() to avoid getting a TypeError when mock_nri_cls isn't # a proper subclass of Indexer expected = [400] mock_indexer_cls = type('MockIndexer', (Indexer,), {}) mock_indexer_cls.__init__ = MagicMock() mock_indexer_cls.__init__.return_value = None mock_indexer_cls.run = MagicMock() mock_indexer_cls.run.return_value = expected actual = self.ind_piece.get_data([noterest.NoteRestIndexer, mock_indexer_cls]) self.assertEqual(2, mock_tv.call_count) mock_tv.assert_has_calls([call([noterest.NoteRestIndexer, mock_indexer_cls]), call([mock_indexer_cls])]) mock_gnri.assert_called_once_with(known_opus=False) mock_indexer_cls.__init__.assert_called_once_with(mock_gnri.return_value, None) mock_indexer_cls.run.assert_called_once_with() self.assertEqual(expected, actual) def test_get_data_10(self): # That get_data() calls _get_note_rest_index() if asked for NoteRestIndexer. This is a # regression test to monitor a bug found after implementing caching of NoteRestIndexer # results. Also ensure that NoteRestIndexer is only instantiated once with patch.object(IndexedPiece, u'_get_note_rest_index') as mock_gnri: mock_gnri.return_value = 42 with patch.object(IndexedPiece, u'_type_verifier') as mock_tv: # we'll mock _type_verifier() to avoid getting a TypeError when mock_nri_cls isn't # a proper subclass of Indexer actual = self.ind_piece.get_data([noterest.NoteRestIndexer]) mock_tv.assert_called_once_with([noterest.NoteRestIndexer]) mock_gnri.assert_called_once_with(known_opus=False) self.assertEqual(mock_gnri.return_value, actual) def test_get_data_11(self): # That get_data() correctly passes its "known_opus" parameter to _get_note_rest_indexer() # and _import_score() with patch.object(IndexedPiece, u'_get_note_rest_index') as mock_gnri: self.ind_piece.get_data([noterest.NoteRestIndexer], known_opus='battery') mock_gnri.assert_called_once_with(known_opus='battery') with patch.object(IndexedPiece, u'_import_score') as mock_is: with patch.object(IndexedPiece, u'_type_verifier') as mock_tv: mock_ind = MagicMock() mock_ind.required_score_type = music21.stream.Part self.ind_piece.get_data([mock_ind], known_opus='horse') mock_is.assert_called_once_with(known_opus='horse') def test_get_data_12(self): # get data for an Experimenter requiring other data; this is test 2, slightly modified mock_experimenter_cls = type('MockExperimenter', (Experimenter,), {}) mock_experimenter_cls.__init__ = MagicMock(return_value=None) mock_experimenter_cls.run = MagicMock(return_value=u'ahhh!') prev_data= u'data from the previous analyzers' self.assertEqual(u'ahhh!', self.ind_piece.get_data([mock_experimenter_cls], {}, prev_data)) mock_experimenter_cls.run.assert_called_once_with() mock_experimenter_cls.__init__.assert_called_once_with(prev_data, {}) def test_type_verifier_1(self): # with an Indexer # pylint: disable=W0212 self.assertEqual(None, IndexedPiece._type_verifier([noterest.NoteRestIndexer])) def test_type_verifier_2(self): # with an Experimenter # pylint: disable=W0212 cls = type('TestExperimenter', (Experimenter,), {}) self.assertEqual(None, IndexedPiece._type_verifier([cls])) def test_type_verifier_3(self): # with another class # pylint: disable=W0212 cls = type('TestGarbage', (object,), {}) self.assertRaises(TypeError, IndexedPiece._type_verifier, [cls]) def test_type_verifier_4(self): # with a bunch of valid classes # pylint: disable=W0212 cls_1 = type('TestExperimenter1', (Experimenter,), {}) cls_2 = type('TestExperimenter2', (Experimenter,), {}) cls_3 = type('TestIndexer', (Indexer,), {}) self.assertEqual(None, IndexedPiece._type_verifier([cls_1, cls_2, cls_3])) def test_type_verifier_5(self): # with a bunch of valid, but one invalid, class # pylint: disable=W0212 cls_1 = type('TestExperimenter1', (Experimenter,), {}) cls_2 = type('TestIndexer', (Indexer,), {}) cls_3 = type('TestGarbage', (object,), {}) self.assertRaises(TypeError, IndexedPiece._type_verifier, [cls_1, cls_2, cls_3]) def test_get_nrindex_1(self): # pylint: disable=W0212 # That _get_note_rest_index() returns self._noterest_results if it's not None. self.ind_piece._noterest_results = 42 self.assertEqual(42, self.ind_piece._get_note_rest_index()) def test_get_nrindex_2(self): # pylint: disable=W0212 # That we run the NoteRestIndexer and store results in self._note_rest_results if is None. with patch(u'vis.models.indexed_piece.noterest.NoteRestIndexer') as mock_nri_cls: mock_nri = MagicMock(return_value=[14]) mock_nri.run = MagicMock() mock_nri.run.return_value = [14] mock_nri_cls.return_value = mock_nri expected = [14] actual = self.ind_piece._get_note_rest_index() mock_nri.run.assert_called_once_with() self.assertEqual(expected, actual) self.assertEqual(expected, self.ind_piece._noterest_results) def test_get_nrindex_3(self): # That _get_note_rest_index() just flat-out calls _import_score() when "known_opus" is True with patch.object(IndexedPiece, u'_import_score') as mock_is: with patch.object(noterest.NoteRestIndexer, u'run') as mock_nri: self.ind_piece._get_note_rest_index(known_opus=True) mock_is.assert_called_once_with(known_opus=True) self.assertEqual(0, mock_nri.call_count) def test_unicode_1(self): # __unicode__() without having imported yet # NB: adjusting _imported is the whole point of the test # pylint: disable=W0212 self.ind_piece._imported = False with patch(u'vis.models.indexed_piece.IndexedPiece.metadata') as mock_meta: mock_meta.return_value = u'42' expected = u'<IndexedPiece (42)>' actual = self.ind_piece.__unicode__() self.assertEqual(expected, actual) mock_meta.assert_called_once_with(u'pathname') def test_unicode_2(self): # __unicode__() with proper title and composer # NB: adjusting _imported is the whole point of the test # pylint: disable=W0212 self.ind_piece._imported = True with patch(u'vis.models.indexed_piece.IndexedPiece.metadata') as mock_meta: returns = [u'some ridiculous piece', u'a no-name composer'] def side_effect(*args): # NB: we need to accept "args" as a mock framework formality # pylint: disable=W0613 return returns.pop(0) mock_meta.side_effect = side_effect expected = u'<IndexedPiece (some ridiculous piece by a no-name composer)>' actual = self.ind_piece.__unicode__() self.assertEqual(expected, actual) expected_calls = [call(u'title'), call(u'composer')] self.assertSequenceEqual(expected_calls, mock_meta.call_args_list)
class TestIndexedPieceA(TestCase): """The 'A' part of tests for IndexedPiece.""" def setUp(self): """Set up some stuff.""" self._pathname = 'test_path' self.ind_piece = IndexedPiece(self._pathname) def test_metadata(self): """access fields that are set by default""" pathname = self.ind_piece.metadata('pathname') self.assertEquals(self._pathname, pathname, "pathname has changed!") # assign a value to a valid field self.ind_piece.metadata('date', 2) value = self.ind_piece.metadata('date') self.assertEquals( 2, value, "extracted metadata field doesn't match assigned value") # assign a value to an invalid field self.assertRaises(AttributeError, self.ind_piece.metadata, 'field', 2) # access an invalid value self.assertRaises(AttributeError, self.ind_piece.metadata, 'invalid_field') # try accessing keys with invalid types self.assertRaises(TypeError, self.ind_piece.metadata, 2) self.assertRaises(TypeError, self.ind_piece.metadata, []) self.assertRaises(TypeError, self.ind_piece.metadata, {}) def test_get_data_0(self): """try getting data for a non-Indexer, non-Experimenter class""" non_analyzer = Mock() self.assertRaises(KeyError, self.ind_piece.get_data, non_analyzer) def test_get_data_1(self): """ That get_data() complains when an Indexer expects the results of another Indexer but doesn't get them. """ mock_indexer_cls = type('MockIndexer', (Indexer, ), {}) mock_indexer_cls.required_score_type = pandas.DataFrame self.assertRaises(RuntimeWarning, self.ind_piece.get_data, vis.analyzers.indexers.ngram.NGramIndexer) def test_get_data_2(self): """ That get_data() complains when you call it with something that isn't either an Indexer or Experimenter. """ self.assertRaises(KeyError, self.ind_piece.get_data, TestIndexedPieceA) def test_get_nrindex_1(self): """That _get_noterest() returns self._analyses['noterest'] if it's not None.""" # pylint: disable=W0212 self.ind_piece._analyses['noterest'] = 42 self.assertEqual(42, self.ind_piece._get_noterest()) def test_str_1(self): """__str__() without having imported yet""" # NB: adjusting _imported is the whole point of the test # pylint: disable=W0212 self.ind_piece._imported = False with patch( 'vis.models.indexed_piece.IndexedPiece.metadata') as mock_meta: mock_meta.return_value = '42' expected = '<IndexedPiece (42)>' actual = self.ind_piece.__str__() self.assertEqual(expected, actual) mock_meta.assert_called_once_with('pathname') def test_str_2(self): """__str__() with proper title and composer""" # NB: adjusting _imported is the whole point of the test # pylint: disable=W0212 self.ind_piece._imported = True with patch( 'vis.models.indexed_piece.IndexedPiece.metadata') as mock_meta: returns = ['some ridiculous piece', 'a no-name composer'] def side_effect(*args): # NB: we need to accept "args" as a mock framework formality # pylint: disable=W0613 return returns.pop(0) mock_meta.side_effect = side_effect expected = '<IndexedPiece (some ridiculous piece by a no-name composer)>' actual = self.ind_piece.__str__() self.assertEqual(expected, actual) expected_calls = [call('title'), call('composer')] self.assertSequenceEqual(expected_calls, mock_meta.call_args_list)
class TestIndexedPieceA(TestCase): """The 'A' part of tests for IndexedPiece.""" def setUp(self): """Set up some stuff.""" self._pathname = 'test_path' self.ind_piece = IndexedPiece(self._pathname) def test_metadata(self): """access fields that are set by default""" pathname = self.ind_piece.metadata('pathname') self.assertEquals(self._pathname, pathname, "pathname has changed!") # assign a value to a valid field self.ind_piece.metadata('date', 2) value = self.ind_piece.metadata('date') self.assertEquals(2, value, "extracted metadata field doesn't match assigned value") # assign a value to an invalid field self.assertRaises(AttributeError, self.ind_piece.metadata, 'field', 2) # access an invalid value self.assertRaises(AttributeError, self.ind_piece.metadata, 'invalid_field') # try accessing keys with invalid types self.assertRaises(TypeError, self.ind_piece.metadata, 2) self.assertRaises(TypeError, self.ind_piece.metadata, []) self.assertRaises(TypeError, self.ind_piece.metadata, {}) def test_get_data_0(self): """try getting data for a non-Indexer, non-Experimenter class""" non_analyzer = Mock() self.assertRaises(KeyError, self.ind_piece.get_data, non_analyzer) def test_get_data_1(self): """ That get_data() complains when an Indexer expects the results of another Indexer but doesn't get them. """ mock_indexer_cls = type('MockIndexer', (Indexer,), {}) mock_indexer_cls.required_score_type = pandas.DataFrame self.assertRaises(RuntimeWarning, self.ind_piece.get_data, vis.analyzers.indexers.ngram.NGramIndexer) def test_get_data_2(self): """ That get_data() complains when you call it with something that isn't either an Indexer or Experimenter. """ self.assertRaises(KeyError, self.ind_piece.get_data, TestIndexedPieceA) def test_get_nrindex_1(self): """That _get_noterest() returns self._analyses['noterest'] if it's not None.""" # pylint: disable=W0212 self.ind_piece._analyses['noterest'] = 42 self.assertEqual(42, self.ind_piece._get_noterest()) def test_str_1(self): """__str__() without having imported yet""" # NB: adjusting _imported is the whole point of the test # pylint: disable=W0212 self.ind_piece._imported = False with patch('vis.models.indexed_piece.IndexedPiece.metadata') as mock_meta: mock_meta.return_value = '42' expected = '<IndexedPiece (42)>' actual = self.ind_piece.__str__() self.assertEqual(expected, actual) mock_meta.assert_called_once_with('pathname') def test_str_2(self): """__str__() with proper title and composer""" # NB: adjusting _imported is the whole point of the test # pylint: disable=W0212 self.ind_piece._imported = True with patch('vis.models.indexed_piece.IndexedPiece.metadata') as mock_meta: returns = ['some ridiculous piece', 'a no-name composer'] def side_effect(*args): # NB: we need to accept "args" as a mock framework formality # pylint: disable=W0613 return returns.pop(0) mock_meta.side_effect = side_effect expected = '<IndexedPiece (some ridiculous piece by a no-name composer)>' actual = self.ind_piece.__str__() self.assertEqual(expected, actual) expected_calls = [call('title'), call('composer')] self.assertSequenceEqual(expected_calls, mock_meta.call_args_list)
def test_meta(self): meta = os.path.join(VIS_PATH, 'tests', 'corpus', 'meta') piece = os.path.join(VIS_PATH, 'tests', 'corpus', 'Missa-Fortuna-desperata_Kyrie_Josquin-Des-Prez_file6.xml') ind = IndexedPiece(piece, metafile=meta) self.assertEqual('Sacred', ind.metadata('religiosity'))