def test_tabulator_stream_series_paginated_follow(document, comm):
    df = makeMixedDataFrame()
    table = Tabulator(df, pagination='remote', page_size=2)

    model = table.get_root(document, comm)

    stream_value = pd.Series({
        'A': 5,
        'B': 1,
        'C': 'foo6',
        'D': dt.datetime(2009, 1, 8)
    })

    table.stream(stream_value, follow=True)

    assert table.page == 3
    assert len(table.value) == 6

    expected = {
        'index':
        np.array([4, 5]),
        'A':
        np.array([4, 5]),
        'B':
        np.array([0, 1]),
        'C':
        np.array(['foo5', 'foo6']),
        'D':
        np.array(
            ['2009-01-07T00:00:00.000000000', '2009-01-08T00:00:00.000000000'],
            dtype='datetime64[ns]').astype(np.int64) / 10e5
    }
    for col, values in model.source.data.items():
        np.testing.assert_array_equal(values, expected[col])
def test_tabulator_empty_table(document, comm):
    value_df = makeMixedDataFrame()
    empty_df = pd.DataFrame([], columns=value_df.columns)
    table = Tabulator(empty_df)

    table.get_root(document, comm)

    assert table.value.shape == empty_df.shape

    table.stream(value_df, follow=True)

    assert table.value.shape == value_df.shape
def test_tabulator_stream_dataframe_selectable_rows(document, comm):
    df = makeMixedDataFrame()
    table = Tabulator(df,
                      selectable_rows=lambda df: list(range(0, len(df), 2)))

    model = table.get_root(document, comm)

    assert model.selectable_rows == [0, 2, 4]

    stream_value = pd.DataFrame({
        'A': [5, 6],
        'B': [1, 0],
        'C': ['foo6', 'foo7'],
        'D': [dt.datetime(2009, 1, 8),
              dt.datetime(2009, 1, 9)]
    })

    table.stream(stream_value)

    print(len(table._processed))

    assert model.selectable_rows == [0, 2, 4, 6]
def test_tabulator_stream_dataframe(document, comm):
    df = makeMixedDataFrame()
    table = Tabulator(df)

    model = table.get_root(document, comm)

    stream_value = pd.DataFrame({
        'A': [5, 6],
        'B': [1, 0],
        'C': ['foo6', 'foo7'],
        'D': [dt.datetime(2009, 1, 8),
              dt.datetime(2009, 1, 9)]
    })

    table.stream(stream_value)

    assert len(table.value) == 7

    expected = {
        'index':
        np.array([0, 1, 2, 3, 4, 5, 6]),
        'A':
        np.array([0, 1, 2, 3, 4, 5, 6]),
        'B':
        np.array([0, 1, 0, 1, 0, 1, 0]),
        'C':
        np.array(['foo1', 'foo2', 'foo3', 'foo4', 'foo5', 'foo6', 'foo7']),
        'D':
        np.array([
            '2009-01-01T00:00:00.000000000', '2009-01-02T00:00:00.000000000',
            '2009-01-05T00:00:00.000000000', '2009-01-06T00:00:00.000000000',
            '2009-01-07T00:00:00.000000000', '2009-01-08T00:00:00.000000000',
            '2009-01-09T00:00:00.000000000'
        ],
                 dtype='datetime64[ns]').astype(np.int64) / 10e5
    }
    for col, values in model.source.data.items():
        np.testing.assert_array_equal(values, expected[col])
def test_tabulator_stream_df_rollover(document, comm):
    df = makeMixedDataFrame()
    table = Tabulator(df)

    model = table.get_root(document, comm)

    stream_value = pd.Series({
        'A': 5,
        'B': 1,
        'C': 'foo6',
        'D': dt.datetime(2009, 1, 8)
    }).to_frame().T

    table.stream(stream_value, rollover=5)

    assert len(table.value) == 5

    expected = {
        'index':
        np.array([1, 2, 3, 4, 5]),
        'A':
        np.array([1, 2, 3, 4, 5]),
        'B':
        np.array([1, 0, 1, 0, 1]),
        'C':
        np.array(['foo2', 'foo3', 'foo4', 'foo5', 'foo6']),
        'D':
        np.array([
            '2009-01-02T00:00:00.000000000', '2009-01-05T00:00:00.000000000',
            '2009-01-06T00:00:00.000000000', '2009-01-07T00:00:00.000000000',
            '2009-01-08T00:00:00.000000000'
        ],
                 dtype='datetime64[ns]').astype(np.int64) / 10e5
    }
    for col, values in model.source.data.items():
        np.testing.assert_array_equal(values, expected[col])