Esempio n. 1
0
def test_get_history():
    spreadsheet = SpreadsheetWidget(df=create_df())

    # Initial history
    last_history = spreadsheet.get_history()[-1]
    expected_history = "unfiltered_df = df.copy()"
    assert last_history == expected_history

    # Edit cell
    spreadsheet._handle_view_msg_helper({
        "column": "A",
        "row_index": 0,
        "type": "edit_cell",
        "unfiltered_index": 0,
        "value": "hello world",
    })
    last_history = spreadsheet.get_history()[-1]
    expected_history = "# Edit cell\ndf.loc[(0, 'A')]='hello world'\nunfiltered_df.loc[(0, 'A')]='hello world'"
    assert last_history == expected_history

    spreadsheet._handle_view_msg_helper(
        {
            "row_index": 0,
            "column": "Date",
            "unfiltered_index": 0,
            "value": "2013-01-10T00:00:00.000+00:00",
            "type": "edit_cell",
        }, )
    last_history = spreadsheet.get_history()[-1]
    expected_history = "# Edit cell\ndf.loc[(0, 'Date')]=Timestamp('2013-01-10 00:00:00')\nunfiltered_df.loc[(0, 'Date')]=Timestamp('2013-01-10 00:00:00')"
    assert last_history == expected_history

    # Remove row
    spreadsheet._handle_view_msg_helper({"type": "remove_row"}, )
    last_history = spreadsheet.get_history()[-1]
    expected_history = (
        "# Remove rows\ndf.drop([], inplace=True)\nunfiltered_df.drop([], inplace=True)"
    )
    assert last_history == expected_history

    # Change selection
    spreadsheet._handle_view_msg_helper(
        {
            "rows": [],
            "type": "change_selection"
        }, )
    spreadsheet._handle_view_msg_helper(
        {
            "rows": [1],
            "type": "change_selection"
        }, )
    spreadsheet._handle_view_msg_helper(
        {
            "rows": [1, 2, 3],
            "type": "change_selection"
        }, )
    # History should not be changed
    last_history = spreadsheet.get_history()[-1]
    assert last_history == expected_history

    # Change viewport
    spreadsheet._handle_view_msg_helper({
        "type": "change_viewport",
        "top": 7124,
        "bottom": 7136
    })
    # History should not be changed
    last_history = spreadsheet.get_history()[-1]
    assert last_history == expected_history

    # Add row
    spreadsheet._handle_view_msg_helper({"type": "add_row"})
    last_history = spreadsheet.get_history()[-1]
    expected_history = "# Add row\nlast = df.loc[max(df.index)].copy()\ndf.loc[last.name+1] = last.values\nunfiltered_df.loc[last.name+1] = last.values"
    assert last_history == expected_history

    # Change filter
    spreadsheet._handle_view_msg_helper(
        {
            "type": "change_filter",
            "field": "Date",
            "filter_info": {
                "field": "Date",
                "type": "date",
                "min": 1351900800000,
                "max": 1357862399999,
            },
        }, )
    last_history = spreadsheet.get_history()[-1]
    expected_history = "# Filter columns\ndf = unfiltered_df[(unfiltered_df['Date'] >= pd.to_datetime(1351900800000, unit='ms'))&(unfiltered_df['Date'] <= pd.to_datetime(1357862399999, unit='ms'))].copy()"
    assert last_history == expected_history

    # Change sort
    spreadsheet._handle_view_msg_helper({
        "type": "change_sort",
        "sort_field": "A",
        "sort_ascending": True
    })
    last_history = spreadsheet.get_history()[-1]
    expected_history = "# Sort mixed type column\ndf['A_modin_spreadsheet_sort_column'] = df['A'].map(str)\ndf.sort_values('A_modin_spreadsheet_sort_column', ascending=True, inplace=True)\ndf.drop(columns='A_modin_spreadsheet_sort_column', inplace=True)"
    assert last_history == expected_history

    # Reset filters
    spreadsheet._handle_view_msg_helper({"type": "reset_filters_end"})
    last_history = spreadsheet.get_history()[-1]
    expected_history = "# Sort mixed type column\ndf['A_modin_spreadsheet_sort_column'] = df['A'].map(str)\ndf.sort_values('A_modin_spreadsheet_sort_column', ascending=True, inplace=True)\ndf.drop(columns='A_modin_spreadsheet_sort_column', inplace=True)"
    assert last_history == expected_history

    # Reset sort
    spreadsheet._handle_view_msg_helper({"type": "reset_sort"})
    last_history = spreadsheet.get_history()[-1]
    expected_history = "# Reset sort\ndf.sort_index(ascending=True, inplace=True)"
    assert last_history == expected_history

    # Clear history
    spreadsheet._handle_view_msg_helper({"type": "clear_history"})
    history = spreadsheet.get_history()
    expected_history = ["unfiltered_df = df.copy()"]
    assert history == expected_history
