1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-05 13:35:23 +02:00

fix: Make Mealie Timezone-Aware (#3847)

Co-authored-by: boc-the-git <3479092+boc-the-git@users.noreply.github.com>
This commit is contained in:
Michael Genson 2024-07-08 16:12:20 -05:00 committed by GitHub
parent 17f9eef551
commit d5f7a883df
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
69 changed files with 250 additions and 176 deletions

View file

@ -1,4 +1,4 @@
from datetime import date, datetime, timedelta
from datetime import datetime, timedelta, timezone
from fastapi.testclient import TestClient
from pydantic import UUID4
@ -34,8 +34,10 @@ def test_new_mealplan_event(api_client: TestClient, unique_user: TestUser):
response_json = response.json()
initial_event_count = len(response_json["items"])
new_plan = CreatePlanEntry(date=date.today(), entry_type="dinner", recipe_id=recipe_id).model_dump(by_alias=True)
new_plan["date"] = date.today().isoformat()
new_plan = CreatePlanEntry(
date=datetime.now(timezone.utc).date(), entry_type="dinner", recipe_id=recipe_id
).model_dump(by_alias=True)
new_plan["date"] = datetime.now(timezone.utc).date().isoformat()
new_plan["recipeId"] = str(recipe_id)
response = api_client.post(api_routes.groups_mealplans, json=new_plan, headers=unique_user.token)
@ -63,7 +65,7 @@ def test_new_mealplan_event(api_client: TestClient, unique_user: TestUser):
response = api_client.get(api_routes.recipes_slug(recipe_name), headers=unique_user.token)
new_recipe_data: dict = response.json()
recipe = RecipeSummary.model_validate(new_recipe_data)
assert recipe.last_made.date() == date.today() # type: ignore
assert recipe.last_made.date() == datetime.now(timezone.utc).date() # type: ignore
# make sure nothing else was updated
for data in [original_recipe_data, new_recipe_data]:
@ -99,8 +101,10 @@ def test_new_mealplan_event_duplicates(api_client: TestClient, unique_user: Test
response_json = response.json()
initial_event_count = len(response_json["items"])
new_plan = CreatePlanEntry(date=date.today(), entry_type="dinner", recipe_id=recipe_id).model_dump(by_alias=True)
new_plan["date"] = date.today().isoformat()
new_plan = CreatePlanEntry(
date=datetime.now(timezone.utc).date(), entry_type="dinner", recipe_id=recipe_id
).model_dump(by_alias=True)
new_plan["date"] = datetime.now(timezone.utc).date().isoformat()
new_plan["recipeId"] = str(recipe_id)
response = api_client.post(api_routes.groups_mealplans, json=new_plan, headers=unique_user.token)
@ -143,10 +147,10 @@ def test_new_mealplan_events_with_multiple_recipes(api_client: TestClient, uniqu
for recipe in recipes:
mealplan_count_by_recipe_id[recipe.id] = 0 # type: ignore
for _ in range(random_int(1, 5)):
new_plan = CreatePlanEntry(date=date.today(), entry_type="dinner", recipe_id=str(recipe.id)).model_dump(
by_alias=True
)
new_plan["date"] = date.today().isoformat()
new_plan = CreatePlanEntry(
date=datetime.now(timezone.utc).date(), entry_type="dinner", recipe_id=str(recipe.id)
).model_dump(by_alias=True)
new_plan["date"] = datetime.now(timezone.utc).date().isoformat()
new_plan["recipeId"] = str(recipe.id)
response = api_client.post(api_routes.groups_mealplans, json=new_plan, headers=unique_user.token)
@ -196,15 +200,17 @@ def test_preserve_future_made_date(api_client: TestClient, unique_user: TestUser
recipe = RecipeSummary.model_validate(response.json())
recipe_id = str(recipe.id)
future_dt = datetime.now() + timedelta(days=random_int(1, 10))
future_dt = datetime.now(timezone.utc) + timedelta(days=random_int(1, 10))
recipe.last_made = future_dt
response = api_client.put(
api_routes.recipes_slug(recipe.slug), json=utils.jsonify(recipe), headers=unique_user.token
)
assert response.status_code == 200
new_plan = CreatePlanEntry(date=date.today(), entry_type="dinner", recipe_id=recipe_id).model_dump(by_alias=True)
new_plan["date"] = date.today().isoformat()
new_plan = CreatePlanEntry(
date=datetime.now(timezone.utc).date(), entry_type="dinner", recipe_id=recipe_id
).model_dump(by_alias=True)
new_plan["date"] = datetime.now(timezone.utc).date().isoformat()
new_plan["recipeId"] = str(recipe_id)
response = api_client.post(api_routes.groups_mealplans, json=new_plan, headers=unique_user.token)

View file

@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone
from mealie.repos.repository_factory import AllRepositories
from mealie.schema.group.group_shopping_list import ShoppingListItemCreate, ShoppingListItemOut, ShoppingListSave
@ -40,7 +40,7 @@ def test_cleanup(database: AllRepositories, unique_user: TestUser):
for item in unchecked_items + checked_items:
assert item in shopping_list.list_items
checked_items.sort(key=lambda x: x.update_at or datetime.now(), reverse=True)
checked_items.sort(key=lambda x: x.update_at or datetime.now(timezone.utc), reverse=True)
expected_kept_items = unchecked_items + checked_items[:MAX_CHECKED_ITEMS]
expected_deleted_items = checked_items[MAX_CHECKED_ITEMS:]

View file

@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from pydantic import UUID4
@ -23,7 +23,7 @@ def webhook_factory(
name=name or random_string(),
url=url or random_string(),
webhook_type=webhook_type,
scheduled_time=scheduled_time.time() if scheduled_time else datetime.now().time(),
scheduled_time=scheduled_time.time() if scheduled_time else datetime.now(timezone.utc).time(),
group_id=group_id,
)
@ -35,7 +35,7 @@ def test_get_scheduled_webhooks_filter_query(database: AllRepositories, unique_u
expected: list[SaveWebhook] = []
start = datetime.now()
start = datetime.now(timezone.utc)
for _ in range(5):
new_item = webhook_factory(group_id=unique_user.group_id, enabled=random_bool())
@ -52,7 +52,7 @@ def test_get_scheduled_webhooks_filter_query(database: AllRepositories, unique_u
expected.append(new_item)
event_bus_listener = WebhookEventListener(unique_user.group_id) # type: ignore
results = event_bus_listener.get_scheduled_webhooks(start, datetime.now() + timedelta(minutes=5))
results = event_bus_listener.get_scheduled_webhooks(start, datetime.now(timezone.utc) + timedelta(minutes=5))
assert len(results) == len(expected)