package store import ( "database/sql" "path/filepath" "testing" "time" _ "github.com/mattn/go-sqlite3" "task-dashboard/internal/models" ) // setupTestStoreWithMeals creates a test store with meals table func setupTestStoreWithMeals(t *testing.T) *Store { t.Helper() tempDir := t.TempDir() dbPath := filepath.Join(tempDir, "test.db") db, err := sql.Open("sqlite3", dbPath) if err != nil { t.Fatalf("Failed to open test database: %v", err) } db.SetMaxOpenConns(1) store := &Store{db: db} schema := ` CREATE TABLE IF NOT EXISTS meals ( id TEXT PRIMARY KEY, recipe_name TEXT NOT NULL, date DATETIME, meal_type TEXT, recipe_url TEXT, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ); ` if _, err := db.Exec(schema); err != nil { t.Fatalf("Failed to create schema: %v", err) } return store } func TestGetMealsByDateRange(t *testing.T) { store := setupTestStoreWithMeals(t) defer func() { _ = store.Close() }() now := time.Now() tomorrow := now.Add(24 * time.Hour) meals := []models.Meal{ {ID: "1", RecipeName: "Meal 1", Date: now, MealType: "lunch"}, {ID: "2", RecipeName: "Meal 2", Date: tomorrow, MealType: "dinner"}, } if err := store.SaveMeals(meals); err != nil { t.Fatalf("Failed to save meals: %v", err) } start := now.Add(-1 * time.Hour) end := now.Add(1 * time.Hour) results, err := store.GetMealsByDateRange(start, end) if err != nil { t.Fatalf("GetMealsByDateRange failed: %v", err) } if len(results) != 1 { t.Errorf("Expected 1 meal, got %d", len(results)) } if results[0].ID != "1" { t.Errorf("Expected meal 1, got %s", results[0].ID) } }