Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correctly deserialize blob length #252

Merged
merged 9 commits into from
Oct 17, 2024
3 changes: 2 additions & 1 deletion azure/functions/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def decode(cls, data: meta.Datum, *, trigger_metadata) -> Any:
trigger_metadata, 'Properties', python_type=dict)
if properties:
blob_properties = properties
length = properties.get('Length')
length = properties.get('ContentLength') or \
properties.get('Length')
length = int(length) if length else None
else:
blob_properties = None
Expand Down
55 changes: 49 additions & 6 deletions tests/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_blob_input_with_metadata_no_blob_properties(self):
self.assertEqual(result.metadata, None)

def test_blob_input_with_metadata_no_trigger_metadata(self):
sample_blob_properties = '{"Length": "12"}'
sample_blob_properties = '{"ContentLength": "12"}'
datum: Datum = Datum(value=b'blob_content', type='bytes')
trigger_metadata: Dict[str, Any] = {
'Properties': Datum(sample_blob_properties, 'json'),
Expand All @@ -97,7 +97,7 @@ def test_blob_input_with_metadata_no_trigger_metadata(self):
# Verify result metadata
self.assertIsInstance(result, InputStream)
self.assertEqual(result.name, 'blob_trigger_name')
self.assertEqual(result.length, len(b'blob_content'))
self.assertEqual(result.length, 12)
self.assertEqual(result.uri, 'https://test.io/blob_trigger')
self.assertEqual(result.blob_properties,
json.loads(sample_blob_properties))
Expand All @@ -115,7 +115,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self):
"LeaseStatus": 2,
"LeaseState": 1,
"LeaseDuration": 0,
"Length": "12"
"ContentLength": "12"
}'''
datum: Datum = Datum(value=b'blob_content', type='bytes')
trigger_metadata: Dict[str, Any] = {
Expand All @@ -130,7 +130,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self):
# Verify result metadata
self.assertIsInstance(result, InputStream)
self.assertEqual(result.name, 'blob_trigger_name')
self.assertEqual(result.length, len(b'blob_content'))
self.assertEqual(result.length, 12)
self.assertEqual(result.uri, 'https://test.io/blob_trigger')
self.assertEqual(result.blob_properties,
json.loads(sample_blob_properties))
Expand All @@ -139,7 +139,7 @@ def test_blob_input_with_metadata_with_trigger_metadata(self):

def test_blob_input_with_metadata_with_incorrect_trigger_metadata(self):
sample_metadata = 'Hello World'
sample_blob_properties = '''{"Length": "12"}'''
sample_blob_properties = '''{"ContentLength": "12"}'''
datum: Datum = Datum(value=b'blob_content', type='bytes')
trigger_metadata: Dict[str, Any] = {
'Metadata': Datum(sample_metadata, 'string'),
Expand All @@ -153,7 +153,7 @@ def test_blob_input_with_metadata_with_incorrect_trigger_metadata(self):
# Verify result metadata
self.assertIsInstance(result, InputStream)
self.assertEqual(result.name, 'blob_trigger_name')
self.assertEqual(result.length, len(b'blob_content'))
self.assertEqual(result.length, 12)
self.assertEqual(result.uri, 'https://test.io/blob_trigger')
self.assertEqual(result.blob_properties,
json.loads(sample_blob_properties))
Expand Down Expand Up @@ -228,3 +228,46 @@ def read(self) -> Datum:

check_output_type = afb.BlobConverter.check_output_type_annotation
self.assertTrue(check_output_type(CustomOutput))

def test_blob_input_with_metadata_with_length(self):
sample_blob_properties = '{"Length": "12"}'
datum: Datum = Datum(value=b'blob_content', type='bytes')
trigger_metadata: Dict[str, Any] = {
'Properties': Datum(sample_blob_properties, 'json')
}
result: InputStream = afb. \
BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata)

# Verify result metadata
self.assertIsInstance(result, InputStream)
self.assertEqual(result.length, 12)

def test_blob_input_with_metadata_with_both_length(self):
sample_blob_properties = '''{
"ContentLength": "12",
"Length": "10"
}'''
datum: Datum = Datum(value=b'blob_content', type='bytes')
trigger_metadata: Dict[str, Any] = {
'Properties': Datum(sample_blob_properties, 'json')
}
result: InputStream = afb. \
BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata)

# Verify result metadata.
# This should be 12, since we check for ContentLength first
self.assertIsInstance(result, InputStream)
self.assertEqual(result.length, 12)

def test_blob_input_with_metadata_with_no_length(self):
sample_blob_properties = '''{}'''
datum: Datum = Datum(value=b'blob_content', type='bytes')
trigger_metadata: Dict[str, Any] = {
'Properties': Datum(sample_blob_properties, 'json')
}
result: InputStream = afb. \
BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata)

# Verify result metadata.
self.assertIsInstance(result, InputStream)
self.assertEqual(result.length, None)