Skip to content

Commit

Permalink
fix: Purge Group Exports type mismatch (#3314)
Browse files Browse the repository at this point in the history
* cast string to datetime

* added test
  • Loading branch information
michael-genson committed Mar 14, 2024
1 parent f2735ba commit d960947
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions mealie/services/scheduler/tasks/purge_group_exports.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d960947

Please sign in to comment.