mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-09 03:04:54 -04:00
added test
This commit is contained in:
parent
f2e7deb5cb
commit
72052be92f
@ -1,10 +1,14 @@
|
|||||||
|
import os
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
from zipfile import ZipFile
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from mealie.schema.group.group_migration import SupportedMigrations
|
from mealie.schema.group.group_migration import SupportedMigrations
|
||||||
|
from mealie.schema.reports.reports import ReportEntryOut
|
||||||
from tests import data as test_data
|
from tests import data as test_data
|
||||||
from tests.utils import api_routes
|
from tests.utils import api_routes
|
||||||
from tests.utils.assertion_helpers import assert_derserialize
|
from tests.utils.assertion_helpers import assert_derserialize
|
||||||
@ -60,6 +64,8 @@ def test_recipe_migration(api_client: TestClient, unique_user: TestUser, mig: Mi
|
|||||||
response = api_client.get(api_routes.groups_reports_item_id(report_id), headers=unique_user.token)
|
response = api_client.get(api_routes.groups_reports_item_id(report_id), headers=unique_user.token)
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
response_json = response.json()
|
||||||
|
assert response_json["entries"]
|
||||||
|
|
||||||
for item in response.json()["entries"]:
|
for item in response.json()["entries"]:
|
||||||
assert item["success"]
|
assert item["success"]
|
||||||
@ -77,3 +83,63 @@ def test_recipe_migration(api_client: TestClient, unique_user: TestUser, mig: Mi
|
|||||||
query_data = assert_derserialize(response)
|
query_data = assert_derserialize(response)
|
||||||
events = query_data["items"]
|
events = query_data["items"]
|
||||||
assert len(events)
|
assert len(events)
|
||||||
|
|
||||||
|
|
||||||
|
def test_bad_mealie_alpha_data_is_ignored(api_client: TestClient, unique_user: TestUser):
|
||||||
|
with TemporaryDirectory() as tmpdir:
|
||||||
|
with ZipFile(test_data.migrations_mealie) as zf:
|
||||||
|
zf.extractall(tmpdir)
|
||||||
|
|
||||||
|
invalid_recipe_dir = os.path.join(tmpdir, "mealie_2021-Dec-08", "recipes", "invalid-recipe")
|
||||||
|
os.makedirs(invalid_recipe_dir, exist_ok=True)
|
||||||
|
invalid_json_path = os.path.join(invalid_recipe_dir, "invalid-recipe.json")
|
||||||
|
try:
|
||||||
|
with open(invalid_json_path, "w"):
|
||||||
|
pass # write nothing to the file, which is invalid JSON
|
||||||
|
except Exception:
|
||||||
|
raise Exception(os.listdir(tmpdir))
|
||||||
|
|
||||||
|
modified_test_data = os.path.join(tmpdir, "modified-test-data.zip")
|
||||||
|
with ZipFile(modified_test_data, "w") as zf:
|
||||||
|
for root, _, files in os.walk(tmpdir):
|
||||||
|
for file in files:
|
||||||
|
file_path = os.path.join(root, file)
|
||||||
|
zf.write(file_path, arcname=os.path.relpath(file_path, tmpdir))
|
||||||
|
|
||||||
|
payload = {
|
||||||
|
"migration_type": SupportedMigrations.mealie_alpha.value,
|
||||||
|
}
|
||||||
|
|
||||||
|
file_payload = {
|
||||||
|
"archive": Path(modified_test_data).read_bytes(),
|
||||||
|
}
|
||||||
|
|
||||||
|
response = api_client.post(
|
||||||
|
api_routes.groups_migrations, data=payload, files=file_payload, headers=unique_user.token
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
report_id = response.json()["id"]
|
||||||
|
|
||||||
|
# Validate Results
|
||||||
|
response = api_client.get(api_routes.groups_reports_item_id(report_id), headers=unique_user.token)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
response_json = response.json()
|
||||||
|
assert response_json["entries"]
|
||||||
|
|
||||||
|
failed_item = None
|
||||||
|
failed_item_count = 0
|
||||||
|
for item in response_json["entries"]:
|
||||||
|
if item["success"]:
|
||||||
|
continue
|
||||||
|
|
||||||
|
failed_item = item
|
||||||
|
failed_item_count += 1
|
||||||
|
|
||||||
|
assert failed_item
|
||||||
|
assert failed_item_count == 1
|
||||||
|
|
||||||
|
report_entry = ReportEntryOut.model_validate(failed_item)
|
||||||
|
assert report_entry.message == "Failed to import invalid-recipe.json"
|
||||||
|
assert report_entry.exception == "JSONDecodeError: Expecting value: line 1 column 1 (char 0)"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user