Skip to content

Commit c9be02c

Browse files
authored
Add checks for parameter of get_dataframe (#20)
1 parent 9ece735 commit c9be02c

File tree

2 files changed

+180
-12
lines changed

2 files changed

+180
-12
lines changed

exasol_udf_mock_python/mock_context.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ def __init__(self, input_groups: Iterator[Group], metadata: MockMetaData):
1414
self._input_groups = input_groups
1515
self._output_groups = []
1616
self._input_group = None # type: Group
17-
self._output_group_list = None # type: List
17+
self._output_group_list = None # type: List
1818
self._output_group = None # type: Group
1919
self._iter = None # type: Iterator[Tuple]
20-
self._len = None # type: int
20+
self._len = None # type: int
2121
self._metadata = metadata
2222
self._name_position_map = \
2323
{column.name: position
@@ -51,7 +51,14 @@ def _next_group(self):
5151
self.next()
5252
return True
5353

54+
def _is_positive_integer(self, value):
55+
return value is not None and isinstance(value, int) and value > 0
56+
5457
def get_dataframe(self, num_rows='all', start_col=0):
58+
if not (num_rows == 'all' or self._is_positive_integer(num_rows)):
59+
raise RuntimeError("get_dataframe() parameter 'num_rows' must be 'all' or an integer > 0")
60+
if not (self._is_positive_integer(start_col) or start_col == 0):
61+
raise RuntimeError("get_dataframe() parameter 'start_col' must be an integer >= 0")
5562
if self._data is None:
5663
return None
5764
columns_ = [column.name for column in self._metadata.input_columns]
@@ -66,15 +73,15 @@ def get_dataframe(self, num_rows='all', start_col=0):
6673
df = df.append(df_current)
6774
if not self.next():
6875
break
69-
i+=1
76+
i += 1
7077
if df is not None:
7178
df = df.reset_index(drop=True)
7279
return df
7380

7481
def __getattr__(self, name):
7582
return self._data[self._name_position_map[name]]
7683

77-
def next(self, reset:bool = False):
84+
def next(self, reset: bool = False):
7885
if reset:
7986
self.reset()
8087
else:

tests/test_executor_context_set_emits.py

Lines changed: 169 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def run(ctx):
2828
result = executor.run([Group([(1,), (5,), (6,)])], exa)
2929
assert result == [Group([(1,), (5,), (6,)])]
3030

31+
3132
def test_emit_single_column_none():
3233
def udf_wrapper():
33-
3434
def run(ctx):
3535
ctx.emit(None)
3636

@@ -46,11 +46,11 @@ def run(ctx):
4646
result = executor.run([Group([(1,), (5,), (6,)])], exa)
4747
assert result == [Group([(None,)])]
4848

49+
4950
def test_emit_multi_column_none():
5051
def udf_wrapper():
51-
5252
def run(ctx):
53-
ctx.emit(None,None)
53+
ctx.emit(None, None)
5454

5555
executor = UDFMockExecutor()
5656
meta = MockMetaData(
@@ -63,7 +63,8 @@ def run(ctx):
6363
)
6464
exa = MockExaEnvironment(meta)
6565
result = executor.run([Group([(1,), (5,), (6,)])], exa)
66-
assert result == [Group([(None,None)])]
66+
assert result == [Group([(None, None)])]
67+
6768

6869
def test_next_emit_reset():
6970
def udf_wrapper():
@@ -75,7 +76,7 @@ def run(ctx):
7576
break
7677
ctx.reset()
7778
while True:
78-
ctx.emit(ctx.t+1)
79+
ctx.emit(ctx.t + 1)
7980
if not ctx.next():
8081
break
8182

@@ -91,6 +92,7 @@ def run(ctx):
9192
result = executor.run([Group([(1,), (5,), (6,)])], exa)
9293
assert result == [Group([(1,), (5,), (6,), (2,), (6,), (7,)])]
9394

95+
9496
def test_next_reset_combined():
9597
def udf_wrapper():
9698

@@ -101,7 +103,7 @@ def run(ctx):
101103
break
102104
ctx.next(reset=True)
103105
for i in range(2):
104-
ctx.emit(ctx.t+1)
106+
ctx.emit(ctx.t + 1)
105107
if not ctx.next():
106108
break
107109

@@ -115,7 +117,7 @@ def run(ctx):
115117
)
116118
exa = MockExaEnvironment(meta)
117119
result = executor.run([Group([(1,), (5,), (6,)])], exa)
118-
assert result == [Group([(1,), (5,),(2,), (6,)])]
120+
assert result == [Group([(1,), (5,), (2,), (6,)])]
119121

120122

121123
def test_get_dataframe_all():
@@ -185,9 +187,168 @@ def run(ctx):
185187
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)
186188
assert result == [Group([(1,), (2,), (4,), (5,)])]
187189

