Repository navigation
Expand file tree
/
Copy pathtest_lambda.py
More file actions
43 lines (35 loc) · 1.69 KB
/
Copy pathtest_lambda.py
File metadata and controls
43 lines (35 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import unittest
from unittest.mock import patch, MagicMock
import os
import json
import lambda_function
import datetime
class TestLambdaFunction(unittest.TestCase):
def setUp(self):
os.environ['SIMPLEPUSH_KEY'] = 'TEST_KEY'
@patch('lambda_function.urllib.request.urlopen')
def test_send_notification_success(self, mock_urlopen):
# Mock successful response
mock_response = MagicMock()
mock_response.read.return_value = b'{"status": "OK"}'
mock_response.__enter__.return_value = mock_response
mock_urlopen.return_value = mock_response
# Force time to be within window (e.g., 12:00 PM EST)
# We need to patch get_eastern_time or datetime
with patch('lambda_function.get_eastern_time') as mock_time:
mock_time.return_value = datetime.datetime(2023, 1, 1, 12, 0, 0) # 12 PM
response = lambda_function.lambda_handler(None, None)
self.assertEqual(response['statusCode'], 200)
self.assertIn('Hydration reminder sent!', response['body'])
@patch('lambda_function.urllib.request.urlopen')
def test_outside_window(self, mock_urlopen):
# Force time to be outside window (e.g., 4:00 AM EST)
with patch('lambda_function.get_eastern_time') as mock_time:
mock_time.return_value = datetime.datetime(2023, 1, 1, 4, 0, 0) # 4 AM
response = lambda_function.lambda_handler(None, None)
self.assertEqual(response['statusCode'], 200)
self.assertIn('Outside active hours', response['body'])
# Ensure no request was made
mock_urlopen.assert_not_called()
if __name__ == '__main__':
unittest.main()