From bd57926c803589a891616a8c2c3ac02a8767d517 Mon Sep 17 00:00:00 2001 From: seangchan-ryu-nih Date: Wed, 26 Feb 2020 15:38:23 -0500 Subject: [PATCH] add unittests add test --- tests/data/s3_test_data.json | 7 ++++ tests/data/sqs_test_event.json | 3 ++ tests/unittests/awshelper_t/__init__.py | 0 tests/unittests/awshelper_t/test_awsenv.py | 45 ++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 tests/data/s3_test_data.json create mode 100644 tests/data/sqs_test_event.json create mode 100644 tests/unittests/awshelper_t/__init__.py create mode 100644 tests/unittests/awshelper_t/test_awsenv.py diff --git a/tests/data/s3_test_data.json b/tests/data/s3_test_data.json new file mode 100644 index 0000000..0e2000e --- /dev/null +++ b/tests/data/s3_test_data.json @@ -0,0 +1,7 @@ +{ + "data_id": "test_1", + "content": { + "name": "test_name", + "blah": "test_blah" + } +} \ No newline at end of file diff --git a/tests/data/sqs_test_event.json b/tests/data/sqs_test_event.json new file mode 100644 index 0000000..2619d67 --- /dev/null +++ b/tests/data/sqs_test_event.json @@ -0,0 +1,3 @@ +{"Records": [{"eventSource": "aws:sqs", + "body": "{\"key\": \"test_path/data.json\"]}" + }]} \ No newline at end of file diff --git a/tests/unittests/awshelper_t/__init__.py b/tests/unittests/awshelper_t/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unittests/awshelper_t/test_awsenv.py b/tests/unittests/awshelper_t/test_awsenv.py new file mode 100644 index 0000000..ddedb35 --- /dev/null +++ b/tests/unittests/awshelper_t/test_awsenv.py @@ -0,0 +1,45 @@ +""" +Test awsenv module. mocking aws ssm access +""" +import json +from unittest.mock import patch +from botocore.exceptions import ClientError +from projectname.awshelper.awsenv import get_params_from_ssm, get_params_from_secretsmanager + + +@patch("projectname.awshelper.awsenv.boto3.Session") +def test_get_ssm_success(boto3_session): + """ + Test successful case when get_params_from_ssm returns param + :param boto_session: + :return: None + """ + PARAM = {'Parameter': {'Value': json.dumps({"TEST_TABLE_NAME": "TEST_TABLE"})}} + boto3_session().client().get_parameter.return_value = PARAM + + response = get_params_from_ssm("/test/config") + assert response == json.loads(PARAM['Parameter']['Value']) + + # When param doesn't exists + boto3_session().client().get_parameter.return_value = {} + response = get_params_from_ssm("/test/config") + assert response == {} + + +@patch("projectname.awshelper.awsenv.boto3.Session") +def test_get_param_success(boto3_session): + """ + Test successful case when get_params_from_ssm returns param + :param boto_session: + :return: None + """ + PARAM = {'SecretString': json.dumps({'SOME_SECRET': "abc"})} + boto3_session().client().get_secret_value.return_value = PARAM + + response = get_params_from_secretsmanager("/test/config") + # assert response == json.loads(PARAM['SecretString']) + + # When param doesn't exists + boto3_session().client().get_secret_value.return_value = {} + response = get_params_from_secretsmanager("/test/config") + assert response == {}