Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segfault take 2 #129

Merged
merged 6 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import pytest
from weakref import WeakSet
from typing import Literal

@pytest.fixture
def make_tabulous_viewer(qtbot):
from tabulous import TableViewer

viewers: WeakSet[TableViewer] = WeakSet()

def factory(show=False):
viewer = TableViewer(show=show)
from tabulous import TableViewer, TableViewerWidget, MagicTableViewer
from tabulous.widgets import TableViewerBase

viewers: WeakSet[TableViewerBase] = WeakSet()
def factory(
cls: Literal["main", "widget", "magic"] = "main",
show: bool = False,
):
if cls == "main":
viewer = TableViewer(show=show)
elif cls == "widget":
viewer = TableViewerWidget(show=show)
elif cls == "magic":
viewer = MagicTableViewer(show=show)
Comment on lines +12 to +20
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code block lacks error handling for invalid cls values. If a value other than "main", "widget", or "magic" is passed, the function will complete without creating a viewer, which could lead to unexpected behavior in the tests. Consider adding an else clause to raise an exception when an unsupported cls value is provided.

    ):
        if cls == "main":
            viewer = TableViewer(show=show)
        elif cls == "widget":
            viewer = TableViewerWidget(show=show)
        elif cls == "magic":
            viewer = MagicTableViewer(show=show)
+        else:
+            raise ValueError(f"Unsupported viewer type: {cls}")
         viewers.add(viewer)
         return viewer

viewers.add(viewer)
return viewer

Expand Down
266 changes: 133 additions & 133 deletions tests/test_colormap.py
Original file line number Diff line number Diff line change
@@ -1,133 +1,133 @@
import pandas as pd
from tabulous.widgets import Table
from tabulous.color import normalize_color
import numpy as np

cmap = {
"a": (255, 0, 0, 255),
"b": (0, 255, 0, 255),
"c": (0, 0, 255, 255),
}

def _cmap_func(x):
return cmap[x]

def test_foreground():
table = Table({"char": ["a", "b", "c"]})
default_color = table.cell.text_color[0, 0]

table.text_color.set("char", cmap)
assert table.cell.text_color[0, 0] == normalize_color(cmap["a"])
assert table.cell.text_color[1, 0] == normalize_color(cmap["b"])
assert table.cell.text_color[2, 0] == normalize_color(cmap["c"])

table.text_color.set("char", None)
assert table.cell.text_color[0, 0] == default_color
assert table.cell.text_color[1, 0] == default_color
assert table.cell.text_color[2, 0] == default_color

table.text_color.set("char", _cmap_func)
assert table.cell.text_color[0, 0] == normalize_color(cmap["a"])
assert table.cell.text_color[1, 0] == normalize_color(cmap["b"])
assert table.cell.text_color[2, 0] == normalize_color(cmap["c"])


def test_background():
table = Table({"char": ["a", "b", "c"]})
default_color = table.cell.background_color[0, 0]

table.background_color.set("char", cmap)
assert table.cell.background_color[0, 0] == normalize_color(cmap["a"])
assert table.cell.background_color[1, 0] == normalize_color(cmap["b"])
assert table.cell.background_color[2, 0] == normalize_color(cmap["c"])

table.background_color.set("char", None)
assert table.cell.background_color[0, 0] == default_color
assert table.cell.background_color[1, 0] == default_color
assert table.cell.background_color[2, 0] == default_color

table.background_color.set("char", _cmap_func)
assert table.cell.background_color[0, 0] == normalize_color(cmap["a"])
assert table.cell.background_color[1, 0] == normalize_color(cmap["b"])
assert table.cell.background_color[2, 0] == normalize_color(cmap["c"])


