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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
from __future__ import annotations
from typing import Any, NamedTuple
from unittest import mock
import pytest
from hcloud.actions import ActionsPageResult
from hcloud.core import BaseDomain, BoundModelBase, ClientEntityBase, Meta
class TestBoundModelBase:
@pytest.fixture()
def bound_model_class(self):
class Model(BaseDomain):
__api_properties__ = ("id", "name", "description")
__slots__ = __api_properties__
def __init__(self, id, name="", description=""):
self.id = id
self.name = name
self.description = description
class BoundModel(BoundModelBase, Model):
model = Model
return BoundModel
@pytest.fixture()
def client(self):
client = mock.MagicMock()
return client
def test_get_exists_model_attribute_complete_model(self, bound_model_class, client):
bound_model = bound_model_class(
client=client,
data={"id": 1, "name": "name", "description": "my_description"},
)
description = bound_model.description
client.get_by_id.assert_not_called()
assert description == "my_description"
def test_get_non_exists_model_attribute_complete_model(
self, bound_model_class, client
):
bound_model = bound_model_class(
client=client, data={"id": 1, "name": "name", "description": "description"}
)
with pytest.raises(AttributeError):
bound_model.content
client.get_by_id.assert_not_called()
def test_get_exists_model_attribute_incomplete_model(
self, bound_model_class, client
):
bound_model = bound_model_class(client=client, data={"id": 101}, complete=False)
client.get_by_id.return_value = bound_model_class(
client=client,
data={"id": 101, "name": "name", "description": "super_description"},
)
description = bound_model.description
client.get_by_id.assert_called_once_with(101)
assert description == "super_description"
assert bound_model.complete is True
def test_get_filled_model_attribute_incomplete_model(
self, bound_model_class, client
):
bound_model = bound_model_class(client=client, data={"id": 101}, complete=False)
id = bound_model.id
client.get_by_id.assert_not_called()
assert id == 101
assert bound_model.complete is False
def test_get_non_exists_model_attribute_incomplete_model(
self, bound_model_class, client
):
bound_model = bound_model_class(client=client, data={"id": 1}, complete=False)
with pytest.raises(AttributeError):
bound_model.content
client.get_by_id.assert_not_called()
assert bound_model.complete is False
class TestClientEntityBase:
@pytest.fixture()
def client_class_constructor(self):
def constructor(json_content_function):
class CandiesPageResult(NamedTuple):
candies: list[Any]
meta: Meta
class CandiesClient(ClientEntityBase):
def get_list(self, status=None, page=None, per_page=None):
json_content = json_content_function(page)
results = [
(r, page, status, per_page) for r in json_content["candies"]
]
return CandiesPageResult(results, Meta.parse_meta(json_content))
return CandiesClient(mock.MagicMock())
return constructor
@pytest.fixture()
def client_class_with_actions_constructor(self):
def constructor(json_content_function):
class CandiesClient(ClientEntityBase):
def get_actions_list(self, status, page=None, per_page=None):
json_content = json_content_function(page)
results = [
(r, page, status, per_page) for r in json_content["actions"]
]
return ActionsPageResult(results, Meta.parse_meta(json_content))
return CandiesClient(mock.MagicMock())
return constructor
def test_iter_pages_no_meta(self, client_class_constructor):
json_content = {"candies": [1, 2]}
def json_content_function(p):
return json_content
candies_client = client_class_constructor(json_content_function)
result = candies_client._iter_pages(candies_client.get_list, status="sweet")
assert result == [(1, 1, "sweet", 50), (2, 1, "sweet", 50)]
def test_iter_pages_no_next_page(self, client_class_constructor):
json_content = {
"candies": [1, 2],
"meta": {"pagination": {"page": 1, "per_page": 11, "next_page": None}},
}
def json_content_function(p):
return json_content
candies_client = client_class_constructor(json_content_function)
result = candies_client._iter_pages(candies_client.get_list, status="sweet")
assert result == [(1, 1, "sweet", 50), (2, 1, "sweet", 50)]
def test_iter_pages_ok(self, client_class_constructor):
def json_content_function(p):
return {
"candies": [10 + p, 20 + p],
"meta": {
"pagination": {
"page": p,
"per_page": 11,
"next_page": p + 1 if p < 3 else None,
}
},
}
candies_client = client_class_constructor(json_content_function)
result = candies_client._iter_pages(candies_client.get_list, status="sweet")
assert result == [
(11, 1, "sweet", 50),
(21, 1, "sweet", 50),
(12, 2, "sweet", 50),
(22, 2, "sweet", 50),
(13, 3, "sweet", 50),
(23, 3, "sweet", 50),
]
def test_get_actions_ok(self, client_class_with_actions_constructor):
def json_content_function(p):
return {
"actions": [10 + p, 20 + p],
"meta": {
"pagination": {
"page": p,
"per_page": 11,
"next_page": p + 1 if p < 3 else None,
}
},
}
candies_client = client_class_with_actions_constructor(json_content_function)
result = candies_client._iter_pages(
candies_client.get_actions_list, status="sweet"
)
assert result == [
(11, 1, "sweet", 50),
(21, 1, "sweet", 50),
(12, 2, "sweet", 50),
(22, 2, "sweet", 50),
(13, 3, "sweet", 50),
(23, 3, "sweet", 50),
]
def test_get_first_by_result_exists(self, client_class_constructor):
json_content = {"candies": [1]}
def json_content_function(p):
return json_content
candies_client = client_class_constructor(json_content_function)
result = candies_client._get_first_by(status="sweet")
assert result == (1, None, "sweet", None)
def test_get_first_by_result_does_not_exist(self, client_class_constructor):
json_content = {"candies": []}
def json_content_function(p):
return json_content
candies_client = client_class_constructor(json_content_function)
result = candies_client._get_first_by(status="sweet")
assert result is None
|