def test_search_comments_with_api_throws_error(mock_youtube_resource): api_key = "fakeapikey" ssm_setup(api_key) ddb_setup(os.environ["TARGET_DDB_TABLE"]) kds_client = stream_setup(os.environ["STREAM_NAME"]) video_id = "fakeVideoId" event = { "version": "0", "id": "fakeID", "detailtype": "Video", "source": "com.youtube.video", "account": "fakeaccount", "time": "2020-06-13T23:14:19Z", "region": "us-east-1", "resources": [], "detail": { "VideoId": video_id, "SearchQuery": "fakeQuery", "Title": "fakeTitle" }, } import googleapiclient.errors import mock mock_youtube_resource.return_value.commentThreads.return_value.list.return_value.execute.side_effect = ( googleapiclient.errors.HttpError(mock.Mock(status=403), "Error invoking API".encode("utf-8"))) assert None == search_comments(event)
def test_lambda_search_comments(mock_youtube_resource): api_key = "fakeapikey" ssm_setup(api_key) ddb_setup(os.environ["TARGET_DDB_TABLE"]) kds_client = stream_setup(os.environ["STREAM_NAME"]) video_id = "fakeVideoId" event = { "version": "0", "id": "fakeID", "detailtype": "Video", "source": "com.youtube.video", "account": "fakeaccount", "time": "2020-06-13T23:14:19Z", "region": "us-east-1", "resources": [], "detail": {"VideoId": video_id, "SearchQuery": "fakeQuery", "Title": "fakeTitle"}, } mock_youtube_resource.return_value.commentThreads.return_value.list.return_value.execute.return_value = { "items": [ { "id": "fakeId", "kind": "youtube#commentThread", "snippet": { "topLevelComment": { "id": "fakeCommentId", "kind": "youtube#comment", "snippet": { "textDisplay": "Omg " "love " "it", "textOriginal": "Omg " "love " "it", "videoId": video_id, "viewerRating": 2, "likeCount": 0, "publishedAt": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), "updatedAt": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"), }, }, "videoId": video_id, }, } ], "kind": "youtube#commentThreadListResponse", "pageInfo": {"resultsPerPage": 100, "totalResults": 1}, "nextPageToken": None, } from lambda_function import search_comments search_comments(event, None)
def test_search_comments_not_publishing_record(mock_youtube_resource): api_key = "fakeapikey" ssm_setup(api_key) table_name = os.environ["TARGET_DDB_TABLE"] ddb = ddb_setup(table_name) video_id = "fakeVideoId" current_time = datetime.now(timezone.utc) expiry_window = str( int((current_time + timedelta(days=int( os.environ.get("VIDEO_SEARCH_INGESTION_WINDOW", 7)))).timestamp() * 1000)) ddb_item = { "VIDEO_ID": video_id, "LAST_QUERIED_TIMESTAMP": current_time.isoformat(), "EXP_DATE": { "N": expiry_window }, } table = ddb.Table(table_name) table.put_item(Item=ddb_item) # stream_setup(os.environ["STREAM_NAME"]) event = { "version": "0", "id": "fakeID", "detailtype": "Video", "source": "com.youtube.video", "account": "fakeaccount", "time": "2020-06-13T23:14:19Z", "region": "us-east-1", "resources": [], "detail": { "VideoId": video_id, "SearchQuery": "fakeQuery", "Title": "fakeTitle" }, } mock_youtube_resource.return_value.commentThreads.return_value.list.return_value.execute.return_value = { "items": [{ "id": "fakeId", "kind": "youtube#commentThread", "snippet": { "topLevelComment": { "id": "fakeCommentId", "kind": "youtube#comment", "snippet": { "textDisplay": "Omg " "love " "it", "textOriginal": "Omg " "love " "it", "videoId": video_id, "viewerRating": 2, "likeCount": 0, "publishedAt": (current_time - timedelta(days=3)).strftime(api_response_time_format), "updatedAt": (current_time - timedelta(days=2)).strftime(api_response_time_format), }, }, "videoId": video_id, }, }], "kind": "youtube#commentThreadListResponse", "pageInfo": { "resultsPerPage": 100, "totalResults": 1 }, "nextPageToken": None, } assert None == search_comments(event)