1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
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)
}
}
|