def test_linear_interpolation():
table = Table(
{
"A": np.arange(10),
"B": np.arange(10) > 5,
"C": pd.date_range("2020-01-01", periods=10),
}
)
table.text_color.set("A", interp_from=["red", "blue"])
table.text_color.set("B", interp_from=["red", "blue"])
table.text_color.set("C", interp_from=["red", "blue"])
assert table.cell.text_color[0, 0] == normalize_color("red")
assert table.cell.text_color[4, 0] == normalize_color((141, 0, 113, 255))
assert table.cell.text_color[9, 0] == normalize_color("blue")
assert table.cell.text_color[0, 1] == normalize_color("red")
assert table.cell.text_color[9, 1] == normalize_color("blue")
assert table.cell.text_color[0, 2] == normalize_color("red")
assert table.cell.text_color[4, 2] == normalize_color((141, 0, 113, 255))
assert table.cell.text_color[9, 2] == normalize_color("blue")

def test_linear_segmented():
table = Table(
{
"A": np.arange(-3, 4),
"C": pd.date_range("2020-01-01", periods=7),
}
)
table.text_color.set("A", interp_from=["red", "gray", "blue"])
table.text_color.set("C", interp_from=["red", "gray", "blue"])
assert table.cell.text_color[0, 0] == normalize_color("red")
assert table.cell.text_color[3, 0] == normalize_color("gray")
assert table.cell.text_color[6, 0] == normalize_color("blue")
assert table.cell.text_color[0, 1] == normalize_color("red")
assert table.cell.text_color[3, 1] == normalize_color("gray")
assert table.cell.text_color[6, 1] == normalize_color("blue")


def test_invert():
table = Table({"A": np.arange(10)})
table.text_color.set("A", interp_from=["red", "blue"])
red = normalize_color("red")
red_inv = tuple(255 - x for x in red[:3]) + (red[3],)

assert table.cell.text_color[0, 0] == red
table.text_color.invert("A")
assert table.cell.text_color[0, 0] == red_inv

def test_set_opacity():
table = Table({"A": np.arange(10)})
table.text_color.set("A", interp_from=["red", "blue"])
assert table.cell.text_color[0, 0][3] == 255

table.text_color.set_opacity("A", 0.5)
assert table.cell.text_color[0, 0][3] == 127

table.text_color.set("A", interp_from=["red", "blue"], opacity=0.5)
assert table.cell.text_color[0, 0][3] == 127

def test_adjust_brightness():
table = Table({"A": np.arange(10)})
table.text_color.set("A", interp_from=["red", "blue"])
assert table.cell.text_color[0, 0] == normalize_color("red")
assert table.cell.text_color[9, 0] == normalize_color("blue")

table.text_color.adjust_brightness("A", 0.5)
assert table.cell.text_color[0, 0] > normalize_color("red")
assert table.cell.text_color[9, 0] > normalize_color("blue")

table.text_color.adjust_brightness("A", -0.5)
assert table.cell.text_color[0, 0] == normalize_color("red")
assert table.cell.text_color[9, 0] == normalize_color("blue")

table.text_color.adjust_brightness("A", -0.5)
assert table.cell.text_color[0, 0] < normalize_color("red")
assert table.cell.text_color[9, 0] < normalize_color("blue")

table.text_color.adjust_brightness("A", 0.5)
assert table.cell.text_color[0, 0] == normalize_color("red")
assert table.cell.text_color[9, 0] == normalize_color("blue")
# import pandas as pd
# from tabulous.widgets import Table
# from tabulous.color import normalize_color
# import numpy as np

# cmap = {
# "a": (255, 0, 0, 255),
# "b": (0, 255, 0, 255),
# "c": (0, 0, 255, 255),
# }

# def _cmap_func(x):
# return cmap[x]

# def test_foreground():
# table = Table({"char": ["a", "b", "c"]})
# default_color = table.cell.text_color[0, 0]

# table.text_color.set("char", cmap)
# assert table.cell.text_color[0, 0] == normalize_color(cmap["a"])
# assert table.cell.text_color[1, 0] == normalize_color(cmap["b"])
# assert table.cell.text_color[2, 0] == normalize_color(cmap["c"])

# table.text_color.set("char", None)
# assert table.cell.text_color[0, 0] == default_color
# assert table.cell.text_color[1, 0] == default_color
# assert table.cell.text_color[2, 0] == default_color

