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 setup_test_case(): stream_setup() ddb_setup()
def stream_setup(): from test.test_stream_helper import stream_setup return stream_setup(os.environ["STREAM_NAME"])
def create_kinesis_streams(): stream_setup(os.environ["STREAM_NAME"])
def test_search_comments_with_replies(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 - timedelta(days=2)).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": datetime.now( timezone.utc).strftime(api_response_time_format), "updatedAt": datetime.now( timezone.utc).strftime(api_response_time_format), }, }, "videoId": video_id, }, "replies": { "comments": [{ "id": "fakeCommentId#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(api_response_time_format), "updatedAt": datetime.now( timezone.utc).strftime(api_response_time_format), }, }] }, }], "kind": "youtube#commentThreadListResponse", "pageInfo": { "resultsPerPage": 100, "totalResults": 1 }, "nextPageToken": None, } assert None == search_comments(event)