1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
from __future__ import annotations
import pytest
from dateutil.parser import isoparse
from hcloud.core import BaseDomain, DomainIdentityMixin, Meta, Pagination
class TestMeta:
@pytest.mark.parametrize("json_content", [None, "", {}])
def test_parse_meta_empty_json(self, json_content):
result = Meta.parse_meta(json_content)
assert result is None
def test_parse_meta_json_no_paginaton(self):
json_content = {"meta": {}}
result = Meta.parse_meta(json_content)
assert isinstance(result, Meta)
assert result.pagination is None
def test_parse_meta_json_ok(self):
json_content = {
"meta": {
"pagination": {
"page": 2,
"per_page": 10,
"previous_page": 1,
"next_page": 3,
"last_page": 10,
"total_entries": 100,
}
}
}
result = Meta.parse_meta(json_content)
assert isinstance(result, Meta)
assert isinstance(result.pagination, Pagination)
assert result.pagination.page == 2
assert result.pagination.per_page == 10
assert result.pagination.next_page == 3
assert result.pagination.last_page == 10
assert result.pagination.total_entries == 100
class SomeDomain(BaseDomain, DomainIdentityMixin):
__api_properties__ = ("id", "name")
__slots__ = __api_properties__
def __init__(self, id=None, name=None):
self.id = id
self.name = name
class TestDomainIdentityMixin:
@pytest.mark.parametrize(
"domain,expected_result",
[
(SomeDomain(id=1, name="name"), 1),
(SomeDomain(id=1), 1),
(SomeDomain(name="name"), "name"),
],
)
def test_id_or_name_ok(self, domain, expected_result):
assert domain.id_or_name == expected_result
def test_id_or_name_exception(self):
domain = SomeDomain()
with pytest.raises(ValueError) as exception_info:
_ = domain.id_or_name
error = exception_info.value
assert str(error) == "id or name must be set"
@pytest.mark.parametrize(
"other, expected",
[
(SomeDomain(id=1), True),
(SomeDomain(name="name1"), True),
(SomeDomain(id=1, name="name1"), True),
(SomeDomain(id=2), False),
(SomeDomain(name="name2"), False),
(SomeDomain(id=2, name="name2"), False),
],
)
def test_has_id_or_name_exception(self, other, expected):
domain = SomeDomain(id=1, name="name1")
assert domain.has_id_or_name(other.id_or_name) == expected
class ActionDomain(BaseDomain, DomainIdentityMixin):
__api_properties__ = ("id", "name", "started")
__slots__ = __api_properties__
def __init__(self, id, name="name1", started=None):
self.id = id
self.name = name
self.started = isoparse(started) if started else None
class SomeOtherDomain(BaseDomain):
__api_properties__ = ("id", "name", "child")
__slots__ = __api_properties__
def __init__(self, id=None, name=None, child=None):
self.id = id
self.name = name
self.child = child
class TestBaseDomain:
@pytest.mark.parametrize(
"data_dict,expected_result",
[
({"id": 1}, {"id": 1, "name": "name1", "started": None}),
({"id": 2, "name": "name2"}, {"id": 2, "name": "name2", "started": None}),
(
{"id": 3, "foo": "boo", "description": "new"},
{"id": 3, "name": "name1", "started": None},
),
(
{
"id": 4,
"foo": "boo",
"description": "new",
"name": "name-name3",
"started": "2016-01-30T23:50+00:00",
},
{
"id": 4,
"name": "name-name3",
"started": isoparse("2016-01-30T23:50+00:00"),
},
),
],
)
def test_from_dict_ok(self, data_dict, expected_result):
model = ActionDomain.from_dict(data_dict)
for k, v in expected_result.items():
assert getattr(model, k) == v
@pytest.mark.parametrize(
"data,expected",
[
(
SomeOtherDomain(id=1, name="name1"),
"SomeOtherDomain(id=1, name='name1', child=None)",
),
(
SomeOtherDomain(
id=2,
name="name2",
child=SomeOtherDomain(id=3, name="name3"),
),
"SomeOtherDomain(id=2, name='name2', child=SomeOtherDomain(id=3, name='name3', child=None))",
),
],
)
def test_repr_ok(self, data, expected):
assert data.__repr__() == expected
|