summaryrefslogtreecommitdiff
path: root/internal/store/sqlite_test.go
blob: 6bf778376eeabefd1a706e97f7ffe98861b4feb0 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package store

import (
	"database/sql"
	"path/filepath"
	"testing"
	"time"

	_ "github.com/mattn/go-sqlite3"
	"task-dashboard/internal/models"
)

// setupTestStoreWithTasks creates a test store with tasks table
func setupTestStoreWithTasks(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 tasks (
			id TEXT PRIMARY KEY,
			content TEXT NOT NULL,
			description TEXT,
			project_id TEXT,
			project_name TEXT,
			due_date DATETIME,
			priority INTEGER DEFAULT 1,
			completed BOOLEAN DEFAULT FALSE,
			labels TEXT,
			url TEXT,
			created_at DATETIME,
			updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
		);
	`
	if _, err := db.Exec(schema); err != nil {
		t.Fatalf("Failed to create schema: %v", err)
	}

	return store
}

// setupTestStoreWithCards creates a test store with boards and cards tables
func setupTestStoreWithCards(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 boards (
			id TEXT PRIMARY KEY,
			name TEXT NOT NULL,
			updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
		);
		CREATE TABLE IF NOT EXISTS cards (
			id TEXT PRIMARY KEY,
			name TEXT NOT NULL,
			board_id TEXT NOT NULL,
			list_id TEXT,
			list_name TEXT,
			due_date DATETIME,
			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)
	defer store.Close()

	// Save some tasks
	tasks := []models.Task{
		{
			ID:          "task1",
			Content:     "Task 1",
			Description: "Description 1",
			Priority:    1,
			Completed:   false,
			Labels:      []string{},
			CreatedAt:   time.Now(),
		},
		{
			ID:          "task2",
			Content:     "Task 2",
			Description: "Description 2",
			Priority:    2,
			Completed:   false,
			Labels:      []string{},
			CreatedAt:   time.Now(),
		},
		{
			ID:          "task3",
			Content:     "Task 3",
			Description: "Description 3",
			Priority:    3,
			Completed:   false,
			Labels:      []string{},
			CreatedAt:   time.Now(),
		},
	}

	if err := store.SaveTasks(tasks); err != nil {
		t.Fatalf("Failed to save tasks: %v", err)
	}

	// Verify all 3 tasks exist
	result, err := store.GetTasks()
	if err != nil {
		t.Fatalf("Failed to get tasks: %v", err)
	}
	if len(result) != 3 {
		t.Fatalf("Expected 3 tasks, got %d", len(result))
	}

	// Delete task2
	if err := store.DeleteTask("task2"); err != nil {
		t.Fatalf("DeleteTask failed: %v", err)
	}

	// Verify only 2 tasks remain
	result, err = store.GetTasks()
	if err != nil {
		t.Fatalf("Failed to get tasks after delete: %v", err)
	}
	if len(result) != 2 {
		t.Errorf("Expected 2 tasks after delete, got %d", len(result))
	}

	// Verify task2 is gone
	for _, task := range result {
		if task.ID == "task2" {
			t.Errorf("task2 should have been deleted but was found")
		}
	}

	// Verify task1 and task3 still exist
	foundTask1, foundTask3 := false, false
	for _, task := range result {
		if task.ID == "task1" {
			foundTask1 = true
		}
		if task.ID == "task3" {
			foundTask3 = true
		}
	}
	if !foundTask1 {
		t.Error("task1 should still exist")
	}
	if !foundTask3 {
		t.Error("task3 should still exist")
	}
}

// TestDeleteTask_NonExistent verifies that deleting a non-existent task doesn't error
func TestDeleteTask_NonExistent(t *testing.T) {
	store := setupTestStoreWithTasks(t)
	defer store.Close()

	// Delete a task that doesn't exist - should not error
	err := store.DeleteTask("nonexistent")
	if err != nil {
		t.Errorf("DeleteTask on non-existent task should not error, got: %v", err)
	}
}

// TestDeleteCard verifies that DeleteCard removes a card from the cache
func TestDeleteCard(t *testing.T) {
	store := setupTestStoreWithCards(t)
	defer store.Close()

	// First create a 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)
	}

	// Insert cards directly
	cards := []struct {
		id      string
		name    string
		boardID string
	}{
		{"card1", "Card 1", "board1"},
		{"card2", "Card 2", "board1"},
		{"card3", "Card 3", "board1"},
	}

	for _, card := range cards {
		_, err := store.db.Exec(
			`INSERT INTO cards (id, name, board_id, list_id, list_name) VALUES (?, ?, ?, ?, ?)`,
			card.id, card.name, card.boardID, "list1", "To Do",
		)
		if err != nil {
			t.Fatalf("Failed to insert card: %v", err)
		}
	}

	// Verify all 3 cards exist
	var count int
	err = store.db.QueryRow(`SELECT COUNT(*) FROM cards`).Scan(&count)
	if err != nil {
		t.Fatalf("Failed to count cards: %v", err)
	}
	if count != 3 {
		t.Fatalf("Expected 3 cards, got %d", count)
	}

	// Delete card2
	if err := store.DeleteCard("card2"); err != nil {
		t.Fatalf("DeleteCard failed: %v", err)
	}

	// Verify only 2 cards remain
	err = store.db.QueryRow(`SELECT COUNT(*) FROM cards`).Scan(&count)
	if err != nil {
		t.Fatalf("Failed to count cards after delete: %v", err)
	}
	if count != 2 {
		t.Errorf("Expected 2 cards after delete, got %d", count)
	}

	// Verify card2 is gone
	var exists int
	err = store.db.QueryRow(`SELECT COUNT(*) FROM cards WHERE id = ?`, "card2").Scan(&exists)
	if err != nil {
		t.Fatalf("Failed to check card2: %v", err)
	}
	if exists != 0 {
		t.Errorf("card2 should have been deleted")
	}
}

// TestDeleteCard_NonExistent verifies that deleting a non-existent card doesn't error
func TestDeleteCard_NonExistent(t *testing.T) {
	store := setupTestStoreWithCards(t)
	defer store.Close()

	// Delete a card that doesn't exist - should not error
	err := store.DeleteCard("nonexistent")
	if err != nil {
		t.Errorf("DeleteCard on non-existent card should not error, got: %v", err)
	}
}