Merge lp:~samothjtm/openlp/bug-1695587 into lp:openlp

Proposed by Johannes Thomas Meyer
Status: Merged
Merged at revision: 2748
Proposed branch: lp:~samothjtm/openlp/bug-1695587
Merge into: lp:openlp
Diff against target: 73 lines (+42/-4)
2 files modified
openlp/plugins/songs/lib/mediaitem.py (+8/-3)
tests/functional/openlp_plugins/songs/test_mediaitem.py (+34/-1)
To merge this branch: bzr merge lp:~samothjtm/openlp/bug-1695587
Reviewer Review Type Date Requested Status
Tim Bentley Approve
Samuel Mehrbrodt (community) Approve
Tomas Groth Approve
Raoul Snyman Pending
Review via email: mp+325118@code.launchpad.net

This proposal supersedes a proposal from 2017-06-03.

Commit message

added SongBook name and Song Number to "Entire Song" Search

To post a comment you must log in.
Revision history for this message
Tomas Groth (tomasgroth) wrote : Posted in a previous version of this proposal

Hi Johannes,

Thank you for contributing to OpenLP!
To get your code merged you must create a test that proves it actually works. Have a look at the wiki for inspiration: https://wiki.openlp.org/Development:Unit_Tests

review: Needs Fixing
Revision history for this message
Johannes Thomas Meyer (samothjtm) wrote : Posted in a previous version of this proposal

I did some manual testing...

My new code is only using one database call - I can't find an example of how and if at all any database calls are being tested. I'd need some help on this... is there a test database with example data that I can just query to proof my code working?

Revision history for this message
Johannes Thomas Meyer (samothjtm) wrote : Posted in a previous version of this proposal

There are still some other tests failing not done by me - even after merging the trunk with some test fixing

