Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Oct 29, 2024
1 parent 20f54d2 commit 4222d6c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
24 changes: 18 additions & 6 deletions js_tests/native.dict.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createClient, Dict } from "znsocket";

const ZNSOCKET_URL = "http://127.0.0.1:4748";
let client;
let lst;
let dct;

beforeEach(async () => {
client = createClient({ url: ZNSOCKET_URL });
Expand All @@ -20,17 +20,29 @@ test("native_dict_setitem_callback", async () => {
callback_value = true;
},
};
lst = new Dict({ client: client, key: "list:test", callbacks: callbacks });
await lst.setitem("key", "value");
dct = new Dict({ client: client, key: "dict:test", callbacks: callbacks });
await dct.setitem("key", "value");
expect(callback_value).toBe(true);
});

test("native_dict_setitem_socket_callback", async () => {
let callback_value = false;
lst = new Dict({ client: client, key: "list:test" });
lst.onRefresh((data) => {
dct = new Dict({ client: client, key: "dict:test" });
dct.onRefresh((data) => {
callback_value = data;
});
await lst.setitem("key", "value");
await dct.setitem("key", "value");
expect(callback_value).toEqual({ keys: ["key"] });
});

test("native_dict_items", async () => {
dct = new Dict({ client: client, key: "dict:test" });
await dct.setitem(5, "5");
await dct.setitem(6, "6");

const items = await dct.items();
expect(items).toEqual([
[5, "5"],
[6, "6"],
]);
});
20 changes: 20 additions & 0 deletions tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,23 @@ def test_dict_refresh_delitem(client, request, znsclient):
assert dct == {}
znsclient.sio.sleep(0.2)
mock.assert_called_with({"keys": ["a"]})


@pytest.mark.parametrize(
"client", ["znsclient", "znsclient_w_redis", "redisclient", "empty"]
)
def test_dct_clear(client, request):
c = request.getfixturevalue(client)
if c is not None:
dct = znsocket.Dict(r=c, key="list:test")
assert isinstance(dct, ZnSocketObject)
else:
dct = {}

assert len(dct) == 0
dct.clear()
assert len(dct) == 0
dct.update({"a": "1", "b": "2"})
assert len(dct) == 2
assert dct == {"a": "1", "b": "2"}

0 comments on commit 4222d6c

Please sign in to comment.