@@ -120,6 +120,72 @@ def test_encode_extension_attribute(self):
120120 '''
121121 self .assertEqual (json .loads (target ), json .loads (encoded ))
122122
123+ def test_encode_batch_0_items (self ):
124+ self .assertEqual ("[]" , Json .encode ([]))
125+
126+ def test_encode_batch_1_item (self ):
127+ event_batch = [
128+ CloudEvent (
129+ type = "OximeterMeasured" ,
130+ source = "oximeter/123" ,
131+ id = "1000" ,
132+ datacontenttype = "application/json" ,
133+ data = json .dumps ({"spo2" : 99 }),
134+ )
135+ ]
136+ encoded_batch = Json .encode (event_batch )
137+ target = r'''
138+ [{
139+ "type":"OximeterMeasured",
140+ "source":"oximeter/123",
141+ "id":"1000",
142+ "specversion":"1.0",
143+ "datacontenttype": "application/json",
144+ "data": "{\"spo2\": 99}"
145+ }]
146+ '''
147+ self .assertEqual (json .loads (target ), json .loads (encoded_batch ))
148+
149+ def test_encode_batch_2_items (self ):
150+ event_batch = [
151+ CloudEvent (
152+ type = "OximeterMeasured" ,
153+ source = "oximeter/123" ,
154+ id = "1000" ,
155+ datacontenttype = "application/json" ,
156+ data = json .dumps ({"spo2" : 99 }),
157+ ),
158+ CloudEvent (
159+ type = "OximeterMeasured" ,
160+ source = "oximeter/123" ,
161+ id = "1001" ,
162+ datacontenttype = "application/json" ,
163+ data = b'\x01 binarydata\x02 ' ,
164+ ),
165+ ]
166+ encoded_batch = Json .encode (event_batch )
167+ target = r'''
168+ [
169+ {
170+ "type":"OximeterMeasured",
171+ "source":"oximeter/123",
172+ "id":"1000",
173+ "specversion":"1.0",
174+ "datacontenttype": "application/json",
175+ "data": "{\"spo2\": 99}"
176+ },
177+ {
178+ "type":"OximeterMeasured",
179+ "source":"oximeter/123",
180+ "id":"1001",
181+ "specversion":"1.0",
182+ "datacontenttype": "application/json",
183+ "data_base64": "AWJpbmFyeWRhdGEC"
184+ }
185+ ]
186+ '''
187+ self .assertEqual (json .loads (target ), json .loads (encoded_batch ))
188+
123189
124190class JsonDecoderTests (unittest .TestCase ):
125191
@@ -222,3 +288,67 @@ def test_decode_extension_attribute(self):
222288 )
223289 event = Json .decode (encoded_event )
224290 self .assertEqual (target , event )
291+
292+ def test_decode_batch_0_items (self ):
293+ self .assertEqual ([], Json .decode ("[]" ))
294+
295+ def test_decode_batch_1_item (self ):
296+ encoded_batch = r'''
297+ [{
298+ "type":"OximeterMeasured",
299+ "source":"oximeter/123",
300+ "id":"1000",
301+ "specversion":"1.0",
302+ "datacontenttype": "application/json",
303+ "data": "{\"spo2\": 99}"
304+ }]
305+ '''
306+ target = [
307+ CloudEvent (
308+ type = "OximeterMeasured" ,
309+ source = "oximeter/123" ,
310+ id = "1000" ,
311+ datacontenttype = "application/json" ,
312+ data = json .dumps ({"spo2" : 99 }),
313+ )
314+ ]
315+ self .assertEqual (target , Json .decode (encoded_batch ))
316+
317+ def test_decode_batch_2_items (self ):
318+ encoded_batch = r'''
319+ [
320+ {
321+ "type":"OximeterMeasured",
322+ "source":"oximeter/123",
323+ "id":"1000",
324+ "specversion":"1.0",
325+ "datacontenttype": "application/json",
326+ "data": "{\"spo2\": 99}"
327+ },
328+ {
329+ "type":"OximeterMeasured",
330+ "source":"oximeter/123",
331+ "id":"1001",
332+ "specversion":"1.0",
333+ "datacontenttype": "application/json",
334+ "data_base64": "AWJpbmFyeWRhdGEC"
335+ }
336+ ]
337+ '''
338+ target = [
339+ CloudEvent (
340+ type = "OximeterMeasured" ,
341+ source = "oximeter/123" ,
342+ id = "1000" ,
343+ datacontenttype = "application/json" ,
344+ data = json .dumps ({"spo2" : 99 }),
345+ ),
346+ CloudEvent (
347+ type = "OximeterMeasured" ,
348+ source = "oximeter/123" ,
349+ id = "1001" ,
350+ datacontenttype = "application/json" ,
351+ data = b'\x01 binarydata\x02 ' ,
352+ ),
353+ ]
354+ self .assertEqual (target , Json .decode (encoded_batch ))
0 commit comments