summaryrefslogtreecommitdiff
path: root/internal/store/sqlite_test.go
diff options
context:
space:
mode:
authorPeter Stone <thepeterstone@gmail.com>2026-01-23 21:37:18 -1000
committerPeter Stone <thepeterstone@gmail.com>2026-01-23 21:37:18 -1000
commit465093343ddd398ce5f6377fc9c472d8251c618b (patch)
treed333a2f1c8879f7b114817e929c95e9fcf5f4c3b /internal/store/sqlite_test.go
parente23c85577cbb0eac8b847dd989072698ff4e7a30 (diff)
Refactor: reduce code duplication with shared abstractions
- Add BaseClient HTTP abstraction (internal/api/http.go) to eliminate duplicated HTTP boilerplate across Todoist, Trello, and PlanToEat clients - Add response helpers (internal/handlers/response.go) for JSON/HTML responses - Add generic cache wrapper (internal/handlers/cache.go) using Go generics - Consolidate HandleCompleteAtom/HandleUncompleteAtom into handleAtomToggle - Merge TabsHandler into Handler, delete tabs.go - Extract sortTasksByUrgency and filterAndSortTrelloTasks helpers - Update tests to work with new BaseClient structure Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Diffstat (limited to 'internal/store/sqlite_test.go')
-rw-r--r--internal/store/sqlite_test.go139
1 files changed, 139 insertions, 0 deletions
diff --git a/internal/store/sqlite_test.go b/internal/store/sqlite_test.go
index c09f3ca..7027cbe 100644
--- a/internal/store/sqlite_test.go
+++ b/internal/store/sqlite_test.go
@@ -89,6 +89,39 @@ func setupTestStoreWithCards(t *testing.T) *Store {
return store
}
+// 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
+}
+
// TestDeleteTask verifies that DeleteTask removes a task from the cache
func TestDeleteTask(t *testing.T) {
store := setupTestStoreWithTasks(t)
@@ -430,3 +463,109 @@ func TestSaveAndGetBoards_ManyBoards(t *testing.T) {
t.Errorf("Expected %d total cards, got %d", expectedTotal, totalCards)
}
}
+
+func TestGetTasksByDateRange(t *testing.T) {
+ store := setupTestStoreWithTasks(t)
+ defer store.Close()
+
+ now := time.Now()
+ tomorrow := now.Add(24 * time.Hour)
+ nextWeek := now.Add(7 * 24 * time.Hour)
+
+ tasks := []models.Task{
+ {ID: "1", Content: "Task 1", DueDate: &now},
+ {ID: "2", Content: "Task 2", DueDate: &tomorrow},
+ {ID: "3", Content: "Task 3", DueDate: &nextWeek},
+ }
+
+ if err := store.SaveTasks(tasks); err != nil {
+ t.Fatalf("Failed to save tasks: %v", err)
+ }
+
+ // Test range covering today and tomorrow
+ start := now.Add(-1 * time.Hour)
+ end := tomorrow.Add(1 * time.Hour)
+
+ results, err := store.GetTasksByDateRange(start, end)
+ if err != nil {
+ t.Fatalf("GetTasksByDateRange failed: %v", err)
+ }
+
+ if len(results) != 2 {
+ t.Errorf("Expected 2 tasks, got %d", len(results))
+ }
+}
+
+func TestGetMealsByDateRange(t *testing.T) {
+ store := setupTestStoreWithMeals(t)
+ defer 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)
+ }
+}
+
+func TestGetCardsByDateRange(t *testing.T) {
+ store := setupTestStoreWithCards(t)
+ defer store.Close()
+
+ now := time.Now()
+ tomorrow := now.Add(24 * time.Hour)
+
+ // Create board
+ _, err := store.db.Exec(`INSERT INTO boards (id, name) VALUES (?, ?)`, "board1", "Test Board")
+ if err != nil {
+ t.Fatalf("Failed to create board: %v", err)
+ }
+
+ // Create cards
+ _, err = store.db.Exec(`
+ INSERT INTO cards (id, name, board_id, due_date)
+ VALUES
+ (?, ?, ?, ?),
+ (?, ?, ?, ?)
+ `,
+ "card1", "Card 1", "board1", now,
+ "card2", "Card 2", "board1", tomorrow)
+ if err != nil {
+ t.Fatalf("Failed to insert cards: %v", err)
+ }
+
+ start := now.Add(-1 * time.Hour)
+ end := now.Add(1 * time.Hour)
+
+ results, err := store.GetCardsByDateRange(start, end)
+ if err != nil {
+ t.Fatalf("GetCardsByDateRange failed: %v", err)
+ }
+
+ if len(results) != 1 {
+ t.Errorf("Expected 1 card, got %d", len(results))
+ }
+ if results[0].ID != "card1" {
+ t.Errorf("Expected card1, got %s", results[0].ID)
+ }
+}