Skip to content

Commit

Permalink
Add __Client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nnsnodnb committed Jan 9, 2023
1 parent 895cb32 commit 3b9c43a
Show file tree
Hide file tree
Showing 9 changed files with 280 additions and 2 deletions.
22 changes: 21 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -56,4 +57,4 @@ exclude = '''
[tool.isort]
include_trailing_comma = true
line_length = 120
multi_line_output = 5
multi_line_output = 3
Empty file added tests/clients/__init__.py
Empty file.
Empty file.
17 changes: 17 additions & 0 deletions tests/clients/client/test_base_url.py
Original file line number Diff line number Diff line change
@@ -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
176 changes: 176 additions & 0 deletions tests/clients/client/test_handle_error.py
Original file line number Diff line number Diff line change
@@ -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'"
15 changes: 15 additions & 0 deletions tests/clients/client/test_init_client.py
Original file line number Diff line number Diff line change
@@ -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) == ""
32 changes: 32 additions & 0 deletions tests/clients/client/test_make_url.py
Original file line number Diff line number Diff line change
@@ -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'"
17 changes: 17 additions & 0 deletions tests/clients/client/test_send_message.py
Original file line number Diff line number Diff line change
@@ -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) == ""

0 comments on commit 3b9c43a

Please sign in to comment.