From 26a26d7100ed0e3793faf853da64a8978d8e2a2b Mon Sep 17 00:00:00 2001 From: mibe Date: Tue, 24 Oct 2023 14:04:02 +0100 Subject: [PATCH] validate_emit is taken out of the StandaloneMockContext class --- exasol_udf_mock_python/mock_context.py | 29 +++++++++++++++++--------- tests/test_mock_context_standalone.py | 10 ++++----- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/exasol_udf_mock_python/mock_context.py b/exasol_udf_mock_python/mock_context.py index e266774..86635c2 100644 --- a/exasol_udf_mock_python/mock_context.py +++ b/exasol_udf_mock_python/mock_context.py @@ -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. @@ -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 @@ -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}") diff --git a/tests/test_mock_context_standalone.py b/tests/test_mock_context_standalone.py index 6ffc10c..9ce794d 100644 --- a/tests/test_mock_context_standalone.py +++ b/tests/test_mock_context_standalone.py @@ -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(): @@ -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):