Skip to content

Commit

Permalink
Add Card tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Apr 13, 2020
1 parent c39819a commit 8d0b644
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
4 changes: 4 additions & 0 deletions panel/layout/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def __init__(self, *objects, **params):
self.param.watch(self._update_header, ['title', 'header'])
self._update_header()

def _cleanup(self, root):
super(Card, self)._cleanup(root)
self._header_layout._cleanup(root)

def _update_header(self, *events):
from ..pane import HTML, panel
if self.header is None:
Expand Down
20 changes: 10 additions & 10 deletions panel/tests/layout/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from bokeh.models import Column as BkColumn, Div, Row as BkRow

from panel.layout import (
Column, ListPanel, Row, Tabs, Spacer, WidgetBox
Card, Column, ListPanel, Row, Tabs, Spacer, WidgetBox
)
from panel.param import Param
from panel.pane import Bokeh
Expand All @@ -20,18 +20,18 @@
def test_layout_signature(panel):
from inspect import signature
parameters = signature(panel).parameters
assert len(parameters) == 2
assert len(parameters) == 2, 'Found following parameters %r' % parameters
assert 'objects' in parameters


@pytest.mark.parametrize('layout', [Column, Row, Tabs, Spacer])
@pytest.mark.parametrize('layout', [Column, Row, Tabs, Spacer, Card])
def test_layout_properties(layout, document, comm):
l = layout()
model = l.get_root(document, comm)
check_layoutable_properties(l, model)


@pytest.mark.parametrize('layout', [Column, Row, Tabs, Spacer])
@pytest.mark.parametrize('layout', [Card, Column, Row, Tabs, Spacer])
def test_layout_model_cache_cleanup(layout, document, comm):
l = layout()

Expand All @@ -44,7 +44,7 @@ def test_layout_model_cache_cleanup(layout, document, comm):
assert l._models == {}


@pytest.mark.parametrize('panel', [Column, Row])
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_constructor(panel):
div1 = Div()
div2 = Div()
Expand All @@ -53,7 +53,7 @@ def test_layout_constructor(panel):
assert all(isinstance(p, Bokeh) for p in layout.objects)


@pytest.mark.parametrize('panel', [Column, Row])
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_getitem(panel):
div1 = Div()
div2 = Div()
Expand All @@ -63,7 +63,7 @@ def test_layout_getitem(panel):
assert layout[1].object is div2


@pytest.mark.parametrize('panel', [Column, Row])
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_repr(panel):
div1 = Div()
div2 = Div()
Expand All @@ -73,7 +73,7 @@ def test_layout_repr(panel):
assert repr(layout) == '%s\n [0] Bokeh(Div)\n [1] Bokeh(Div)' % name


@pytest.mark.parametrize('panel', [Column, Row])
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_select_by_type(panel):
div1 = Div()
div2 = Div()
Expand All @@ -86,7 +86,7 @@ def test_layout_select_by_type(panel):
assert panes[1].object is div2


@pytest.mark.parametrize('panel', [Column, Row])
@pytest.mark.parametrize('panel', [Card, Column, Row])
def test_layout_select_by_function(panel):
div1 = Div()
div2 = Div()
Expand All @@ -98,7 +98,7 @@ def test_layout_select_by_function(panel):


@pytest.mark.parametrize(['panel', 'model_type'], [(Column, BkColumn), (Row, BkRow)])
def test_layoutget_root(panel, model_type, document, comm):
def test_layout_get_root(panel, model_type, document, comm):
div1 = Div()
div2 = Div()
layout = panel(div1, div2)
Expand Down
71 changes: 71 additions & 0 deletions panel/tests/layout/test_card.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from bokeh.models import Div

from panel.layout import Card
from panel.models import Card as CardModel
from panel.pane import HTML


def test_card_model_cache_cleanup(document, comm):
html = HTML()
l = Card(header=html)

model = l.get_root(document, comm)
ref = model.ref['id']

assert ref in l._models
assert l._models[ref] == (model, None)
assert ref in html._models

l._cleanup(model)
assert l._models == {}
assert html._models == {}


def test_card_get_root(document, comm):
div1 = Div()
div2 = Div()
layout = Card(div1, div2)

model = layout.get_root(document, comm=comm)
ref = model.ref['id']
header = layout._header_layout._models[ref][0]

assert isinstance(model, CardModel)
assert model.children == [header, div1, div2]
assert header.children[0].text == "​"


def test_card_get_root_title(document, comm):
div1 = Div()
div2 = Div()
layout = Card(div1, div2, title='Test')

model = layout.get_root(document, comm=comm)
ref = model.ref['id']
header = layout._header_layout._models[ref][0]

assert isinstance(model, CardModel)
assert model.children == [header, div1, div2]
assert header.children[0].text == "Test"

div3 = Div()
layout.header = div3
assert header.children[0] is div3

layout.header = None
assert header.children[0].text == "Test"


def test_card_get_root_header(document, comm):
div1 = Div()
div2 = Div()
div3 = Div()
layout = Card(div1, div2, header=div3)

model = layout.get_root(document, comm=comm)
ref = model.ref['id']
header = layout._header_layout._models[ref][0]

assert isinstance(model, CardModel)
assert model.children == [header, div1, div2]
assert header.children[0] is div3

0 comments on commit 8d0b644

Please sign in to comment.