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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
"""Asynchronous Python client for Mealie."""
from __future__ import annotations
import asyncio
from datetime import date
from typing import TYPE_CHECKING, Any
import aiohttp
from aiohttp.hdrs import METH_GET, METH_POST, METH_PUT, METH_DELETE
from aioresponses import CallbackResult, aioresponses
import pytest
from yarl import URL
from aiomealie.exceptions import (
MealieAuthenticationError,
MealieConnectionError,
MealieValidationError,
MealieError,
MealieNotFoundError,
MealieBadRequestError,
)
from aiomealie.mealie import MealieClient
from aiomealie.models import MutateShoppingItem, MealplanEntryType
from tests import load_fixture
from .const import HEADERS, MEALIE_URL
if TYPE_CHECKING:
from syrupy import SnapshotAssertion
async def test_putting_in_own_session(
responses: aioresponses,
) -> None:
"""Test putting in own session."""
responses.get(
f"{MEALIE_URL}/api/app/about/startup-info",
status=200,
body=load_fixture("startup_info.json"),
)
async with aiohttp.ClientSession() as session:
analytics = MealieClient(session=session, api_host="https://demo.mealie.io")
await analytics.get_startup_info()
assert analytics.session is not None
assert not analytics.session.closed
await analytics.close()
assert not analytics.session.closed
async def test_creating_own_session(
responses: aioresponses,
) -> None:
"""Test creating own session."""
responses.get(
f"{MEALIE_URL}/api/app/about/startup-info",
status=200,
body=load_fixture("startup_info.json"),
)
mealie_client = MealieClient(api_host="https://demo.mealie.io", token="XXX")
await mealie_client.get_startup_info()
assert mealie_client.session is not None
assert not mealie_client.session.closed
await mealie_client.close()
assert mealie_client.session.closed
async def test_unexpected_server_response(
responses: aioresponses,
mealie_client: MealieClient,
) -> None:
"""Test handling unexpected response."""
responses.get(
f"{MEALIE_URL}/api/app/about/startup-info",
status=200,
headers={"Content-Type": "plain/text"},
body="Yes",
)
with pytest.raises(MealieError):
assert await mealie_client.get_startup_info()
async def test_authentication_error(
responses: aioresponses,
mealie_client: MealieClient,
) -> None:
"""Test authentication error from mealie."""
responses.get(
f"{MEALIE_URL}/api/groups/self",
status=401,
body=load_fixture("authentication_error.json"),
)
with pytest.raises(MealieAuthenticationError):
assert await mealie_client.get_groups_self()
async def test_validation_error(
responses: aioresponses,
mealie_client: MealieClient,
) -> None:
"""Test validation error from mealie."""
item_id: str = "64207a44-7b40-4392-a06a-bc4e10394622"
item = MutateShoppingItem(
list_id="27edbaab-2ec6-441f-8490-0283ea77585f", note="Bread", position=0
)
responses.put(
f"{MEALIE_URL}/api/households/shopping/items/{item_id}",
status=422,
body=load_fixture("validation_error.json"),
)
with pytest.raises(MealieValidationError):
await mealie_client.update_shopping_item(item_id, item)
async def test_not_found_error(
responses: aioresponses,
mealie_client: MealieClient,
) -> None:
"""Test not found error from mealie."""
responses.get(
f"{MEALIE_URL}/api/recipes/original-sacher-torte-2",
status=404,
body=load_fixture("not_found_error.json"),
)
with pytest.raises(MealieNotFoundError):
await mealie_client.get_recipe("original-sacher-torte-2")
async def test_bad_request_error(
responses: aioresponses,
mealie_client: MealieClient,
) -> None:
"""Test not found error from mealie."""
responses.post(
f"{MEALIE_URL}/api/recipes/create/url",
status=400,
body=load_fixture("bad_request_error.json"),
)
with pytest.raises(MealieBadRequestError):
await mealie_client.import_recipe(
"https://www.sacher.com/en/original-sacher-torte/recipe/"
)
async def test_timeout(
responses: aioresponses,
) -> None:
"""Test request timeout."""
# Faking a timeout by sleeping
async def response_handler(_: str, **_kwargs: Any) -> CallbackResult:
"""Response handler for this test."""
await asyncio.sleep(2)
return CallbackResult(body="Goodmorning!")
responses.get(
f"{MEALIE_URL}/api/app/about/startup-info",
callback=response_handler,
)
async with MealieClient(
request_timeout=1, api_host="https://demo.mealie.io"
) as mealie_client:
with pytest.raises(MealieConnectionError):
assert await mealie_client.get_startup_info()
async def test_client_connection_error() -> None:
"""Test client connection error from mealie."""
async with MealieClient(api_host="https://bad-url") as mealie_client:
with pytest.raises(MealieConnectionError):
assert await mealie_client.get_startup_info()
async def test_about(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving about."""
responses.get(
f"{MEALIE_URL}/api/app/about",
status=200,
body=load_fixture("about.json"),
)
assert await mealie_client.get_about() == snapshot
async def test_startup_info(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving startup info."""
responses.get(
f"{MEALIE_URL}/api/app/about/startup-info",
status=200,
body=load_fixture("startup_info.json"),
)
assert await mealie_client.get_startup_info() == snapshot
async def test_groups_self(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving groups self."""
responses.get(
f"{MEALIE_URL}/api/groups/self",
status=200,
body=load_fixture("groups_self.json"),
)
assert await mealie_client.get_groups_self() == snapshot
async def test_theme(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving theme."""
responses.get(
f"{MEALIE_URL}/api/app/about/theme",
status=200,
body=load_fixture("theme.json"),
)
assert await mealie_client.get_theme() == snapshot
async def test_recipes(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving recipes."""
responses.get(
f"{MEALIE_URL}/api/recipes",
status=200,
body=load_fixture("recipes.json"),
)
assert await mealie_client.get_recipes() == snapshot
async def test_retrieving_recipe(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving recipe."""
responses.get(
f"{MEALIE_URL}/api/recipes/original-sacher-torte-2",
status=200,
body=load_fixture("recipe.json"),
)
assert await mealie_client.get_recipe("original-sacher-torte-2") == snapshot
async def test_importing_recipe(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test importing recipe."""
responses.post(
f"{MEALIE_URL}/api/recipes/create/url",
status=201,
body=load_fixture("scrape_recipe.json"),
)
responses.get(
f"{MEALIE_URL}/api/recipes/original-sacher-torte-2",
status=200,
body=load_fixture("recipe.json"),
)
assert (
await mealie_client.import_recipe(
"https://www.sacher.com/en/original-sacher-torte/recipe/"
)
== snapshot
)
responses.assert_called_with(
f"{MEALIE_URL}/api/recipes/create/url",
METH_POST,
headers=HEADERS,
params=None,
json={
"url": "https://www.sacher.com/en/original-sacher-torte/recipe/",
"include_tags": False,
},
)
responses.assert_called_with(
f"{MEALIE_URL}/api/recipes/original-sacher-torte-2",
METH_GET,
headers=HEADERS,
params=None,
json=None,
)
async def test_mealplan_today(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving mealplan."""
responses.get(
f"{MEALIE_URL}/api/households/mealplans/today",
status=200,
body=load_fixture("mealplan_today.json"),
)
assert await mealie_client.get_mealplan_today() == snapshot
async def test_mealplans(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving mealplan."""
params: dict[str, Any] = {
"perPage": -1,
}
url = URL(MEALIE_URL).joinpath("api/households/mealplans").with_query(params)
responses.get(
url,
status=200,
body=load_fixture("mealplans.json"),
)
assert await mealie_client.get_mealplans() == snapshot
async def test_user_info(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving user info."""
responses.get(
f"{MEALIE_URL}/api/users/self",
status=200,
body=load_fixture("users_self.json"),
)
assert await mealie_client.get_user_info() == snapshot
@pytest.mark.parametrize(
("kwargs", "params"),
[
({}, {"perPage": -1}),
(
{
"start_date": date(2021, 1, 1),
"end_date": date(2021, 1, 2),
},
{
"start_date": "2021-01-01",
"end_date": "2021-01-02",
"perPage": -1,
},
),
],
)
async def test_mealplans_parameters(
responses: aioresponses,
mealie_client: MealieClient,
kwargs: dict[str, Any],
params: dict[str, Any],
) -> None:
"""Test retrieving mealplans."""
url = URL(MEALIE_URL).joinpath("api/households/mealplans").with_query(params)
responses.get(
url,
status=200,
body=load_fixture("mealplans.json"),
)
assert await mealie_client.get_mealplans(**kwargs)
responses.assert_called_once_with(
f"{MEALIE_URL}/api/households/mealplans",
METH_GET,
headers=HEADERS,
params=params,
json=None,
)
async def test_shopping_lists(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving shopping lists."""
params: dict[str, Any] = {
"perPage": -1,
}
url = URL(MEALIE_URL).joinpath("api/households/shopping/lists").with_query(params)
responses.get(
url,
status=200,
body=load_fixture("shopping_lists.json"),
)
assert await mealie_client.get_shopping_lists() == snapshot
async def test_shopping_items(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving shopping items."""
shopping_list_id: str = "27edbaab-2ec6-441f-8490-0283ea77585f"
params: dict[str, Any] = {
"queryFilter": f"shoppingListId={shopping_list_id}",
"orderBy": "position",
"orderDirection": "asc",
"perPage": -1,
}
url = URL(MEALIE_URL).joinpath("api/households/shopping/items").with_query(params)
responses.get(
url,
status=200,
body=load_fixture("shopping_items.json"),
)
assert (
await mealie_client.get_shopping_items(shopping_list_id=shopping_list_id)
== snapshot
)
responses.assert_called_once_with(
f"{MEALIE_URL}/api/households/shopping/items",
METH_GET,
headers=HEADERS,
params=params,
json=None,
)
async def test_add_shopping_item(
responses: aioresponses,
mealie_client: MealieClient,
) -> None:
"""Test adding shopping item."""
item = MutateShoppingItem(
list_id="27edbaab-2ec6-441f-8490-0283ea77585f", note="Bread", position=0
)
responses.post(
f"{MEALIE_URL}/api/households/shopping/items",
status=201,
)
await mealie_client.add_shopping_item(item=item)
responses.assert_called_once_with(
f"{MEALIE_URL}/api/households/shopping/items",
METH_POST,
headers=HEADERS,
params=None,
json={
"shoppingListId": "27edbaab-2ec6-441f-8490-0283ea77585f",
"note": "Bread",
"position": 0,
},
)
async def test_update_shopping_item(
responses: aioresponses,
mealie_client: MealieClient,
) -> None:
"""Test updating shopping item."""
item_id: str = "64207a44-7b40-4392-a06a-bc4e10394622"
item = MutateShoppingItem(
list_id="27edbaab-2ec6-441f-8490-0283ea77585f", note="Bread", position=0
)
responses.put(
f"{MEALIE_URL}/api/households/shopping/items/{item_id}",
status=201,
)
await mealie_client.update_shopping_item(item_id=item_id, item=item)
responses.assert_called_once_with(
f"{MEALIE_URL}/api/households/shopping/items/{item_id}",
METH_PUT,
headers=HEADERS,
params=None,
json={
"shoppingListId": "27edbaab-2ec6-441f-8490-0283ea77585f",
"note": "Bread",
"position": 0,
},
)
async def test_delete_shopping_item(
responses: aioresponses,
mealie_client: MealieClient,
) -> None:
"""Test deleting shopping item."""
item_id: str = "64207a44-7b40-4392-a06a-bc4e10394622"
responses.delete(
f"{MEALIE_URL}/api/households/shopping/items/{item_id}",
status=201,
)
await mealie_client.delete_shopping_item(item_id=item_id)
responses.assert_called_once_with(
f"{MEALIE_URL}/api/households/shopping/items/{item_id}",
METH_DELETE,
headers=HEADERS,
params=None,
json=None,
)
async def test_statistics(
responses: aioresponses,
mealie_client: MealieClient,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving statistics."""
responses.get(
f"{MEALIE_URL}/api/households/statistics",
status=200,
body=load_fixture("statistics.json"),
)
assert await mealie_client.get_statistics() == snapshot
async def test_random_mealplan(
responses: aioresponses, mealie_client: MealieClient, snapshot: SnapshotAssertion
) -> None:
"""Test setting random mealplan."""
responses.post(
f"{MEALIE_URL}/api/households/mealplans/random",
status=201,
body=load_fixture("mealplan.json"),
)
assert (
await mealie_client.random_mealplan(
at=date(2021, 1, 1), entry_type=MealplanEntryType.BREAKFAST
)
) == snapshot
responses.assert_called_once_with(
f"{MEALIE_URL}/api/households/mealplans/random",
METH_POST,
headers=HEADERS,
params=None,
json={
"date": "2021-01-01",
"entryType": "breakfast",
},
)
@pytest.mark.parametrize(
("kwargs", "data"),
[
({"recipe_id": "abc"}, {"recipeId": "abc"}),
({"note_title": "title"}, {"title": "title"}),
(
{"note_title": "title", "note_text": "description"},
{"title": "title", "text": "description"},
),
],
)
async def test_set_mealplan(
responses: aioresponses,
mealie_client: MealieClient,
kwargs: dict[str, Any],
data: dict[str, Any],
) -> None:
"""Test setting mealplan."""
responses.post(
f"{MEALIE_URL}/api/households/mealplans",
status=201,
body=load_fixture("mealplan.json"),
)
await mealie_client.set_mealplan(
at=date(2021, 1, 1), entry_type=MealplanEntryType.BREAKFAST, **kwargs
)
responses.assert_called_once_with(
f"{MEALIE_URL}/api/households/mealplans",
METH_POST,
headers=HEADERS,
params=None,
json={
"date": "2021-01-01",
"entryType": "breakfast",
}
| data,
)
async def test_household_support(
responses: aioresponses,
mealie_client: MealieClient,
) -> None:
"""Test household support."""
responses.get(f"{MEALIE_URL}/api/households/mealplans/today", status=404, body="")
assert await mealie_client.define_household_support() is False
assert mealie_client.household_support is False
async def test_no_household_support(
responses: aioresponses,
mealie_client: MealieClient,
) -> None:
"""Test no household support."""
mealie_client.household_support = None
responses.get(
f"{MEALIE_URL}/api/households/mealplans/today",
status=200,
body=load_fixture("mealplan_today.json"),
)
assert await mealie_client.define_household_support() is True
assert mealie_client.household_support is True
|