forked from amazon-ion/ion-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_reader_base.py
232 lines (212 loc) · 7.31 KB
/
test_reader_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at:
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the
# License.
# Python 2/3 compatibility
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from functools import partial
from io import BytesIO
from pytest import raises
from tests import is_exception, parametrize
from tests.event_aliases import e_int
from tests.trampoline_util import always_self
from tests.trampoline_util import trampoline_scaffold
from amazon.ion.core import Transition
from amazon.ion.core import ION_VERSION_MARKER_EVENT
from amazon.ion.core import ION_STREAM_INCOMPLETE_EVENT, ION_STREAM_END_EVENT
from amazon.ion.reader import read_data_event, reader_trampoline, blocking_reader
from amazon.ion.reader import NEXT_EVENT, SKIP_EVENT
from amazon.ion.util import coroutine, record
_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],
),
_P(
desc='ALWAYS IVM NEXT',
coroutine=always_self(_ivm_transition),
input=[NEXT_EVENT] * 4,
expected=[ION_VERSION_MARKER_EVENT] * 4,
),
_P(
desc='ALWAYS IVM NEXT THEN SKIP',
coroutine=always_self(_ivm_transition),
input=[NEXT_EVENT, SKIP_EVENT],
expected=[ION_VERSION_MARKER_EVENT, TypeError],
),
_P(
desc='ALWAYS INCOMPLETE NEXT EOF',
coroutine=always_self(_incomplete_transition),
input=[NEXT_EVENT, NEXT_EVENT],
expected=[ION_STREAM_INCOMPLETE_EVENT, TypeError],
allow_flush=False
),
_P(
desc='ALWAYS INCOMPLETE NEXT FLUSH',
coroutine=always_self(_incomplete_transition),
input=[NEXT_EVENT, NEXT_EVENT],
expected=[ION_STREAM_INCOMPLETE_EVENT, ION_STREAM_INCOMPLETE_EVENT],
allow_flush=True
),
_P(
desc='ALWAYS INCOMPLETE SKIP',
coroutine=always_self(_incomplete_transition),
input=[NEXT_EVENT, SKIP_EVENT],
expected=[ION_STREAM_INCOMPLETE_EVENT, TypeError],
),
_P(
desc='ALWAYS INCOMPLETE DATA',
coroutine=always_self(_incomplete_transition),
input=[NEXT_EVENT] + [_TRIVIAL_DATA_EVENT] * 4,
expected=[ION_STREAM_INCOMPLETE_EVENT] * 5,
),
_P(
desc='ALWAYS END NEXT',
coroutine=always_self(_end_transition),
input=[NEXT_EVENT, NEXT_EVENT],
expected=[ION_STREAM_END_EVENT, TypeError],
),
_P(
desc='ALWAYS END SKIP',
coroutine=always_self(_end_transition),
input=[NEXT_EVENT, SKIP_EVENT],
expected=[ION_STREAM_END_EVENT, TypeError],
),
_P(
desc='ALWAYS END DATA',
coroutine=always_self(_end_transition),
input=[NEXT_EVENT] + [_TRIVIAL_DATA_EVENT] * 4,
expected=[ION_STREAM_END_EVENT] * 5,
),
_P(
desc='ALWAYS END EMPTY DATA',
coroutine=always_self(_end_transition),
input=[NEXT_EVENT] + [_EMPTY_DATA_EVENT],
expected=[ION_STREAM_END_EVENT, ValueError],
),
_P(
desc='ALWAYS EVENT DATA',
coroutine=always_self(_event_transition),
input=[NEXT_EVENT] + [_TRIVIAL_DATA_EVENT],
expected=[_TRIVIAL_ION_EVENT, TypeError]
)
)
def test_trampoline(p):
trampoline_scaffold(reader_trampoline, p, p.allow_flush)
class _P(record('desc', 'coroutine', 'data', 'input', 'expected')):
def __str__(self):
return self.desc
@coroutine
def _asserts_events(expecteds, outputs, allow_flush=False):
output = None
for expected, next_output in zip(expecteds, outputs):
actual = yield output
assert expected == actual
output = next_output
yield output
if not allow_flush:
raise EOFError()
yield ION_STREAM_END_EVENT
@parametrize(
_P(
desc='ALWAYS COMPLETE',
coroutine=_asserts_events(
[NEXT_EVENT, SKIP_EVENT],
[ION_VERSION_MARKER_EVENT] * 2),
data=b'',
input=[NEXT_EVENT, SKIP_EVENT],
expected=[ION_VERSION_MARKER_EVENT] * 2
),
_P(
desc='FIRST INCOMPLETE, THEN COMPLETE',
coroutine=_asserts_events(
[NEXT_EVENT, read_data_event(b'a'), SKIP_EVENT],
[ION_STREAM_INCOMPLETE_EVENT] + ([ION_VERSION_MARKER_EVENT] * 2)
),
data=b'a',
input=[NEXT_EVENT, SKIP_EVENT],
expected=[ION_VERSION_MARKER_EVENT] * 2
),
_P(
desc='FIRST STREAM END, THEN COMPLETE',
coroutine=_asserts_events(
[NEXT_EVENT, read_data_event(b'a'), SKIP_EVENT],
[ION_STREAM_END_EVENT] + ([ION_VERSION_MARKER_EVENT] * 2)
),
data=b'a',
input=[NEXT_EVENT, SKIP_EVENT],
expected=[ION_VERSION_MARKER_EVENT] * 2
),
_P(
desc='PREMATURE EOF',
coroutine=_asserts_events(
[NEXT_EVENT, read_data_event(b'a'), read_data_event(b'b')],
[ION_STREAM_END_EVENT, ION_STREAM_INCOMPLETE_EVENT, ION_STREAM_INCOMPLETE_EVENT],
allow_flush=False
),
data=b'ab',
input=[NEXT_EVENT],
expected=[EOFError]
),
_P(
desc='FLUSH',
coroutine=_asserts_events(
[NEXT_EVENT, read_data_event(b'a'), read_data_event(b'b')],
[ION_STREAM_END_EVENT, ION_STREAM_INCOMPLETE_EVENT, ION_STREAM_INCOMPLETE_EVENT],
allow_flush=True
),
data=b'ab',
input=[NEXT_EVENT],
expected=[ION_STREAM_END_EVENT]
),
_P(
desc='SINGLE EVENT, THEN NATURAL EOF',
coroutine=_asserts_events(
[NEXT_EVENT, read_data_event(b'a'), NEXT_EVENT],
[ION_STREAM_END_EVENT, ION_VERSION_MARKER_EVENT, ION_STREAM_END_EVENT]
),
data=b'a',
input=[NEXT_EVENT, NEXT_EVENT],
expected=[ION_VERSION_MARKER_EVENT, ION_STREAM_END_EVENT]
),
)
def test_blocking_reader(p):
buf = BytesIO(p.data)
reader = blocking_reader(p.coroutine, buf, buffer_size=1)
for input, expected in zip(p.input, p.expected):
if is_exception(expected):
with raises(expected):
reader.send(input)
else:
actual = reader.send(input)
assert expected == actual