Skip to content

Commit 2f0e3ba

Browse files
committed
All python tests are passing
1 parent 3aede43 commit 2f0e3ba

File tree

3 files changed

+201
-21
lines changed

3 files changed

+201
-21
lines changed

sdk/python/packages/flet/src/flet/controls/base_control.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
2+
import logging
23
import sys
4+
import weakref
35
from dataclasses import InitVar, dataclass, field
46
from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, Union
57

@@ -8,6 +10,8 @@
810
from flet.controls.ref import Ref
911
from flet.utils.strings import random_string
1012

13+
logger = logging.getLogger("flet")
14+
1115
# Try importing `dataclass_transform()` for Python 3.11+, else use a no-op function
1216
if sys.version_info >= (3, 11): # Only use it for Python 3.11+
1317
from typing import dataclass_transform
@@ -116,6 +120,10 @@ def __post_init__(self, ref: Optional[Ref[Any]]):
116120

117121
self.__method_calls: dict[str, asyncio.Event] = {}
118122
self.__method_call_results: dict[asyncio.Event, tuple[Any, Optional[str]]] = {}
123+
control_id = self._i
124+
weakref.finalize(
125+
self, lambda: logger.debug(f"Control was garbage collected: {control_id}")
126+
)
119127

120128
def __hash__(self) -> int:
121129
return object.__hash__(self)
@@ -149,9 +157,11 @@ def before_event(self, e: ControlEvent):
149157
return True
150158

151159
def did_mount(self):
160+
print(f"\n\ndid_mount: {self._i}")
152161
pass
153162

154163
def will_unmount(self):
164+
print(f"\n\nwill_unmount: {self._i}")
155165
pass
156166

157167
# public methods

sdk/python/packages/flet/tests/test_object_diff_in_place.py

Lines changed: 190 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from flet.messaging.session import Session
2424
from flet.pubsub.pubsub_hub import PubSubHub
2525

26-
from .common import b_unpack, make_diff, make_msg
26+
from .common import b_unpack, cmp_ops, make_diff, make_msg
2727

2828

2929
@control
@@ -118,12 +118,74 @@ def test_simple_page():
118118

119119
print(u_msg)
120120

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+
# ]
127189

128190
# update sub-tree
129191
page.on_login = None
@@ -148,32 +210,99 @@ def test_simple_page():
148210
assert hasattr(page.views[0], "__changes")
149211
assert len(added_controls) == 2
150212
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)
151249

152250
# replace control in a list
153251
page.controls[0].controls[0] = SuperElevatedButton("Foo")
154252
_, patch, _, added_controls, removed_controls = make_msg(page, show_details=True)
155-
for ac in added_controls:
156-
print("\nADDED CONTROL:", ac)
157-
for rc in removed_controls:
158-
print("\nREMOVED CONTROL:", rc)
253+
# for ac in added_controls:
254+
# print("\nADDED CONTROL:", ac)
255+
# for rc in removed_controls:
256+
# print("\nREMOVED CONTROL:", rc)
159257
assert len(added_controls) == 1
160258
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+
)
161269

162270
# insert a new button to the start of a list
163271
page.controls[0].controls.insert(0, SuperElevatedButton("Bar"))
164272
page.controls[0].controls[1].content = "Baz"
165273
_, patch, _, added_controls, removed_controls = make_msg(page, show_details=True)
166-
for ac in added_controls:
167-
print("\nADDED CONTROL:", ac)
168-
for rc in removed_controls:
169-
print("\nREMOVED CONTROL:", rc)
170274
assert len(added_controls) == 1
171275
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+
)
172291

173292
page.controls[0].controls.clear()
174293
_, patch, _, added_controls, removed_controls = make_msg(page, show_details=True)
175294
assert len(added_controls) == 0
176295
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+
)
177306

178307

179308
def test_floating_action_button():
@@ -207,7 +336,20 @@ def btn_click(e):
207336
),
208337
)
209338

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)
211353

212354

213355
def test_changes_tracking():
@@ -230,7 +372,28 @@ def test_changes_tracking():
230372
# t2 = Text("BBB")
231373
page.controls.append(Text("Line 2"))
232374

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+
)
234397

235398

236399
def test_large_updates():
@@ -310,9 +473,16 @@ def test_add_remove_lists():
310473
chart.data_series[0].data_points.append(ft.LineChartDataPoint(x=3, y=4))
311474

312475
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+
],
316486
)
317487

318488

sdk/python/packages/flet/tests/test_patch_dataclass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def test_page_patch_dataclass():
115115
graph_patch = patch.to_message()
116116
print("PATCH 1:", graph_patch)
117117

118-
assert graph_patch == [[0], []]
118+
assert graph_patch == [[0]]
119119

120120
page.media.padding.left = 1
121121
page.platform_brightness = Brightness.DARK

0 commit comments

Comments
 (0)