Fix the tests

This commit is contained in:
Raoul Snyman 2016-08-11 23:13:12 +02:00
parent ae93a6e33d
commit b3893c2c1b
3 changed files with 43 additions and 25 deletions

View File

@ -135,7 +135,7 @@ class Song(BaseModel):
def add_author(self, author, author_type=None):
"""
Add an author to the song if it not yet exists
Add an author to the song if it doesn't exist yet
:param author: Author object
:param author_type: AuthorType constant or None
@ -162,7 +162,7 @@ class Song(BaseModel):
def add_songbook_entry(self, songbook, entry):
"""
Add a Songbook Entry to the song if it not yet exists
Add a Songbook Entry to the song if it doesn't exist yet
:param songbook_name: Name of the Songbook.
:param entry: Entry in the Songbook (usually a number)

View File

@ -252,11 +252,11 @@ class SongSelectImport(object):
last_name = name_parts[1]
author = Author.populate(first_name=first_name, last_name=last_name, display_name=author_name)
db_song.add_author(author)
for topic_name in song['topics']:
for topic_name in song.get('topics', []):
topic = self.db_manager.get_object_filtered(Topic, Topic.name == topic_name)
if not topic:
topic = Topic.populate(name=topic_name)
db_song.topics.append(topic)
db_song.add_topic(topic)
self.db_manager.save_object(db_song)
return db_song

View File

@ -71,7 +71,7 @@ class TestSongSelectImport(TestCase, TestMixin):
mocked_opener = MagicMock()
mocked_build_opener.return_value = mocked_opener
mocked_login_page = MagicMock()
mocked_login_page.find.return_value = {'value': 'blah'}
mocked_login_page.find.side_effect = [{'value': 'blah'}, None]
MockedBeautifulSoup.return_value = mocked_login_page
mock_callback = MagicMock()
importer = SongSelectImport(None)
@ -112,7 +112,7 @@ class TestSongSelectImport(TestCase, TestMixin):
mocked_opener = MagicMock()
mocked_build_opener.return_value = mocked_opener
mocked_login_page = MagicMock()
mocked_login_page.find.side_effect = [{'value': 'blah'}, None]
mocked_login_page.find.side_effect = [{'value': 'blah'}, MagicMock()]
MockedBeautifulSoup.return_value = mocked_login_page
mock_callback = MagicMock()
importer = SongSelectImport(None)
@ -165,7 +165,7 @@ class TestSongSelectImport(TestCase, TestMixin):
self.assertEqual(0, mock_callback.call_count, 'callback should not have been called')
self.assertEqual(1, mocked_opener.open.call_count, 'open should have been called once')
self.assertEqual(1, mocked_results_page.find_all.call_count, 'find_all should have been called once')
mocked_results_page.find_all.assert_called_with('li', 'result pane')
mocked_results_page.find_all.assert_called_with('div', 'song-result')
self.assertEqual([], results, 'The search method should have returned an empty list')
@patch('openlp.plugins.songs.lib.songselect.build_opener')
@ -177,12 +177,18 @@ class TestSongSelectImport(TestCase, TestMixin):
# GIVEN: A bunch of mocked out stuff and an importer object
# first search result
mocked_result1 = MagicMock()
mocked_result1.find.side_effect = [MagicMock(string='Title 1'), {'href': '/url1'}]
mocked_result1.find_all.return_value = [MagicMock(string='Author 1-1'), MagicMock(string='Author 1-2')]
mocked_result1.find.side_effect = [
MagicMock(find=MagicMock(return_value=MagicMock(string='Title 1'))),
MagicMock(string='James, John'),
MagicMock(find=MagicMock(return_value={'href': '/url1'}))
]
# second search result
mocked_result2 = MagicMock()
mocked_result2.find.side_effect = [MagicMock(string='Title 2'), {'href': '/url2'}]
mocked_result2.find_all.return_value = [MagicMock(string='Author 2-1'), MagicMock(string='Author 2-2')]
mocked_result2.find.side_effect = [
MagicMock(find=MagicMock(return_value=MagicMock(string='Title 2'))),
MagicMock(string='Philip'),
MagicMock(find=MagicMock(return_value={'href': '/url2'}))
]
# rest of the stuff
mocked_opener = MagicMock()
mocked_build_opener.return_value = mocked_opener
@ -196,13 +202,14 @@ class TestSongSelectImport(TestCase, TestMixin):
results = importer.search('text', 1000, mock_callback)
# THEN: callback was never called, open was called once, find_all was called once, an empty list returned
self.maxDiff = None
self.assertEqual(2, mock_callback.call_count, 'callback should have been called twice')
self.assertEqual(2, mocked_opener.open.call_count, 'open should have been called twice')
self.assertEqual(2, mocked_results_page.find_all.call_count, 'find_all should have been called twice')
mocked_results_page.find_all.assert_called_with('li', 'result pane')
mocked_results_page.find_all.assert_called_with('div', 'song-result')
expected_list = [
{'title': 'Title 1', 'authors': ['Author 1-1', 'Author 1-2'], 'link': BASE_URL + '/url1'},
{'title': 'Title 2', 'authors': ['Author 2-1', 'Author 2-2'], 'link': BASE_URL + '/url2'}
{'title': 'Title 1', 'authors': ['James', 'John'], 'link': BASE_URL + '/url1'},
{'title': 'Title 2', 'authors': ['Philip'], 'link': BASE_URL + '/url2'}
]
self.assertListEqual(expected_list, results, 'The search method should have returned two songs')
@ -215,16 +222,25 @@ class TestSongSelectImport(TestCase, TestMixin):
# GIVEN: A bunch of mocked out stuff and an importer object
# first search result
mocked_result1 = MagicMock()
mocked_result1.find.side_effect = [MagicMock(string='Title 1'), {'href': '/url1'}]
mocked_result1.find_all.return_value = [MagicMock(string='Author 1-1'), MagicMock(string='Author 1-2')]
mocked_result1.find.side_effect = [
MagicMock(find=MagicMock(return_value=MagicMock(string='Title 1'))),
MagicMock(string='James, John'),
MagicMock(find=MagicMock(return_value={'href': '/url1'}))
]
# second search result
mocked_result2 = MagicMock()
mocked_result2.find.side_effect = [MagicMock(string='Title 2'), {'href': '/url2'}]
mocked_result2.find_all.return_value = [MagicMock(string='Author 2-1'), MagicMock(string='Author 2-2')]
mocked_result2.find.side_effect = [
MagicMock(find=MagicMock(return_value=MagicMock(string='Title 2'))),
MagicMock(string='Philip'),
MagicMock(find=MagicMock(return_value={'href': '/url2'}))
]
# third search result
mocked_result3 = MagicMock()
mocked_result3.find.side_effect = [MagicMock(string='Title 3'), {'href': '/url3'}]
mocked_result3.find_all.return_value = [MagicMock(string='Author 3-1'), MagicMock(string='Author 3-2')]
mocked_result3.find.side_effect = [
MagicMock(find=MagicMock(return_value=MagicMock(string='Title 3'))),
MagicMock(string='Luke, Matthew'),
MagicMock(find=MagicMock(return_value={'href': '/url3'}))
]
# rest of the stuff
mocked_opener = MagicMock()
mocked_build_opener.return_value = mocked_opener
@ -241,9 +257,9 @@ class TestSongSelectImport(TestCase, TestMixin):
self.assertEqual(2, mock_callback.call_count, 'callback should have been called twice')
self.assertEqual(2, mocked_opener.open.call_count, 'open should have been called twice')
self.assertEqual(2, mocked_results_page.find_all.call_count, 'find_all should have been called twice')
mocked_results_page.find_all.assert_called_with('li', 'result pane')
expected_list = [{'title': 'Title 1', 'authors': ['Author 1-1', 'Author 1-2'], 'link': BASE_URL + '/url1'},
{'title': 'Title 2', 'authors': ['Author 2-1', 'Author 2-2'], 'link': BASE_URL + '/url2'}]
mocked_results_page.find_all.assert_called_with('div', 'song-result')
expected_list = [{'title': 'Title 1', 'authors': ['James', 'John'], 'link': BASE_URL + '/url1'},
{'title': 'Title 2', 'authors': ['Philip'], 'link': BASE_URL + '/url2'}]
self.assertListEqual(expected_list, results, 'The search method should have returned two songs')
@patch('openlp.plugins.songs.lib.songselect.build_opener')
@ -337,7 +353,7 @@ class TestSongSelectImport(TestCase, TestMixin):
self.assertIsNotNone(result, 'The get_song() method should have returned a song dictionary')
self.assertEqual(2, mocked_lyrics_page.find.call_count, 'The find() method should have been called twice')
self.assertEqual(2, mocked_find_all.call_count, 'The find_all() method should have been called twice')
self.assertEqual([call('section', 'lyrics'), call('section', 'lyrics')],
self.assertEqual([call('div', 'song-viewer lyrics'), call('div', 'song-viewer lyrics')],
mocked_lyrics_page.find.call_args_list,
'The find() method should have been called with the right arguments')
self.assertEqual([call('p'), call('h3')], mocked_find_all.call_args_list,
@ -419,8 +435,9 @@ class TestSongSelectImport(TestCase, TestMixin):
self.assertEqual(1, len(result.authors_songs), 'There should only be one author')
@patch('openlp.plugins.songs.lib.songselect.clean_song')
@patch('openlp.plugins.songs.lib.songselect.Topic')
@patch('openlp.plugins.songs.lib.songselect.Author')
def save_song_unknown_author_test(self, MockedAuthor, mocked_clean_song):
def save_song_unknown_author_test(self, MockedAuthor, MockedTopic, mocked_clean_song):
"""
Test that saving a song with an author name of only one word performs the correct actions
"""
@ -437,6 +454,7 @@ class TestSongSelectImport(TestCase, TestMixin):
'ccli_number': '123456'
}
MockedAuthor.display_name.__eq__.return_value = False
MockedTopic.name.__eq__.return_value = False
mocked_db_manager = MagicMock()
mocked_db_manager.get_object_filtered.return_value = None
importer = SongSelectImport(mocked_db_manager)