Skip to content

Commit

Permalink
test(state): get 100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jourdain committed Jan 4, 2025
1 parent bf20291 commit d9db56f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pytest
trame
trame-client
trame-client
pytest-asyncio
106 changes: 106 additions & 0 deletions tests/test_state.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio
import pytest
from trame_server.state import State
from trame_server.core import Translator

Expand Down Expand Up @@ -139,3 +141,107 @@ def on_change_exec(a, **kwargs):
# print(result)

assert expected == result


def test_client_only():
server = FakeServer()
server.add_event("test_client_only")
state = State(commit_fn=server._push_state)
state.ready()

state.aa = 1
state.client_only("aa")


def test_dict_api():
server = FakeServer()
server.add_event("test_dict_api")
state = State(commit_fn=server._push_state)
state.flush() # should return right away since not ready
state.ready()

state.a = 1
state.c = []
assert state.has("a")
assert not state.has("b")
state.setdefault("a", 10)
state.setdefault("b", 20)
assert state.has("b")
assert state.a == 1
assert state.b == 20

assert state.is_dirty_all("a", "b")
assert state.is_dirty("a", "b")
state.flush()
assert not state.is_dirty("a", "b")
assert state.setdefault("a", 30) == 1

state.c.append("item")
assert not state.is_dirty("c")
state.dirty("c")
assert state.is_dirty("c")

assert state.initial == {"a": 1, "b": 20, "c": ["item"]}


@pytest.mark.asyncio
async def test_change_detection():
"""
0 msg : test_change_detection
1 push : {'a': 2}
2 msg : a changed (sync)
3 msg : a changed (async)
"""
server = FakeServer()
server.add_event("test_change_detection")
state = State(commit_fn=server._push_state, hot_reload=True)
state.ready()

state.a = 1

@state.change("a")
def regular_callback(**__kwargs):
server.add_event("a changed (sync)")

@state.change("a")
async def coroutine_callback(**__kwargs):
server.add_event("a changed (async)")

assert "a" in state._pending_update
state.clean("a")
assert "a" not in state._pending_update

with state:
state.a = 2

await asyncio.sleep(0.1)

result = [line.strip() for line in str(server).split("\n")]
expected = [line.strip() for line in str(test_change_detection.__doc__).split("\n")]

# Grab new scenario output
# print(expected)
# print("-"*60)
# print(result)

assert expected == result


def test_dunder():
server = FakeServer()
server.add_event("test_dunder")
state = State(commit_fn=server._push_state, hot_reload=True)
state.ready()

# get dunder
assert state.__dict__ != state.__getattr__("__dict__")

# get private (not in state)
assert state._something is None

# set private (not in state)
state._something = 1
assert state._something == 1

state.flush()
assert state.to_dict() == {}

0 comments on commit d9db56f

Please sign in to comment.