|
| 1 | +import unittest |
| 2 | +import json |
| 3 | + |
| 4 | +from azure.functions.durable_functions import ( |
| 5 | + OrchestrationTriggerConverter, |
| 6 | + ActivityTriggerConverter |
| 7 | +) |
| 8 | +from azure.functions._durable_functions import OrchestrationContext |
| 9 | +from azure.functions.meta import Datum |
| 10 | + |
| 11 | + |
| 12 | +class TestDurableFunctions(unittest.TestCase): |
| 13 | + def test_orchestration_context_string_body(self): |
| 14 | + raw_string = '{ "name": "great function" }' |
| 15 | + context = OrchestrationContext(raw_string) |
| 16 | + self.assertIsNotNone(getattr(context, 'body', None)) |
| 17 | + |
| 18 | + content = json.loads(context.body) |
| 19 | + self.assertEqual(content.get('name'), 'great function') |
| 20 | + |
| 21 | + def test_orchestration_context_string_cast(self): |
| 22 | + raw_string = '{ "name": "great function" }' |
| 23 | + context = OrchestrationContext(raw_string) |
| 24 | + self.assertEqual(str(context), raw_string) |
| 25 | + |
| 26 | + content = json.loads(str(context)) |
| 27 | + self.assertEqual(content.get('name'), 'great function') |
| 28 | + |
| 29 | + def test_orchestration_context_bytes_body(self): |
| 30 | + raw_bytes = '{ "name": "great function" }'.encode('utf-8') |
| 31 | + context = OrchestrationContext(raw_bytes) |
| 32 | + self.assertIsNotNone(getattr(context, 'body', None)) |
| 33 | + |
| 34 | + content = json.loads(context.body) |
| 35 | + self.assertEqual(content.get('name'), 'great function') |
| 36 | + |
| 37 | + def test_orchestration_context_bytes_cast(self): |
| 38 | + raw_bytes = '{ "name": "great function" }'.encode('utf-8') |
| 39 | + context = OrchestrationContext(raw_bytes) |
| 40 | + self.assertIsNotNone(getattr(context, 'body', None)) |
| 41 | + |
| 42 | + content = json.loads(context.body) |
| 43 | + self.assertEqual(content.get('name'), 'great function') |
| 44 | + |
| 45 | + def test_orchestration_trigger_converter(self): |
| 46 | + datum = Datum(value='{ "name": "great function" }', |
| 47 | + type=str) |
| 48 | + otc = OrchestrationTriggerConverter.decode(datum, |
| 49 | + trigger_metadata=None) |
| 50 | + content = json.loads(otc.body) |
| 51 | + self.assertEqual(content.get('name'), 'great function') |
| 52 | + |
| 53 | + def test_orchestration_trigger_converter_type(self): |
| 54 | + datum = Datum(value='{ "name": "great function" }'.encode('utf-8'), |
| 55 | + type=bytes) |
| 56 | + otc = OrchestrationTriggerConverter.decode(datum, |
| 57 | + trigger_metadata=None) |
| 58 | + content = json.loads(otc.body) |
| 59 | + self.assertEqual(content.get('name'), 'great function') |
| 60 | + |
| 61 | + def test_orchestration_trigger_check_good_annotation(self): |
| 62 | + for dt in (OrchestrationContext,): |
| 63 | + self.assertTrue( |
| 64 | + OrchestrationTriggerConverter.check_input_type_annotation(dt) |
| 65 | + ) |
| 66 | + |
| 67 | + def test_orchestration_trigger_check_bad_annotation(self): |
| 68 | + for dt in (str, bytes, int): |
| 69 | + self.assertFalse( |
| 70 | + OrchestrationTriggerConverter.check_input_type_annotation(dt) |
| 71 | + ) |
| 72 | + |
| 73 | + def test_orchestration_trigger_has_implicit_return(self): |
| 74 | + self.assertTrue( |
| 75 | + OrchestrationTriggerConverter.has_implicit_output() |
| 76 | + ) |
| 77 | + |
| 78 | + def test_activity_trigger_accepts_any_types(self): |
| 79 | + datum_set = { |
| 80 | + Datum('string', str), |
| 81 | + Datum(123, int), |
| 82 | + Datum(1234.56, float), |
| 83 | + Datum('string'.encode('utf-8'), bytes), |
| 84 | + Datum(Datum('{ "json": true }', str), Datum) |
| 85 | + } |
| 86 | + |
| 87 | + for datum in datum_set: |
| 88 | + out = ActivityTriggerConverter.decode(datum, trigger_metadata=None) |
| 89 | + self.assertEqual(out, datum.value) |
| 90 | + self.assertEqual(type(out), datum.type) |
| 91 | + |
| 92 | + def test_activity_trigger_has_implicit_return(self): |
| 93 | + self.assertTrue( |
| 94 | + ActivityTriggerConverter.has_implicit_output() |
| 95 | + ) |
0 commit comments