Skip to content

Commit 3d81ecd

Browse files
committed
Add unit test to smoke test the tool RPC handler
1 parent 8954f08 commit 3d81ecd

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

extensions/positron-python/python_files/posit/positron/data_explorer.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def get_schema(self, params: GetSchemaParams):
267267
column_schemas.append(self._get_single_column_schema(column_index))
268268

269269
# Return the column schemas.
270-
return TableSchema(columns=column_schemas).dict()
270+
return TableSchema(columns=column_schemas)
271271

272272
def _get_single_column_schema(self, column_index: int) -> ColumnSchema:
273273
raise NotImplementedError
@@ -289,7 +289,7 @@ def search_schema(self, params: SearchSchemaParams):
289289
return SearchSchemaResult(
290290
matches=TableSchema(columns=[self._get_single_column_schema(i) for i in matches_slice]),
291291
total_num_matches=len(matches),
292-
).dict()
292+
)
293293

294294
def _column_filter_get_matches(self, filters: list[ColumnFilter]):
295295
matchers = self._get_column_filter_functions(filters)
@@ -429,7 +429,7 @@ def _set_row_filters(self, filters: list[RowFilter]):
429429
# Simply reset if empty filter set passed
430430
self.filtered_indices = None
431431
self._update_row_view_indices()
432-
return FilterResult(selected_num_rows=len(self.table), had_errors=False).dict()
432+
return FilterResult(selected_num_rows=len(self.table), had_errors=False)
433433

434434
# Evaluate all the filters and combine them using the
435435
# indicated conditions
@@ -469,7 +469,7 @@ def _set_row_filters(self, filters: list[RowFilter]):
469469

470470
# Update the view indices, re-sorting if needed
471471
self._update_row_view_indices()
472-
return FilterResult(selected_num_rows=selected_num_rows, had_errors=had_errors).dict()
472+
return FilterResult(selected_num_rows=selected_num_rows, had_errors=had_errors)
473473

474474
def _mask_to_indices(self, mask):
475475
raise NotImplementedError
@@ -582,7 +582,7 @@ def get_state(self, _unused):
582582
row_filters=self.state.row_filters,
583583
sort_keys=self.state.sort_keys,
584584
supported_features=self.FEATURES,
585-
).dict()
585+
)
586586

587587
def _recompute(self):
588588
# Re-setting the column filters will trigger filtering AND
@@ -1583,10 +1583,10 @@ def _export_tabular(self, row_selector, column_selector, fmt: ExportFormat):
15831583
if result[-1] == "\n":
15841584
result = result[:-1]
15851585

1586-
return ExportedData(data=result, format=fmt).dict()
1586+
return ExportedData(data=result, format=fmt)
15871587

15881588
def _export_cell(self, row_index: int, column_index: int, fmt: ExportFormat):
1589-
return ExportedData(data=str(self.table.iloc[row_index, column_index]), format=fmt).dict()
1589+
return ExportedData(data=str(self.table.iloc[row_index, column_index]), format=fmt)
15901590

15911591
def _mask_to_indices(self, mask):
15921592
if mask is not None:
@@ -2415,10 +2415,10 @@ def _export_tabular(self, row_selector, column_selector, fmt: ExportFormat):
24152415
elif fmt == ExportFormat.Html:
24162416
raise NotImplementedError(f"Unsupported export format {fmt}")
24172417

2418-
return ExportedData(data=result, format=fmt).dict()
2418+
return ExportedData(data=result, format=fmt)
24192419

24202420
def _export_cell(self, row_index: int, column_index: int, fmt: ExportFormat):
2421-
return ExportedData(data=str(self.table[row_index, column_index]), format=fmt).dict()
2421+
return ExportedData(data=str(self.table[row_index, column_index]), format=fmt)
24222422

24232423
SUPPORTED_FILTERS = frozenset(
24242424
{
@@ -3032,6 +3032,9 @@ def handle_msg(self, msg: CommMessage[DataExplorerBackendMessageContent], _raw_m
30323032

30333033
# To help remember to convert pydantic types to dicts
30343034
if result is not None:
3035+
# Convert pydantic types to dict
3036+
if not isinstance(result, dict):
3037+
result = result.dict()
30353038
if isinstance(result, list):
30363039
for x in result:
30373040
assert isinstance(x, dict)

extensions/positron-python/python_files/posit/positron/tests/test_variables.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ def _assign_variables(shell: PositronShell, variables_comm: DummyComm, **variabl
915915
def test_query_table_summary(shell: PositronShell, variables_comm: DummyComm):
916916
from .test_data_explorer import SIMPLE_PANDAS_DF
917917

918-
_assign_variables(shell, variables_comm, df=SIMPLE_PANDAS_DF)
918+
_assign_variables(shell, variables_comm, df=SIMPLE_PANDAS_DF.iloc[:, :2])
919919

920920
msg = json_rpc_request(
921921
"query_table_summary",
@@ -927,8 +927,16 @@ def test_query_table_summary(shell: PositronShell, variables_comm: DummyComm):
927927
assert variables_comm.messages == [
928928
json_rpc_response(
929929
{
930-
"summary": ANY,
931-
"version": 0,
930+
"num_rows": 5,
931+
"num_columns": 2,
932+
"column_schemas": [
933+
'{"column_name": "a", "column_index": 0, "type_name": "int64", "type_display": "number", "description": null, "children": null, "precision": null, "scale": null, "timezone": null, "type_size": null}',
934+
'{"column_name": "b", "column_index": 1, "type_name": "bool", "type_display": "boolean", "description": null, "children": null, "precision": null, "scale": null, "timezone": null, "type_size": null}',
935+
],
936+
"column_profiles": [
937+
'{"column_name": "a", "type_display": "number", "summary_stats": {"type_display": "number", "number_stats": {"min_value": "1.0000", "max_value": "5.0000", "mean": "3.0000", "median": "3.0000", "stdev": "1.5811"}, "string_stats": null, "boolean_stats": null, "date_stats": null, "datetime_stats": null, "other_stats": null}}',
938+
'{"column_name": "b", "type_display": "boolean", "summary_stats": {"type_display": "boolean", "number_stats": null, "string_stats": null, "boolean_stats": {"true_count": 3, "false_count": 1}, "date_stats": null, "datetime_stats": null, "other_stats": null}}',
939+
],
932940
}
933941
)
934942
]

extensions/positron-python/python_files/posit/positron/variables.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,12 +714,14 @@ def _send_details(self, _path: list[str], value: Any = None):
714714

715715
def _perform_get_table_summary(self, path: list[str], query_types: list[str]) -> None:
716716
"""RPC handler for getting table summary."""
717+
import traceback
718+
717719
try:
718720
self._get_table_summary(path, query_types)
719721
except Exception as err:
720722
self._send_error(
721723
JsonRpcErrorCode.INTERNAL_ERROR,
722-
f"Error summarizing table at '{path}': {err}",
724+
f"Error summarizing table at '{path}': {err}\n{traceback.format_exc()}",
723725
)
724726

725727
def _get_table_summary(self, path: list[str], query_types: list[str]) -> None:
@@ -750,7 +752,7 @@ def _get_table_summary(self, path: list[str], query_types: list[str]) -> None:
750752
# Get schema using the helper function
751753
num_rows = table_view.table.shape[0]
752754
num_columns = table_view.table.shape[1]
753-
schema = table_view.get_schema(GetSchemaParams(list(range(num_columns))))
755+
schema = table_view.get_schema(GetSchemaParams(column_indices=list(range(num_columns))))
754756

755757
# Create default format options
756758
format_options = FormatOptions(

0 commit comments

Comments
 (0)