188-
def test_emit_tuple_exception():
190+
191+
def test_get_dataframe_num_rows_1():
192+
def udf_wrapper():
193+
def run(ctx):
194+
df = ctx.get_dataframe(num_rows=1)
195+
ctx.emit(df)
196+
197+
executor = UDFMockExecutor()
198+
meta = MockMetaData(
199+
script_code_wrapper_function=udf_wrapper,
200+
input_type="SET",
201+
input_columns=[Column("t", int, "INTEGER")],
202+
output_type="EMITS",
203+
output_columns=[Column("t", int, "INTEGER")]
204+
)
205+
exa = MockExaEnvironment(meta)
206+
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)
207+
assert result == [Group([(1,), ])]
208+
209+
210+
def test_get_dataframe_num_rows_0():
211+
def udf_wrapper():
212+
def run(ctx):
213+
df = ctx.get_dataframe(num_rows=0)
214+
215+
executor = UDFMockExecutor()
216+
meta = MockMetaData(
217+
script_code_wrapper_function=udf_wrapper,
218+
input_type="SET",
219+
input_columns=[Column("t", int, "INTEGER")],
220+
output_type="EMITS",
221+
output_columns=[Column("t", int, "INTEGER")]
222+
)
223+
exa = MockExaEnvironment(meta)
224+
with pytest.raises(RuntimeError) as excinfo:
225+
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)
226+
227+
228+
def test_get_dataframe_num_rows_float():
229+
def udf_wrapper():
230+
def run(ctx):
231+
df = ctx.get_dataframe(num_rows=1.5)
232+
233+
executor = UDFMockExecutor()
234+
meta = MockMetaData(
235+
script_code_wrapper_function=udf_wrapper,
236+
input_type="SET",
237+
input_columns=[Column("t", int, "INTEGER")],
238+
output_type="EMITS",
239+
output_columns=[Column("t", int, "INTEGER")]
240+
)
241+
exa = MockExaEnvironment(meta)
242+
with pytest.raises(RuntimeError) as excinfo:
243+
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)
244+
245+
def test_get_dataframe_num_rows_None():
246+
def udf_wrapper():
247+
def run(ctx):
248+
df = ctx.get_dataframe(num_rows=None)
249+
250+
executor = UDFMockExecutor()
251+
meta = MockMetaData(
252+
script_code_wrapper_function=udf_wrapper,
253+
input_type="SET",
254+
input_columns=[Column("t", int, "INTEGER")],
255+
output_type="EMITS",
256+
output_columns=[Column("t", int, "INTEGER")]
257+
)
258+
exa = MockExaEnvironment(meta)
259+
with pytest.raises(RuntimeError) as excinfo:
260+
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)
261+
262+
263+
def test_get_dataframe_num_rows_negative():
264+
def udf_wrapper():
265+
def run(ctx):
266+
df = ctx.get_dataframe(num_rows=-1)
267+
268+
executor = UDFMockExecutor()
269+
meta = MockMetaData(
270+
script_code_wrapper_function=udf_wrapper,
271+
input_type="SET",
272+
input_columns=[Column("t", int, "INTEGER")],
273+
output_type="EMITS",
274+
output_columns=[Column("t", int, "INTEGER")]
275+
)
276+
exa = MockExaEnvironment(meta)
277+
with pytest.raises(RuntimeError) as excinfo:
278+
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)
279+
280+
def test_get_dataframe_start_col_None():
281+
def udf_wrapper():
282+
def run(ctx):
283+
df = ctx.get_dataframe(num_rows=10, start_col=None)
284+
285+
executor = UDFMockExecutor()
286+
meta = MockMetaData(
287+
script_code_wrapper_function=udf_wrapper,
288+
input_type="SET",
289+
input_columns=[Column("t", int, "INTEGER")],
290+
output_type="EMITS",
291+
output_columns=[Column("t", int, "INTEGER")]
292+
)
293+
exa = MockExaEnvironment(meta)
294+
with pytest.raises(RuntimeError) as excinfo:
295+
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)
296+
297+
def test_get_dataframe_start_col_negative():
298+
def udf_wrapper():
299+
def run(ctx):
300+
df = ctx.get_dataframe(num_rows=10, start_col=-1)
301+
302+
executor = UDFMockExecutor()
303+
meta = MockMetaData(
304+
script_code_wrapper_function=udf_wrapper,
305+
input_type="SET",
306+
input_columns=[Column("t", int, "INTEGER")],
307+
output_type="EMITS",
308+
output_columns=[Column("t", int, "INTEGER")]
309+
)
310+
exa = MockExaEnvironment(meta)
311+
with pytest.raises(RuntimeError) as excinfo:
312+
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)
313+
314+
def test_get_dataframe_start_col_0():
189315
def udf_wrapper():
316+
def run(ctx):
317+
df = ctx.get_dataframe(num_rows=1, start_col=0)
318+
ctx.emit(df)
319+
320+
executor = UDFMockExecutor()
321+
meta = MockMetaData(
322+
script_code_wrapper_function=udf_wrapper,
323+
input_type="SET",
324+
input_columns=[Column("t", int, "INTEGER")],
325+
output_type="EMITS",
326+
output_columns=[Column("t", int, "INTEGER")]
327+
)
328+
exa = MockExaEnvironment(meta)
329+
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)
330+
assert result == [Group([(1,), ])]
190331

332+
def test_get_dataframe_start_col_positive():
333+
def udf_wrapper():
334+
def run(ctx):
335+
df = ctx.get_dataframe(num_rows=1, start_col=1)
336+
ctx.emit(df)
337+
338+
executor = UDFMockExecutor()
339+
meta = MockMetaData(
340+
script_code_wrapper_function=udf_wrapper,
341+
input_type="SET",
342+
input_columns=[Column("t", int, "INTEGER")],
343+
output_type="EMITS",
344+
output_columns=[Column("t", int, "INTEGER")]
345+
)
346+
exa = MockExaEnvironment(meta)
347+
result = executor.run([Group([(1,), (2,), (3,), (4,), (5,), (6,)])], exa)
348+
assert result == [Group([(1,), ])]
349+
350+
def test_emit_tuple_exception():
351+
def udf_wrapper():
191352
def run(ctx):
192353
while True:
193354
ctx.emit((1,))

0 commit comments

Comments
 (0)