145 lines
3.8 KiB
Python
145 lines
3.8 KiB
Python
import pandas as pd
|
|
import pytest
|
|
from fasthtml.components import *
|
|
from fasthtml.xtend import Script
|
|
|
|
from components.datagrid_new.components.ColumnsSettings import ColumnsSettings
|
|
from components.datagrid_new.components.DataGrid import DataGrid
|
|
from components.datagrid_new.constants import ColumnType
|
|
from core.settings_management import SettingsManager, MemoryDbEngine
|
|
from helpers import matches, search_elements_by_path
|
|
|
|
COLUMNS_SETTINGS_ID = "columns_settings_id"
|
|
TEST_GRID_ID = "test_grid_id"
|
|
TEST_GRID_KEY = "test_grid_key"
|
|
|
|
|
|
@pytest.fixture()
|
|
def empty_dg(session):
|
|
return DataGrid(session,
|
|
_id=TEST_GRID_ID,
|
|
settings_manager=SettingsManager(MemoryDbEngine()),
|
|
key=TEST_GRID_KEY)
|
|
|
|
|
|
@pytest.fixture()
|
|
def dg(empty_dg):
|
|
df = pd.DataFrame({
|
|
'Name': ['Alice', 'Bob'],
|
|
'Age': [20, 25],
|
|
'Male': [False, True],
|
|
})
|
|
|
|
# hack for the test because multiple dg are created with the same id
|
|
empty_dg._columns_settings._owner = empty_dg
|
|
|
|
empty_dg.init_from_dataframe(df)
|
|
return empty_dg
|
|
|
|
|
|
@pytest.fixture
|
|
def cs(session, dg):
|
|
return ColumnsSettings(session, COLUMNS_SETTINGS_ID, owner=dg)
|
|
|
|
|
|
def test_i_can_render(cs):
|
|
actual = cs.__ft__()
|
|
expected = Div(
|
|
Div(
|
|
H1("Columns Settings"),
|
|
Div(
|
|
Div(), # header
|
|
Div(), # body
|
|
Div(), # buttons
|
|
cls="dt2-cs-container"
|
|
),
|
|
),
|
|
Script(f"bindColumnsSettings('{cs.get_id()}')"),
|
|
id=cs.get_id(),
|
|
)
|
|
|
|
assert matches(actual, expected)
|
|
|
|
|
|
def test_i_can_render_when_no_dataframe(empty_dg):
|
|
columns_settings = empty_dg._columns_settings
|
|
actual = columns_settings.__ft__()
|
|
|
|
expected = Div(
|
|
Div(
|
|
H1("Columns Settings"),
|
|
Div(
|
|
Div(), # header
|
|
Div(), # body
|
|
Div(), # buttons
|
|
cls="dt2-cs-container"
|
|
),
|
|
),
|
|
Script(f"bindColumnsSettings('{columns_settings.get_id()}')"),
|
|
id=columns_settings.get_id(),
|
|
)
|
|
|
|
assert matches(actual, expected)
|
|
|
|
|
|
def test_i_can_render_columns_settings(cs):
|
|
def _mk_options(selected_value):
|
|
return [Option(value.value, selected=True if selected_value == value else None) for value in ColumnType]
|
|
|
|
actual = cs.__ft__()
|
|
to_compare = search_elements_by_path(actual, "div", attrs={"class": "dt2-cs-container"})[0]
|
|
|
|
expected = Div(
|
|
Div(
|
|
Div(cls="place-self-center"),
|
|
Div("Title"),
|
|
Div("Type"),
|
|
Div("Visible"),
|
|
Div("Usable"),
|
|
Div("Width"),
|
|
cls="dt2-cs-header dt2-cs-columns"
|
|
),
|
|
Div(
|
|
Div(
|
|
A(cls="dt2-item-handle"),
|
|
Input(name="title_name", type="input", value="Name"),
|
|
Select(
|
|
*_mk_options(ColumnType.Text),
|
|
name="type_name",
|
|
),
|
|
Input(name=f"visible_name", type="checkbox", checked=True),
|
|
Input(name=f"usable_name", type="checkbox", checked=True),
|
|
Input(name=f"width_name", type="input", value=100),
|
|
data_col="name",
|
|
),
|
|
Div(
|
|
A(cls="dt2-item-handle"),
|
|
Input(name="title_age", type="input", value="Age"),
|
|
Select(
|
|
*_mk_options(ColumnType.Number),
|
|
name="type_age",
|
|
),
|
|
Input(name=f"visible_age", type="checkbox", checked=True),
|
|
Input(name=f"usable_age", type="checkbox", checked=True),
|
|
Input(name=f"width_age", type="input", value=100),
|
|
data_col="age",
|
|
),
|
|
Div(
|
|
A(cls="dt2-item-handle"),
|
|
Input(name="title_male", type="input", value="Male"),
|
|
Select(
|
|
*_mk_options(ColumnType.Bool),
|
|
name="type_male",
|
|
),
|
|
Input(name=f"visible_male", type="checkbox", checked=True),
|
|
Input(name=f"usable_male", type="checkbox", checked=True),
|
|
Input(name=f"width_male", type="input", value=100),
|
|
data_col="male",
|
|
),
|
|
),
|
|
Div(), # buttons
|
|
cls="dt2-cs-container",
|
|
)
|
|
|
|
assert matches(to_compare, expected)
|