# THEN: The correct formatted results are returned self.assertEqual(search_results, [[123, 'My Song', 'My alternative']])
+
+ @patch('openlp.plugins.songs.lib.mediaitem.Book')
+ @patch('openlp.plugins.songs.lib.mediaitem.SongBookEntry')
+ @patch('openlp.plugins.songs.lib.mediaitem.Song')
+ @patch('openlp.plugins.songs.lib.mediaitem.or_')
+ def test_entire_song_search(self, mocked_or, MockedSong, MockedSongBookEntry, MockedBook):
+ """
+ Test that searching the entire song does the right queries
+ """
+ # GIVEN: A song media item, some keywords and some mocks
+ keywords = 'Jesus'
+ mocked_song = MagicMock()
+ mocked_or.side_effect = lambda a, b, c, d, e: ' '.join([a, b, c, d, e])
+ MockedSong.search_title.like.side_effect = lambda a: a
+ MockedSong.search_lyrics.like.side_effect = lambda a: a
+ MockedSong.comments.like.side_effect = lambda a: a
+ MockedSongBookEntry.entry.like = lambda a: a
+ MockedBook.name.like = lambda a: a
+ self.mocked_plugin.manager.get_all_objects.return_value = [mocked_song]
+
+ # WHEN: search_entire_song() is called with the keywords
+ result = self.media_item.search_entire(keywords)
+
+ # THEN: The correct calls were made
+ MockedSong.search_title.like.assert_called_once_with('Jesus')
+ MockedSong.search_lyrics.like.assert_called_once_with('Jesus')
+ MockedSong.comment.like.assert_called_once_with('Jesus')
+ MockedSongBookEntry.entry.like.assert_called_once_with('Jesus')
+ MockedBook.name.like.assert_called_once_with('Jesus')
+ mocked_or.assert_called_once_with('Jesus', 'Jesus', 'Jesus', 'Jesus', 'Jesus')
+ self.mocked_plugin.manager.get_all_objects.assert_called_once_with(MockedSong,
+ 'Jesus Jesus Jesus Jesus Jesus')
+ assert result == [mocked_song]
Here, I've fixed your issues for you and written a test for you. Next time it's on you.
=== modified file 'openlp/ plugins/ songs/lib/ mediaitem. py' plugins/ songs/lib/ mediaitem. py 2017-06-03 10:44:28 +0000 plugins/ songs/lib/ mediaitem. py 2017-06-04 20:18:23 +0000
--- openlp/
+++ openlp/
@@ -231,13 +231,13 @@
def search_entire(self, search_keywords):
search_ string = '%{text} %'.format( text=clean_ string( search_ keywords) ) manager. session. query(Song) ntry, isouter=True) or_(Book. name.like( search_ string) , SongBookEntry. entry.like( search_ string) , title.like( search_ string) , Song.search_ lyrics. like(search_ string) , like(search_ string) , Song.alternate_ title.like( search_ string) )) manager. session. query(Song) \ ntry, isouter=True)\ or_(Book. name.like( search_ string) , SongBookEntry. entry.like( search_ string) , title.like( search_ string) , Song.search_ lyrics. like(search_ string) , like(search_ string) ))\
- return (self.plugin.
- .join(SongBookE
- .join(Book, isouter=True)
- .filter(
- Song.search_
- Song.comments.
- .all())
+ return self.plugin.
+ .join(SongBookE
+ .join(Book, isouter=True)\
+ .filter(
+ Song.search_
+ Song.comments.
+ .all()
def on_song_ list_load( self):
"""
=== modified file 'tests/ functional/ openlp_ plugins/ songs/test_ mediaitem. py' l/openlp_ plugins/ songs/test_ mediaitem. py 2017-04-24 05:17:55 +0000 l/openlp_ plugins/ songs/test_ mediaitem. py 2017-06-04 20:21:52 +0000
Registry. create( )
Registry( ).register( 'service_ list', MagicMock())
Registry( ).register( 'main_window' , MagicMock()) openlp. core.lib. mediamanagerite m.MediaManagerI tem._setup' ), \
patch( 'openlp. plugins. songs.forms. editsongform. EditSongForm. __init_ _'):
self. media_item. save_auto_ select_ id = MagicMock()
self. media_item. list_view = MagicMock()
self. media_item. list_view. save_auto_ select_ id = MagicMock()
--- tests/functiona
+++ tests/functiona
@@ -46,9 +46,10 @@
+ self.mocked_plugin = MagicMock()
with patch('
- self.media_item = SongMediaItem(None, MagicMock())
+ self.media_item = SongMediaItem(None, self.mocked_plugin)
@@ -558,3 +559,36 @@
# THEN: The correct formatted results are returned
self. assertEqual( search_ results, [[123, 'My Song', 'My alternative']]) 'openlp. plugins. songs.lib. mediaitem. Book') 'openlp. plugins. songs.lib. mediaitem. SongBookEntry' ) 'openlp. plugins. songs.lib. mediaitem. Song') 'openlp. plugins. songs.lib. mediaitem. or_') song_search( self, mocked_or, MockedSong, MockedSongBookE ntry, MockedBook): or.side_ effect = lambda a, b, c, d, e: ' '.join([a, b, c, d, e]) search_ title.like. side_effect = lambda a: a search_ lyrics. like.side_ effect = lambda a: a comments. like.side_ effect = lambda a: a ntry.entry. like = lambda a: a name.like = lambda a: a plugin. manager. get_all_ objects. return_ value = [mocked_song] entire_ song() is called with the keywords item.search_ entire( keywords) search_ title.like. assert_ called_ once_with( 'Jesus' ) search_ lyrics. like.assert_ called_ once_with( 'Jesus' ) comment. like.assert_ called_ once_with( 'Jesus' ) ntry.entry. like.assert_ called_ once_with( 'Jesus' ) name.like. assert_ called_ once_with( 'Jesus' ) or.assert_ called_ once_with( 'Jesus' , 'Jesus', 'Jesus', 'Jesus', 'Jesus') plugin. manager. get_all_ objects. assert_ called_ once_with( MockedSong,
+
+ @patch(
+ @patch(
+ @patch(
+ @patch(
+ def test_entire_
+ """
+ Test that searching the entire song does the right queries
+ """
+ # GIVEN: A song media item, some keywords and some mocks
+ keywords = 'Jesus'
+ mocked_song = MagicMock()
+ mocked_
+ MockedSong.
+ MockedSong.
+ MockedSong.
+ MockedSongBookE
+ MockedBook.
+ self.mocked_
+
+ # WHEN: search_
+ result = self.media_
+
+ # THEN: The correct calls were made
+ MockedSong.
+ MockedSong.
+ MockedSong.
+ MockedSongBookE
+ MockedBook.
+ mocked_
+ self.mocked_
+ 'Jesus Jesus Jesus Jesus Jesus')
+ assert result == [mocked_song]