[server]: Update return_all_coordinates unit tests
This commit is contained in:
@ -5,34 +5,32 @@ import os
|
|||||||
from functions.return_all_coordinates.lambda_function import lambda_handler
|
from functions.return_all_coordinates.lambda_function import lambda_handler
|
||||||
|
|
||||||
|
|
||||||
class TestReturnAllCoordinates(unittest.TestCase):
|
class TestReturnLatestCoordinates(unittest.TestCase):
|
||||||
|
|
||||||
# Mock environment variable before each test
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
patch.dict(os.environ, {'TABLE_NAME': 'test-table'}).start()
|
patch.dict(os.environ, {'TABLE_NAME': 'test-table'}).start()
|
||||||
|
|
||||||
# Clean up patches after each test
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
patch.stopall()
|
patch.stopall()
|
||||||
|
|
||||||
@patch('functions.return_all_coordinates.lambda_function.dynamodb.Table')
|
@patch('functions.return_all_coordinates.lambda_function.dynamodb.Table')
|
||||||
def test_lambda_handler_with_coordinates(self, mock_table):
|
def test_lambda_handler_with_coordinates(self, mock_table):
|
||||||
"""Test function when the database contains valid latitude and longitude values."""
|
"""Test function when the database contains valid latitude and longitude values."""
|
||||||
# Mock scan response with items containing latitude & longitude
|
# First scan returns timestamps
|
||||||
mock_table.return_value.scan.return_value = {
|
# Second scan returns matching items
|
||||||
'Items': [
|
mock_table.return_value.scan.side_effect = [
|
||||||
{'latitude': 53.3498, 'longitude': -6.2603},
|
{'Items': [{'timestamp': '1001'}, {'timestamp': '1002'}]}, # First scan: get timestamps
|
||||||
{'latitude': 51.8985, 'longitude': -8.4756}
|
{'Items': [ # Second scan: get items with latest timestamp
|
||||||
|
{'timestamp': '1002', 'latitude': 53.3498, 'longitude': -6.2603},
|
||||||
|
{'timestamp': '1002', 'latitude': 51.8985, 'longitude': -8.4756}
|
||||||
|
]}
|
||||||
]
|
]
|
||||||
}
|
|
||||||
|
|
||||||
event = {} # No query parameters needed
|
event = {}
|
||||||
result = lambda_handler(event, {})
|
result = lambda_handler(event, {})
|
||||||
self.assertEqual(result['statusCode'], 200)
|
self.assertEqual(result['statusCode'], 200)
|
||||||
|
|
||||||
# Parse result body
|
|
||||||
body = json.loads(result['body'])
|
body = json.loads(result['body'])
|
||||||
self.assertIn('coordinates', body)
|
|
||||||
self.assertEqual(len(body['coordinates']), 2)
|
self.assertEqual(len(body['coordinates']), 2)
|
||||||
self.assertEqual(body['coordinates'][0], [53.3498, -6.2603])
|
self.assertEqual(body['coordinates'][0], [53.3498, -6.2603])
|
||||||
self.assertEqual(body['coordinates'][1], [51.8985, -8.4756])
|
self.assertEqual(body['coordinates'][1], [51.8985, -8.4756])
|
||||||
@ -40,11 +38,15 @@ class TestReturnAllCoordinates(unittest.TestCase):
|
|||||||
@patch('functions.return_all_coordinates.lambda_function.dynamodb.Table')
|
@patch('functions.return_all_coordinates.lambda_function.dynamodb.Table')
|
||||||
def test_lambda_handler_with_pagination(self, mock_table):
|
def test_lambda_handler_with_pagination(self, mock_table):
|
||||||
"""Test function correctly handles paginated responses from DynamoDB."""
|
"""Test function correctly handles paginated responses from DynamoDB."""
|
||||||
# Mock paginated scan responses
|
# First scan returns paginated timestamps
|
||||||
|
# Then paginated scan with latest timestamp
|
||||||
mock_table.return_value.scan.side_effect = [
|
mock_table.return_value.scan.side_effect = [
|
||||||
{'Items': [{'latitude': 53.3498, 'longitude': -6.2603}], 'LastEvaluatedKey': 'key1'},
|
{'Items': [{'timestamp': '1001'}], 'LastEvaluatedKey': 'key1'},
|
||||||
{'Items': [{'latitude': 51.8985, 'longitude': -8.4756}], 'LastEvaluatedKey': 'key2'},
|
{'Items': [{'timestamp': '1002'}]}, # Final timestamp batch
|
||||||
{'Items': [{'latitude': 54.5973, 'longitude': -5.9301}]}
|
|
||||||
|
{'Items': [{'timestamp': '1002', 'latitude': 53.3498, 'longitude': -6.2603}], 'LastEvaluatedKey': 'key2'},
|
||||||
|
{'Items': [{'timestamp': '1002', 'latitude': 51.8985, 'longitude': -8.4756}], 'LastEvaluatedKey': 'key3'},
|
||||||
|
{'Items': [{'timestamp': '1002', 'latitude': 54.5973, 'longitude': -5.9301}]}
|
||||||
]
|
]
|
||||||
|
|
||||||
event = {}
|
event = {}
|
||||||
@ -60,12 +62,13 @@ class TestReturnAllCoordinates(unittest.TestCase):
|
|||||||
@patch('functions.return_all_coordinates.lambda_function.dynamodb.Table')
|
@patch('functions.return_all_coordinates.lambda_function.dynamodb.Table')
|
||||||
def test_lambda_handler_with_no_coordinates(self, mock_table):
|
def test_lambda_handler_with_no_coordinates(self, mock_table):
|
||||||
"""Test function when no items contain latitude or longitude."""
|
"""Test function when no items contain latitude or longitude."""
|
||||||
mock_table.return_value.scan.return_value = {
|
mock_table.return_value.scan.side_effect = [
|
||||||
'Items': [
|
{'Items': [{'timestamp': '1001'}, {'timestamp': '1002'}]},
|
||||||
{'objectID': '1', 'objectType': 'Bus'}, # Missing lat/lon
|
{'Items': [
|
||||||
{'name': 'Train Station'} # Missing lat/lon
|
{'timestamp': '1002', 'objectType': 'Bus'},
|
||||||
|
{'timestamp': '1002', 'name': 'Train Station'}
|
||||||
|
]}
|
||||||
]
|
]
|
||||||
}
|
|
||||||
|
|
||||||
event = {}
|
event = {}
|
||||||
result = lambda_handler(event, {})
|
result = lambda_handler(event, {})
|
||||||
@ -78,13 +81,14 @@ class TestReturnAllCoordinates(unittest.TestCase):
|
|||||||
@patch('functions.return_all_coordinates.lambda_function.dynamodb.Table')
|
@patch('functions.return_all_coordinates.lambda_function.dynamodb.Table')
|
||||||
def test_lambda_handler_with_partial_data(self, mock_table):
|
def test_lambda_handler_with_partial_data(self, mock_table):
|
||||||
"""Test function when some items have lat/lon while others do not."""
|
"""Test function when some items have lat/lon while others do not."""
|
||||||
mock_table.return_value.scan.return_value = {
|
mock_table.return_value.scan.side_effect = [
|
||||||
'Items': [
|
{'Items': [{'timestamp': '1002'}]},
|
||||||
{'latitude': 53.3498, 'longitude': -6.2603},
|
{'Items': [
|
||||||
{'objectID': '1', 'objectType': 'Train'}, # No lat/lon
|
{'timestamp': '1002', 'latitude': 53.3498, 'longitude': -6.2603},
|
||||||
{'latitude': 54.5973} # Missing longitude
|
{'timestamp': '1002', 'objectType': 'Train'},
|
||||||
|
{'timestamp': '1002', 'latitude': 54.5973}
|
||||||
|
]}
|
||||||
]
|
]
|
||||||
}
|
|
||||||
|
|
||||||
event = {}
|
event = {}
|
||||||
result = lambda_handler(event, {})
|
result = lambda_handler(event, {})
|
||||||
|
Reference in New Issue
Block a user