Beispiel #1
0
def test_scrape_no_reviews():
    """
    Tests that the scrape function works for page with no reviews
    """
    scraper = DrugRatingzScraper()
    scraper.scrape(
        'https://www.drugratingz.com/reviews/18589/Drug-Adderall.html')
    assert len(scraper.reviews) == 0
Beispiel #2
0
def test_scrape_with_parameters():
    """
    Tests that, when calling the scrape function with a scraper of non-default parameters, the
    correct types of data are stored in the 'reviews' attribute
    """
    scraper = DrugRatingzScraper(collect_urls=True)
    scraper.scrape('https://www.drugratingz.com/reviews/472/Drug-Lutera.html')
    assert len(scraper.reviews) > 10
    data_collected = list(scraper.reviews[0].keys())
    assert len(data_collected) == 5
    assert 'url' in data_collected
Beispiel #3
0
def test_scrape_empty_reviews():
    """
    Tests to make sure that the scrape function would discard the reviews
    of a scraper object that already has data collected in 'reviews'
    """
    scraper = DrugRatingzScraper()
    scraper.scrape(
        'https://www.drugratingz.com/reviews/258/Drug-Glucophage.html')
    num_reviews = len(scraper.reviews)
    scraper.scrape(
        'https://www.drugratingz.com/reviews/258/Drug-Glucophage.html')
    assert num_reviews == len(scraper.reviews)
Beispiel #4
0
def test_scrape_correct_review_data():
    """
    Tests to make sure that the last review in the scraped reviews list has
    the correct data when the scrape function is called
    (this data is from the oldest review of the drug)
    """
    scraper = DrugRatingzScraper(collect_urls=True)
    scraper.scrape(
        'https://www.drugratingz.com/reviews/141/Drug-Prednisone.html')
    assert scraper.reviews[-1]['comment'][:10] == 'This is a '
    assert scraper.reviews[-1]['comment'][-10:] == 'ing on it.'
    assert scraper.reviews[-1]['rating']['effectiveness'] == 5
    assert scraper.reviews[-1]['rating']['no side effects'] == 1
    assert scraper.reviews[-1]['rating']['convenience'] == 1
    assert scraper.reviews[-1]['rating']['value'] == 3
    assert scraper.reviews[-1]['date'] == '8/18/05'
Beispiel #5
0
def test_scrape_assert_title_error():
    """
    Tests that when the scrape function is called with an invalid url that does have a
    title, but the title is wrong (doesn't have the phrase 'drug reviews') that an AssertionError
    is raised and the function returns 0
    """
    scraper = DrugRatingzScraper()
    returned = scraper.scrape('https://www.drugratingz.com/ShowThingCats.jsp')
    assert returned == 0
Beispiel #6
0
def test_scrape_invalid_url_no_title():
    """
    Tests that when the scrape function is called on a url that lacks a title
    (invalid url), it raises an AttributeError and returns 'None'
    """
    scraper = DrugRatingzScraper()
    returned = scraper.scrape(
        'https://www.drugratingz.com/reviews/258/Drug.html')
    assert not returned
Beispiel #7
0
def test_drugratingz_scrape():
    """Test drug ratingz scrape"""
    input_url = 'https://www.drugratingz.com/reviews/75/Drug-Adderall-XR.html'
    drug_scraper = DrugRatingzScraper()
    review_list = drug_scraper.scrape(input_url)
    assert len(review_list) > 5

    keys = list(review_list[-1].keys())
    assert 'comment' in keys
    assert 'effectiveness' in keys
    assert 'no side effects' in keys
    assert 'convenience' in keys
    assert 'value' in keys