From 3b9c43a11cc55fec43381093ad95ea6d7f815526 Mon Sep 17 00:00:00 2001 From: Yuya Oka Date: Tue, 10 Jan 2023 06:20:59 +0900 Subject: [PATCH] Add __Client tests --- poetry.lock | 22 ++- pyproject.toml | 3 +- tests/clients/__init__.py | 0 tests/clients/client/__init__.py | 0 tests/clients/client/test_base_url.py | 17 +++ tests/clients/client/test_handle_error.py | 176 ++++++++++++++++++++++ tests/clients/client/test_init_client.py | 15 ++ tests/clients/client/test_make_url.py | 32 ++++ tests/clients/client/test_send_message.py | 17 +++ 9 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 tests/clients/__init__.py create mode 100644 tests/clients/client/__init__.py create mode 100644 tests/clients/client/test_base_url.py create mode 100644 tests/clients/client/test_handle_error.py create mode 100644 tests/clients/client/test_init_client.py create mode 100644 tests/clients/client/test_make_url.py create mode 100644 tests/clients/client/test_send_message.py diff --git a/poetry.lock b/poetry.lock index f3d49bb..8251fe0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -779,6 +779,26 @@ tomli = {version = ">=1.0.0", markers = "python_version < \"3.11\""} [package.extras] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"] +[[package]] +name = "pytest-asyncio" +version = "0.20.3" +description = "Pytest support for asyncio" +category = "dev" +optional = false +python-versions = ">=3.7" +files = [ + {file = "pytest-asyncio-0.20.3.tar.gz", hash = "sha256:83cbf01169ce3e8eb71c6c278ccb0574d1a7a3bb8eaaf5e50e0ad342afb33b36"}, + {file = "pytest_asyncio-0.20.3-py3-none-any.whl", hash = "sha256:f129998b209d04fcc65c96fc85c11e5316738358909a8399e93be553d7656442"}, +] + +[package.dependencies] +pytest = ">=6.1.0" +typing-extensions = {version = ">=3.7.2", markers = "python_version < \"3.8\""} + +[package.extras] +docs = ["sphinx (>=5.3)", "sphinx-rtd-theme (>=1.0)"] +testing = ["coverage (>=6.2)", "flaky (>=3.5.0)", "hypothesis (>=5.7.1)", "mypy (>=0.931)", "pytest-trio (>=0.7.0)"] + [[package]] name = "pytest-cov" version = "4.0.0" @@ -963,4 +983,4 @@ testing = ["func-timeout", "jaraco.itertools", "pytest (>=6)", "pytest-black (>= [metadata] lock-version = "2.0" python-versions = "^3.7" -content-hash = "4d5f287b63daa9a0bd42e35898f1d78852933f985166387138fd869601d1600b" +content-hash = "c4dd20cedc9ab95a3c11d9ef4f3dc712f43c217f114bf631ba1632be4c33cc00" diff --git a/pyproject.toml b/pyproject.toml index 59d0f13..8bc5e72 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,6 +34,7 @@ mypy = "^0.971" pytest-cov = "^4.0.0" coveralls = "^3.3.1" pytest-httpx = "^0.21.2" +pytest-asyncio = "^0.20.3" [build-system] requires = ["poetry-core>=1.4.0"] @@ -56,4 +57,4 @@ exclude = ''' [tool.isort] include_trailing_comma = true line_length = 120 -multi_line_output = 5 +multi_line_output = 3 diff --git a/tests/clients/__init__.py b/tests/clients/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/clients/client/__init__.py b/tests/clients/client/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/clients/client/test_base_url.py b/tests/clients/client/test_base_url.py new file mode 100644 index 0000000..8293954 --- /dev/null +++ b/tests/clients/client/test_base_url.py @@ -0,0 +1,17 @@ +import pytest + +from kalyke.clients import __Client + + +@pytest.mark.parametrize( + "use_sandbox, expect", + [ + (True, "https://api.sandbox.push.apple.com"), + (False, "https://api.push.apple.com"), + ], +) +def test_base_url(use_sandbox, expect): + client = __Client() + client.use_sandbox = use_sandbox + + assert client._base_url == expect diff --git a/tests/clients/client/test_handle_error.py b/tests/clients/client/test_handle_error.py new file mode 100644 index 0000000..948e06e --- /dev/null +++ b/tests/clients/client/test_handle_error.py @@ -0,0 +1,176 @@ +import pytest + +from kalyke.clients import __Client +from kalyke.exceptions import ( + BadCertificate, + BadCertificateEnvironment, + BadCollapseId, + BadDeviceToken, + BadExpirationDate, + BadMessageId, + BadPath, + BadPriority, + BadTopic, + DeviceTokenNotForTopic, + DuplicateHeaders, + ExpiredProviderToken, + ExpiredToken, + Forbidden, + IdleTimeout, + InternalServerError, + InvalidProviderToken, + InvalidPushType, + MethodNotAllowed, + MissingDeviceToken, + MissingProviderToken, + MissingTopic, + PayloadEmpty, + PayloadTooLarge, + ServiceUnavailable, + Shutdown, + TooManyProviderTokenUpdates, + TooManyRequests, + TopicDisallowed, + Unregistered, +) + + +@pytest.mark.parametrize( + "reason, expect", + [ + ( + "BadCollapseId", + BadCollapseId, + ), + ( + "BadDeviceToken", + BadDeviceToken, + ), + ( + "BadExpirationDate", + BadExpirationDate, + ), + ( + "BadMessageId", + BadMessageId, + ), + ( + "BadPriority", + BadPriority, + ), + ( + "BadTopic", + BadTopic, + ), + ( + "DeviceTokenNotForTopic", + DeviceTokenNotForTopic, + ), + ( + "DuplicateHeaders", + DuplicateHeaders, + ), + ( + "IdleTimeout", + IdleTimeout, + ), + ( + "InvalidPushType", + InvalidPushType, + ), + ( + "MissingDeviceToken", + MissingDeviceToken, + ), + ( + "MissingTopic", + MissingTopic, + ), + ( + "PayloadEmpty", + PayloadEmpty, + ), + ( + "TopicDisallowed", + TopicDisallowed, + ), + ( + "BadCertificate", + BadCertificate, + ), + ( + "BadCertificateEnvironment", + BadCertificateEnvironment, + ), + ( + "ExpiredProviderToken", + ExpiredProviderToken, + ), + ( + "Forbidden", + Forbidden, + ), + ( + "InvalidProviderToken", + InvalidProviderToken, + ), + ( + "MissingProviderToken", + MissingProviderToken, + ), + ( + "BadPath", + BadPath, + ), + ( + "MethodNotAllowed", + MethodNotAllowed, + ), + ( + "ExpiredToken", + ExpiredToken, + ), + ( + "Unregistered", + Unregistered, + ), + ( + "PayloadTooLarge", + PayloadTooLarge, + ), + ( + "TooManyProviderTokenUpdates", + TooManyProviderTokenUpdates, + ), + ( + "TooManyRequests", + TooManyRequests, + ), + ( + "InternalServerError", + InternalServerError, + ), + ( + "ServiceUnavailable", + ServiceUnavailable, + ), + ( + "Shutdown", + Shutdown, + ), + ], +) +def test_handle_error(reason, expect): + client = __Client() + exception = client._handle_error(error_json={"reason": reason}) + + assert type(exception) == expect + + +def test_attributed_error(): + client = __Client() + + with pytest.raises(AttributeError) as e: + _ = client._handle_error(error_json={"reason": "Unknown"}) + + assert str(e.value) == "module 'kalyke.exceptions' has no attribute 'Unknown'" diff --git a/tests/clients/client/test_init_client.py b/tests/clients/client/test_init_client.py new file mode 100644 index 0000000..64705fe --- /dev/null +++ b/tests/clients/client/test_init_client.py @@ -0,0 +1,15 @@ +import pytest + +from kalyke import ApnsConfig +from kalyke.clients import __Client + + +@pytest.mark.asyncio +async def test_not_implemented_error(): + client = __Client() + with pytest.raises(NotImplementedError) as e: + _ = client._init_client( + apns_config=ApnsConfig(topic="com.example.App"), + ) + + assert str(e.value) == "" diff --git a/tests/clients/client/test_make_url.py b/tests/clients/client/test_make_url.py new file mode 100644 index 0000000..5445cf5 --- /dev/null +++ b/tests/clients/client/test_make_url.py @@ -0,0 +1,32 @@ +import pytest + +from kalyke.clients import __Client + + +@pytest.mark.parametrize( + "use_sandbox, expect", + [ + ( + True, + "https://api.sandbox.push.apple.com/3/device/stub_device_token", + ), + ( + False, + "https://api.push.apple.com/3/device/stub_device_token", + ), + ], +) +def test_make_url(use_sandbox, expect): + client = __Client() + client.use_sandbox = use_sandbox + url = client._make_url(device_token="stub_device_token") + + assert url == expect + + +def test_attribute_error(): + client = __Client() + with pytest.raises(AttributeError) as e: + _ = client._make_url(device_token="stub_device_token") + + assert str(e.value) == "'__Client' object has no attribute 'use_sandbox'" diff --git a/tests/clients/client/test_send_message.py b/tests/clients/client/test_send_message.py new file mode 100644 index 0000000..34268b4 --- /dev/null +++ b/tests/clients/client/test_send_message.py @@ -0,0 +1,17 @@ +import pytest + +from kalyke import ApnsConfig +from kalyke.clients import __Client + + +@pytest.mark.asyncio +async def test_not_implemented_error(): + client = __Client() + with pytest.raises(NotImplementedError) as e: + _ = await client.send_message( + device_token="stub_device_token", + payload={}, + apns_config=ApnsConfig(topic="com.example.App"), + ) + + assert str(e.value) == ""