Skip to content

Commit

Permalink
on_refresh self test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Oct 29, 2024
1 parent 776a3a1 commit a62d47a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
23 changes: 21 additions & 2 deletions tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,9 @@ def test_dict_nested(a, b, request):
def test_dict_refresh_setitem(client, request, znsclient):
r = request.getfixturevalue(client)
dct = znsocket.Dict(r=r, key="dct:test", socket=znsclient)
dct2 = znsocket.Dict(r=r, key="dct:test", socket=znsocket.Client.from_url(znsclient.address))
mock = MagicMock()
dct.on_refresh(mock)
dct2.on_refresh(mock)

dct["a"] = 1
assert dct == {"a": 1}
Expand All @@ -299,8 +300,9 @@ def test_dict_refresh_setitem(client, request, znsclient):
def test_dict_refresh_delitem(client, request, znsclient):
r = request.getfixturevalue(client)
dct = znsocket.Dict(r=r, key="dct:test", socket=znsclient)
dct2 = znsocket.Dict(r=r, key="dct:test", socket=znsocket.Client.from_url(znsclient.address))
mock = MagicMock()
dct.on_refresh(mock)
dct2.on_refresh(mock)

dct["a"] = 1
assert dct == {"a": 1}
Expand All @@ -312,6 +314,23 @@ def test_dict_refresh_delitem(client, request, znsclient):
mock.assert_called_with({"keys": ["a"]})


@pytest.mark.parametrize("client", ["znsclient", "znsclient_w_redis", "redisclient"])
def test_dict_refresh_delitem_self(client, request, znsclient):
r = request.getfixturevalue(client)
dct = znsocket.Dict(r=r, key="dct:test", socket=znsclient)
mock = MagicMock()
dct.on_refresh(mock)

dct["a"] = 1
assert dct == {"a": 1}
znsclient.sio.sleep(0.2)
mock.assert_not_called()
del dct["a"]
assert dct == {}
znsclient.sio.sleep(0.2)
mock.assert_not_called()


@pytest.mark.parametrize(
"client", ["znsclient", "znsclient_w_redis", "redisclient", "empty"]
)
Expand Down
36 changes: 32 additions & 4 deletions tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,9 @@ def test_list_nested(a, b, request):
def test_list_refresh_append(client, request, znsclient):
r = request.getfixturevalue(client)
lst = znsocket.List(r=r, key="list:test", socket=znsclient)
lst2 = znsocket.List(r=r, key="list:test", socket=znsocket.Client.from_url(znsclient.address))
mock = MagicMock()
lst.on_refresh(mock)
lst2.on_refresh(mock)
assert len(lst) == 0
lst.append(1)
znsclient.sio.sleep(0.01)
Expand All @@ -363,8 +364,9 @@ def test_list_refresh_append(client, request, znsclient):
def test_list_refresh_insert(client, request, znsclient):
r = request.getfixturevalue(client)
lst = znsocket.List(r=r, key="list:test", socket=znsclient)
lst2 = znsocket.List(r=r, key="list:test", socket=znsocket.Client.from_url(znsclient.address))
mock = MagicMock()
lst.on_refresh(mock)
lst2.on_refresh(mock)
assert len(lst) == 0
lst.insert(0, 1)
znsclient.sio.sleep(0.01)
Expand All @@ -382,8 +384,9 @@ def test_list_refresh_insert(client, request, znsclient):
def test_list_refresh_delitem(client, request, znsclient):
r = request.getfixturevalue(client)
lst = znsocket.List(r=r, key="list:test", socket=znsclient)
lst2 = znsocket.List(r=r, key="list:test", socket=znsocket.Client.from_url(znsclient.address))
mock = MagicMock()
lst.on_refresh(mock)
lst2.on_refresh(mock)
lst.extend([1, 2, 3])
znsclient.sio.sleep(0.01)
# assert mock called 3 times
Expand All @@ -407,8 +410,9 @@ def test_list_refresh_delitem(client, request, znsclient):
def test_list_refresh_setitem(client, request, znsclient):
r = request.getfixturevalue(client)
lst = znsocket.List(r=r, key="list:test", socket=znsclient)
lst2 = znsocket.List(r=r, key="list:test", socket=znsocket.Client.from_url(znsclient.address))
mock = MagicMock()
lst.on_refresh(mock)
lst2.on_refresh(mock)
lst.extend([1, 2, 3])
znsclient.sio.sleep(0.01)
# assert mock called 3 times
Expand All @@ -426,3 +430,27 @@ def test_list_refresh_setitem(client, request, znsclient):
znsclient.sio.sleep(0.01)
assert len(lst) == 3
mock.assert_called_with({"indices": [1]})


@pytest.mark.parametrize("client", ["znsclient", "znsclient_w_redis", "redisclient"])
def test_list_refresh_setitem_self_trigger(client, request, znsclient):
r = request.getfixturevalue(client)
lst = znsocket.List(r=r, key="list:test", socket=znsclient)
mock = MagicMock()
lst.on_refresh(mock)
lst.extend([1, 2, 3])
znsclient.sio.sleep(0.01)
mock.assert_not_called()

assert len(lst) == 3
lst[0] = 4
znsclient.sio.sleep(0.01)
assert len(lst) == 3
# assert mock was not called
mock.assert_not_called()

# set again
lst[1] = 5
znsclient.sio.sleep(0.01)
assert len(lst) == 3
mock.assert_not_called()

0 comments on commit a62d47a

Please sign in to comment.