def test_upsert_review(mock_upsert_record, mock_calculate_ttl): # Given user_id = "test-user-id" review_id = ReviewId("test-biz-id", "test-review-id") review_metadata = ReviewMetadata("test-biz-name", "test-biz-address", "test-review-data") ttl = "test-ttl" mock_calculate_ttl.return_value = ttl # When upsert_review(user_id, review_id, review_metadata) # Then mock_upsert_record.assert_called_once_with( user_id, "Review#test-biz-id", "set BizId=:biz_id, ReviewId=:review_id, BizName=:biz_name, BizAddress=:biz_address, ReviewDate=:review_date, TimeToLive=:ttl", { ":biz_id": review_id.biz_id, ":review_id": review_id.review_id, ":biz_name": review_metadata.biz_name, ":biz_address": review_metadata.biz_address, ":review_date": review_metadata.review_date, ":ttl": ttl, }, )
def test_write_result(mock_upsert_review): # Given user_id = "test-user-id" url = f"https://www.yelp.com/user_details_reviews_self?userid={user_id}" result = ParsedReviewsPage([ ParsedReviewMetadata( "test-biz-id-1", "test-biz-name-1", "test-biz-address-1", "test-review-id-1", "test-review-date-1", ), ParsedReviewMetadata( "test-biz-id-2", "test-biz-name-2", "test-biz-address-2", "test-review-id-2", "test-review-date-2", ), ]) # When parser = ReviewsPageParser() parser.write_result(url, result) # Then mock_upsert_review.assert_has_calls([ call( user_id=user_id, review_id=ReviewId("test-biz-id-1", "test-review-id-1"), review_metadata=ReviewMetadata("test-biz-name-1", "test-biz-address-1", "test-review-date-1"), ), call( user_id=user_id, review_id=ReviewId("test-biz-id-2", "test-review-id-2"), review_metadata=ReviewMetadata("test-biz-name-2", "test-biz-address-2", "test-review-date-2"), ), ])
def write_result(self, url, result: ParsedResult): for scraped in result.reviews: upsert_review( user_id=ReviewsPageParser.get_user_id_from_url(url), review_id=ReviewId(biz_id=scraped.biz_id, review_id=scraped.review_id), review_metadata=ReviewMetadata( biz_name=scraped.biz_name, biz_address=scraped.biz_address, review_date=scraped.review_date, ), )
def test_update_review_status(mock_upsert_record): # Given user_id = "test-user-id" review_id = ReviewId("test-biz-id", "test-review-id") status = "test-status" # When update_review_status(user_id, review_id, status) # Then mock_upsert_record.assert_called_once_with( user_id, "Review#test-biz-id", "set ReviewStatus=:status", {":status": status}, )
def test_parse(): # Given soup = to_soup( get_file("unit/resources/biz/las-galas-los-angeles?hrid=q2pionpcY_-WZPwSWelTFw_dead.html") ) # When result = ReviewStatusParser().parse( "https://yelp.com/biz/las-galas-los-angeles?hrid=q2pionpcY_-WZPwSWelTFw", soup ) # Then assert result == ParsedReviewStatus( review_id_tuple=ReviewId( biz_id="las-galas-los-angeles", review_id="q2pionpcY_-WZPwSWelTFw" ), is_alive=False, )
def test_write_result(mock_upsert_review, mock_get_user_id_from_review_id): # Given review_id_tuple = ReviewId(biz_id="test-biz-id", review_id="test-review-id") is_alive = True result = ParsedReviewStatus( review_id_tuple=review_id_tuple, is_alive=is_alive, ) user_id = "test-user-id" mock_get_user_id_from_review_id.return_value = user_id # When parser = ReviewStatusParser() parser.write_result("", result) # Then mock_upsert_review.assert_called_once_with( user_id=user_id, review_id=review_id_tuple, status=is_alive )
def test_parse_dead(): # Given soup = to_soup( get_file( "unit/resources/biz/thanh-son-tofu-garden-grove-3?hrid=OcEneH8BXu1z8-fpFFyrAg_alive.html" ) ) # When result = ReviewStatusParser().parse( "https://yelp.com/biz/thanh-son-tofu-garden-grove-3?hrid=OcEneH8BXu1z8-fpFFyrAg", soup, ) # Then assert result == ParsedReviewStatus( review_id_tuple=ReviewId( biz_id="thanh-son-tofu-garden-grove-3", review_id="OcEneH8BXu1z8-fpFFyrAg" ), is_alive=True, )
def get_review_id_from_url(url) -> ReviewId: biz_id = re.search(r"/biz/([a-z0-9-]+)[\?&]?", url).group(1) review_id = re.search(r"hrid=(\S+)&?", url).group(1) return ReviewId(biz_id, review_id)