Skip to content

Commit 9470c31

Browse files
authored
Merge pull request #219 from posit-dev/fix-add-more-tests-for-validate-py
fix: add more tests for `validate.py` file
2 parents c7e3cfe + 3c4569c commit 9470c31

File tree

2 files changed

+983
-113
lines changed

2 files changed

+983
-113
lines changed

pointblank/validate.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,10 @@ def _generate_display_table(
14441444
column_values = gt.gt._get_column_of_values(built_gt, column_name=column, context="html")
14451445

14461446
# Get the maximum number of characters in the column
1447-
max_length_col_vals.append(max([len(str(val)) for val in column_values]))
1447+
if column_values: # Check if column_values is not empty
1448+
max_length_col_vals.append(max([len(str(val)) for val in column_values]))
1449+
else:
1450+
max_length_col_vals.append(0) # Use 0 for empty columns
14481451

14491452
length_col_names = [len(column) for column in col_dtype_dict.keys()]
14501453
length_data_types = [len(dtype) for dtype in col_dtype_dict_short.values()]
@@ -1515,8 +1518,12 @@ def _generate_display_table(
15151518

15161519
# Get the highest number in the `row_number_list` and calculate a width that will
15171520
# safely fit a number of that magnitude
1518-
max_row_num = max(row_number_list)
1519-
max_row_num_width = len(str(max_row_num)) * 7.8 + 10
1521+
if row_number_list: # Check if list is not empty
1522+
max_row_num = max(row_number_list)
1523+
max_row_num_width = len(str(max_row_num)) * 7.8 + 10
1524+
else:
1525+
# If row_number_list is empty, use a default width
1526+
max_row_num_width = 7.8 * 2 + 10 # Width for 2-digit numbers
15201527

15211528
# Update the col_width_dict to include the row number column
15221529
col_width_dict = {"_row_num_": f"{max_row_num_width}px"} | col_width_dict
@@ -9154,37 +9161,47 @@ def interrogate(
91549161

91559162
# Determine whether any preprocessing functions are to be applied to the table
91569163
if validation.pre is not None:
9157-
# Read the text of the preprocessing function
9158-
pre_text = _pre_processing_funcs_to_str(validation.pre)
9164+
try:
9165+
# Read the text of the preprocessing function
9166+
pre_text = _pre_processing_funcs_to_str(validation.pre)
9167+
9168+
# Determine if the preprocessing function is a lambda function; return a boolean
9169+
is_lambda = re.match(r"^lambda", pre_text) is not None
91599170

9160-
# Determine if the preprocessing function is a lambda function; return a boolean
9161-
is_lambda = re.match(r"^lambda", pre_text) is not None
9171+
# If the preprocessing function is a lambda function, then check if there is
9172+
# a keyword argument called `dfn` in the lamda signature; if so, that's a cue
9173+
# to use a Narwhalified version of the table
9174+
if is_lambda:
9175+
# Get the signature of the lambda function
9176+
sig = inspect.signature(validation.pre)
91629177

9163-
# If the preprocessing function is a lambda function, then check if there is
9164-
# a keyword argument called `dfn` in the lamda signature; if so, that's a cue
9165-
# to use a Narwhalified version of the table
9166-
if is_lambda:
9167-
# Get the signature of the lambda function
9168-
sig = inspect.signature(validation.pre)
9178+
# Check if the lambda function has a keyword argument called `dfn`
9179+
if "dfn" in sig.parameters:
9180+
# Convert the table to a Narwhals DataFrame
9181+
data_tbl_step = nw.from_native(data_tbl_step)
91699182

9170-
# Check if the lambda function has a keyword argument called `dfn`
9171-
if "dfn" in sig.parameters:
9172-
# Convert the table to a Narwhals DataFrame
9173-
data_tbl_step = nw.from_native(data_tbl_step)
9183+
# Apply the preprocessing function to the table
9184+
data_tbl_step = validation.pre(dfn=data_tbl_step)
91749185

9175-
# Apply the preprocessing function to the table
9176-
data_tbl_step = validation.pre(dfn=data_tbl_step)
9186+
# Convert the table back to its original format
9187+
data_tbl_step = nw.to_native(data_tbl_step)
91779188

9178-
# Convert the table back to its original format
9179-
data_tbl_step = nw.to_native(data_tbl_step)
9189+
else:
9190+
# Apply the preprocessing function to the table
9191+
data_tbl_step = validation.pre(data_tbl_step)
91809192

9181-
else:
9182-
# Apply the preprocessing function to the table
9193+
# If the preprocessing function is a function, apply it to the table
9194+
elif isinstance(validation.pre, Callable):
91839195
data_tbl_step = validation.pre(data_tbl_step)
91849196

9185-
# If the preprocessing function is a function, apply it to the table
9186-
elif isinstance(validation.pre, Callable):
9187-
data_tbl_step = validation.pre(data_tbl_step)
9197+
except Exception:
9198+
# If preprocessing fails, mark the validation as having an eval_error
9199+
validation.eval_error = True
9200+
end_time = datetime.datetime.now(datetime.timezone.utc)
9201+
validation.proc_duration_s = (end_time - start_time).total_seconds()
9202+
validation.time_processed = end_time.isoformat(timespec="milliseconds")
9203+
validation.active = False
9204+
continue
91889205

91899206
# ------------------------------------------------
91909207
# Segmentation stage

0 commit comments

Comments
 (0)