* Use json for annoucements cache Parsing this XML file is very slow, so there's no reason to do it more than we have to. See 4480-2021-12-11T13:23:48-05:00.json, MainThread -> parse On my system, this reduces the time for /plugin/announcements/channels from 250ms CPU time to 50ms. The rough improvment in this specific function call is from 8% to 0.32% of total runtime. * ♻️ Refactor json serializer * Move back to octoprint.util.json * Leave old encoding helper as is but add additional one focused on serialization (with full roundtrips). Use each as needed (primarily encoding one). octoprint.util.encoding -> old one octoprint.util.serializing -> new one Each serves different purpose, we don't want to end up with `__jsonclass__` stuff in our API output at some point, so separate the concerns here and allow different handling. * Ensure wrappers are in place for backwards compatibility (e.g. dump vs dumps). * 🐛 Fix handling of invalid cache We get different exceptions now and in any case just throw *anything* away that we can't parse. * 🎨 Use fallback typing annotation for Py3.7.{0,1} Better solution than getting stuck to Dict only, thanks @flaviut for the suggestion! * 🐛 Fix bytes encoding bytes objects are explicitly not always UTF-8, so encode them in a way that doesn't lead to errors and encoding/decoding problems when they are actually binary data. Co-authored-by: Gina Häußge <gina@octoprint.org>
81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
__license__ = "GNU Affero General Public License http://www.gnu.org/licenses/agpl.html"
|
|
__copyright__ = "Copyright (C) 2022 The OctoPrint Project - Released under terms of the AGPLv3 License"
|
|
|
|
import datetime
|
|
import time
|
|
import warnings
|
|
|
|
import pytest
|
|
from frozendict import frozendict
|
|
|
|
from octoprint.util import json
|
|
|
|
|
|
class SomeClass:
|
|
...
|
|
|
|
|
|
class SomeSubclass(frozendict):
|
|
...
|
|
|
|
|
|
def test_deprecated_dump():
|
|
warnings.simplefilter("always")
|
|
with warnings.catch_warnings(record=True) as w:
|
|
assert json.dump({"foo": "bar"}) == '{"foo":"bar"}'
|
|
|
|
assert len(w) == 1
|
|
assert issubclass(w[-1].category, DeprecationWarning)
|
|
assert "dump has been renamed to dumps" in str(w[-1].message)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"val,expected",
|
|
[
|
|
pytest.param({"foo": "bar"}, '{"foo":"bar"}', id="dict"),
|
|
pytest.param(frozendict({"foo": "bar"}), '{"foo":"bar"}', id="frozendict"),
|
|
pytest.param(b"foo", '"foo"', id="bytes"),
|
|
],
|
|
)
|
|
def test_encoding_dumps(val, expected):
|
|
assert json.encoding.dumps(val) == expected
|
|
|
|
|
|
def test_encoding_loads():
|
|
assert json.encoding.loads('{"foo":"bar"}') == {"foo": "bar"}
|
|
|
|
|
|
def test_encoding_dumps_typeerror():
|
|
with pytest.raises(TypeError):
|
|
json.encoding.dumps(SomeClass())
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"val",
|
|
[
|
|
pytest.param({"foo": "bar"}, id="dict"),
|
|
pytest.param(b"\x00\x01\x02\x03", id="bytes"),
|
|
# invalid bytes from https://stackoverflow.com/a/3886015
|
|
pytest.param(
|
|
b"\xc3\x28\xa0\xa1\xe2\x28\xa1\xe2\x82\x28\xf0"
|
|
b"\x28\x8c\xbc\xf0\x90\x28\xbc\xf0\x28\x8c\x28",
|
|
id="invalid utf-8",
|
|
),
|
|
pytest.param(frozendict({"foo": "bar"}), id="frozendict"),
|
|
pytest.param(time.struct_time((2018, 1, 1, 0, 0, 0, 0, 0, 0)), id="struct_time"),
|
|
pytest.param(
|
|
datetime.datetime(2022, 3, 21, 5, 24, 0, 0, tzinfo=datetime.timezone.utc),
|
|
id="datetime",
|
|
),
|
|
pytest.param(frozendict({"a": b"b"}), id="nested"),
|
|
pytest.param(SomeSubclass({"a": b"b"}), id="subclass"),
|
|
],
|
|
)
|
|
def test_serializing_roundtrips(val):
|
|
assert json.serializing.loads(json.serializing.dumps(val)) == val
|
|
|
|
|
|
def test_serializing_dumps_typeerror():
|
|
with pytest.raises(TypeError):
|
|
json.serializing.dumps(SomeClass())
|