Ejemplo n.º 1
0
 def test_extract_results_single_page(self):
     # set up the mock
     soup_mock = create_autospec(BeautifulSoup)
     soup_mock.find.return_value = 'placeholder'
 
     # stub out extract_links since it's already tested
     funct = Utils.extract_links
     Utils.extract_links = create_autospec(Utils.extract_links)
 
     Utils.extract_results_single_page(soup_mock)
 
     soup_mock.find.assert_called_once_with('div',{'class':'list-group'})
     Utils.extract_links.assert_called_once_with('placeholder')
 
     # reset extract_links back to it's original value
     Utils.extract_links = funct
Ejemplo n.º 2
0
 def test_extract_results_single_page_none(self):
     # set up the mock
     soup_mock = create_autospec(BeautifulSoup)
 
     # set return value of find to None to test None case
     soup_mock.find.return_value = None
     assert Utils.extract_results_single_page(soup_mock) == []
     soup_mock.find.assert_called_once_with('div',{'class':'list-group'})
Ejemplo n.º 3
0
def get_list():
    
    # initialize arguments
    song_type = request.args.get('song_type', type=str)
    letter = request.args.get('letter', type=str)
    testament = request.args.get('testament', type=str)
    
    # make song_type lower case if it isn't None
    if song_type is not None:
        song_type = song_type.lower()
    
    # error checking
    message = None
    if song_type is None:
        message = {Constants.PUBLIC : Constants.ERROR_MESSAGE % SONG_TYPE}
    elif (song_type == 'h' or song_type =='ns') and letter is None:
        message = {Constants.PUBLIC : Constants.ERROR_MESSAGE % SEARCH_LETTER}
    elif song_type == 'scripture' and testament is None:
        message = {Constants.PUBLIC: Constants.ERROR_MESSAGE % TESTAMENT}

    # if message is not None, then return 400 with the message
    if message is not None:
        message['status_code'] = 400
        return (json.dumps(message), 400)
    
    # if song_type is scripture, we have to handle it a little bit differently
    if (song_type == 'scripture'):
        return get_list_scripture(testament)
    
    # data to be returned as json
    json_data = {}

    if song_type == 'nt' or song_type == 'c':
        # New Tunes and Children's songs aren't listed by letter, so create path without the letter
        path = SONG_INDEX_PATH_FORMAT % song_type
    else:
        # make letter uppercase if it isn't already
        letter = letter.upper()
        
        # create url
        path = SONG_INDEX_LETTER_PATH_FORMAT % (song_type, letter)

    # make http GET request
    r = requests.get(URL_FORMAT % path)
    log('request made for: %s' % path)

    # create BeautifulSoup object out of html content
    soup = BeautifulSoup(r.content, "html.parser")

    # extract results
    results = Utils.extract_results_single_page(soup)

    if len(results) == 0:
        json_data[Constants.EMPTY_LIST_MESSAGE] = EMPTY_RESULT_ERROR_MESSAGE
    json_data[Constants.RESULTS] = results

    return json.dumps(json_data, sort_keys=False)
Ejemplo n.º 4
0
def fetch_single_results_page(search_parameter, page_num):
    # make http GET request to search page
    req = requests.get(URL_FORMAT % (search_parameter, page_num))
    log('request sent for: %s, Page %d' % (search_parameter, page_num))
    
    # create BeautifulSoup object out of html content
    soup = BeautifulSoup(req.content, "html.parser")

    # extract results from the single page along with whether page_num is the last page
    return (Utils.extract_results_single_page(soup), Utils.is_last_page(soup, page_num))