Skip to content

Commit

Permalink
test invalid json
Browse files Browse the repository at this point in the history
  • Loading branch information
PythonFZ committed Nov 8, 2024
1 parent c9fff3b commit e6dd3a5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 31 additions & 0 deletions tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,3 +394,34 @@ def test_dct_merge(client, request):
new_dct = dct | dct2
assert new_dct == {"a": "1", "b": "3", "c": "4"}
assert isinstance(new_dct, dict)


@pytest.mark.parametrize("client", ["znsclient", "znsclient_w_redis", "redisclient"])
def test_dict_refresh_update(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()
dct2.on_refresh(mock)

dct.update({"a": 1, "b": [1, 2, 3]})
assert dct == {"a": 1, "b": [1, 2, 3]}
znsclient.sio.sleep(0.2)
mock.assert_called_with({"keys": ["a", "b"]})


@pytest.mark.parametrize("client", ["znsclient", "znsclient_w_redis", "redisclient"])
def test_invalid_json(client, request):
c = request.getfixturevalue(client)
dct = znsocket.Dict(r=c, key="list:test")

with pytest.raises(ValueError, match="Invalid JSON"):
dct.update({"a": float("inf")})
with pytest.raises(ValueError, match="Invalid JSON"):
dct.update({"a": float("nan")})
with pytest.raises(ValueError, match="Invalid JSON"):
dct.update({"a": float("-inf")})


4 changes: 2 additions & 2 deletions znsocket/objects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ZnSocketObject:

def _encode(self, data: t.Any) -> str:
if self.converter is not None:
return json.dumps(data, cls=znjson.ZnEncoder.from_converters(self.converter))
return json.dumps(data)
return json.dumps(data, cls=znjson.ZnEncoder.from_converters(self.converter), allow_nan=False)
return json.dumps(data, allow_nan=False)


def _decode(self, data: str) -> t.Any:
Expand Down

0 comments on commit e6dd3a5

Please sign in to comment.