Exemplo n.º 1
0
    def test_view_computed_with_row_pivots_replace(self):
        table = Table({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]})

        view = table.view(
            row_pivots=["computed"],
            computed_columns=[{
                "column": "computed",
                "computed_function_name": "+",
                "inputs": ["a", "b"],
            }],
        )

        assert view.to_columns() == {
            "__ROW_PATH__": [[], [6], [8], [10], [12]],
            "a": [10, 1, 2, 3, 4],
            "b": [26, 5, 6, 7, 8],
            "computed": [36.0, 6.0, 8.0, 10.0, 12.0],
        }

        table.replace({"a": [10, 20, 30, 40], "b": [50, 60, 70, 80]})

        assert view.to_columns() == {
            "__ROW_PATH__": [[], [60], [80], [100], [120]],
            "a": [100, 10, 20, 30, 40],
            "b": [260, 50, 60, 70, 80],
            "computed": [360.0, 60.0, 80.0, 100.0, 120.0],
        }
Exemplo n.º 2
0
 def test_view_computed_multiple_dependents_replace(self):
     table = Table({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]})
     view = table.view(computed_columns=[
         {
             "column": "computed",
             "computed_function_name": "+",
             "inputs": ["a", "b"],
         },
         {
             "column": "final",
             "computed_function_name": "pow2",
             "inputs": ["computed"],
         },
     ])
     assert view.to_columns() == {
         "a": [1, 2, 3, 4],
         "b": [5, 6, 7, 8],
         "computed": [6, 8, 10, 12],
         "final": [36, 64, 100, 144],
     }
     table.replace({"a": [10, 20, 30, 40], "b": [50, 60, 70, 80]})
     assert view.schema() == {
         "a": int,
         "b": int,
         "computed": float,
         "final": float,
     }
     assert view.to_columns() == {
         "a": [10, 20, 30, 40],
         "b": [50, 60, 70, 80],
         "computed": [60, 80, 100, 120],
         "final": [3600, 6400, 10000, 14400],
     }
Exemplo n.º 3
0
 def test_table_replace_views_should_preserve(self):
     data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
     data2 = [{"a": 3, "b": 4}, {"a": 1, "b": 2}]
     tbl = Table(data)
     view = tbl.view(row_pivots=["a"], column_pivots=["b"])
     first = view.to_records()
     tbl.replace(data2)
     assert view.to_records() == first
Exemplo n.º 4
0
 def test_table_replace_views_should_preserve(self):
     data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
     data2 = [{"a": 3, "b": 4}, {"a": 1, "b": 2}]
     tbl = Table(data)
     view = tbl.view(group_by=["a"], split_by=["b"])
     first = view.to_records()
     tbl.replace(data2)
     assert view.to_records() == first
Exemplo n.º 5
0
    def test_table_replace_should_fire_on_update(self, sentinel):
        data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
        data2 = [{"a": 3, "b": 4}, {"a": 1, "b": 2}]
        tbl = Table(data)
        view = tbl.view()

        s = sentinel(False)

        def updater(port_id):
            assert port_id == 0
            s.set(True)

        view.on_update(updater)
        tbl.replace(data2)
        assert s.get() is True
Exemplo n.º 6
0
    def test_table_replace_should_fire_on_update_with_delta(self, sentinel):
        data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}]
        data2 = [{"a": 3, "b": 4}, {"a": 1, "b": 2}]
        tbl = Table(data)
        view = tbl.view()

        s = sentinel(False)

        def updater(port_id, delta):
            assert port_id == 0
            table2 = Table(delta)
            assert table2.view().to_records() == data2
            s.set(True)

        view.on_update(updater, mode="row")
        tbl.replace(data2)
        assert s.get() is True
Exemplo n.º 7
0
    def test_view_computed_multiple_views_should_all_replace(self):
        table = Table({"a": [1, 2, 3, 4], "b": [5, 6, 7, 8]})

        view = table.view(computed_columns=[{
            "column": "computed",
            "computed_function_name": "+",
            "inputs": ["a", "b"],
        }])

        view2 = table.view(computed_columns=[{
            "column": "computed2",
            "computed_function_name": "-",
            "inputs": ["a", "b"],
        }])

        assert view.schema() == {"a": int, "b": int, "computed": float}

        assert view2.schema() == {"a": int, "b": int, "computed2": float}

        assert view.to_columns() == {
            "a": [1, 2, 3, 4],
            "b": [5, 6, 7, 8],
            "computed": [6, 8, 10, 12],
        }

        assert view2.to_columns() == {
            "a": [1, 2, 3, 4],
            "b": [5, 6, 7, 8],
            "computed2": [-4, -4, -4, -4],
        }

        table.replace({"a": [10, 20, 30, 40], "b": [50, 60, 70, 80]})

        assert view.to_columns() == {
            "a": [10, 20, 30, 40],
            "b": [50, 60, 70, 80],
            "computed": [60, 80, 100, 120],
        }

        assert view2.to_columns() == {
            "a": [10, 20, 30, 40],
            "b": [50, 60, 70, 80],
            "computed2": [-40, -40, -40, -40],
        }