def test_reading_events_non_blocking(): # http://amzn.github.io/ion-docs/guides/cookbook.html#reading-and-writing-ion-data reader = managed_reader(text_reader()) event = reader.send(NEXT_EVENT) # No data has been provided, so the reader is at STREAM_END # and will wait for data. assert event.event_type == IonEventType.STREAM_END # Send an incomplete Ion value. event = reader.send(read_data_event(b'{hello:')) # Enough data was available for the reader to determine that # the start of a struct value has been encountered. assert event.event_type == IonEventType.CONTAINER_START assert event.ion_type == IonType.STRUCT # Advancing the reader causes it to step into the struct. event = reader.send(NEXT_EVENT) # The reader reached the end of the data before completing # a value. Therefore, an INCOMPLETE event is returned. assert event.event_type == IonEventType.INCOMPLETE # Send the rest of the value. event = reader.send(read_data_event(b'"world"}')) # The reader now finishes parsing the value within the struct. assert event.event_type == IonEventType.SCALAR assert event.ion_type == IonType.STRING hello = event.field_name.text world = event.value # Advance the reader past the string value. event = reader.send(NEXT_EVENT) # The reader has reached the end of the struct. assert event.event_type == IonEventType.CONTAINER_END # Advancing the reader causes it to step out of the struct. event = reader.send(NEXT_EVENT) # There is no more data and a value has been completed. # Therefore, the reader conveys STREAM_END. assert event.event_type == IonEventType.STREAM_END assert u'hello world' == u'%s %s' % (hello, world)
def annotated(): for input_event, output_event in param.event_pairs: if input_event.type is ReadEventType.DATA: data_len = _TEST_ANNOTATION_LEN + len(input_event.data) data = _gen_type_len(_TypeID.ANNOTATION, data_len) \ + _TEST_ANNOTATION_DATA \ + input_event.data input_event = read_data_event(data) output_event = output_event.derive_annotations(_TEST_ANNOTATION_SIDS) yield input_event, output_event
_ivm_transition = partial(Transition, ION_VERSION_MARKER_EVENT) _incomplete_transition = partial(Transition, ION_STREAM_INCOMPLETE_EVENT) _end_transition = partial(Transition, ION_STREAM_END_EVENT) _event_transition = partial(Transition, _TRIVIAL_ION_EVENT) class ReaderTrampolineParameters( record('desc', 'coroutine', 'input', 'expected', ('allow_flush', False))): def __str__(self): return self.desc _P = ReaderTrampolineParameters _TRIVIAL_DATA_EVENT = read_data_event(b'DATA') _EMPTY_DATA_EVENT = read_data_event(b'') @parametrize( _P( desc='START WITH NONE', coroutine=always_self(_ivm_transition), input=[None], expected=[TypeError], ), _P( desc='START WITH SKIP', coroutine=always_self(_ivm_transition), input=[SKIP_EVENT], expected=[TypeError],
_TRIVIAL_ION_EVENT = e_int(0) _ivm_transition = partial(Transition, ION_VERSION_MARKER_EVENT) _incomplete_transition = partial(Transition, ION_STREAM_INCOMPLETE_EVENT) _end_transition = partial(Transition, ION_STREAM_END_EVENT) _event_transition = partial(Transition, _TRIVIAL_ION_EVENT) class ReaderTrampolineParameters(record('desc', 'coroutine', 'input', 'expected', ('allow_flush', False))): def __str__(self): return self.desc _P = ReaderTrampolineParameters _TRIVIAL_DATA_EVENT = read_data_event(b'DATA') _EMPTY_DATA_EVENT = read_data_event(b'') @parametrize( _P( desc='START WITH NONE', coroutine=always_self(_ivm_transition), input=[None], expected=[TypeError], ), _P( desc='START WITH SKIP', coroutine=always_self(_ivm_transition), input=[SKIP_EVENT], expected=[TypeError],