fix: Purge Group Exports type mismatch (#3314)

* cast string to datetime

* added test
This commit is contained in:
Michael Genson 2024-03-14 14:20:59 -05:00 committed by GitHub
parent f2735ba22b
commit d960947258
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 2 deletions

View File

@ -1,7 +1,7 @@
import datetime
from pathlib import Path
from sqlalchemy import select
from sqlalchemy import DateTime, cast, select
from mealie.core import root_logger
from mealie.core.config import get_app_dirs
@ -19,7 +19,7 @@ def purge_group_data_exports(max_minutes_old=ONE_DAY_AS_MINUTES):
limit = datetime.datetime.now() - datetime.timedelta(minutes=max_minutes_old)
with session_context() as session:
stmt = select(GroupDataExportsModel).filter(GroupDataExportsModel.expires <= limit)
stmt = select(GroupDataExportsModel).filter(cast(GroupDataExportsModel.expires, DateTime) <= limit)
results = session.execute(stmt).scalars().all()
total_removed = 0

View File

@ -0,0 +1,37 @@
import tempfile
from pathlib import Path
from mealie.repos.repository_factory import AllRepositories
from mealie.schema.recipe.recipe import Recipe
from mealie.services.recipe.recipe_bulk_service import RecipeBulkActionsService
from mealie.services.scheduler.tasks.purge_group_exports import purge_group_data_exports
from tests.utils.factories import random_int, random_string
from tests.utils.fixture_schemas import TestUser
def test_purge_group_exports(database: AllRepositories, unique_user: TestUser):
# create the export
group = database.groups.get_one(unique_user.group_id)
assert group
user = database.users.get_one(unique_user.user_id)
assert user
recipe_exporter = RecipeBulkActionsService(database, user, group)
recipes = [
database.recipes.create(Recipe(name=random_string(), group_id=group.id)) for _ in range(random_int(2, 5))
]
with tempfile.NamedTemporaryFile() as tmpfile:
recipe_exporter.export_recipes(Path(tmpfile.name), [recipe.slug for recipe in recipes])
exports = recipe_exporter.get_exports()
assert len(exports) == 1
export = exports[0]
export_path = Path(export.path)
assert export_path.exists()
# purge the export and confirm all data is removed
purge_group_data_exports(-525600) # 1 year into the future
assert not export_path.exists()
exports = recipe_exporter.get_exports()
assert not exports