-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_attributes.py
More file actions
71 lines (51 loc) · 1.89 KB
/
test_attributes.py
File metadata and controls
71 lines (51 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import asyncio
from typing import Optional
import pytest
from pycrdt import Text
from ypywidgets import Reactive
from ypywidgets.comm import CommWidget
class Widget1(CommWidget):
foo = Reactive[str]("foo1")
bar = Reactive[str]("bar1")
baz = Reactive[Optional[str]](None)
class Widget2(CommWidget):
foo = Reactive[str]("")
@foo.watch
def _watch_foo(self, old, new):
print(f"foo changed: '{old}'->'{new}'")
@pytest.mark.asyncio
async def test_create_ydoc(synced_widgets):
local_widget, remote_widget = synced_widgets
local_text = Text()
local_widget.ydoc["text"] = local_text
text = "hello world!"
local_text += text
remote_text = Text()
remote_widget.ydoc["text"] = remote_text
await asyncio.sleep(0.01)
assert str(remote_text) == text
@pytest.mark.asyncio
@pytest.mark.parametrize("widget_factories", ((Widget1, Widget1),))
async def test_sync_attribute(widget_factories, synced_widgets):
local_widget, remote_widget = synced_widgets
with pytest.raises(AttributeError):
assert local_widget.wrong_attr1
with pytest.raises(AttributeError):
assert remote_widget.wrong_attr2
local_widget.foo = "foo2"
assert remote_widget.foo == "foo1" # not synced yet
await asyncio.sleep(0.01) # wait for sync
assert remote_widget.foo == "foo2"
remote_widget.baz = "baz2"
assert local_widget.baz is None # not synced yet
await asyncio.sleep(0.01) # wait for sync
assert local_widget.baz == "baz2"
@pytest.mark.asyncio
@pytest.mark.parametrize("widget_factories", ((Widget1, Widget2),))
async def test_watch_attribute(widget_factories, synced_widgets, capfd):
local_widget, remote_widget = synced_widgets
local_widget.foo = "foo"
# we're seeing the remote widget watch callback
await asyncio.sleep(0.01)
out, err = capfd.readouterr()
assert out == "foo changed: ''->'foo'\n"