def test_activity_trigger_inputs(self): # Activity Trigger only accept string type from durable extensions # It will be JSON deserialized into expected data type data = [{ 'input': Datum('sample', 'string'), 'expected_value': 'sample', 'expected_type': str }, { 'input': Datum('123', 'string'), 'expected_value': 123, 'expected_type': int }, { 'input': Datum('1234.56', 'string'), 'expected_value': 1234.56, 'expected_type': float }, { 'input': Datum('[ "do", "re", "mi" ]', 'string'), 'expected_value': ["do", "re", "mi"], 'expected_type': list }, { 'input': Datum('{ "number": "42" }', 'string'), 'expected_value': { "number": "42" }, 'expected_type': dict }] for datum in data: decoded = ActivityTriggerConverter.decode(data=datum['input'], trigger_metadata=None) self.assertEqual(decoded, datum['expected_value']) self.assertEqual(type(decoded), datum['expected_type'])
def test_blob_input_with_metadata_with_trigger_metadata(self): sample_metadata = '{"Hello": "World"}' sample_blob_properties = '''{ "ContentMD5": "B54d+wzLC8IlnxyyZxwPsw==", "ContentType": "application/octet-stream", "ETag": "0x8D8989BC453467D", "Created": "2020-12-03T08:07:26+00:00", "LastModified": "2020-12-04T21:30:05+00:00", "BlobType": 2, "LeaseStatus": 2, "LeaseState": 1, "LeaseDuration": 0, "Length": "12" }''' datum: Datum = Datum(value=b'blob_content', type='bytes') trigger_metadata: Dict[str, Any] = { 'Metadata': Datum(sample_metadata, 'json'), 'Properties': Datum(sample_blob_properties, 'json'), 'BlobTrigger': Datum('blob_trigger_name', 'string'), 'Uri': Datum('https://test.io/blob_trigger', 'string') } result: InputStream = afb.BlobConverter.decode( data=datum, trigger_metadata=trigger_metadata) # 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.uri, 'https://test.io/blob_trigger') self.assertEqual(result.blob_properties, json.loads(sample_blob_properties)) self.assertEqual(result.metadata, json.loads(sample_metadata))
def test_activity_trigger_encode(self): # Activity Trigger allow any JSON serializable as outputs # The return value will be carried back to the Orchestrator function data = [{ 'output': str('sample'), 'expected_value': Datum('"sample"', 'json'), }, { 'output': int(123), 'expected_value': Datum('123', 'json'), }, { 'output': float(1234.56), 'expected_value': Datum('1234.56', 'json') }, { 'output': list(["do", "re", "mi"]), 'expected_value': Datum('["do", "re", "mi"]', 'json') }, { 'output': dict({"number": "42"}), 'expected_value': Datum('{"number": "42"}', 'json') }] for datum in data: encoded = ActivityTriggerConverter.encode(obj=datum['output'], expected_type=type( datum['output'])) self.assertEqual(encoded, datum['expected_value'])
def test_activity_trigger_decode(self): # Activity Trigger allow inputs to be any JSON serializables # The input values to the trigger should be passed into arguments data = [{ 'input': Datum('sample_string', 'string'), 'expected_value': str('sample_string') }, { 'input': Datum('"sample_json_string"', 'json'), 'expected_value': str('sample_json_string') }, { 'input': Datum('{ "invalid": "json"', 'json'), 'expected_value': str('{ "invalid": "json"') }, { 'input': Datum('true', 'json'), 'expected_value': bool(True), }, { 'input': Datum('123', 'json'), 'expected_value': int(123), }, { 'input': Datum('1234.56', 'json'), 'expected_value': float(1234.56) }, { 'input': Datum('["do", "re", "mi"]', 'json'), 'expected_value': list(["do", "re", "mi"]) }, { 'input': Datum('{"number": "42"}', 'json'), 'expected_value': dict({"number": "42"}) }] for datum in data: decoded = ActivityTriggerConverter.decode(data=datum['input'], trigger_metadata=None) self.assertEqual(decoded, datum['expected_value'])
def test_blob_input_with_metadata(self): datum: Datum = Datum(value=b'blob_content', type='bytes') metadata: Dict[str, Any] = { 'Properties': Datum('{"Length": "12"}', 'json'), 'BlobTrigger': Datum('blob_trigger_name', 'string'), 'Uri': Datum('https://test.io/blob_trigger', 'string') } result: InputStream = afb.BlobConverter.decode( data=datum, trigger_metadata=metadata) # 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.uri, 'https://test.io/blob_trigger')
def test_cosmosdb_convert_json_multiple_nulls(self): datum: Datum = Datum("[null]", "json") result: func.DocumentList = cdb.CosmosDBConverter.decode( data=datum, trigger_metadata=None) self.assertIsNotNone(result) self.assertEqual(len(result), 1) self.assertEqual(result[0], None)
def test_orchestration_trigger_converter_type(self): datum = Datum(value='{ "name": "great function" }'.encode('utf-8'), type=bytes) otc = OrchestrationTriggerConverter.decode(datum, trigger_metadata=None) content = json.loads(otc.body) self.assertEqual(content.get('name'), 'great function')
def test_blob_input_with_metadata_no_blob_properties(self): datum: Datum = Datum(value=b'blob_content', type='bytes') trigger_metadata: Dict[str, Any] = { 'BlobTrigger': Datum('blob_trigger_name', 'string'), 'Uri': Datum('https://test.io/blob_trigger', 'string') } result: InputStream = afb.\ BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata) # Verify result metadata self.assertIsInstance(result, InputStream) self.assertEqual(result.name, 'blob_trigger_name') self.assertEqual(result.length, None) self.assertEqual(result.uri, 'https://test.io/blob_trigger') self.assertEqual(result.blob_properties, None) self.assertEqual(result.metadata, None)
def test_blob_input_with_metadata_with_incorrect_trigger_metadata(self): sample_metadata = 'Hello World' sample_blob_properties = '''{"Length": "12"}''' datum: Datum = Datum(value=b'blob_content', type='bytes') trigger_metadata: Dict[str, Any] = { 'Metadata': Datum(sample_metadata, 'string'), 'Properties': Datum(sample_blob_properties, 'json'), 'BlobTrigger': Datum('blob_trigger_name', 'string'), 'Uri': Datum('https://test.io/blob_trigger', 'string') } result: InputStream = afb.\ BlobConverter.decode(data=datum, trigger_metadata=trigger_metadata) # 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.uri, 'https://test.io/blob_trigger') self.assertEqual(result.blob_properties, json.loads(sample_blob_properties)) self.assertEqual(result.metadata, None)
def test_cosmosdb_convert_json_name_is_null(self): datum: Datum = Datum(""" { "id": "1", "name": null } """, "json") result: func.DocumentList = cdb.CosmosDBConverter.decode( data=datum, trigger_metadata=None) self.assertIsNotNone(result) self.assertEqual(len(result), 1) self.assertEqual(result[0]['name'], None)
def test_cosmosdb_convert_bytes(self): datum: Datum = Datum(""" { "id": "1", "name": "awesome_name" } """.encode(), "bytes") result: func.DocumentList = cdb.CosmosDBConverter.decode( data=datum, trigger_metadata=None) self.assertIsNotNone(result) self.assertEqual(len(result), 1) self.assertEqual(result[0]['name'], 'awesome_name')
def test_blob_input_bytes_no_metadata(self): datum: Datum = Datum(value=b'bytes_content', type='bytes') result: InputStream = afb.BlobConverter.decode(data=datum, trigger_metadata=None) self.assertIsNotNone(result) # Verify result metadata self.assertIsInstance(result, InputStream) self.assertIsNone(result.name) self.assertIsNone(result.length) self.assertIsNone(result.uri) self.assertTrue(result.readable()) self.assertFalse(result.seekable()) self.assertFalse(result.writable()) # Verify result content content: bytes = result.read() self.assertEqual(content, b'bytes_content')
def test_activity_trigger_accepts_any_types(self): datum_set = { Datum('string', str), Datum(123, int), Datum(1234.56, float), Datum('string'.encode('utf-8'), bytes), Datum(Datum('{ "json": true }', str), Datum) } for datum in datum_set: out = ActivityTriggerConverter.decode(datum, trigger_metadata=None) self.assertEqual(out, datum.value) self.assertEqual(type(out), datum.type)
def test_cosmosdb_convert_json_multiple_entries(self): datum: Datum = Datum(""" [ { "id": "1", "name": "awesome_name" }, { "id": "2", "name": "bossy_name" } ] """, "json") result: func.DocumentList = cdb.CosmosDBConverter.decode( data=datum, trigger_metadata=None) self.assertIsNotNone(result) self.assertEqual(len(result), 2) self.assertEqual(result[0]['name'], 'awesome_name') self.assertEqual(result[1]['name'], 'bossy_name')
def test_trigger_converter(self): datum = Datum(value='{ "name": "great function" }', type=str) for converter in CONVERTERS: otc = converter.decode(datum, trigger_metadata=None) content = json.loads(otc.body) self.assertEqual(content.get('name'), 'great function')
def test_blob_input_incorrect_type(self): datum: Datum = Datum(value=b'string_content', type='bytearray') with self.assertRaises(ValueError): afb.BlobConverter.decode(data=datum, trigger_metadata=None)
def read(self) -> Datum: return Datum(b'custom_output_content', 'types')
def test_blob_incomplete_read(self): datum: Datum = Datum(value=b'blob_content', type='bytes') result: InputStream = afb.BlobConverter.decode(data=datum, trigger_metadata=None) self.assertEqual(result.read(size=3), b'blo')