def test_mixed_type_sort(): df = pd.DataFrame({ "Mixed": [pd.Timestamp("2017-02-02"), np.nan, 1e10, 5, "hey"], 1: [5, 4, "hello", 2, 1], }) spreadsheet = SpreadsheetWidget(df=df) spreadsheet._handle_view_msg_helper({ "type": "change_sort", "sort_field": "Mixed", "sort_ascending": True }) expected_order_mixed = [1e10, pd.Timestamp("2017-02-02"), 5, "hey", np.nan] sorted_order_mixed = list(spreadsheet.get_changed_df()["Mixed"]) assert expected_order_mixed[:4] == sorted_order_mixed[:4] # np.nan != np.nan by definition, so check if value at index 4 are both np.nan assert np.isnan(expected_order_mixed[4]) and np.isnan( sorted_order_mixed[4]) # check sorting on number column names works spreadsheet._handle_view_msg_helper({ "type": "change_sort", "sort_field": 1, "sort_ascending": True }) expected_order_1 = [1, 2, 4, 5, "hello"] sorted_order_1 = list(spreadsheet.get_changed_df()[1]) assert expected_order_1 == sorted_order_1
def test_series_of_text_filters(): view = SpreadsheetWidget(df=create_df()) view._handle_view_msg_helper({ "type": "show_filter_dropdown", "field": "E", "search_val": None }) view._handle_view_msg_helper({ "field": "E", "filter_info": { "field": "E", "selected": [0, 1], "type": "text", "excluded": [], }, "type": "change_filter", }) filtered_df = view.get_changed_df() assert len(filtered_df) == 2 # reset the filter... view._handle_view_msg_helper({ "field": "E", "filter_info": { "field": "E", "selected": None, "type": "text", "excluded": [], }, "type": "change_filter", }) # ...and apply a text filter on a different column view._handle_view_msg_helper({ "type": "show_filter_dropdown", "field": "F", "search_val": None }) view._handle_view_msg_helper({ "field": "F", "filter_info": { "field": "F", "selected": [0, 1], "type": "text", "excluded": [], }, "type": "change_filter", }) filtered_df = view.get_changed_df() assert len(filtered_df) == 2
def test_integer_index_filter(): view = SpreadsheetWidget(df=create_df()) view._handle_view_msg_helper({ "field": "index", "filter_info": { "field": "index", "max": None, "min": 2, "type": "slider", }, "type": "change_filter", }) filtered_df = view.get_changed_df() assert len(filtered_df) == 2
def test_apply_history(): df = create_df() df_copy = df.copy() spreadsheet = SpreadsheetWidget(df=df) spreadsheet._handle_view_msg_helper({ "row_index": 1, "column": "A", "unfiltered_index": 1, "value": 5, "type": "edit_cell", }) spreadsheet._handle_view_msg_helper( { "type": "change_filter", "field": "A", "filter_info": { "field": "A", "type": "slider", "min": None, "max": 4.14 }, }, ) spreadsheet._handle_view_msg_helper({ "row_index": 2, "column": "F", "unfiltered_index": 3, "value": "foo", "type": "edit_cell", }) spreadsheet._handle_view_msg_helper({ "type": "change_filter", "field": "Date", "filter_info": { "field": "Date", "type": "date", "min": 1356998400000, "max": 1357171199999, }, }) spreadsheet._handle_view_msg_helper({ "type": "change_sort", "sort_field": "Date", "sort_ascending": True }) spreadsheet._handle_view_msg_helper({ "row_index": 1, "column": "A", "unfiltered_index": 2, "value": 2, "type": "edit_cell", }) spreadsheet._handle_view_msg_helper({ "row_index": 2, "column": "A", "unfiltered_index": 3, "value": 3, "type": "edit_cell", }) changed_df = spreadsheet.get_changed_df() # Checks that spreadsheet df is not modified in place assert not changed_df.equals(df) applied_df = spreadsheet.apply_history(df_copy) # Checks that apply_history does not modify in place assert df.equals(df_copy) # Checks that the history is applied properly assert changed_df.equals(applied_df)