|
6 | 6 |
|
7 | 7 | from azure.functions.durable_functions import (
|
8 | 8 | OrchestrationTriggerConverter,
|
| 9 | + EnitityTriggerConverter, |
9 | 10 | ActivityTriggerConverter
|
10 | 11 | )
|
11 |
| -from azure.functions._durable_functions import OrchestrationContext |
| 12 | +from azure.functions._durable_functions import ( |
| 13 | + OrchestrationContext, |
| 14 | + EntityContext |
| 15 | +) |
12 | 16 | from azure.functions.meta import Datum
|
13 | 17 |
|
| 18 | +CONTEXT_CLASSES = [OrchestrationContext, EntityContext] |
| 19 | +CONVERTERS = [OrchestrationTriggerConverter, EnitityTriggerConverter] |
14 | 20 |
|
15 |
| -class TestDurableFunctions(unittest.TestCase): |
16 |
| - def test_orchestration_context_string_body(self): |
17 |
| - raw_string = '{ "name": "great function" }' |
18 |
| - context = OrchestrationContext(raw_string) |
19 |
| - self.assertIsNotNone(getattr(context, 'body', None)) |
20 |
| - |
21 |
| - content = json.loads(context.body) |
22 |
| - self.assertEqual(content.get('name'), 'great function') |
23 |
| - |
24 |
| - def test_orchestration_context_string_cast(self): |
25 |
| - raw_string = '{ "name": "great function" }' |
26 |
| - context = OrchestrationContext(raw_string) |
27 |
| - self.assertEqual(str(context), raw_string) |
28 |
| - |
29 |
| - content = json.loads(str(context)) |
30 |
| - self.assertEqual(content.get('name'), 'great function') |
31 |
| - |
32 |
| - def test_orchestration_context_bytes_body(self): |
33 |
| - raw_bytes = '{ "name": "great function" }'.encode('utf-8') |
34 |
| - context = OrchestrationContext(raw_bytes) |
35 |
| - self.assertIsNotNone(getattr(context, 'body', None)) |
36 |
| - |
37 |
| - content = json.loads(context.body) |
38 |
| - self.assertEqual(content.get('name'), 'great function') |
39 |
| - |
40 |
| - def test_orchestration_context_bytes_cast(self): |
41 |
| - raw_bytes = '{ "name": "great function" }'.encode('utf-8') |
42 |
| - context = OrchestrationContext(raw_bytes) |
43 |
| - self.assertIsNotNone(getattr(context, 'body', None)) |
44 | 21 |
|
45 |
| - content = json.loads(context.body) |
46 |
| - self.assertEqual(content.get('name'), 'great function') |
47 |
| - |
48 |
| - def test_orchestration_trigger_converter(self): |
| 22 | +class TestDurableFunctions(unittest.TestCase): |
| 23 | + def test_context_string_body(self): |
| 24 | + body = '{ "name": "great function" }' |
| 25 | + for ctx in CONTEXT_CLASSES: |
| 26 | + context = ctx(body) |
| 27 | + self.assertIsNotNone(getattr(context, 'body', None)) |
| 28 | + |
| 29 | + content = json.loads(context.body) |
| 30 | + self.assertEqual(content.get('name'), 'great function') |
| 31 | + |
| 32 | + def test_context_string_cast(self): |
| 33 | + body = '{ "name": "great function" }' |
| 34 | + for ctx in CONTEXT_CLASSES: |
| 35 | + context = ctx(body) |
| 36 | + self.assertEqual(str(context), body) |
| 37 | + |
| 38 | + content = json.loads(str(context)) |
| 39 | + self.assertEqual(content.get('name'), 'great function') |
| 40 | + |
| 41 | + def test_context_bytes_body(self): |
| 42 | + body = '{ "name": "great function" }'.encode('utf-8') |
| 43 | + for ctx in CONTEXT_CLASSES: |
| 44 | + context = ctx(body) |
| 45 | + self.assertIsNotNone(getattr(context, 'body', None)) |
| 46 | + |
| 47 | + content = json.loads(context.body) |
| 48 | + self.assertEqual(content.get('name'), 'great function') |
| 49 | + |
| 50 | + def test_context_bytes_cast(self): |
| 51 | + # TODO: this is just like the test above |
| 52 | + # (test_orchestration_context_bytes_body) |
| 53 | + body = '{ "name": "great function" }'.encode('utf-8') |
| 54 | + for ctx in CONTEXT_CLASSES: |
| 55 | + context = ctx(body) |
| 56 | + self.assertIsNotNone(getattr(context, 'body', None)) |
| 57 | + |
| 58 | + content = json.loads(context.body) |
| 59 | + self.assertEqual(content.get('name'), 'great function') |
| 60 | + |
| 61 | + def test_trigger_converter(self): |
49 | 62 | datum = Datum(value='{ "name": "great function" }',
|
50 | 63 | type=str)
|
51 |
| - otc = OrchestrationTriggerConverter.decode(datum, |
52 |
| - trigger_metadata=None) |
53 |
| - content = json.loads(otc.body) |
54 |
| - self.assertEqual(content.get('name'), 'great function') |
| 64 | + for converter in CONVERTERS: |
| 65 | + otc = converter.decode(datum, trigger_metadata=None) |
| 66 | + content = json.loads(otc.body) |
| 67 | + self.assertEqual(content.get('name'), 'great function') |
55 | 68 |
|
56 |
| - def test_orchestration_trigger_converter_type(self): |
| 69 | + def test_trigger_converter_type(self): |
57 | 70 | datum = Datum(value='{ "name": "great function" }'.encode('utf-8'),
|
58 | 71 | type=bytes)
|
59 |
| - otc = OrchestrationTriggerConverter.decode(datum, |
60 |
| - trigger_metadata=None) |
61 |
| - content = json.loads(otc.body) |
62 |
| - self.assertEqual(content.get('name'), 'great function') |
| 72 | + for converter in CONVERTERS: |
| 73 | + otc = converter.decode(datum, trigger_metadata=None) |
| 74 | + content = json.loads(otc.body) |
| 75 | + self.assertEqual(content.get('name'), 'great function') |
63 | 76 |
|
64 |
| - def test_orchestration_trigger_check_good_annotation(self): |
65 |
| - for dt in (OrchestrationContext,): |
| 77 | + def test_trigger_check_good_annotation(self): |
| 78 | + |
| 79 | + for converter, ctx in zip(CONVERTERS, CONTEXT_CLASSES): |
66 | 80 | self.assertTrue(
|
67 |
| - OrchestrationTriggerConverter.check_input_type_annotation(dt) |
| 81 | + converter.check_input_type_annotation(ctx) |
68 | 82 | )
|
69 | 83 |
|
70 |
| - def test_orchestration_trigger_check_bad_annotation(self): |
| 84 | + def test_trigger_check_bad_annotation(self): |
71 | 85 | for dt in (str, bytes, int):
|
72 |
| - self.assertFalse( |
73 |
| - OrchestrationTriggerConverter.check_input_type_annotation(dt) |
74 |
| - ) |
| 86 | + for converter in CONVERTERS: |
| 87 | + self.assertFalse( |
| 88 | + converter.check_input_type_annotation(dt) |
| 89 | + ) |
75 | 90 |
|
76 |
| - def test_orchestration_trigger_has_implicit_return(self): |
77 |
| - self.assertTrue( |
78 |
| - OrchestrationTriggerConverter.has_implicit_output() |
79 |
| - ) |
| 91 | + def test_trigger_has_implicit_return(self): |
| 92 | + for converter in CONVERTERS: |
| 93 | + self.assertTrue( |
| 94 | + converter.has_implicit_output() |
| 95 | + ) |
80 | 96 |
|
81 | 97 | def test_activity_trigger_inputs(self):
|
82 | 98 | # Activity Trigger only accept string type from durable extensions
|
|
0 commit comments