def test_search_returns_ccli_song_number_result(self, MockedBeautifulSoup, mocked_build_opener): """ Test that search can find a single song by CCLI number """ # GIVEN: A bunch of mocked out stuff and an importer object mocked_opener = MagicMock() mocked_build_opener.return_value = mocked_opener mocked_results_page = MagicMock() mocked_results_page.find_all.return_value = [] MockedBeautifulSoup.return_value = mocked_results_page mock_callback = MagicMock() importer = SongSelectImport(None) importer.subscription_level = 'premium' # WHEN: The search is performed results = importer.search('1234567', 1000, mock_callback) # THEN: callback was called once and the results are as expected assert 1 == mock_callback.call_count, 'callback should not have been called' assert 1 == mocked_opener.open.call_count, 'open should have been called once' assert 1 == mocked_results_page.find_all.call_count, 'find_all should have been called once' mocked_results_page.find_all.assert_called_with('div', 'song-result') assert 1 == len(results), 'The search method should have returned an single song in a list' assert 'https://songselect.ccli.com/Songs/1234567' == results[0]['link'],\ 'The correct link should have been returned'
def test_search_returns_no_results(self, MockedBeautifulSoup, mocked_build_opener): """ Test that when the search finds no results, it simply returns an empty list """ # GIVEN: A bunch of mocked out stuff and an importer object mocked_opener = MagicMock() mocked_build_opener.return_value = mocked_opener mocked_results_page = MagicMock() mocked_results_page.find_all.return_value = [] MockedBeautifulSoup.return_value = mocked_results_page mock_callback = MagicMock() importer = SongSelectImport(None) # WHEN: The login method is called after being rigged to fail 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.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('div', 'song-result') self.assertEqual( [], results, 'The search method should have returned an empty list')
def search_returns_two_results_test(self, MockedBeautifulSoup, mocked_build_opener): """ Test that when the search finds 2 results, it simply returns a list with 2 results """ # 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')] # 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')] # rest of the stuff mocked_opener = MagicMock() mocked_build_opener.return_value = mocked_opener mocked_results_page = MagicMock() mocked_results_page.find_all.side_effect = [[mocked_result1, mocked_result2], []] MockedBeautifulSoup.return_value = mocked_results_page mock_callback = MagicMock() importer = SongSelectImport(None) # WHEN: The login method is called after being rigged to fail 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.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'} ] self.assertListEqual(expected_list, results, 'The search method should have returned two songs')
def test_search_reaches_max_results(self, MockedBeautifulSoup, mocked_build_opener): """ Test that when the search finds MAX (2) results, it simply returns a list with those (2) """ # GIVEN: A bunch of mocked out stuff and an importer object # first search result mocked_result1 = MagicMock() 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(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(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 mocked_results_page = MagicMock() mocked_results_page.find_all.side_effect = [[ mocked_result1, mocked_result2, mocked_result3 ], []] MockedBeautifulSoup.return_value = mocked_results_page mock_callback = MagicMock() importer = SongSelectImport(None) # WHEN: The search method is called results = importer.search('text', 2, mock_callback) # THEN: callback was called twice, open was called twice, find_all was called twice, max results returned assert 2 == mock_callback.call_count, 'callback should have been called twice' assert 2 == mocked_opener.open.call_count, 'open should have been called twice' assert 2 == mocked_results_page.find_all.call_count, 'find_all should have been called twice' 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' }] assert expected_list == results, 'The search method should have returned two songs'
def test_search_returns_two_results(self, MockedBeautifulSoup, mocked_build_opener): """ Test that when the search finds 2 results, it simply returns a list with 2 results """ # GIVEN: A bunch of mocked out stuff and an importer object # first search result mocked_result1 = MagicMock() 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(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 mocked_results_page = MagicMock() mocked_results_page.find_all.side_effect = [[ mocked_result1, mocked_result2 ], []] MockedBeautifulSoup.return_value = mocked_results_page mock_callback = MagicMock() importer = SongSelectImport(None) # WHEN: The search method is called 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.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('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')
def test_search_reaches_max_results(self, MockedBeautifulSoup, mocked_build_opener): """ Test that when the search finds MAX (2) results, it simply returns a list with those (2) """ # GIVEN: A bunch of mocked out stuff and an importer object # first search result mocked_result1 = MagicMock() 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(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(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 mocked_results_page = MagicMock() mocked_results_page.find_all.side_effect = [[mocked_result1, mocked_result2, mocked_result3], []] MockedBeautifulSoup.return_value = mocked_results_page mock_callback = MagicMock() importer = SongSelectImport(None) # WHEN: The search method is called results = importer.search('text', 2, mock_callback) # THEN: callback was called twice, open was called twice, find_all was called twice, max results returned 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('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')
def test_search_returns_no_results(self, MockedBeautifulSoup, mocked_build_opener): """ Test that when the search finds no results, it simply returns an empty list """ # GIVEN: A bunch of mocked out stuff and an importer object mocked_opener = MagicMock() mocked_build_opener.return_value = mocked_opener mocked_results_page = MagicMock() mocked_results_page.find_all.return_value = [] MockedBeautifulSoup.return_value = mocked_results_page mock_callback = MagicMock() importer = SongSelectImport(None) # WHEN: The login method is called after being rigged to fail 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.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('div', 'song-result') self.assertEqual([], results, 'The search method should have returned an empty list')
def search_reaches_max_results_test(self, MockedBeautifulSoup, mocked_build_opener): """ Test that when the search finds MAX (2) results, it simply returns a list with those (2) """ # 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') ] # 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') ] # 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') ] # rest of the stuff mocked_opener = MagicMock() mocked_build_opener.return_value = mocked_opener mocked_results_page = MagicMock() mocked_results_page.find_all.side_effect = [[ mocked_result1, mocked_result2, mocked_result3 ], []] MockedBeautifulSoup.return_value = mocked_results_page mock_callback = MagicMock() importer = SongSelectImport(None) # WHEN: The login method is called after being rigged to fail results = importer.search('text', 2, mock_callback) # THEN: callback was never called, open was called once, find_all was called once, an empty list returned 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' }] self.assertListEqual( expected_list, results, 'The search method should have returned two songs')