Skip to content

Commit 5886f79

Browse files
Mike ProsserMike Prosser
authored andcommitted
add tests for close() in test_streamlit_panel
1 parent 2cc10b2 commit 5886f79

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

tests/unit/test_panel_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
from nipanel._panel_client import PanelClient
55

66

7-
def test___enumerate_is_empty(fake_panel_channel: grpc.Channel):
7+
def test___enumerate_is_empty(fake_panel_channel: grpc.Channel) -> None:
88
client = create_panel_client(fake_panel_channel)
99

1010
assert client.enumerate_panels() == []
1111

1212

13-
def test___open_panels___enumerate_has_panels(fake_panel_channel: grpc.Channel):
13+
def test___open_panels___enumerate_has_panels(fake_panel_channel: grpc.Channel) -> None:
1414
client = create_panel_client(fake_panel_channel)
1515

1616
client.open_panel("panel1", "uri1")
@@ -21,7 +21,7 @@ def test___open_panels___enumerate_has_panels(fake_panel_channel: grpc.Channel):
2121

2222
def test___open_panels___close_panel_1___enumerate_has_panel_2(
2323
fake_panel_channel: grpc.Channel,
24-
):
24+
) -> None:
2525
client = create_panel_client(fake_panel_channel)
2626
client.open_panel("panel1", "uri1")
2727
client.open_panel("panel2", "uri2")
@@ -31,14 +31,14 @@ def test___open_panels___close_panel_1___enumerate_has_panel_2(
3131
assert client.enumerate_panels() == ["panel2"]
3232

3333

34-
def test___get_value_raises(fake_panel_channel: grpc.Channel):
34+
def test___get_unset_value_raises_exception(fake_panel_channel: grpc.Channel) -> None:
3535
client = create_panel_client(fake_panel_channel)
3636

3737
with pytest.raises(Exception):
3838
client.get_value("panel1", "unset_id")
3939

4040

41-
def test___set_value___gets_value(fake_panel_channel: grpc.Channel):
41+
def test___set_value___gets_value(fake_panel_channel: grpc.Channel) -> None:
4242
client = create_panel_client(fake_panel_channel)
4343

4444
client.set_value("panel1", "val1", "value1")

tests/unit/test_streamlit_panel.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,35 @@ def test___opened_panel___accessor_set_value___panel_gets_same_value(
6464
assert panel.get_value(value_id) == string_value
6565

6666

67+
def test___opened_panel_with_value___close_without_reset___gets_value(
68+
fake_panel_channel: grpc.Channel,
69+
) -> None:
70+
panel = StreamlitPanel("my_panel", "path/to/script", grpc_channel=fake_panel_channel)
71+
panel.open_panel()
72+
value_id = "test_id"
73+
string_value = "test_value"
74+
panel.set_value(value_id, string_value)
75+
76+
panel.close_panel(reset=False)
77+
78+
assert panel.get_value(value_id) == string_value
79+
80+
81+
def test___opened_panel_with_value___close_with_reset___get_throws(
82+
fake_panel_channel: grpc.Channel,
83+
) -> None:
84+
panel = StreamlitPanel("my_panel", "path/to/script", grpc_channel=fake_panel_channel)
85+
panel.open_panel()
86+
value_id = "test_id"
87+
string_value = "test_value"
88+
panel.set_value(value_id, string_value)
89+
90+
panel.close_panel(reset=True)
91+
92+
with pytest.raises(grpc.RpcError):
93+
panel.get_value(value_id)
94+
95+
6796
def test___first_open_panel_fails___open_panel___gets_value(
6897
fake_python_panel_service: FakePythonPanelService,
6998
fake_panel_channel: grpc.Channel,

tests/utils/_fake_python_panel_servicer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class FakePythonPanelServicer(PythonPanelServiceServicer):
2121

2222
def __init__(self) -> None:
2323
"""Initialize the fake PythonPanelServicer."""
24-
self._values = {}
25-
self._panel_ids = []
24+
self._values: dict[str, Any] = {}
25+
self._panel_ids: list[str] = []
2626
self._fail_next_open_panel = False
2727

2828
def OpenPanel(self, request: OpenPanelRequest, context: Any) -> OpenPanelResponse: # noqa: N802
@@ -38,6 +38,8 @@ def ClosePanel( # noqa: N802
3838
) -> ClosePanelResponse:
3939
"""Trivial implementation for testing."""
4040
self._panel_ids.remove(request.panel_id)
41+
if request.reset:
42+
self._values.clear()
4143
return ClosePanelResponse()
4244

4345
def EnumeratePanels( # noqa: N802

0 commit comments

Comments
 (0)