[server]: Refactor tests to reflect mass-renaming
This commit is contained in:
@ -2,7 +2,7 @@ import unittest
|
||||
from unittest.mock import patch, MagicMock
|
||||
import json
|
||||
import os
|
||||
from functions.permanent_data.permanent_data import (
|
||||
from functions.fetch_permanent_data.lambda_function import (
|
||||
fetch_train_stations_with_type,
|
||||
fetch_train_stations,
|
||||
fetch_luas,
|
||||
@ -15,8 +15,8 @@ from functions.permanent_data.permanent_data import (
|
||||
|
||||
class TestPermanentData(unittest.TestCase):
|
||||
|
||||
@patch('functions.permanent_data.permanent_data.session.get')
|
||||
@patch('functions.permanent_data.permanent_data.xmltodict.parse')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.session.get')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.xmltodict.parse')
|
||||
def test_fetch_train_stations_with_type(self, mock_parse, mock_get):
|
||||
# Mock API response and xmltodict parsing
|
||||
mock_get.return_value.text = '<xml></xml>'
|
||||
@ -39,8 +39,8 @@ class TestPermanentData(unittest.TestCase):
|
||||
self.assertEqual(len(result), 3) # Three types: M, S, D
|
||||
self.assertEqual(result[0]['trainStationCode'], 'DUB')
|
||||
|
||||
@patch('functions.permanent_data.permanent_data.session.get')
|
||||
@patch('functions.permanent_data.permanent_data.xmltodict.parse')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.session.get')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.xmltodict.parse')
|
||||
def test_fetch_train_stations(self, mock_parse, mock_get):
|
||||
# Mock API response and xmltodict parsing
|
||||
mock_get.return_value.text = '<xml></xml>'
|
||||
@ -63,7 +63,7 @@ class TestPermanentData(unittest.TestCase):
|
||||
self.assertEqual(len(result), 1)
|
||||
self.assertEqual(result[0]['trainStationCode'], 'DUB')
|
||||
|
||||
@patch('functions.permanent_data.permanent_data.session.get')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.session.get')
|
||||
def test_fetch_luas(self, mock_get):
|
||||
# Mock API response for Luas stops
|
||||
mock_get.return_value.content = 'Abbreviation\tName\tIrishName\tLatitude\tLongitude\tStopID\tLineID\tSortOrder\tIsEnabled\tIsParkAndRide\tIsCycleAndRide\tZoneCountA\tZoneCountB\nABB\tAbbey Street\tSráid na Mainistreach\t53.0\t-6.0\t1\t1\t1\ttrue\tfalse\tfalse\t1\t2'.encode('utf-8-sig')
|
||||
@ -72,8 +72,8 @@ class TestPermanentData(unittest.TestCase):
|
||||
self.assertEqual(len(result), 1)
|
||||
self.assertEqual(result[0]['luasStopName'], 'Abbey Street')
|
||||
|
||||
@patch('functions.permanent_data.permanent_data.session.get')
|
||||
@patch('functions.permanent_data.permanent_data.zipfile.ZipFile')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.session.get')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.zipfile.ZipFile')
|
||||
def test_fetch_gtfs(self, mock_zip, mock_get):
|
||||
# Mock API response for the GTFS zip file
|
||||
mock_get.return_value.content = b'zipfilecontent'
|
||||
@ -118,7 +118,7 @@ class TestPermanentData(unittest.TestCase):
|
||||
self.assertEqual(result[1]['busRouteLongName'], 'Ballinteer to Phoenix Park')
|
||||
self.assertEqual(result[2]['busStopName'], 'Stop 1')
|
||||
|
||||
@patch('functions.permanent_data.permanent_data.table')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.table')
|
||||
def test_batch_upload_to_dynamodb(self, mock_table):
|
||||
# Mock DynamoDB batch_writer
|
||||
mock_batch_writer = MagicMock()
|
||||
@ -129,10 +129,10 @@ class TestPermanentData(unittest.TestCase):
|
||||
|
||||
mock_batch_writer.put_item.assert_called_once_with(Item=data[0])
|
||||
|
||||
@patch('functions.permanent_data.permanent_data.fetch_train_stations')
|
||||
@patch('functions.permanent_data.permanent_data.fetch_luas')
|
||||
@patch('functions.permanent_data.permanent_data.fetch_gtfs')
|
||||
@patch('functions.permanent_data.permanent_data.batch_upload_to_dynamodb')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.fetch_train_stations')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.fetch_luas')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.fetch_gtfs')
|
||||
@patch('functions.fetch_permanent_data.lambda_function.batch_upload_to_dynamodb')
|
||||
def test_lambda_handler(self, mock_upload, mock_gtfs, mock_luas, mock_stations):
|
||||
# Mock data fetching functions
|
||||
mock_stations.return_value = [{"objectID": "station1", "objectType": "IrishRailStation"}]
|
@ -1,7 +1,7 @@
|
||||
import unittest
|
||||
from unittest.mock import patch, MagicMock
|
||||
import os
|
||||
from functions.transient_data.transient_data import (
|
||||
from functions.fetch_transient_data.lambda_function import (
|
||||
fetch_trains,
|
||||
fetch_buses
|
||||
)
|
||||
@ -12,7 +12,7 @@ class TestTransientData(unittest.TestCase):
|
||||
"""
|
||||
|
||||
@patch.dict(os.environ, {"PERMANENT_DATA_API": "http://mockapi.com"})
|
||||
@patch('functions.transient_data.transient_data.session.get')
|
||||
@patch('functions.fetch_transient_data.lambda_function.session.get')
|
||||
def test_fetch_buses(self, mock_get):
|
||||
"""
|
||||
Test the fetch_buses function to ensure it returns the correct data.
|
||||
@ -54,8 +54,8 @@ class TestTransientData(unittest.TestCase):
|
||||
self.assertEqual(result[0]['busID'], 'bus1')
|
||||
self.assertEqual(result[0]['busRouteAgencyName'], 'Dublin Bus')
|
||||
|
||||
@patch('functions.transient_data.transient_data.session.get')
|
||||
@patch('functions.transient_data.transient_data.timestamp', '1234567890')
|
||||
@patch('functions.fetch_transient_data.lambda_function.session.get')
|
||||
@patch('functions.fetch_transient_data.lambda_function.timestamp', '1234567890')
|
||||
def test_fetch_trains(self, mock_get):
|
||||
"""
|
||||
Test the fetch_trains function to ensure it returns the correct data.
|
||||
@ -89,7 +89,7 @@ class TestTransientData(unittest.TestCase):
|
||||
'''
|
||||
mock_get.return_value = mock_response
|
||||
|
||||
with patch('functions.transient_data.transient_data.xmltodict.parse') as mock_parse:
|
||||
with patch('functions.fetch_transient_data.lambda_function.xmltodict.parse') as mock_parse:
|
||||
# Mock xmltodict to return a dictionary directly
|
||||
mock_parse.return_value = {
|
||||
"ArrayOfObjTrainPositions": {
|
@ -2,7 +2,7 @@ import unittest
|
||||
from unittest.mock import patch, MagicMock
|
||||
import json
|
||||
import os
|
||||
from functions.return_all_data.lambda_function import lambda_handler
|
||||
from functions.return_permanent_data.lambda_function import lambda_handler
|
||||
|
||||
|
||||
class TestLambdaFunction(unittest.TestCase):
|
||||
@ -15,7 +15,7 @@ class TestLambdaFunction(unittest.TestCase):
|
||||
def tearDown(self):
|
||||
patch.stopall()
|
||||
|
||||
@patch('functions.return_all_data.lambda_function.dynamodb.Table')
|
||||
@patch('functions.return_permanent_data.lambda_function.dynamodb.Table')
|
||||
def test_lambda_handler_with_object_type(self, mock_table):
|
||||
# Mock scan response for specific object types
|
||||
mock_table.return_value.scan.return_value = {
|
||||
@ -41,7 +41,7 @@ class TestLambdaFunction(unittest.TestCase):
|
||||
self.assertEqual(body[0]['objectType'], 'Bus')
|
||||
self.assertEqual(body[1]['objectType'], 'Train')
|
||||
|
||||
@patch('functions.return_all_data.lambda_function.dynamodb.Table')
|
||||
@patch('functions.return_permanent_data.lambda_function.dynamodb.Table')
|
||||
def test_lambda_handler_with_pagination(self, mock_table):
|
||||
# Mock paginated scan responses
|
||||
mock_table.return_value.scan.side_effect = [
|
||||
@ -65,7 +65,7 @@ class TestLambdaFunction(unittest.TestCase):
|
||||
self.assertEqual(body[1]['objectType'], 'Train')
|
||||
self.assertEqual(body[2]['objectType'], 'Luas')
|
||||
|
||||
@patch('functions.return_all_data.lambda_function.dynamodb.Table')
|
||||
@patch('functions.return_permanent_data.lambda_function.dynamodb.Table')
|
||||
def test_lambda_handler_without_object_type(self, mock_table):
|
||||
# Mock scan response for full table scan
|
||||
mock_table.return_value.scan.return_value = {
|
||||
@ -88,7 +88,7 @@ class TestLambdaFunction(unittest.TestCase):
|
||||
self.assertEqual(body[0]['objectType'], 'Bus')
|
||||
self.assertEqual(body[1]['objectType'], 'Train')
|
||||
|
||||
@patch('functions.return_all_data.lambda_function.dynamodb.Table')
|
||||
@patch('functions.return_permanent_data.lambda_function.dynamodb.Table')
|
||||
def test_lambda_handler_with_no_items(self, mock_table):
|
||||
# Mock empty scan response
|
||||
mock_table.return_value.scan.return_value = {
|
||||
@ -106,7 +106,7 @@ class TestLambdaFunction(unittest.TestCase):
|
||||
body = json.loads(result['body'])
|
||||
self.assertEqual(len(body), 0)
|
||||
|
||||
@patch('functions.return_all_data.lambda_function.dynamodb.Table')
|
||||
@patch('functions.return_permanent_data.lambda_function.dynamodb.Table')
|
||||
def test_lambda_handler_error(self, mock_table):
|
||||
# Mock table scan to raise an exception
|
||||
mock_table.return_value.scan.side_effect = Exception('DynamoDB error')
|
@ -2,7 +2,7 @@ import unittest
|
||||
from unittest.mock import patch, MagicMock
|
||||
import json
|
||||
import os
|
||||
from functions.return_newest_data.lambda_function import lambda_handler
|
||||
from functions.return_transient_data.lambda_function import lambda_handler
|
||||
|
||||
|
||||
class TestLambdaFunction(unittest.TestCase):
|
||||
@ -15,7 +15,7 @@ class TestLambdaFunction(unittest.TestCase):
|
||||
def tearDown(self):
|
||||
patch.stopall()
|
||||
|
||||
@patch('functions.return_newest_data.lambda_function.dynamodb.Table')
|
||||
@patch('functions.return_transient_data.lambda_function.dynamodb.Table')
|
||||
def test_lambda_handler_with_object_type(self, mock_table):
|
||||
# Mock scan responses for timestamps
|
||||
mock_table.return_value.scan.side_effect = [
|
||||
@ -38,7 +38,7 @@ class TestLambdaFunction(unittest.TestCase):
|
||||
self.assertEqual(len(body), 1)
|
||||
self.assertEqual(body[0]['objectType'], 'Bus')
|
||||
|
||||
@patch('functions.return_newest_data.lambda_function.dynamodb.Table')
|
||||
@patch('functions.return_transient_data.lambda_function.dynamodb.Table')
|
||||
def test_lambda_handler_without_object_type(self, mock_table):
|
||||
# Mock scan responses for timestamps and data
|
||||
mock_table.return_value.scan.side_effect = [
|
||||
@ -59,7 +59,7 @@ class TestLambdaFunction(unittest.TestCase):
|
||||
self.assertEqual(len(body), 1)
|
||||
self.assertEqual(body[0]['objectType'], 'Bus')
|
||||
|
||||
@patch('functions.return_newest_data.lambda_function.dynamodb.Table')
|
||||
@patch('functions.return_transient_data.lambda_function.dynamodb.Table')
|
||||
def test_lambda_handler_with_pagination(self, mock_table):
|
||||
# Mock paginated scan responses for timestamps and data
|
||||
mock_table.return_value.scan.side_effect = [
|
||||
@ -83,7 +83,7 @@ class TestLambdaFunction(unittest.TestCase):
|
||||
self.assertEqual(body[0]['objectType'], 'Bus')
|
||||
self.assertEqual(body[1]['objectType'], 'Train')
|
||||
|
||||
@patch('functions.return_newest_data.lambda_function.dynamodb.Table')
|
||||
@patch('functions.return_transient_data.lambda_function.dynamodb.Table')
|
||||
def test_lambda_handler_no_data(self, mock_table):
|
||||
# Mock empty scan response
|
||||
mock_table.return_value.scan.return_value = {'Items': []}
|
||||
@ -98,7 +98,7 @@ class TestLambdaFunction(unittest.TestCase):
|
||||
body = json.loads(result['body'])
|
||||
self.assertEqual(len(body), 0)
|
||||
|
||||
@patch('functions.return_newest_data.lambda_function.dynamodb.Table')
|
||||
@patch('functions.return_transient_data.lambda_function.dynamodb.Table')
|
||||
def test_lambda_handler_error(self, mock_table):
|
||||
# Mock table scan to raise an exception
|
||||
mock_table.return_value.scan.side_effect = Exception('DynamoDB error')
|
Reference in New Issue
Block a user