lp:~samothjtm/openlp/bug-1695587 (revision 2747)
[←[1;32mSUCCESS←[1;m] https://ci.openlp.io/job/Branch-01-Pull/2060/
[←[1;31mFAILURE←[1;m] https://ci.openlp.io/job/Branch-02-Functional-Tests/1970/
Stopping after failure

Revision history for this message
Johannes Thomas Meyer (samothjtm) wrote : Posted in a previous version of this proposal

I did a test search with 1629 songs in a Database taking aproximately 1 second searching for "1" in Entire Song with my patch. It only seems to take this long to load all the results into the GUI as searching for "11" takes only an instant...

Revision history for this message
Raoul Snyman (raoul-snyman) wrote : Posted in a previous version of this proposal

I've got a couple of comments below for you to take a look at.

Also, as previously noted, you need a code test before we'll accept your merge proposal. I'm afraid that manual testing, while nice, does not constitute a test for merging purposes.

Revision history for this message
Raoul Snyman (raoul-snyman) : Posted in a previous version of this proposal
review: Needs Fixing
Revision history for this message
Raoul Snyman (raoul-snyman) wrote : Posted in a previous version of this proposal
Download full text (4.3 KiB)

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'
--- openlp/plugins/songs/lib/mediaitem.py 2017-06-03 10:44:28 +0000
+++ openlp/plugins/songs/lib/mediaitem.py 2017-06-04 20:18:23 +0000
@@ -231,13 +231,13 @@

     def search_entire(self, search_keywords):
         search_string = '%{text}%'.format(text=clean_string(search_keywords))
- return (self.plugin.manager.session.query(Song)
- .join(SongBookEntry, isouter=True)
- .join(Book, isouter=True)
- .filter(or_(Book.name.like(search_string), SongBookEntry.entry.like(search_string),
- Song.search_title.like(search_string), Song.search_lyrics.like(search_string),
- Song.comments.like(search_string), Song.alternate_title.like(search_string)))
- .all())
+ return self.plugin.manager.session.query(Song)\
+ .join(SongBookEntry, isouter=True)\
+ .join(Book, isouter=True)\
+ .filter(or_(Book.name.like(search_string), SongBookEntry.entry.like(search_string),
+ Song.search_title.like(search_string), Song.search_lyrics.like(search_string),
+ Song.comments.like(search_string)))\
+ .all()

     def on_song_list_load(self):
         """

=== modified file 'tests/functional/openlp_plugins/songs/test_mediaitem.py'
--- tests/functional/openlp_plugins/songs/test_mediaitem.py 2017-04-24 05:17:55 +0000
+++ tests/functional/openlp_plugins/songs/test_mediaitem.py 2017-06-04 20:21:52 +0000
@@ -46,9 +46,10 @@
         Registry.create()
         Registry().register('service_list', MagicMock())
         Registry().register('main_window', MagicMock())
+ self.mocked_plugin = MagicMock()
         with patch('openlp.core.lib.mediamanageritem.MediaManagerItem._setup'), \
                 patch('openlp.plugins.songs.forms.editsongform.EditSongForm.__init__'):
- self.media_item = SongMediaItem(None, MagicMock())
+ self.media_item = SongMediaItem(None, self.mocked_plugin)
             self.media_item.save_auto_select_id = MagicMock()
             self.media_item.list_view = MagicMock()
             self.media_item.list_view.save_auto_select_id = MagicMock()
@@ -558,3 +559,36 @@

         # 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.li...

Read more...

Revision history for this message
Raoul Snyman (raoul-snyman) wrote : Posted in a previous version of this proposal

Once you've fixed up the issues, you'll need to resubmit the merge proposal (link in the top right hand corner)

Revision history for this message
Tomas Groth (tomasgroth) :
review: Approve
Revision history for this message
Samuel Mehrbrodt (sam92) :
review: Approve
Revision history for this message
Tim Bentley (trb143) wrote :

Approved

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'openlp/plugins/songs/lib/mediaitem.py'
2--- openlp/plugins/songs/lib/mediaitem.py 2017-01-25 21:17:27 +0000
3+++ openlp/plugins/songs/lib/mediaitem.py 2017-06-05 21:58:05 +0000
4@@ -231,9 +231,14 @@
5
6 def search_entire(self, search_keywords):
7 search_string = '%{text}%'.format(text=clean_string(search_keywords))
8- return self.plugin.manager.get_all_objects(
9- Song, or_(Song.search_title.like(search_string), Song.search_lyrics.like(search_string),
10- Song.comments.like(search_string)))
11+ return self.plugin.manager.session.query(Song) \
12+ .join(SongBookEntry, isouter=True) \
13+ .join(Book, isouter=True) \
14+ .filter(or_(Book.name.like(search_string), SongBookEntry.entry.like(search_string),
15+ # hint: search_title contains alternate title
16+ Song.search_title.like(search_string), Song.search_lyrics.like(search_string),
17+ Song.comments.like(search_string))) \
18+ .all()
19
20 def on_song_list_load(self):
21 """
22
23=== modified file 'tests/functional/openlp_plugins/songs/test_mediaitem.py'
24--- tests/functional/openlp_plugins/songs/test_mediaitem.py 2017-04-24 05:17:55 +0000
25+++ tests/functional/openlp_plugins/songs/test_mediaitem.py 2017-06-05 21:58:05 +0000
26@@ -46,9 +46,10 @@
27 Registry.create()
28 Registry().register('service_list', MagicMock())
29 Registry().register('main_window', MagicMock())
30+ self.mocked_plugin = MagicMock()
31 with patch('openlp.core.lib.mediamanageritem.MediaManagerItem._setup'), \
32 patch('openlp.plugins.songs.forms.editsongform.EditSongForm.__init__'):
33- self.media_item = SongMediaItem(None, MagicMock())
34+ self.media_item = SongMediaItem(None, self.mocked_plugin)
35 self.media_item.save_auto_select_id = MagicMock()
36 self.media_item.list_view = MagicMock()
37 self.media_item.list_view.save_auto_select_id = MagicMock()
38@@ -558,3 +559,35 @@
39
40 # THEN: The correct formatted results are returned
41 self.assertEqual(search_results, [[123, 'My Song', 'My alternative']])
42+
43+ @patch('openlp.plugins.songs.lib.mediaitem.Book')
44+ @patch('openlp.plugins.songs.lib.mediaitem.SongBookEntry')
45+ @patch('openlp.plugins.songs.lib.mediaitem.Song')
46+ @patch('openlp.plugins.songs.lib.mediaitem.or_')
47+ def test_entire_song_search(self, mocked_or, MockedSong, MockedSongBookEntry, MockedBook):
48+ """
49+ Test that searching the entire song does the right queries
50+ """
51+ # GIVEN: A song media item, a keyword and some mocks
52+ keyword = 'Jesus'
53+ mocked_song = MagicMock()
54+ mocked_or.side_effect = lambda a, b, c, d, e: ' '.join([a, b, c, d, e])
55+ MockedSong.search_title.like.side_effect = lambda a: a
56+ MockedSong.search_lyrics.like.side_effect = lambda a: a
57+ MockedSong.comments.like.side_effect = lambda a: a
58+ MockedSongBookEntry.entry.like.side_effect = lambda a: a
59+ MockedBook.name.like.side_effect = lambda a: a
60+
61+ # WHEN: search_entire_song() is called with the keyword
62+ self.media_item.search_entire(keyword)
63+
64+ # THEN: The correct calls were made
65+ MockedSong.search_title.like.assert_called_once_with('%jesus%')
66+ MockedSong.search_lyrics.like.assert_called_once_with('%jesus%')
67+ MockedSong.comments.like.assert_called_once_with('%jesus%')
68+ MockedSongBookEntry.entry.like.assert_called_once_with('%jesus%')
69+ MockedBook.name.like.assert_called_once_with('%jesus%')
70+ mocked_or.assert_called_once_with('%jesus%', '%jesus%', '%jesus%', '%jesus%', '%jesus%')
71+ self.mocked_plugin.manager.session.query.assert_called_once_with(MockedSong)
72+
73+ self.assertEqual(self.mocked_plugin.manager.session.query.mock_calls[4][0], '().join().join().filter().all')