aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_extract_lambda.py46
1 files changed, 45 insertions, 1 deletions
diff --git a/tests/test_extract_lambda.py b/tests/test_extract_lambda.py
index e94a8a4..4b61b83 100644
--- a/tests/test_extract_lambda.py
+++ b/tests/test_extract_lambda.py
@@ -3,9 +3,10 @@ import boto3
from moto import mock_aws
from unittest.mock import patch, MagicMock
from unittest import TestCase
-from src.extract_lambda import list_existing_s3_files, connect_to_database, DBConnectionException, process_and_upload_tables
+from src.extract_lambda import list_existing_s3_files, connect_to_database, DBConnectionException, lambda_handler, process_and_upload_tables
import os
import logging
+import json
@pytest.fixture(scope='class')
def mock_config():
@@ -33,6 +34,49 @@ def s3_client(aws_credentials):
with mock_aws():
yield boto3.client('s3')
+class TestLambdaHandler:
+ def test_lambda_handler_files_processed_and_uploaded_successfully(self, mocker):
+ mock_db = MagicMock()
+ mock_db.run.side_effect = [
+ [['Fruits']],
+ [['Vegetable', 'Sour', 'Green'], ['Berry', 'Sweet', 'Red']],
+ [['Food_type'], ['Flavour'], ['Colour']]
+ ]
+ mock_db.columns.return_value = [{'name': 'Food_type'}, {'name': 'Flavour'}, {'name': 'Colour'}]
+ with patch("src.extract_lambda.connect_to_database", return_value=mock_db):
+ mock_process_and_upload_tables = mocker.patch("src.extract_lambda.process_and_upload_tables", return_value=mock_db)
+ mock_list_existing_s3_files = mocker.patch("src.extract_lambda.list_existing_s3_files", return_value={})
+ event = {}
+ context = {}
+ response = lambda_handler(event, context)
+ assert response['statusCode'] == 200
+ assert json.loads(response['body']) == 'CSV files processed and uploaded successfully.'
+ mock_list_existing_s3_files.assert_called_once()
+ mock_process_and_upload_tables.assert_called_once_with(mock_db, {})
+ mock_db.close.assert_called_once()
+
+ def test_lambda_handler_no_changes_detected_no_files_uploaded(self, mocker):
+ mock_db = MagicMock()
+ mock_db.run.side_effect = [
+ [['Fruits']],
+ [['Vegetable', 'Sour', 'Green'], ['Berry', 'Sweet', 'Red']],
+ [['Food_type'], ['Flavour'], ['Colour']]
+ ]
+ mock_db.columns.return_value = [{'name': 'Food_type'}, {'name': 'Flavour'}, {'name': 'Colour'}]
+
+ with patch("src.extract_lambda.connect_to_database", return_value=mock_db):
+ mock_process_and_upload_tables = mocker.patch("src.extract_lambda.process_and_upload_tables", return_value=False)
+ mock_list_existing_s3_files = mocker.patch("src.extract_lambda.list_existing_s3_files", return_value={})
+ event = {}
+ context = {}
+ response = lambda_handler(event, context)
+ assert response['statusCode'] == 200
+ assert json.loads(response['body']) == 'No changes detected, no CSV files were uploaded.'
+ mock_list_existing_s3_files.assert_called_once()
+ mock_process_and_upload_tables.assert_called_once_with(mock_db, {})
+ mock_db.close.assert_called_once()
+
+
class TestListExistingS3Files:
def test_error_if_no_bucket(self, s3_client, caplog):
git.ajschof.me — hosted by ajschofield — powered by cgit