Skip to content
Snippets Groups Projects
Commit a59c78be authored by Krzysztof Mochocki's avatar Krzysztof Mochocki
Browse files

Move Url tests from test_tools

parent 223c68ee
No related branches found
No related tags found
No related merge requests found
from __future__ import annotations
from typing import Final
from hive_transfer_protocol import HttpUrl, WsUrl
URL_TYPES = type[HttpUrl] | type[WsUrl]
DEFAULT_ADDRESS: Final[str] = "127.0.0.1"
DEFAULT_PORT: Final[int] = 8080
from __future__ import annotations
import pytest
from hive_transfer_protocol import HttpUrl, WsUrl
from tests.unit.constants import DEFAULT_ADDRESS, DEFAULT_PORT, URL_TYPES
@pytest.mark.parametrize(
("input_url", "expected_protocol", "url_type"),
[
(f"http://{DEFAULT_ADDRESS}:{DEFAULT_PORT}", "http", HttpUrl),
(f"ws://{DEFAULT_ADDRESS}:{DEFAULT_PORT}", "ws", WsUrl),
],
)
def test_url_parsing_without_expected_protocol(input_url: str, expected_protocol: str, url_type: URL_TYPES) -> None:
url = url_type(input_url)
assert url.protocol == expected_protocol
assert url.address == DEFAULT_ADDRESS
assert url.port == DEFAULT_PORT
@pytest.mark.parametrize(
("input_url", "expected_protocol", "url_type"),
[
(f"{DEFAULT_ADDRESS}:{DEFAULT_PORT}", "http", HttpUrl),
(f"{DEFAULT_ADDRESS}:{DEFAULT_PORT}", "ws", WsUrl),
],
)
def test_url_parsing_with_expected_protocol(input_url: str, expected_protocol: str, url_type: URL_TYPES) -> None:
url = url_type(input_url, protocol=expected_protocol) # type: ignore[arg-type]
assert url.protocol == expected_protocol
assert url.address == DEFAULT_ADDRESS
assert url.port == DEFAULT_PORT
@pytest.mark.parametrize(
("input_url", "expected_protocol", "url_type"),
[
(DEFAULT_ADDRESS, "http", HttpUrl),
(DEFAULT_ADDRESS, "ws", WsUrl),
],
)
def test_url_parsing_without_port_given(input_url: str, expected_protocol: str, url_type: URL_TYPES) -> None:
url = url_type(input_url, protocol=expected_protocol) # type: ignore[arg-type]
assert url.protocol == expected_protocol
assert url.address == DEFAULT_ADDRESS
assert url.port is None
@pytest.mark.parametrize("url_type", [HttpUrl, WsUrl])
def test_url_parsing_without_address_given(url_type: URL_TYPES) -> None:
with pytest.raises(ValueError) as exception: # noqa: PT011
url_type(f":{DEFAULT_PORT}")
assert str(exception.value) == "Address was not specified."
from __future__ import annotations
import pytest
from hive_transfer_protocol import HttpUrl, WsUrl
from tests.unit.constants import DEFAULT_ADDRESS, DEFAULT_PORT, URL_TYPES
@pytest.mark.parametrize(
("url", "with_protocol", "expected"),
[
(
HttpUrl(f"{DEFAULT_ADDRESS}:{DEFAULT_PORT}", protocol="http"),
True,
f"http://{DEFAULT_ADDRESS}:{DEFAULT_PORT}",
),
(HttpUrl(f"{DEFAULT_ADDRESS}:{DEFAULT_PORT}", protocol="http"), False, f"{DEFAULT_ADDRESS}:{DEFAULT_PORT}"),
(HttpUrl(f"{DEFAULT_ADDRESS}:{DEFAULT_PORT}"), False, f"{DEFAULT_ADDRESS}:{DEFAULT_PORT}"),
],
)
def test_serialization(url: HttpUrl, with_protocol: bool, expected: str) -> None:
assert url.as_string(with_protocol=with_protocol) == expected
@pytest.mark.parametrize(
("input_url", "expected_protocol", "url_type"),
[
(DEFAULT_ADDRESS, "http", HttpUrl),
(DEFAULT_ADDRESS, "ws", WsUrl),
],
)
def test_url_serializing_without_port_given(input_url: str, expected_protocol: int, url_type: URL_TYPES) -> None:
assert (
url_type(input_url, protocol=expected_protocol).as_string(with_protocol=True) == f"{expected_protocol}://127.0.0.1" # type: ignore[arg-type]
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment