Skip to content

Commit

Permalink
validate_emit is taken out of the StandaloneMockContext class
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsimb committed Oct 24, 2023
1 parent 077dec3 commit 26a26d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
29 changes: 19 additions & 10 deletions exasol_udf_mock_python/mock_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ def wrapper(self, *args, **kwargs):
return wrapper


def validate_emit(row: Tuple, columns: List[Column]):
"""
Validates that a data row to be emitted corresponds to the definition of the output columns.
The number of elements in the row should match the number of columns and the type of each
element should match the type of the correspondent column. Raises a ValueError if the first
condition is false or a TypeError if the second condition is false.
:param row: Data row
:param columns: Column definition.
"""
if len(row) != len(columns):
raise ValueError(f"row {row} has not the same number of values as columns are defined")
for i, column in enumerate(columns):
if row[i] is not None and not isinstance(row[i], column.type):
raise TypeError(f"Value {row[i]} ({type(row[i])}) at position {i} is not a {column.type}")


class MockContext(UDFContext):
"""
Implementation of generic UDF Mock Context interface for a SET UDF with groups.
Expand Down Expand Up @@ -213,7 +230,7 @@ def next(self, reset: bool = False):
try:
new_data = next(self._iter)
self._data = new_data
self.validate_emit(self._data, self._metadata.input_columns)
validate_emit(self._data, self._metadata.input_columns)
return True
except StopIteration as e:
self._data = None
Expand All @@ -232,13 +249,5 @@ def emit(self, *args):
else:
tuples = [args]
for row in tuples:
self.validate_emit(row, self._metadata.output_columns)
validate_emit(row, self._metadata.output_columns)
self._output.extend(tuples)

@staticmethod
def validate_emit(row: Tuple, columns: List[Column]):
if len(row) != len(columns):
raise Exception(f"row {row} has not the same number of values as columns are defined")
for i, column in enumerate(columns):
if row[i] is not None and not isinstance(row[i], column.type):
raise TypeError(f"Value {row[i]} ({type(row[i])}) at position {i} is not a {column.type}")
10 changes: 5 additions & 5 deletions tests/test_mock_context_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from exasol_udf_mock_python.column import Column
from exasol_udf_mock_python.mock_meta_data import MockMetaData
from exasol_udf_mock_python.mock_context import StandaloneMockContext
from exasol_udf_mock_python.mock_context import StandaloneMockContext, validate_emit


def udf_wrapper():
Expand Down Expand Up @@ -86,16 +86,16 @@ def test_size(context_set_emits):


def test_validate_emit_good(meta_set_emits):
StandaloneMockContext.validate_emit((10, 'fish'), meta_set_emits.output_columns)
validate_emit((10, 'fish'), meta_set_emits.output_columns)


def test_validate_emit_bad(meta_set_emits):
with pytest.raises(Exception):
StandaloneMockContext.validate_emit((10,), meta_set_emits.output_columns)
validate_emit((10,), meta_set_emits.output_columns)
with pytest.raises(Exception):
StandaloneMockContext.validate_emit((10, 'fish', 4.5), meta_set_emits.output_columns)
validate_emit((10, 'fish', 4.5), meta_set_emits.output_columns)
with pytest.raises(Exception):
StandaloneMockContext.validate_emit((10., 'fish'), meta_set_emits.output_columns)
validate_emit((10., 'fish'), meta_set_emits.output_columns)


def test_emit_df(context_set_emits):
Expand Down

0 comments on commit 26a26d7

Please sign in to comment.