mirror of
https://github.com/sstent/foodplanner.git
synced 2025-12-06 08:01:47 +00:00
tryiong to fix the details page
This commit is contained in:
@@ -5,8 +5,18 @@ from sqlalchemy.orm import sessionmaker
|
||||
from main import app, get_db, Base, Food, Meal, MealFood, Plan, Template, TemplateMeal
|
||||
from datetime import date, timedelta
|
||||
|
||||
# Setup test database
|
||||
SQLALCHEMY_DATABASE_URL = "sqlite:///./test_detailed.db"
|
||||
# Setup test database to match Docker environment
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
# Create test database directory if it doesn't exist
|
||||
test_db_dir = "/app/data"
|
||||
os.makedirs(test_db_dir, exist_ok=True)
|
||||
|
||||
# Use the same database path as Docker container
|
||||
SQLALCHEMY_DATABASE_URL = "sqlite:////app/data/test_detailed.db"
|
||||
print(f"Using test database at: {SQLALCHEMY_DATABASE_URL}")
|
||||
|
||||
test_engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})
|
||||
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=test_engine)
|
||||
|
||||
@@ -34,6 +44,37 @@ def test_detailed_page_no_params(client):
|
||||
assert response.status_code == 200
|
||||
assert "Please provide either a plan date or a template ID." in response.text
|
||||
|
||||
|
||||
def test_detailed_page_default_date(client, session):
|
||||
# Create mock data for today
|
||||
food = Food(name="Apple", serving_size="100", serving_unit="g", calories=52, protein=0.3, carbs=14, fat=0.2)
|
||||
session.add(food)
|
||||
session.commit()
|
||||
session.refresh(food)
|
||||
|
||||
meal = Meal(name="Fruit Snack", meal_type="snack", meal_time="Snack")
|
||||
session.add(meal)
|
||||
session.commit()
|
||||
session.refresh(meal)
|
||||
|
||||
meal_food = MealFood(meal_id=meal.id, food_id=food.id, quantity=1.0)
|
||||
session.add(meal_food)
|
||||
session.commit()
|
||||
|
||||
test_date = date.today()
|
||||
plan = Plan(person="Sarah", date=test_date, meal_id=meal.id, meal_time="Snack")
|
||||
session.add(plan)
|
||||
session.commit()
|
||||
|
||||
# Test that when no plan_date is provided, today's date is used by default
|
||||
response = client.get("/detailed?person=Sarah")
|
||||
assert response.status_code == 200
|
||||
# The apostrophe is HTML-escaped in the template
|
||||
assert "Sarah's Detailed Plan for" in response.text
|
||||
assert test_date.strftime('%B %d, %Y') in response.text # Check if today's date appears in the formatted date
|
||||
assert "Fruit Snack" in response.text
|
||||
|
||||
|
||||
def test_detailed_page_with_plan_date(client, session):
|
||||
# Create mock data
|
||||
food = Food(name="Apple", serving_size="100", serving_unit="g", calories=52, protein=0.3, carbs=14, fat=0.2)
|
||||
@@ -57,9 +98,11 @@ def test_detailed_page_with_plan_date(client, session):
|
||||
|
||||
response = client.get(f"/detailed?person=Sarah&plan_date={test_date.isoformat()}")
|
||||
assert response.status_code == 200
|
||||
assert "Sarah's Detailed Plan for" in response.text
|
||||
# The apostrophe is HTML-escaped in the template
|
||||
assert "Sarah's Detailed Plan for" in response.text
|
||||
assert "Fruit Snack" in response.text
|
||||
|
||||
|
||||
def test_detailed_page_with_template_id(client, session):
|
||||
# Create mock data
|
||||
food = Food(name="Banana", serving_size="100", serving_unit="g", calories=89, protein=1.1, carbs=23, fat=0.3)
|
||||
@@ -90,14 +133,41 @@ def test_detailed_page_with_template_id(client, session):
|
||||
assert "Morning Boost Template" in response.text
|
||||
assert "Banana Smoothie" in response.text
|
||||
|
||||
|
||||
def test_detailed_page_with_invalid_plan_date(client):
|
||||
invalid_date = date.today() + timedelta(days=100) # A date far in the future
|
||||
response = client.get(f"/detailed?person=Sarah&plan_date={invalid_date.isoformat()}")
|
||||
assert response.status_code == 200
|
||||
assert "Sarah's Detailed Plan for" in response.text
|
||||
# The apostrophe is HTML-escaped in the template
|
||||
assert "Sarah's Detailed Plan for" in response.text
|
||||
assert "No meals planned for this day." in response.text
|
||||
|
||||
|
||||
def test_detailed_page_with_invalid_template_id(client):
|
||||
response = client.get(f"/detailed?template_id=99999")
|
||||
assert response.status_code == 200
|
||||
assert "Template Not Found" in response.text
|
||||
|
||||
|
||||
def test_detailed_page_template_dropdown(client, session):
|
||||
# Create multiple templates
|
||||
template1 = Template(name="Morning Boost")
|
||||
template2 = Template(name="Evening Energy")
|
||||
session.add(template1)
|
||||
session.add(template2)
|
||||
session.commit()
|
||||
session.refresh(template1)
|
||||
session.refresh(template2)
|
||||
|
||||
# Test that the template dropdown shows available templates
|
||||
response = client.get("/detailed")
|
||||
assert response.status_code == 200
|
||||
|
||||
# Check that the response contains template selection UI elements
|
||||
assert "Select Template..." in response.text
|
||||
assert "Morning Boost" in response.text
|
||||
assert "Evening Energy" in response.text
|
||||
|
||||
# Verify that template IDs are present in the dropdown options
|
||||
assert f'value="{template1.id}"' in response.text
|
||||
assert f'value="{template2.id}"' in response.text
|
||||
|
||||
Reference in New Issue
Block a user