23
23
from flet .messaging .session import Session
24
24
from flet .pubsub .pubsub_hub import PubSubHub
25
25
26
- from .common import b_unpack , make_diff , make_msg
26
+ from .common import b_unpack , cmp_ops , make_diff , make_msg
27
27
28
28
29
29
@control
@@ -118,12 +118,74 @@ def test_simple_page():
118
118
119
119
print (u_msg )
120
120
121
- assert isinstance (u_msg , dict )
122
- assert "" in u_msg
123
- assert u_msg ["" ]["_i" ] > 0
124
- assert u_msg ["" ]["on_login" ]
125
- assert len (u_msg ["" ]["views" ]) > 0
126
- assert "on_connect" not in u_msg ["" ]
121
+ assert isinstance (u_msg , list )
122
+ assert u_msg [0 ] == [0 ]
123
+ assert len (u_msg [1 ]) == 4
124
+ p = u_msg [1 ][3 ]
125
+ assert p ["_i" ] > 0
126
+ assert p ["on_login" ]
127
+ assert len (p ["views" ]) > 0
128
+ assert "on_connect" not in p
129
+ # assert u_msg == [
130
+ # [0],
131
+ # [
132
+ # 0,
133
+ # 0,
134
+ # 0,
135
+ # {
136
+ # "_i": 1,
137
+ # "_c": "Page",
138
+ # "views": [
139
+ # {
140
+ # "_i": 17,
141
+ # "_c": "View",
142
+ # "controls": [
143
+ # {
144
+ # "_i": 29,
145
+ # "_c": "Div",
146
+ # "cls": "div_1",
147
+ # "some_value": "Text",
148
+ # }
149
+ # ],
150
+ # "bgcolor": "green",
151
+ # }
152
+ # ],
153
+ # "_overlay": {"_i": 18, "_c": "Overlay"},
154
+ # "_dialogs": {"_i": 19, "_c": "Dialogs"},
155
+ # "window": {"_i": 2, "_c": "Window"},
156
+ # "browser_context_menu": {"_i": 21, "_c": "BrowserContextMenu"},
157
+ # "shared_preferences": {"_i": 22, "_c": "SharedPreferences"},
158
+ # "clipboard": {"_i": 23, "_c": "Clipboard"},
159
+ # "storage_paths": {"_i": 24, "_c": "StoragePaths"},
160
+ # "url_launcher": {"_i": 25, "_c": "UrlLauncher"},
161
+ # "_user_services": {
162
+ # "_i": 26,
163
+ # "_c": "ServiceRegistry",
164
+ # "services": [
165
+ # {
166
+ # "_i": 30,
167
+ # "_c": "MyService",
168
+ # "prop_1": "Hello",
169
+ # "prop_2": [1, 2, 3],
170
+ # }
171
+ # ],
172
+ # },
173
+ # "_page_services": {
174
+ # "_i": 27,
175
+ # "_c": "ServiceRegistry",
176
+ # "services": [
177
+ # {"_i": 21, "_c": "BrowserContextMenu"},
178
+ # {"_i": 22, "_c": "SharedPreferences"},
179
+ # {"_i": 23, "_c": "Clipboard"},
180
+ # {"_i": 25, "_c": "UrlLauncher"},
181
+ # {"_i": 24, "_c": "StoragePaths"},
182
+ # ],
183
+ # },
184
+ # "fonts": {"font1": "font_url_1", "font2": "font_url_2"},
185
+ # "on_login": True,
186
+ # },
187
+ # ],
188
+ # ]
127
189
128
190
# update sub-tree
129
191
page .on_login = None
@@ -148,32 +210,99 @@ def test_simple_page():
148
210
assert hasattr (page .views [0 ], "__changes" )
149
211
assert len (added_controls ) == 2
150
212
assert len (removed_controls ) == 0
213
+ assert len (patch ) == 7
214
+ assert cmp_ops (
215
+ patch ,
216
+ [
217
+ {"op" : "replace" , "path" : ["on_login" ], "value" : False },
218
+ {
219
+ "op" : "replace" ,
220
+ "path" : ["views" , 0 , "controls" , 0 , "some_value" ],
221
+ "value" : "Another text" ,
222
+ },
223
+ {
224
+ "op" : "replace" ,
225
+ "path" : ["views" , 0 , "controls" , 0 , "controls" ],
226
+ # "value": [SuperElevatedButton, SuperElevatedButton],
227
+ },
228
+ {"op" : "remove" , "path" : ["fonts" , "font2" ], "value" : "font_url_2" },
229
+ {
230
+ "op" : "remove" ,
231
+ "path" : ["_user_services" , "services" , 0 , "prop_2" , 0 ],
232
+ "value" : 1 ,
233
+ },
234
+ {
235
+ "op" : "add" ,
236
+ "path" : ["_user_services" , "services" , 0 , "prop_2" , 1 ],
237
+ "value" : 6 ,
238
+ },
239
+ {
240
+ "op" : "remove" ,
241
+ "path" : ["_user_services" , "services" , 0 , "prop_2" , 2 ],
242
+ "value" : 3 ,
243
+ },
244
+ ],
245
+ )
246
+ assert len (patch [2 ]["value" ]) == 2
247
+ assert isinstance (patch [2 ]["value" ][0 ], SuperElevatedButton )
248
+ assert isinstance (patch [2 ]["value" ][1 ], SuperElevatedButton )
151
249
152
250
# replace control in a list
153
251
page .controls [0 ].controls [0 ] = SuperElevatedButton ("Foo" )
154
252
_ , patch , _ , added_controls , removed_controls = make_msg (page , show_details = True )
155
- for ac in added_controls :
156
- print ("\n ADDED CONTROL:" , ac )
157
- for rc in removed_controls :
158
- print ("\n REMOVED CONTROL:" , rc )
253
+ # for ac in added_controls:
254
+ # print("\nADDED CONTROL:", ac)
255
+ # for rc in removed_controls:
256
+ # print("\nREMOVED CONTROL:", rc)
159
257
assert len (added_controls ) == 1
160
258
assert len (removed_controls ) == 1
259
+ assert cmp_ops (
260
+ patch ,
261
+ [
262
+ {
263
+ "op" : "replace" ,
264
+ "path" : ["views" , 0 , "controls" , 0 , "controls" , 0 ],
265
+ "value_type" : SuperElevatedButton ,
266
+ }
267
+ ],
268
+ )
161
269
162
270
# insert a new button to the start of a list
163
271
page .controls [0 ].controls .insert (0 , SuperElevatedButton ("Bar" ))
164
272
page .controls [0 ].controls [1 ].content = "Baz"
165
273
_ , patch , _ , added_controls , removed_controls = make_msg (page , show_details = True )
166
- for ac in added_controls :
167
- print ("\n ADDED CONTROL:" , ac )
168
- for rc in removed_controls :
169
- print ("\n REMOVED CONTROL:" , rc )
170
274
assert len (added_controls ) == 1
171
275
assert len (removed_controls ) == 0
276
+ assert cmp_ops (
277
+ patch ,
278
+ [
279
+ {
280
+ "op" : "add" ,
281
+ "path" : ["views" , 0 , "controls" , 0 , "controls" , 0 ],
282
+ "value_type" : SuperElevatedButton ,
283
+ },
284
+ {
285
+ "op" : "replace" ,
286
+ "path" : ["views" , 0 , "controls" , 0 , "controls" , 1 , "content" ],
287
+ "value" : "Baz" ,
288
+ },
289
+ ],
290
+ )
172
291
173
292
page .controls [0 ].controls .clear ()
174
293
_ , patch , _ , added_controls , removed_controls = make_msg (page , show_details = True )
175
294
assert len (added_controls ) == 0
176
295
assert len (removed_controls ) == 3
296
+ assert cmp_ops (
297
+ patch ,
298
+ [
299
+ {
300
+ "op" : "replace" ,
301
+ "path" : ["views" , 0 , "controls" , 0 , "controls" ],
302
+ "value" : [],
303
+ }
304
+ ],
305
+ )
177
306
178
307
179
308
def test_floating_action_button ():
@@ -207,7 +336,20 @@ def btn_click(e):
207
336
),
208
337
)
209
338
210
- make_diff (page , show_details = True )
339
+ patch , _ , added_controls , removed_controls = make_diff (page , show_details = True )
340
+ assert cmp_ops (
341
+ patch ,
342
+ [
343
+ {
344
+ "op" : "replace" ,
345
+ "path" : ["views" , 0 , "floating_action_button" ],
346
+ "value_type" : ft .FloatingActionButton ,
347
+ },
348
+ {"op" : "replace" , "path" : ["views" , 0 , "controls" ]},
349
+ ],
350
+ )
351
+ assert len (patch [1 ]["value" ]) == 1
352
+ assert isinstance (patch [1 ]["value" ][0 ], ft .SafeArea )
211
353
212
354
213
355
def test_changes_tracking ():
@@ -230,7 +372,28 @@ def test_changes_tracking():
230
372
# t2 = Text("BBB")
231
373
page .controls .append (Text ("Line 2" ))
232
374
233
- make_msg (page , show_details = True )
375
+ patch , _ , added_controls , removed_controls = make_diff (page , show_details = True )
376
+ assert cmp_ops (
377
+ patch ,
378
+ [
379
+ {
380
+ "op" : "replace" ,
381
+ "path" : ["views" , 0 , "controls" , 0 , "content" ],
382
+ "value_type" : ft .Text ,
383
+ },
384
+ {
385
+ "op" : "replace" ,
386
+ "path" : ["views" , 0 , "controls" , 0 , "width" ],
387
+ "value" : 300 ,
388
+ },
389
+ {
390
+ "op" : "replace" ,
391
+ "path" : ["views" , 0 , "controls" , 0 , "height" ],
392
+ "value" : 100 ,
393
+ },
394
+ {"op" : "add" , "path" : ["views" , 0 , "controls" , 1 ], "value_type" : ft .Text },
395
+ ],
396
+ )
234
397
235
398
236
399
def test_large_updates ():
@@ -310,9 +473,16 @@ def test_add_remove_lists():
310
473
chart .data_series [0 ].data_points .append (ft .LineChartDataPoint (x = 3 , y = 4 ))
311
474
312
475
patch , _ , _ , _ = make_diff (chart , chart )
313
- assert patch ["data_series" ][0 ]["data_points" ]["$d" ] == [0 ]
314
- assert isinstance (
315
- patch ["data_series" ][0 ]["data_points" ][2 ]["$a" ], ft .LineChartDataPoint
476
+ assert cmp_ops (
477
+ patch ,
478
+ [
479
+ {"op" : "remove" , "path" : ["data_series" , 0 , "data_points" , 0 ]},
480
+ {
481
+ "op" : "add" ,
482
+ "path" : ["data_series" , 0 , "data_points" , 2 ],
483
+ "value_type" : ft .LineChartDataPoint ,
484
+ },
485
+ ],
316
486
)
317
487
318
488
0 commit comments