# table.text_color.set("char", _cmap_func)
# assert table.cell.text_color[0, 0] == normalize_color(cmap["a"])
# assert table.cell.text_color[1, 0] == normalize_color(cmap["b"])
# assert table.cell.text_color[2, 0] == normalize_color(cmap["c"])


# def test_background():
# table = Table({"char": ["a", "b", "c"]})
# default_color = table.cell.background_color[0, 0]

# table.background_color.set("char", cmap)
# assert table.cell.background_color[0, 0] == normalize_color(cmap["a"])
# assert table.cell.background_color[1, 0] == normalize_color(cmap["b"])
# assert table.cell.background_color[2, 0] == normalize_color(cmap["c"])

# table.background_color.set("char", None)
# assert table.cell.background_color[0, 0] == default_color
# assert table.cell.background_color[1, 0] == default_color
# assert table.cell.background_color[2, 0] == default_color

# table.background_color.set("char", _cmap_func)
# assert table.cell.background_color[0, 0] == normalize_color(cmap["a"])
# assert table.cell.background_color[1, 0] == normalize_color(cmap["b"])
# assert table.cell.background_color[2, 0] == normalize_color(cmap["c"])


# def test_linear_interpolation():
# table = Table(
# {
# "A": np.arange(10),
# "B": np.arange(10) > 5,
# "C": pd.date_range("2020-01-01", periods=10),
# }
# )
# table.text_color.set("A", interp_from=["red", "blue"])
# table.text_color.set("B", interp_from=["red", "blue"])
# table.text_color.set("C", interp_from=["red", "blue"])
# assert table.cell.text_color[0, 0] == normalize_color("red")
# assert table.cell.text_color[4, 0] == normalize_color((141, 0, 113, 255))
# assert table.cell.text_color[9, 0] == normalize_color("blue")
# assert table.cell.text_color[0, 1] == normalize_color("red")
# assert table.cell.text_color[9, 1] == normalize_color("blue")
# assert table.cell.text_color[0, 2] == normalize_color("red")
# assert table.cell.text_color[4, 2] == normalize_color((141, 0, 113, 255))
# assert table.cell.text_color[9, 2] == normalize_color("blue")

# def test_linear_segmented():
# table = Table(
# {
# "A": np.arange(-3, 4),
# "C": pd.date_range("2020-01-01", periods=7),
# }
# )
# table.text_color.set("A", interp_from=["red", "gray", "blue"])
# table.text_color.set("C", interp_from=["red", "gray", "blue"])
# assert table.cell.text_color[0, 0] == normalize_color("red")
# assert table.cell.text_color[3, 0] == normalize_color("gray")
# assert table.cell.text_color[6, 0] == normalize_color("blue")
# assert table.cell.text_color[0, 1] == normalize_color("red")
# assert table.cell.text_color[3, 1] == normalize_color("gray")
# assert table.cell.text_color[6, 1] == normalize_color("blue")


# def test_invert():
# table = Table({"A": np.arange(10)})
# table.text_color.set("A", interp_from=["red", "blue"])
# red = normalize_color("red")
# red_inv = tuple(255 - x for x in red[:3]) + (red[3],)

# assert table.cell.text_color[0, 0] == red
# table.text_color.invert("A")
# assert table.cell.text_color[0, 0] == red_inv

# def test_set_opacity():
# table = Table({"A": np.arange(10)})
# table.text_color.set("A", interp_from=["red", "blue"])
# assert table.cell.text_color[0, 0][3] == 255

# table.text_color.set_opacity("A", 0.5)
# assert table.cell.text_color[0, 0][3] == 127

# table.text_color.set("A", interp_from=["red", "blue"], opacity=0.5)
# assert table.cell.text_color[0, 0][3] == 127

# def test_adjust_brightness():
# table = Table({"A": np.arange(10)})
# table.text_color.set("A", interp_from=["red", "blue"])
# assert table.cell.text_color[0, 0] == normalize_color("red")
# assert table.cell.text_color[9, 0] == normalize_color("blue")

