|
| 1 | +import json |
| 2 | +import unittest |
| 3 | + |
| 4 | +from sqs_dynamo_utils import _unwrap_dynamodb_value, extract_sqs_imms_data |
| 5 | +from test_utils import load_sample_sqs_event |
| 6 | + |
| 7 | + |
| 8 | +class TestExtractSqsImmsData(unittest.TestCase): |
| 9 | + """ |
| 10 | + Test SQS Event extraction utility |
| 11 | + """ |
| 12 | + |
| 13 | + @classmethod |
| 14 | + def setUpClass(cls): |
| 15 | + cls.sample_sqs_event = load_sample_sqs_event() |
| 16 | + |
| 17 | + def test_extract_sqs_imms_data(self): |
| 18 | + result = extract_sqs_imms_data(self.sample_sqs_event) |
| 19 | + |
| 20 | + self.assertEqual(result["imms_id"], "d058014c-b0fd-4471-8db9-3316175eb825") |
| 21 | + self.assertEqual(result["supplier_system"], "TPP") |
| 22 | + self.assertEqual(result["vaccine_type"], "hib") |
| 23 | + self.assertEqual(result["operation"], "CREATE") |
| 24 | + self.assertEqual(result["nhs_number"], "9481152782") |
| 25 | + self.assertEqual(result["person_dob"], "20040609") |
| 26 | + self.assertEqual(result["date_and_time"], "20260212T17443700") |
| 27 | + self.assertEqual(result["site_code"], "B0C4P") |
| 28 | + |
| 29 | + def test_extract_imms_data_field_types(self): |
| 30 | + """Test that extracted fields are the correct types.""" |
| 31 | + result = extract_sqs_imms_data(self.sample_sqs_event) |
| 32 | + |
| 33 | + self.assertIsInstance(result["imms_id"], str) |
| 34 | + self.assertIsInstance(result["supplier_system"], str) |
| 35 | + self.assertIsInstance(result["vaccine_type"], str) |
| 36 | + self.assertIsInstance(result["operation"], str) |
| 37 | + self.assertIsInstance(result["nhs_number"], str) |
| 38 | + self.assertIsInstance(result["person_dob"], str) |
| 39 | + self.assertIsInstance(result["date_and_time"], str) |
| 40 | + self.assertIsInstance(result["site_code"], str) |
| 41 | + |
| 42 | + def test_extract_imms_data_invalid_json_body(self): |
| 43 | + """Test extraction when body is invalid JSON.""" |
| 44 | + event = {"body": "invalid json {"} |
| 45 | + |
| 46 | + with self.assertRaises(json.JSONDecodeError): |
| 47 | + extract_sqs_imms_data(event) |
| 48 | + |
| 49 | + |
| 50 | +class TestUnwrapDynamodbValue(unittest.TestCase): |
| 51 | + """Tests for _unwrap_dynamodb_value helper function.""" |
| 52 | + |
| 53 | + def test_unwrap_string_type(self): |
| 54 | + """Test unwrapping DynamoDB String type.""" |
| 55 | + value = {"S": "test-value"} |
| 56 | + result = _unwrap_dynamodb_value(value) |
| 57 | + self.assertEqual(result, "test-value") |
| 58 | + |
| 59 | + def test_unwrap_number_type(self): |
| 60 | + """Test unwrapping DynamoDB Number type.""" |
| 61 | + value = {"N": "123"} |
| 62 | + result = _unwrap_dynamodb_value(value) |
| 63 | + self.assertEqual(result, "123") |
| 64 | + |
| 65 | + def test_unwrap_boolean_type(self): |
| 66 | + """Test unwrapping DynamoDB Boolean type.""" |
| 67 | + value = {"BOOL": True} |
| 68 | + result = _unwrap_dynamodb_value(value) |
| 69 | + self.assertTrue(result) |
| 70 | + |
| 71 | + def test_unwrap_null_type(self): |
| 72 | + """Test unwrapping DynamoDB NULL type.""" |
| 73 | + value = {"NULL": True} |
| 74 | + result = _unwrap_dynamodb_value(value) |
| 75 | + self.assertIsNone(result) |
| 76 | + |
| 77 | + def test_unwrap_map_type(self): |
| 78 | + """Test unwrapping DynamoDB Map type.""" |
| 79 | + value = {"M": {"key": {"S": "value"}}} |
| 80 | + result = _unwrap_dynamodb_value(value) |
| 81 | + self.assertEqual(result, {"key": {"S": "value"}}) |
| 82 | + |
| 83 | + def test_unwrap_list_type(self): |
| 84 | + """Test unwrapping DynamoDB List type.""" |
| 85 | + value = {"L": [{"S": "item1"}, {"S": "item2"}]} |
| 86 | + result = _unwrap_dynamodb_value(value) |
| 87 | + self.assertEqual(result, [{"S": "item1"}, {"S": "item2"}]) |
0 commit comments