1
1
# Copyright (c) Microsoft Corporation. All rights reserved.
2
2
# Licensed under the MIT License.
3
3
4
- import sys
5
4
import unittest
6
5
from typing import List , Optional
7
6
10
9
11
10
from azurefunctions .extensions .bindings .eventhub import EventData , EventDataConverter
12
11
13
- EVENTHUB_SAMPLE_CONTENT = b"\x00 Sr\xc1 \x8e \x08 \xa3 \x1b x-opt-sequence-number-epochT\xff \xa3 \x15 x-opt-sequence-numberU\x04 \xa3 \x0c x-opt-offset\x81 \x00 \x00 \x00 \x01 \x00 \x00 \x01 0\xa3 \x13 x-opt-enqueued-time\x00 \xa3 \x1d com.microsoft:datetime-offset\x81 \x08 \xdd W\x05 \xc3 Q\xcf \x10 \x00 St\xc1 I\x02 \xa1 \r Diagnostic-Id\xa1 700-bdc3fde4889b4e907e0c9dcb46ff8d92-21f637af293ef13b-00\x00 Su\xa0 \x08 message1"
12
+ EVENTHUB_SAMPLE_CONTENT = b"\x00 Sr\xc1 \x8e \x08 \xa3 \x1b x-opt-sequence-number-epochT\xff \xa3 \x15 x-opt-sequence-numberU\x04 \xa3 \x0c x-opt-offset\x81 \x00 \x00 \x00 \x01 \x00 \x00 \x01 0\xa3 \x13 x-opt-enqueued-time\x00 \xa3 \x1d com.microsoft:datetime-offset\x81 \x08 \xdd W\x05 \xc3 Q\xcf \x10 \x00 St\xc1 I\x02 \xa1 \r Diagnostic-Id\xa1 700-bdc3fde4889b4e907e0c9dcb46ff8d92-21f637af293ef13b-00\x00 Su\xa0 \x08 message1" # noqa: E501
13
+
14
14
15
15
# Mock classes for testing
16
16
class MockMBD :
@@ -27,8 +27,8 @@ def data_type(self) -> Optional[int]:
27
27
@property
28
28
def direction (self ) -> int :
29
29
return self ._direction .value
30
-
31
-
30
+
31
+
32
32
class MockCMBD :
33
33
def __init__ (self , model_binding_data_list : List [MockMBD ]):
34
34
self .model_binding_data = model_binding_data_list
@@ -83,18 +83,19 @@ def test_input_empty_mbd(self):
83
83
self .assertIsNone (result )
84
84
85
85
def test_input_empty_cmbd (self ):
86
- datum : Datum = Datum (value = {}, type = "collection_model_binding_data" )
86
+ datum : Datum = Datum (value = MockCMBD ([None ]),
87
+ type = "collection_model_binding_data" )
87
88
result : EventData = EventDataConverter .decode (
88
89
data = datum , trigger_metadata = None , pytype = EventData
89
90
)
90
- self .assertIsNone (result )
91
+ self .assertEqual (result , [ None ] )
91
92
92
93
def test_input_populated_mbd (self ):
93
94
sample_mbd = MockMBD (
94
95
version = "1.0" ,
95
96
source = "AzureEventHubsEventData" ,
96
97
content_type = "application/octet-stream" ,
97
- content = EVENTHUB_SAMPLE_CONTENT
98
+ content = EVENTHUB_SAMPLE_CONTENT
98
99
)
99
100
100
101
datum : Datum = Datum (value = sample_mbd , type = "model_binding_data" )
@@ -115,33 +116,34 @@ def test_input_populated_cmbd(self):
115
116
version = "1.0" ,
116
117
source = "AzureEventHubsEventData" ,
117
118
content_type = "application/octet-stream" ,
118
- content = EVENTHUB_SAMPLE_CONTENT
119
+ content = EVENTHUB_SAMPLE_CONTENT
119
120
)
120
121
121
- datum : Datum = Datum (value = MockCMBD ([sample_mbd , sample_mbd ]), type = "collection_model_binding_data" )
122
+ datum : Datum = Datum (value = MockCMBD ([sample_mbd , sample_mbd ]),
123
+ type = "collection_model_binding_data" )
122
124
result : EventData = EventDataConverter .decode (
123
125
data = datum , trigger_metadata = None , pytype = EventData
124
126
)
125
127
126
128
self .assertIsNotNone (result )
127
- self .assertIsInstance (result , EventDataSdk )
128
-
129
- sdk_result = EventData (data = datum .value ).get_sdk_type ()
130
-
131
- self .assertIsNotNone (sdk_result )
132
- self .assertIsInstance (sdk_result , EventDataSdk )
133
-
134
- def test_input_invalid_pytype (self ):
135
- sample_mbd = MockMBD (
136
- version = "1.0" ,
137
- source = "AzureEventHubsEventData" ,
138
- content_type = "application/octet-stream" ,
139
- content = EVENTHUB_SAMPLE_CONTENT
140
- )
141
-
142
- datum : Datum = Datum (value = sample_mbd , type = "model_binding_data" )
143
- result : EventData = EventDataConverter .decode (
144
- data = datum , trigger_metadata = None , pytype = "str"
129
+ for event_data in result :
130
+ self .assertIsInstance (event_data , EventDataSdk )
131
+
132
+ sdk_results = []
133
+ for mbd in datum .value .model_binding_data :
134
+ sdk_results .append (EventData (data = mbd ).get_sdk_type ())
135
+
136
+ self .assertNotEqual (sdk_results , [None , None ])
137
+ for event_data in sdk_results :
138
+ self .assertIsInstance (event_data , EventDataSdk )
139
+
140
+ def test_input_invalid_datum_type (self ):
141
+ with self .assertRaises (ValueError ) as e :
142
+ datum : Datum = Datum (value = "hello" , type = "str" )
143
+ _ : EventData = EventDataConverter .decode (
144
+ data = datum , trigger_metadata = None , pytype = ""
145
+ )
146
+ self .assertEqual (
147
+ e .exception .args [0 ],
148
+ "Unexpected type of data received for the 'eventhub' binding: 'str'" ,
145
149
)
146
-
147
- self .assertIsNone (result )
0 commit comments