|
1 | 1 | """Unit tests for handle_big_result module""" |
2 | 2 | import hashlib |
| 3 | +import io |
3 | 4 | import json |
4 | 5 | import xml.etree.ElementTree as ET |
5 | 6 | from unittest.mock import MagicMock, patch |
6 | 7 | import boto3 |
7 | 8 | import pytest |
| 9 | +from botocore.response import StreamingBody |
8 | 10 | from moto import mock_s3, mock_sts |
9 | 11 |
|
10 | 12 | import bignbit.utils |
@@ -673,3 +675,35 @@ def test_process_harmony_results_all_variable_omitted(mock_get_harmony_client): |
673 | 675 | assert 'variable' not in result[0] |
674 | 676 | assert result[0]['output_crs'] == 'EPSG:4326' |
675 | 677 | assert result[0]['fileName'] == 'result_image.png' |
| 678 | + |
| 679 | + |
| 680 | +@patch('bignbit.utils.get_harmony_client') |
| 681 | +@patch('bignbit.handle_big_result.boto3') |
| 682 | +def test_process_harmony_results_multipart_etag_fallback(mock_boto3, mock_get_harmony_client): |
| 683 | + """When ETag has a multipart suffix, falls back to streaming the object to compute MD5.""" |
| 684 | + bignbit.utils.AWS_ACCOUNT_ID = '123456789012' |
| 685 | + |
| 686 | + image_data = b'fake image data for multipart test' |
| 687 | + expected_checksum = hashlib.md5(image_data).hexdigest() |
| 688 | + |
| 689 | + bucket_name = 'test-harmony-bucket' |
| 690 | + image_key = 'path/to/result_image.png' |
| 691 | + |
| 692 | + mock_s3_instance = MagicMock() |
| 693 | + mock_s3_instance.head_object.return_value = {'ETag': '"abcdef1234567890abcdef1234567890-5"'} |
| 694 | + mock_s3_instance.get_object.return_value = { |
| 695 | + 'Body': StreamingBody(io.BytesIO(image_data), len(image_data)) |
| 696 | + } |
| 697 | + mock_boto3.client.return_value = mock_s3_instance |
| 698 | + |
| 699 | + mock_harmony_client = MagicMock() |
| 700 | + mock_harmony_client.result_urls.return_value = iter([f's3://{bucket_name}/{image_key}']) |
| 701 | + mock_get_harmony_client.return_value = mock_harmony_client |
| 702 | + |
| 703 | + harmony_job = {'job': 'valid-job-id', 'variable': 'temperature', 'output_crs': 'EPSG:4326'} |
| 704 | + result = process_harmony_results(harmony_job, 'UAT') |
| 705 | + |
| 706 | + assert len(result) == 1 |
| 707 | + assert result[0]['checksumType'] == 'md5' |
| 708 | + assert result[0]['checksum'] == expected_checksum |
| 709 | + mock_s3_instance.get_object.assert_called_once() |
0 commit comments