def test_object_referencecount_delete(self): t = CustomObjectStore(1) t2 = CustomObjectStore(2) t_ref_count = 2 t2_ref_count = 2 tbl = Table({"a": [1], "b": [t]}, index="a") t_ref_count += 1 assert tbl.schema() == {"a": int, "b": object} assert tbl.size() == 1 # Count references # 1 for `t`, 1 for `data`, 1 for argument to sys.getrefcount, and 1 for the table print(sys.getrefcount(t), "should be", t_ref_count) print(sys.getrefcount(t2), "should be", t2_ref_count) tbl.update({"a": [2], "b": [t]}) t_ref_count += 1 tbl.update({"a": [3], "b": [t]}) t_ref_count += 1 assert sys.getrefcount(t) == t_ref_count tbl.remove([1]) tbl.clear() assert tbl.size() == 0 assert tbl.view().to_dict() == {} print(sys.getrefcount(t), "should be", 2) assert sys.getrefcount(t) == 2 print(sys.getrefcount(t2), "should be", 2) assert sys.getrefcount(t2) == 2
def test_view_computed_with_row_pivots_clear(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.clear() assert view.to_columns() == { "__ROW_PATH__": [[]], "a": [None], "b": [None], "computed": [None], }
def test_object_referencecount_update_index(self): t = CustomObjectStore(1) data = {"a": [0], "b": [t]} tbl = Table(data, index="a") assert tbl.schema() == {"a": int, "b": object} assert tbl.size() == 1 assert tbl.view().to_dict() == {"a": [0], "b": [t]} # Count references # 1 for `t`, one for `data`, one for argument to sys.getrefcount, and one for the table assert sys.getrefcount(t) == 4 # do random number of updates count = randint(5, 10) for _ in range(count): tbl.update([data]) # unchanged assert tbl.size() == 1 assert sys.getrefcount(t) == 4 tbl.clear() assert tbl.size() == 0 assert tbl.view().to_dict() == {} # 1 for `t`, one for `data`, one for argument to sys.getrefcount assert sys.getrefcount(t) == 3
def test_view_computed_multiple_dependents_clear(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.clear() assert view.schema() == { "a": int, "b": int, "computed": float, "final": float, } assert view.to_columns() == {}
def test_object_referencecount_clear(self): t = CustomObjectStore(1) data = {"a": [t]} tbl = Table(data) assert tbl.schema() == {"a": object} assert tbl.size() == 1 # Count references # 1 for `t`, one for `data`, one for argument to sys.getrefcount, and one for the table assert sys.getrefcount(t) == 4 tbl.clear() assert tbl.size() == 0 # 1 for `t`, one for `data`, one for argument to sys.getrefcount assert sys.getrefcount(t) == 3
def test_view_computed_create_clear(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"], }]) assert view.to_columns() == { "a": [1, 2, 3, 4], "b": [5, 6, 7, 8], "computed": [6, 8, 10, 12], } table.clear() assert view.schema() == {"a": int, "b": int, "computed": float} assert view.to_columns() == {}
def test_view_computed_multiple_views_should_all_clear(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.clear() assert view.schema() == {"a": int, "b": int, "computed": float} assert view2.schema() == {"a": int, "b": int, "computed2": float} assert view.to_columns() == {} assert view2.to_columns() == {}
def test_table_clear(self): data = [{"a": 1, "b": 2}, {"a": 3, "b": 4}] tbl = Table(data) tbl.clear() view = tbl.view() assert view.to_records() == []