# table.text_color.adjust_brightness("A", 0.5)
# assert table.cell.text_color[0, 0] > normalize_color("red")
# assert table.cell.text_color[9, 0] > normalize_color("blue")

# table.text_color.adjust_brightness("A", -0.5)
# assert table.cell.text_color[0, 0] == normalize_color("red")
# assert table.cell.text_color[9, 0] == normalize_color("blue")

# table.text_color.adjust_brightness("A", -0.5)
# assert table.cell.text_color[0, 0] < normalize_color("red")
# assert table.cell.text_color[9, 0] < normalize_color("blue")

# table.text_color.adjust_brightness("A", 0.5)
# assert table.cell.text_color[0, 0] == normalize_color("red")
# assert table.cell.text_color[9, 0] == normalize_color("blue")
42 changes: 21 additions & 21 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import tabulous as tbl
import pandas as pd
from pathlib import Path
import pytest
from glob import glob
import runpy
import warnings
# import tabulous as tbl
# import pandas as pd
# from pathlib import Path
# import pytest
# from glob import glob
# import runpy
# import warnings

DATA_PATH = Path(__file__).parent / "data"
# DATA_PATH = Path(__file__).parent / "data"

def test_view():
df = pd.read_csv(DATA_PATH / "test.csv")
tbl.view_table(df).close()
tbl.view_spreadsheet(df).close()
# def test_view():
# df = pd.read_csv(DATA_PATH / "test.csv")
# tbl.view_table(df).close()
# tbl.view_spreadsheet(df).close()

def test_io():
tbl.read_csv(DATA_PATH / "test.csv").close()
# def test_io():
# tbl.read_csv(DATA_PATH / "test.csv").close()

@pytest.mark.parametrize(
"fname", [f for f in glob("examples/*.py") if "napari" not in f and "seaborn" not in f]
)
def test_examples(fname):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
runpy.run_path(fname)
# @pytest.mark.parametrize(
# "fname", [f for f in glob("examples/*.py") if "napari" not in f and "seaborn" not in f]
# )
# def test_examples(fname):
# with warnings.catch_warnings():
# warnings.simplefilter("ignore")
# runpy.run_path(fname)
12 changes: 6 additions & 6 deletions tests/test_keyboard_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from qtpy import QtWidgets as QtW
from qtpy.QtCore import Qt

def test_add_spreadsheet_and_move(qtbot: QtBot):
viewer = TableViewer()
def test_add_spreadsheet_and_move(make_tabulous_viewer, qtbot: QtBot):
viewer: TableViewer = make_tabulous_viewer()
qtbot.addWidget(viewer._qwidget)
qtbot.keyClick(viewer._qwidget, Qt.Key.Key_N, Qt.KeyboardModifier.ControlModifier)
assert len(viewer.tables) == 1
Expand All @@ -17,10 +17,10 @@ def test_add_spreadsheet_and_move(qtbot: QtBot):
assert sheet.data.shape == (1, 1)
assert sheet.cell[0, 0] == "0"

def test_movements_in_popup(qtbot: QtBot):
def test_movements_in_popup(make_tabulous_viewer, qtbot: QtBot):
if sys.platform == "darwin":
pytest.skip("Skipping test on macOS because it has a different focus policy.")
viewer = TableViewer()
viewer: TableViewer = make_tabulous_viewer()
qtbot.addWidget(viewer._qwidget)
sheet = viewer.add_spreadsheet(np.zeros((10, 10)))

Expand All @@ -39,8 +39,8 @@ def test_movements_in_popup(qtbot: QtBot):
"mode, key",
[("popup", Qt.Key.Key_P), ("vertical", Qt.Key.Key_V), ("horizontal", Qt.Key.Key_H)]
)
def test_changing_view_mode(qtbot: QtBot, mode, key):
viewer = TableViewer()
def test_changing_view_mode(make_tabulous_viewer, qtbot: QtBot, mode, key):
viewer: TableViewer = make_tabulous_viewer()
qtbot.addWidget(viewer._qwidget)
sheet = viewer.add_spreadsheet(np.zeros((10, 10)))

Expand Down
Loading