예제 #1
0
    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'
예제 #2
0
    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)
        importer.subscription_level = 'premium'

        # WHEN: The search method is called
        results = importer.search('text', 2, mock_callback)

        # THEN: callback was called twice, open was called once, find_all was called once, max results returned
        assert 2 == mock_callback.call_count, 'callback should have been called twice'
        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')
        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'
예제 #3
0
    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)
        importer.subscription_level = 'premium'

        # 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
        assert 0 == 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 [] == results, 'The search method should have returned an empty list'