Skip to content

Commit

Permalink
Merge branch 'hallvictoria/test-312' of https://github.com/Azure/azur…
Browse files Browse the repository at this point in the history
…e-functions-python-library into hallvictoria/test-312
  • Loading branch information
Victoria Hall committed Oct 18, 2024
2 parents 033923b + a5576ce commit a93d9a0
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
2 changes: 1 addition & 1 deletion azure/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@
'BlobSource'
)

__version__ = '1.22.0b2'
__version__ = '1.22.0b4'
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
5 changes: 4 additions & 1 deletion azure/functions/decorators/blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ def __init__(self,
**kwargs):
self.path = path
self.connection = connection
self.source = source.value if source else None
if isinstance(source, BlobSource):
self.source = source.value
else:
self.source = source # type: ignore
super().__init__(name=name, data_type=data_type)

@staticmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/decorators/test_blob.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_blob_trigger_creation_with_source_as_string(self):
trigger = BlobTrigger(name="req",
path="dummy_path",
connection="dummy_connection",
source=BlobSource.EVENT_GRID,
source="EventGrid",
data_type=DataType.UNDEFINED,
dummy_field="dummy")

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)

0 comments on commit a93d9a0

Please sign in to comment.