diff options
| author | Peter Stone <thepeterstone@gmail.com> | 2026-07-15 09:41:03 +0000 |
|---|---|---|
| committer | Peter Stone <thepeterstone@gmail.com> | 2026-07-16 02:50:42 +0000 |
| commit | 7d59a3020c9a98bb8af7240f584ac4c7813a46f6 (patch) | |
| tree | b73648ff848593e4e1ddd017e50471478bee9966 /internal/store/meals_test.go | |
| parent | ce1aa62b4b5a3a9af9933e73e416d7ae80142660 (diff) | |
refactor(store): extract meal methods from sqlite.go into meals.go
Pure move: SaveMeals/GetMeals/GetMealsByDateRange and their test
(plus its setupTestStoreWithMeals helper, used only by that test) out
of the 1400+ line sqlite.go into their own file. No behavior change.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VTUSAEKfsPc6WGDq45yPHD
Diffstat (limited to 'internal/store/meals_test.go')
| -rw-r--r-- | internal/store/meals_test.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/internal/store/meals_test.go b/internal/store/meals_test.go new file mode 100644 index 0000000..b72796e --- /dev/null +++ b/internal/store/meals_test.go @@ -0,0 +1,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) + } +} |
