|
1 |
| -from typing import Any |
| 1 | +from typing import TypedDict |
2 | 2 |
|
3 | 3 | import faker
|
4 | 4 | import pytest
|
| 5 | +from polyfactory.factories.typed_dict_factory import TypedDictFactory |
5 | 6 |
|
6 | 7 | import stompman
|
| 8 | +from stompman.config import MultiHostHostLike |
7 | 9 |
|
8 | 10 |
|
9 |
| -def test_connection_parameters_from_pydantic_multihost_hosts(faker: faker.Faker) -> None: |
10 |
| - full_host: dict[str, Any] = { |
11 |
| - "username": faker.pystr(), |
12 |
| - "password": faker.pystr(), |
13 |
| - "host": faker.pystr(), |
14 |
| - "port": faker.pyint(), |
15 |
| - } |
16 |
| - assert stompman.ConnectionParameters.from_pydantic_multihost_hosts( |
17 |
| - [{**full_host, "port": index} for index in range(5)] # type: ignore[typeddict-item] |
18 |
| - ) == [ |
19 |
| - stompman.ConnectionParameters(full_host["host"], index, full_host["username"], full_host["password"]) |
20 |
| - for index in range(5) |
21 |
| - ] |
22 |
| - |
23 |
| - for key in ("username", "password", "host", "port"): |
24 |
| - with pytest.raises(ValueError, match=f"{key} must be set"): |
25 |
| - assert stompman.ConnectionParameters.from_pydantic_multihost_hosts([{**full_host, key: None}, full_host]) # type: ignore[typeddict-item, list-item] |
| 11 | +class StrictMultiHostHostLike(TypedDict): |
| 12 | + username: str |
| 13 | + password: str |
| 14 | + host: str |
| 15 | + port: int |
| 16 | + |
| 17 | + |
| 18 | +class StrictMultiHostHostLikeFactory(TypedDictFactory[StrictMultiHostHostLike]): ... |
| 19 | + |
| 20 | + |
| 21 | +class MultiHostHostLikeFactory(TypedDictFactory[MultiHostHostLike]): ... |
| 22 | + |
| 23 | + |
| 24 | +class TestConnectionParametersFromPydanticMultiHostHosts: |
| 25 | + def test_ok(self, faker: faker.Faker) -> None: |
| 26 | + hosts: list[MultiHostHostLike] = [ |
| 27 | + {"host": "host1", "port": 1, "username": None, "password": None}, |
| 28 | + {"host": "host2", "port": 2, "username": None, "password": None}, |
| 29 | + {"host": "host3", "port": 3, "username": None, "password": None}, |
| 30 | + {"host": "host4", "port": 4, "username": None, "password": None}, |
| 31 | + ] |
| 32 | + host_with_credentials = faker.pyint(min_value=0, max_value=3) |
| 33 | + hosts[host_with_credentials]["username"] = "lev" |
| 34 | + hosts[host_with_credentials]["password"] = "pass" # noqa: S105 |
| 35 | + |
| 36 | + result = stompman.ConnectionParameters.from_pydantic_multihost_hosts(hosts) |
| 37 | + |
| 38 | + assert result == [ |
| 39 | + stompman.ConnectionParameters("host1", 1, "lev", "pass"), |
| 40 | + stompman.ConnectionParameters("host2", 2, "lev", "pass"), |
| 41 | + stompman.ConnectionParameters("host3", 3, "lev", "pass"), |
| 42 | + stompman.ConnectionParameters("host4", 4, "lev", "pass"), |
| 43 | + ] |
| 44 | + |
| 45 | + def test_no_host_or_port_or_both(self, faker: faker.Faker) -> None: |
| 46 | + cases: list[MultiHostHostLike] = [ |
| 47 | + {"host": None, "port": faker.pyint(), "username": faker.pystr(), "password": faker.pystr()}, |
| 48 | + {"host": faker.pystr(), "port": None, "username": faker.pystr(), "password": faker.pystr()}, |
| 49 | + {"host": None, "port": None, "username": faker.pystr(), "password": faker.pystr()}, |
| 50 | + ] |
| 51 | + |
| 52 | + for host in cases: |
| 53 | + with pytest.raises(ValueError, match="must be set"): |
| 54 | + stompman.ConnectionParameters.from_pydantic_multihost_hosts([host]) |
| 55 | + |
| 56 | + def test_no_username(self, faker: faker.Faker) -> None: |
| 57 | + hosts: list[MultiHostHostLike] = [ |
| 58 | + {"host": faker.pystr(), "port": faker.pyint(), "username": None, "password": faker.pystr()}, |
| 59 | + {"host": faker.pystr(), "port": faker.pyint(), "username": faker.pystr(), "password": faker.pystr()}, |
| 60 | + ] |
| 61 | + |
| 62 | + with pytest.raises(ValueError, match="username must be set"): |
| 63 | + stompman.ConnectionParameters.from_pydantic_multihost_hosts(hosts) |
| 64 | + |
| 65 | + def test_no_password(self, faker: faker.Faker) -> None: |
| 66 | + hosts: list[MultiHostHostLike] = [ |
| 67 | + {"host": faker.pystr(), "port": faker.pyint(), "username": faker.pystr(), "password": None}, |
| 68 | + {"host": faker.pystr(), "port": faker.pyint(), "username": faker.pystr(), "password": faker.pystr()}, |
| 69 | + ] |
| 70 | + |
| 71 | + with pytest.raises(ValueError, match="password must be set"): |
| 72 | + stompman.ConnectionParameters.from_pydantic_multihost_hosts(hosts) |
| 73 | + |
| 74 | + def test_no_credentials(self, faker: faker.Faker) -> None: |
| 75 | + cases: list[MultiHostHostLike] = [ |
| 76 | + {"host": faker.pystr(), "port": faker.pyint(), "username": None, "password": None}, |
| 77 | + {"host": faker.pystr(), "port": faker.pyint(), "username": None, "password": None}, |
| 78 | + ] |
| 79 | + |
| 80 | + for host in cases: |
| 81 | + with pytest.raises(ValueError, match="username and password must be set"): |
| 82 | + stompman.ConnectionParameters.from_pydantic_multihost_hosts([host]) |
| 83 | + |
| 84 | + def test_multiple_credentials(self, faker: faker.Faker) -> None: |
| 85 | + hosts: list[MultiHostHostLike] = [ |
| 86 | + {"host": faker.pystr(), "port": faker.pyint(), "username": None, "password": None}, |
| 87 | + {"host": faker.pystr(), "port": faker.pyint(), "username": faker.pystr(), "password": faker.pystr()}, |
| 88 | + {"host": faker.pystr(), "port": faker.pyint(), "username": faker.pystr(), "password": faker.pystr()}, |
| 89 | + ] |
| 90 | + |
| 91 | + with pytest.raises(ValueError, match="only one username-password pair must be set"): |
| 92 | + stompman.ConnectionParameters.from_pydantic_multihost_hosts(hosts) |
0 commit comments