From 358667356f49d58cc6ada36f9de98f1a987684db Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 30 Sep 2016 14:18:50 +0200 Subject: [PATCH] Bulk up the tests --- openlp/plugins/songs/lib/songselect.py | 1 - .../openlp_plugins/songs/test_songselect.py | 60 +++++++++++++++---- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/openlp/plugins/songs/lib/songselect.py b/openlp/plugins/songs/lib/songselect.py index 9c4aab21a..222187ade 100644 --- a/openlp/plugins/songs/lib/songselect.py +++ b/openlp/plugins/songs/lib/songselect.py @@ -23,7 +23,6 @@ The :mod:`~openlp.plugins.songs.lib.songselect` module contains the SongSelect importer itself. """ import logging -import sys import random import re from http.cookiejar import CookieJar diff --git a/tests/functional/openlp_plugins/songs/test_songselect.py b/tests/functional/openlp_plugins/songs/test_songselect.py index 149ec5d65..fef4d4012 100644 --- a/tests/functional/openlp_plugins/songs/test_songselect.py +++ b/tests/functional/openlp_plugins/songs/test_songselect.py @@ -26,16 +26,16 @@ import os from unittest import TestCase from urllib.error import URLError +from bs4 import NavigableString from PyQt5 import QtWidgets -from tests.helpers.songfileimport import SongImportTestHelper from openlp.core import Registry from openlp.plugins.songs.forms.songselectform import SongSelectForm, SearchWorker from openlp.plugins.songs.lib import Song from openlp.plugins.songs.lib.songselect import SongSelectImport, LOGOUT_URL, BASE_URL -from openlp.plugins.songs.lib.importers.cclifile import CCLIFileImport from tests.functional import MagicMock, patch, call +from tests.helpers.songfileimport import SongImportTestHelper from tests.helpers.testmixin import TestMixin TEST_PATH = os.path.abspath( @@ -391,22 +391,47 @@ class TestSongSelectImport(TestCase, TestMixin): Test that the get_song() method returns the correct song details """ # GIVEN: A bunch of mocked out stuff and an importer object - mocked_song_page = MagicMock() - mocked_copyright = MagicMock() - mocked_copyright.find_all.return_value = [MagicMock(string='Copyright 1'), MagicMock(string='Copyright 2')] - mocked_song_page.find.side_effect = [ - mocked_copyright, - MagicMock(find=MagicMock(string='CCLI: 123456')) + mocked_ul_copyright = MagicMock() + mocked_ul_copyright.find.side_effect = [True, False] + mocked_ul_copyright.find_all.return_value = [ + 'Copyright:', + MagicMock(string='Copyright 1'), + MagicMock(string='Copyright 2') ] + + mocked_ul_themes = MagicMock() + mocked_ul_themes.find.side_effect = [False, True] + mocked_ul_themes.find_all.return_value = [ + 'Themes:', + MagicMock(string='Theme 1'), + MagicMock(string='Theme 2') + ] + + mocked_ccli = MagicMock(string='CCLI: 123456 ') + mocked_find_strong = MagicMock(return_value=mocked_ccli) + mocked_find_li = MagicMock(return_value=mocked_find_strong) + mocked_find_ul = MagicMock(return_value=mocked_find_li) + + mocked_song_page = MagicMock() + mocked_song_page.find_all.return_value = [ + mocked_ul_copyright, + mocked_ul_themes + ] + mocked_song_page.find.return_value = mocked_find_ul + mocked_lyrics_page = MagicMock() mocked_find_all = MagicMock() mocked_find_all.side_effect = [ [ MagicMock(contents='The Lord told Noah: there\'s gonna be a floody, floody'), - MagicMock(contents='So, rise and shine, and give God the glory, glory'), + MagicMock(contents=NavigableString('So, rise and shine, and give God the glory, glory')), MagicMock(contents='The Lord told Noah to build him an arky, arky') ], - [MagicMock(string='Verse 1'), MagicMock(string='Chorus'), MagicMock(string='Verse 2')] + [ + MagicMock(string='Verse 1'), + MagicMock(string='Chorus'), + MagicMock(string='Verse 2') + ] ] mocked_lyrics_page.find.return_value = MagicMock(find_all=mocked_find_all) MockedBeautifulSoup.side_effect = [mocked_song_page, mocked_lyrics_page] @@ -418,8 +443,13 @@ class TestSongSelectImport(TestCase, TestMixin): result = importer.get_song(fake_song, callback=mocked_callback) # THEN: The callback should have been called three times and the song should be returned + mocked_song_page.find_all.assert_called_once_with('ul', 'song-meta-list') + self.assertEqual(2, mocked_ul_copyright.find.call_count) + self.assertEqual(1, mocked_ul_copyright.find_all.call_count) + self.assertEqual(2, mocked_ul_themes.find.call_count) + self.assertEqual(1, mocked_ul_themes.find_all.call_count) self.assertEqual(3, mocked_callback.call_count, 'The callback should have been called twice') - self.assertIsNotNone(result, 'The get_song() method should have returned a song dictionary') + self.assertIsInstance(result, dict, '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('div', 'song-viewer lyrics'), call('div', 'song-viewer lyrics')], @@ -428,7 +458,9 @@ class TestSongSelectImport(TestCase, TestMixin): self.assertEqual([call('p'), call('h3')], mocked_find_all.call_args_list, 'The find_all() method should have been called with the right arguments') self.assertIn('copyright', result, 'The returned song should have a copyright') + self.assertEqual('Copyright 1/Copyright 2', result['copyright']) self.assertIn('ccli_number', result, 'The returned song should have a CCLI number') + # self.assertEqual('123456', result['ccli_number'], result['ccli_number']) self.assertIn('verses', result, 'The returned song should have verses') self.assertEqual(3, len(result['verses']), 'Three verses should have been returned') @@ -444,7 +476,7 @@ class TestSongSelectImport(TestCase, TestMixin): 'authors': ['Public Domain'], 'verses': [ {'label': 'Verse 1', 'lyrics': 'The Lord told Noah: there\'s gonna be a floody, floody'}, - {'label': 'Chorus 1', 'lyrics': 'So, rise and shine, and give God the glory, glory'}, + {'label': 'Chorus', 'lyrics': 'So, rise and shine, and give God the glory, glory'}, {'label': 'Verse 2', 'lyrics': 'The Lord told Noah to build him an arky, arky'} ], 'copyright': 'Public Domain', @@ -520,7 +552,8 @@ class TestSongSelectImport(TestCase, TestMixin): {'label': 'Verse 2', 'lyrics': 'The Lord told Noah to build him an arky, arky'} ], 'copyright': 'Public Domain', - 'ccli_number': '123456' + 'ccli_number': '123456', + 'topics': ['Grace', 'Love'] } MockedAuthor.display_name.__eq__.return_value = False MockedTopic.name.__eq__.return_value = False @@ -540,6 +573,7 @@ class TestSongSelectImport(TestCase, TestMixin): MockedAuthor.populate.assert_called_with(first_name='Unknown', last_name='', display_name='Unknown') self.assertEqual(1, len(result.authors_songs), 'There should only be one author') + # self.assertEqual(2, len(result.topics), 'There should only be two topics') class TestSongSelectForm(TestCase, TestMixin):