diff --git a/amqpworker/app.py b/amqpworker/app.py index 0060504..5c45087 100644 --- a/amqpworker/app.py +++ b/amqpworker/app.py @@ -2,7 +2,7 @@ import asyncio import time from collections.abc import MutableMapping from signal import Signals -from typing import Any, Dict, Iterable, Optional, Callable +from typing import Any, Dict, Iterable, Optional, Callable, Awaitable from amqpworker.conf import logger from amqpworker.connections import Connection, ConnectionsMapping @@ -19,6 +19,15 @@ from amqpworker.signals.handlers.rabbitmq import RabbitMQ from amqpworker.utils import entrypoint +def async_run(coro: Awaitable): + try: + loop = asyncio.get_event_loop() + except RuntimeError: + asyncio.set_event_loop(asyncio.new_event_loop()) + loop = asyncio.get_event_loop() + return loop.run_until_complete(coro) + + class App(MutableMapping, Freezable): shutdown_os_signals = (Signals.SIGINT, Signals.SIGTERM) handlers = (RabbitMQ(),) @@ -102,7 +111,8 @@ class App(MutableMapping, Freezable): Should be called in the event loop along with the request handler. """ if self.loop is None: - self.loop = ThreadPoolExecutor(thread_name_prefix='AMQPWORKER-',max_workers=min(16, sum(1 for _ in self.routes_registry.amqp_routes))) + self.loop = ThreadPoolExecutor(thread_name_prefix='AMQPWORKER-', + max_workers=min(16, sum(1 for _ in self.routes_registry.amqp_routes))) self._on_startup.send(self) def shutdown(self): diff --git a/amqpworker/easyqueue/queue.py b/amqpworker/easyqueue/queue.py index 6686fc5..9170217 100644 --- a/amqpworker/easyqueue/queue.py +++ b/amqpworker/easyqueue/queue.py @@ -1,5 +1,5 @@ import abc -import json +import orjson as json import logging import time import traceback @@ -68,7 +68,7 @@ class BaseQueue(metaclass=abc.ABCMeta): return self.deserialize(content) except TypeError: return self.deserialize(content.decode()) - except json.decoder.JSONDecodeError as e: + except json.JSONDecodeError as e: raise UndecodableMessageException( '"{content}" can\'t be decoded as JSON'.format(content=content) ) @@ -78,10 +78,10 @@ class BaseJsonQueue(BaseQueue): content_type = "application/json" def serialize(self, body: Any, **kwargs) -> str: - return json.dumps(body, **kwargs) + return json.dumps(body, **kwargs).decode('utf-8') def deserialize(self, body: bytes) -> Any: - return json.loads(body.decode()) + return json.loads(body) def _ensure_conn_is_ready(conn_type: ConnType): @@ -234,10 +234,10 @@ class JsonQueue(BaseQueue, Generic[T]): self.connection_fail_callback = connection_fail_callback def serialize(self, body: T, **kwargs) -> str: - return json.dumps(body, **kwargs) + return json.dumps(body, **kwargs).decode('utf-8') def deserialize(self, body: bytes) -> T: - return json.loads(body.decode()) + return json.loads(body) def conn_for(self, type: ConnType) -> AMQPConnection: return self.conn_types[type] @@ -268,7 +268,7 @@ class JsonQueue(BaseQueue, Generic[T]): if isinstance(data, (str, bytes)): serialized_data = data else: - serialized_data = self.serialize(data, ensure_ascii=False) + serialized_data = self.serialize(data) if properties is None: properties = {'Content-Type': 'application/json'} elif not properties.get('Content-Type'): diff --git a/example/rabbitmq.py b/example/rabbitmq.py index 80409f9..ec5f0ea 100644 --- a/example/rabbitmq.py +++ b/example/rabbitmq.py @@ -6,7 +6,7 @@ from amqpworker.connections import AMQPConnection from amqpworker.rabbitmq import RabbitMQMessage from amqpworker.routes import AMQPRouteOptions -amqp_conn = AMQPConnection(hostname='106.15.78.184', username='whc', password='whc', port=32675) +amqp_conn = AMQPConnection(hostname='127.0.0.1', username='guest', password='guest', port=5672) app = App(connections=[amqp_conn]) diff --git a/poetry.lock b/poetry.lock index fe7e259..01ab83b 100644 --- a/poetry.lock +++ b/poetry.lock @@ -79,14 +79,14 @@ reference = "douban" [[package]] name = "exceptiongroup" -version = "1.1.1" +version = "1.1.2" description = "Backport of PEP 654 (exception groups)" category = "dev" optional = false python-versions = ">=3.7" files = [ - {file = "exceptiongroup-1.1.1-py3-none-any.whl", hash = "sha256:232c37c63e4f682982c8b6459f33a8981039e5fb8756b2074364e5055c498c9e"}, - {file = "exceptiongroup-1.1.1.tar.gz", hash = "sha256:d484c3090ba2889ae2928419117447a14daf3c1231d5e30d0aae34f354f01785"}, + {file = "exceptiongroup-1.1.2-py3-none-any.whl", hash = "sha256:e346e69d186172ca7cf029c8c1d16235aa0e04035e5750b4b95039e65204328f"}, + {file = "exceptiongroup-1.1.2.tar.gz", hash = "sha256:12c3e887d6485d16943a309616de20ae5582633e0a2eda17f4e10fd61c1e8af5"}, ] [package.extras] @@ -138,6 +138,71 @@ type = "legacy" url = "https://mirror.baidu.com/pypi/simple" reference = "douban" +[[package]] +name = "orjson" +version = "3.9.10" +description = "Fast, correct Python JSON library supporting dataclasses, datetimes, and numpy" +category = "main" +optional = false +python-versions = ">=3.8" +files = [ + {file = "orjson-3.9.10-cp310-cp310-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:c18a4da2f50050a03d1da5317388ef84a16013302a5281d6f64e4a3f406aabc4"}, + {file = "orjson-3.9.10-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:5148bab4d71f58948c7c39d12b14a9005b6ab35a0bdf317a8ade9a9e4d9d0bd5"}, + {file = "orjson-3.9.10-cp310-cp310-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:4cf7837c3b11a2dfb589f8530b3cff2bd0307ace4c301e8997e95c7468c1378e"}, + {file = "orjson-3.9.10-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c62b6fa2961a1dcc51ebe88771be5319a93fd89bd247c9ddf732bc250507bc2b"}, + {file = "orjson-3.9.10-cp310-cp310-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:deeb3922a7a804755bbe6b5be9b312e746137a03600f488290318936c1a2d4dc"}, + {file = "orjson-3.9.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1234dc92d011d3554d929b6cf058ac4a24d188d97be5e04355f1b9223e98bbe9"}, + {file = "orjson-3.9.10-cp310-cp310-musllinux_1_1_aarch64.whl", hash = "sha256:06ad5543217e0e46fd7ab7ea45d506c76f878b87b1b4e369006bdb01acc05a83"}, + {file = "orjson-3.9.10-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:4fd72fab7bddce46c6826994ce1e7de145ae1e9e106ebb8eb9ce1393ca01444d"}, + {file = "orjson-3.9.10-cp310-none-win32.whl", hash = "sha256:b5b7d4a44cc0e6ff98da5d56cde794385bdd212a86563ac321ca64d7f80c80d1"}, + {file = "orjson-3.9.10-cp310-none-win_amd64.whl", hash = "sha256:61804231099214e2f84998316f3238c4c2c4aaec302df12b21a64d72e2a135c7"}, + {file = "orjson-3.9.10-cp311-cp311-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:cff7570d492bcf4b64cc862a6e2fb77edd5e5748ad715f487628f102815165e9"}, + {file = "orjson-3.9.10-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:ed8bc367f725dfc5cabeed1ae079d00369900231fbb5a5280cf0736c30e2adf7"}, + {file = "orjson-3.9.10-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:c812312847867b6335cfb264772f2a7e85b3b502d3a6b0586aa35e1858528ab1"}, + {file = "orjson-3.9.10-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:9edd2856611e5050004f4722922b7b1cd6268da34102667bd49d2a2b18bafb81"}, + {file = "orjson-3.9.10-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:674eb520f02422546c40401f4efaf8207b5e29e420c17051cddf6c02783ff5ca"}, + {file = "orjson-3.9.10-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:1d0dc4310da8b5f6415949bd5ef937e60aeb0eb6b16f95041b5e43e6200821fb"}, + {file = "orjson-3.9.10-cp311-cp311-musllinux_1_1_aarch64.whl", hash = "sha256:e99c625b8c95d7741fe057585176b1b8783d46ed4b8932cf98ee145c4facf499"}, + {file = "orjson-3.9.10-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:ec6f18f96b47299c11203edfbdc34e1b69085070d9a3d1f302810cc23ad36bf3"}, + {file = "orjson-3.9.10-cp311-none-win32.whl", hash = "sha256:ce0a29c28dfb8eccd0f16219360530bc3cfdf6bf70ca384dacd36e6c650ef8e8"}, + {file = "orjson-3.9.10-cp311-none-win_amd64.whl", hash = "sha256:cf80b550092cc480a0cbd0750e8189247ff45457e5a023305f7ef1bcec811616"}, + {file = "orjson-3.9.10-cp312-cp312-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:602a8001bdf60e1a7d544be29c82560a7b49319a0b31d62586548835bbe2c862"}, + {file = "orjson-3.9.10-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:f295efcd47b6124b01255d1491f9e46f17ef40d3d7eabf7364099e463fb45f0f"}, + {file = "orjson-3.9.10-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:92af0d00091e744587221e79f68d617b432425a7e59328ca4c496f774a356071"}, + {file = "orjson-3.9.10-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:c5a02360e73e7208a872bf65a7554c9f15df5fe063dc047f79738998b0506a14"}, + {file = "orjson-3.9.10-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:858379cbb08d84fe7583231077d9a36a1a20eb72f8c9076a45df8b083724ad1d"}, + {file = "orjson-3.9.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:666c6fdcaac1f13eb982b649e1c311c08d7097cbda24f32612dae43648d8db8d"}, + {file = "orjson-3.9.10-cp312-cp312-musllinux_1_1_aarch64.whl", hash = "sha256:3fb205ab52a2e30354640780ce4587157a9563a68c9beaf52153e1cea9aa0921"}, + {file = "orjson-3.9.10-cp312-cp312-musllinux_1_1_x86_64.whl", hash = "sha256:7ec960b1b942ee3c69323b8721df2a3ce28ff40e7ca47873ae35bfafeb4555ca"}, + {file = "orjson-3.9.10-cp312-none-win_amd64.whl", hash = "sha256:3e892621434392199efb54e69edfff9f699f6cc36dd9553c5bf796058b14b20d"}, + {file = "orjson-3.9.10-cp38-cp38-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:8b9ba0ccd5a7f4219e67fbbe25e6b4a46ceef783c42af7dbc1da548eb28b6531"}, + {file = "orjson-3.9.10-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2e2ecd1d349e62e3960695214f40939bbfdcaeaaa62ccc638f8e651cf0970e5f"}, + {file = "orjson-3.9.10-cp38-cp38-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:7f433be3b3f4c66016d5a20e5b4444ef833a1f802ced13a2d852c637f69729c1"}, + {file = "orjson-3.9.10-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:4689270c35d4bb3102e103ac43c3f0b76b169760aff8bcf2d401a3e0e58cdb7f"}, + {file = "orjson-3.9.10-cp38-cp38-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:4bd176f528a8151a6efc5359b853ba3cc0e82d4cd1fab9c1300c5d957dc8f48c"}, + {file = "orjson-3.9.10-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:3a2ce5ea4f71681623f04e2b7dadede3c7435dfb5e5e2d1d0ec25b35530e277b"}, + {file = "orjson-3.9.10-cp38-cp38-musllinux_1_1_aarch64.whl", hash = "sha256:49f8ad582da6e8d2cf663c4ba5bf9f83cc052570a3a767487fec6af839b0e777"}, + {file = "orjson-3.9.10-cp38-cp38-musllinux_1_1_x86_64.whl", hash = "sha256:2a11b4b1a8415f105d989876a19b173f6cdc89ca13855ccc67c18efbd7cbd1f8"}, + {file = "orjson-3.9.10-cp38-none-win32.whl", hash = "sha256:a353bf1f565ed27ba71a419b2cd3db9d6151da426b61b289b6ba1422a702e643"}, + {file = "orjson-3.9.10-cp38-none-win_amd64.whl", hash = "sha256:e28a50b5be854e18d54f75ef1bb13e1abf4bc650ab9d635e4258c58e71eb6ad5"}, + {file = "orjson-3.9.10-cp39-cp39-macosx_10_15_x86_64.macosx_11_0_arm64.macosx_10_15_universal2.whl", hash = "sha256:ee5926746232f627a3be1cc175b2cfad24d0170d520361f4ce3fa2fd83f09e1d"}, + {file = "orjson-3.9.10-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0a73160e823151f33cdc05fe2cea557c5ef12fdf276ce29bb4f1c571c8368a60"}, + {file = "orjson-3.9.10-cp39-cp39-manylinux_2_17_armv7l.manylinux2014_armv7l.whl", hash = "sha256:c338ed69ad0b8f8f8920c13f529889fe0771abbb46550013e3c3d01e5174deef"}, + {file = "orjson-3.9.10-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:5869e8e130e99687d9e4be835116c4ebd83ca92e52e55810962446d841aba8de"}, + {file = "orjson-3.9.10-cp39-cp39-manylinux_2_17_s390x.manylinux2014_s390x.whl", hash = "sha256:d2c1e559d96a7f94a4f581e2a32d6d610df5840881a8cba8f25e446f4d792df3"}, + {file = "orjson-3.9.10-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:81a3a3a72c9811b56adf8bcc829b010163bb2fc308877e50e9910c9357e78521"}, + {file = "orjson-3.9.10-cp39-cp39-musllinux_1_1_aarch64.whl", hash = "sha256:7f8fb7f5ecf4f6355683ac6881fd64b5bb2b8a60e3ccde6ff799e48791d8f864"}, + {file = "orjson-3.9.10-cp39-cp39-musllinux_1_1_x86_64.whl", hash = "sha256:c943b35ecdf7123b2d81d225397efddf0bce2e81db2f3ae633ead38e85cd5ade"}, + {file = "orjson-3.9.10-cp39-none-win32.whl", hash = "sha256:fb0b361d73f6b8eeceba47cd37070b5e6c9de5beaeaa63a1cb35c7e1a73ef088"}, + {file = "orjson-3.9.10-cp39-none-win_amd64.whl", hash = "sha256:b90f340cb6397ec7a854157fac03f0c82b744abdd1c0941a024c3c29d1340aff"}, + {file = "orjson-3.9.10.tar.gz", hash = "sha256:9ebbdbd6a046c304b1845e96fbcc5559cd296b4dfd3ad2509e33c4d9ce07d6a1"}, +] + +[package.source] +type = "legacy" +url = "https://mirror.baidu.com/pypi/simple" +reference = "douban" + [[package]] name = "oxidized-importer" version = "0.6.0" @@ -207,14 +272,14 @@ reference = "douban" [[package]] name = "pluggy" -version = "1.0.0" +version = "1.2.0" description = "plugin and hook calling mechanisms for python" category = "dev" optional = false -python-versions = ">=3.6" +python-versions = ">=3.7" files = [ - {file = "pluggy-1.0.0-py2.py3-none-any.whl", hash = "sha256:74134bbf457f031a36d68416e1509f34bd5ccc019f0bcc952c7b909d06b37bd3"}, - {file = "pluggy-1.0.0.tar.gz", hash = "sha256:4224373bacce55f955a878bf9cfa763c1e360858e330072059e10bad68531159"}, + {file = "pluggy-1.2.0-py3-none-any.whl", hash = "sha256:c2fd55a7d7a3863cba1a013e4e2414658b1d07b6bc57b3919e0c63c9abb99849"}, + {file = "pluggy-1.2.0.tar.gz", hash = "sha256:d12f0c4b579b15f5e054301bb226ee85eeeba08ffec228092f8defbaa3a4c4b3"}, ] [package.extras] @@ -286,14 +351,14 @@ reference = "douban" [[package]] name = "pytest" -version = "7.3.1" +version = "7.4.0" description = "pytest: simple powerful testing with Python" category = "dev" optional = false python-versions = ">=3.7" files = [ - {file = "pytest-7.3.1-py3-none-any.whl", hash = "sha256:3799fa815351fea3a5e96ac7e503a96fa51cc9942c3753cda7651b93c1cfa362"}, - {file = "pytest-7.3.1.tar.gz", hash = "sha256:434afafd78b1d78ed0addf160ad2b77a30d35d4bdf8af234fe621919d9ed15e3"}, + {file = "pytest-7.4.0-py3-none-any.whl", hash = "sha256:78bf16451a2eb8c7a2ea98e32dc119fd2aa758f1d5d66dbf0a59d69a3969df32"}, + {file = "pytest-7.4.0.tar.gz", hash = "sha256:b4bf8c45bd59934ed84001ad51e11b4ee40d40a1229d2c79f9c592b0a3f6bd8a"}, ] [package.dependencies] @@ -305,7 +370,7 @@ pluggy = ">=0.12,<2.0" tomli = {version = ">=1.0.0", markers = "python_version < \"3.11\""} [package.extras] -testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"] +testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "setuptools", "xmlschema"] [package.source] type = "legacy" @@ -314,14 +379,14 @@ reference = "douban" [[package]] name = "pytest-mock" -version = "3.10.0" +version = "3.11.1" description = "Thin-wrapper around the mock package for easier use with pytest" category = "dev" optional = false python-versions = ">=3.7" files = [ - {file = "pytest-mock-3.10.0.tar.gz", hash = "sha256:fbbdb085ef7c252a326fd8cdcac0aa3b1333d8811f131bdcc701002e1be7ed4f"}, - {file = "pytest_mock-3.10.0-py3-none-any.whl", hash = "sha256:f4c973eeae0282963eb293eb173ce91b091a79c1334455acfac9ddee8a1c784b"}, + {file = "pytest-mock-3.11.1.tar.gz", hash = "sha256:7f6b125602ac6d743e523ae0bfa71e1a697a2f5534064528c6ff84c2f7c2fc7f"}, + {file = "pytest_mock-3.11.1-py3-none-any.whl", hash = "sha256:21c279fff83d70763b05f8874cc9cfb3fcacd6d354247a976f9529d19f9acf39"}, ] [package.dependencies] @@ -354,14 +419,14 @@ reference = "douban" [[package]] name = "typing-extensions" -version = "4.5.0" +version = "4.7.1" description = "Backported and Experimental Type Hints for Python 3.7+" category = "main" optional = false python-versions = ">=3.7" files = [ - {file = "typing_extensions-4.5.0-py3-none-any.whl", hash = "sha256:fb33085c39dd998ac16d1431ebc293a8b3eedd00fd4a32de0ff79002c19511b4"}, - {file = "typing_extensions-4.5.0.tar.gz", hash = "sha256:5cb5f4a79139d699607b3ef622a1dedafa84e115ab0024e0d9c044a9479ca7cb"}, + {file = "typing_extensions-4.7.1-py3-none-any.whl", hash = "sha256:440d5dd3af93b060174bf433bccd69b0babc3b15b1a8dca43789fd7f61514b36"}, + {file = "typing_extensions-4.7.1.tar.gz", hash = "sha256:b75ddc264f0ba5615db7ba217daeb99701ad295353c45f9e95963337ceeeffb2"}, ] [package.source] @@ -392,4 +457,4 @@ reference = "douban" [metadata] lock-version = "2.0" python-versions = "^3.8" -content-hash = "df08e505f86ac2948eb727cac8ef0caa5eceed20b9bc7a616690585394992a9a" +content-hash = "c08ff79fd4eebe7caee4016755cdc0f354bd4db0271528a0cdbbc19b0dff89bb" diff --git a/pyproject.toml b/pyproject.toml index ca59df1..220e785 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ pydantic = {version = "1.10.7", source = "douban"} loguru = {version = "^0.7.0", source = "douban"} cached-property = {version = "^1.5.2", source = "douban"} delayedqueue = {version = "^1.0.0", source = "douban"} +orjson = "^3.9.10" [tool.poetry.group.dev.dependencies] @@ -38,4 +39,19 @@ minversion = "6.0" addopts = "-ra -q" testpaths = [ "tests", -] \ No newline at end of file +] + +[tool.ruff] +exclude = [ + "tests", +] +line-length = 120 +select = [ + "E", + "F", + "W", + "I" +] + +[tool.ruff.isort] +combine-as-imports = true \ No newline at end of file diff --git a/tests/easyqueue/test_base_json_queue.py b/tests/easyqueue/test_base_json_queue.py index 82d6d5c..74f33b1 100644 --- a/tests/easyqueue/test_base_json_queue.py +++ b/tests/easyqueue/test_base_json_queue.py @@ -11,13 +11,7 @@ def base_json_queue(mocker): def test_serialize(base_json_queue): body = {"teste": "aãç"} result = base_json_queue.serialize(body) - assert result == '{"teste": "a\\u00e3\\u00e7"}' - - -def test_serialize_with_ensure_ascii_false(base_json_queue): - body = {"teste": "aãç"} - result = base_json_queue.serialize(body, ensure_ascii=False) - assert '{"teste": "aãç"}' == result + assert '{"teste":"aãç"}' == result def test_deserialize(base_json_queue): diff --git a/tests/easyqueue/test_queue.py b/tests/easyqueue/test_queue.py index 148538a..3991021 100644 --- a/tests/easyqueue/test_queue.py +++ b/tests/easyqueue/test_queue.py @@ -1,4 +1,4 @@ -import json +import orjson as json import logging import amqpstorm @@ -106,7 +106,7 @@ def test_it_puts_messages_into_queue_as_json_if_message_is_a_json_serializeable( mandatory=mandatory, immediate=immediate, ) - expected = mocker.call(body=json.dumps(message).encode(), + expected = mocker.call(body=json.dumps(message), exchange=exchange, routing_key=routing_key, properties=properties, @@ -140,7 +140,7 @@ def test_it_raises_an_error_if_both_data_and_json_are_passed_to_put_message( immediate=immediate, ) expected = mocker.call( - body=json.dumps(message).encode(), + body=json.dumps(message), routing_key=routing_key, exchange_name=exchange, properties=properties, @@ -168,7 +168,7 @@ def test_it_encodes_payload_into_bytes_if_payload_is_str(mocker, connected_queue ) write_conn.channel.basic.publish.assert_called_once_with( - body=payload.encode(), + body=payload, routing_key=routing_key, exchange=exchange, properties=properties, @@ -181,7 +181,7 @@ def test_it_doesnt_encodes_payload_into_bytes_if_payload_is_already_bytes( mocker, connected_queue, write_conn ): Mock = mocker.Mock - payload = json.dumps({"dog": "Xablau"}).encode() + payload = json.dumps({"dog": "Xablau"}) exchange = Mock() routing_key = Mock() properties = SettableMock() @@ -398,7 +398,7 @@ def test_it_calls_on_queue_message_with_the_message_body_wrapped_as_a_AMQPMessag "song": "Não enche", "album": "Livro", } - body = json.dumps(content).encode("utf-8") + body = json.dumps(content) properties, delegate, handler, method = handler_method _handle_callback = mocker.patch.object(handler, "_handle_callback", mocker.Mock()) message = mocker.Mock(channel=connected_queue.connection.channel, body=body, method=method, properties=properties,