1
1
import json
2
2
from functools import partial
3
3
4
+ import pytest
4
5
from pycrdt import Array , Doc , Map , Text
5
6
6
7
@@ -47,7 +48,7 @@ def test_array():
47
48
doc ["array" ] = array
48
49
events = []
49
50
50
- array .observe (partial (callback , events ))
51
+ idx = array .observe (partial (callback , events ))
51
52
ref = [
52
53
- 1 ,
53
54
- 2 ,
@@ -62,6 +63,7 @@ def test_array():
62
63
- 3 ,
63
64
- 4 ,
64
65
- 6 ,
66
+ - 7 ,
65
67
]
66
68
with doc .transaction ():
67
69
array .append ("foo" )
@@ -79,6 +81,7 @@ def test_array():
79
81
array = array + [- 3 , - 4 ]
80
82
array += [- 5 ]
81
83
array [- 1 ] = - 6
84
+ array .extend ([- 7 ])
82
85
83
86
assert json .loads (str (array )) == ref
84
87
assert len (array ) == len (ref )
@@ -92,24 +95,45 @@ def test_array():
92
95
}
93
96
]
94
97
98
+ array .clear ()
99
+ assert array .to_py () == []
100
+
101
+ events .clear ()
102
+ array .unobserve (idx )
103
+ array .append ("foo" )
104
+ assert events == []
105
+
95
106
96
107
def test_observe ():
97
108
doc = Doc ()
98
109
array = Array ()
99
110
doc ["array" ] = array
100
111
101
- def callback (e ):
102
- pass
103
-
104
- sid0 = array .observe (callback )
105
- sid1 = array .observe (callback )
106
- sid2 = array .observe_deep (callback )
107
- sid3 = array .observe_deep (callback )
112
+ sid0 = array .observe (lambda x : x )
113
+ sid1 = array .observe (lambda x : x )
114
+ sid2 = array .observe_deep (lambda x : x )
115
+ sid3 = array .observe_deep (lambda x : x )
108
116
assert sid0 == "o_0"
109
117
assert sid1 == "o_1"
110
118
assert sid2 == "od0"
111
119
assert sid3 == "od1"
112
120
121
+ deep_events = []
122
+
123
+ def cb (events ):
124
+ deep_events .append (events )
125
+
126
+ sid4 = array .observe_deep (cb )
127
+ array .append ("bar" )
128
+ assert (
129
+ str (deep_events [0 ][0 ])
130
+ == """{target: ["bar"], delta: [{'insert': ['bar']}], path: []}"""
131
+ )
132
+ deep_events .clear ()
133
+ array .unobserve (sid4 )
134
+ array .append ("baz" )
135
+ assert deep_events == []
136
+
113
137
114
138
def test_api ():
115
139
# pop
@@ -129,6 +153,50 @@ def test_api():
129
153
array .insert (1 , 4 )
130
154
assert str (array ) == "[1.0,4.0,2.0,3.0]"
131
155
156
+ # slices
157
+ doc = Doc ()
158
+ array = Array ([i for i in range (10 )])
159
+ doc ["array" ] = array
160
+ with pytest .raises (RuntimeError ) as excinfo :
161
+ array [::2 ] = 1
162
+ assert str (excinfo .value ) == "Step not supported"
163
+ with pytest .raises (RuntimeError ) as excinfo :
164
+ array [1 :2 ] = 1
165
+ assert str (excinfo .value ) == "Start and stop must be equal"
166
+ with pytest .raises (RuntimeError ) as excinfo :
167
+ array [- 1 :- 1 ] = 1
168
+ assert str (excinfo .value ) == "Index out of range"
169
+ with pytest .raises (RuntimeError ) as excinfo :
170
+ array ["a" ] = 1
171
+ assert str (excinfo .value ) == "Index must be of type integer"
172
+ with pytest .raises (RuntimeError ) as excinfo :
173
+ array .pop ("a" )
174
+ assert str (excinfo .value ) == "Index must be of type integer"
175
+ with pytest .raises (IndexError ) as excinfo :
176
+ array .pop (len (array ))
177
+ assert str (excinfo .value ) == "Array index out of range"
178
+ with pytest .raises (RuntimeError ) as excinfo :
179
+ del array [::2 ]
180
+ assert str (excinfo .value ) == "Step not supported"
181
+ with pytest .raises (RuntimeError ) as excinfo :
182
+ del array [- 1 :]
183
+ assert str (excinfo .value ) == "Negative start not supported"
184
+ with pytest .raises (RuntimeError ) as excinfo :
185
+ del array [:- 1 ]
186
+ assert str (excinfo .value ) == "Negative stop not supported"
187
+ with pytest .raises (TypeError ) as excinfo :
188
+ del array ["a" ]
189
+ assert str (excinfo .value ) == "array indices must be integers or slices, not str"
190
+
191
+ assert [value for value in array ] == [value for value in range (10 )]
192
+ assert 1 in array
193
+
194
+ array = Array ([0 , 1 , 2 ])
195
+ assert array .to_py () == [0 , 1 , 2 ]
196
+
197
+ array = Array ()
198
+ assert array .to_py () is None
199
+
132
200
133
201
def test_move ():
134
202
doc = Doc ()
0 commit comments