Esempio n. 2
0
def test_filter_relevant_history():
    spreadsheet = SpreadsheetWidget(df=create_df())

    # Initial history
    filtered_history = spreadsheet.filter_relevant_history()
    expected_history = ["unfiltered_df = df.copy()"]
    assert filtered_history == expected_history

    # Empty history
    spreadsheet._history = []
    expected_history = []
    filtered_history = spreadsheet.filter_relevant_history()
    assert filtered_history == expected_history

    # Multiple filters
    spreadsheet._history = [
        "# Filter columns\ndf = unfiltered_df[(unfiltered_df['trip_id'] <= 48802)].copy()",
        "# Filter columns\ndf = unfiltered_df[(unfiltered_df['trip_id'] >= 5495)&(unfiltered_df['trip_id'] <= 48802)].copy()",
        "# Filter columns\ndf = unfiltered_df[(unfiltered_df['trip_id'] >= 5495)&(unfiltered_df['trip_id'] <= 48802)&(unfiltered_df['vendor_id'] >= 2)].copy()",
    ]
    expected_history = [
        "# Filter columns\ndf = unfiltered_df[(unfiltered_df['trip_id'] >= 5495)&(unfiltered_df['trip_id'] <= 48802)&(unfiltered_df['vendor_id'] >= 2)].copy()",
    ]
    filtered_history = spreadsheet.filter_relevant_history()
    assert filtered_history == expected_history

    # Multiple sorts
    spreadsheet._history = [
        "# Sort column\ndf.sort_values('trip_id', ascending=True, inplace=True)",
        "# Sort column\ndf.sort_values('vendor_id', ascending=True, inplace=True)",
        "# Sort column\ndf.sort_values('vendor_id', ascending=False, inplace=True)",
    ]
    expected_history = [
        "# Sort column\ndf.sort_values('vendor_id', ascending=False, inplace=True)",
    ]
    filtered_history = spreadsheet.filter_relevant_history()
    assert filtered_history == expected_history

    # Filter after reset
    spreadsheet._history = [
        "# Filter columns\ndf = unfiltered_df[(unfiltered_df['trip_id'] >= 6485)].copy()",
        "# Reset filter\ndf = unfiltered_df.copy()",
        "# Filter columns\ndf = unfiltered_df[(unfiltered_df['trip_id'] <= 41873)].copy()",
    ]
    expected_history = [
        "# Filter columns\ndf = unfiltered_df[(unfiltered_df['trip_id'] <= 41873)].copy()"
    ]
    filtered_history = spreadsheet.filter_relevant_history()
    assert filtered_history == expected_history

    # Sort after reset
    spreadsheet._history = [
        "# Sort column\ndf.sort_values('trip_id', ascending=True, inplace=True)",
        "# Reset sort\ndf.sort_index(ascending=True, inplace=True)",
        "# Sort column\ndf.sort_values('vendor_id', ascending=True, inplace=True)",
    ]
    expected_history = [
        "# Sort column\ndf.sort_values('vendor_id', ascending=True, inplace=True)"
    ]
    filtered_history = spreadsheet.filter_relevant_history()
    assert filtered_history == expected_history

    # None after reset
    spreadsheet._history = [
        "# Sort column\ndf.sort_values('vendor_id', ascending=True, inplace=True)",
        "# Reset sort\ndf.sort_index(ascending=True, inplace=True)",
        "# Filter columns\ndf = unfiltered_df[(unfiltered_df['trip_id'] >= 6980)].copy()",
        "# Reset all filters\ndf = unfiltered_df.copy()",
    ]
    expected_history = []
    filtered_history = spreadsheet.filter_relevant_history()
    assert filtered_history == expected_history

    # Mixed transformation
    mixed_history = [
        "# Edit cell\ndf.loc[(4, 'trip_id')]=10\nunfiltered_df.loc[(4, 'trip_id')]=10",
        "# Sort column\ndf.sort_values('trip_id', ascending=True, inplace=True)",
        "# Sort column\ndf.sort_values('rate_code_id', ascending=True, inplace=True)",
        "# Reset sort\ndf.sort_index(ascending=True, inplace=True)",
        "# Sort index\ndf.sort_index(ascending=True, inplace=True)",
        "# Remove rows\ndf.drop([6], inplace=True)\nunfiltered_df.drop([6], inplace=True)",
        "# Add row\nlast = df.loc[max(df.index)].copy()\ndf.loc[last.name+1] = last.values\nunfiltered_df.loc[last.name+1] = last.values",
        "# Reset all filters\ndf = unfiltered_df.copy()",
        "# Sort index\ndf.sort_index(ascending=True, inplace=True)",
    ]
    expected_filtered_history = [
        "# Edit cell\ndf.loc[(4, 'trip_id')]=10\nunfiltered_df.loc[(4, 'trip_id')]=10",
        "# Remove rows\ndf.drop([6], inplace=True)\nunfiltered_df.drop([6], inplace=True)",
        "# Add row\nlast = df.loc[max(df.index)].copy()\ndf.loc[last.name+1] = last.values\nunfiltered_df.loc[last.name+1] = last.values",
        "# Sort index\ndf.sort_index(ascending=True, inplace=True)",
    ]
    spreadsheet._history = mixed_history
    filtered_history = spreadsheet.filter_relevant_history()
    assert filtered_history == expected_filtered_history

    # Persist filter history
    spreadsheet._history = mixed_history
    spreadsheet.filter_relevant_history(persist=True)
    assert spreadsheet.get_history() == expected_filtered_history

    # Don't persist filter history
    spreadsheet._history = mixed_history
    filtered_history = spreadsheet.filter_relevant_history(persist=False)
    assert filtered_history == expected_filtered_history
    assert spreadsheet.get_history() == mixed_history