-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
109 lines (93 loc) · 2.89 KB
/
main_test.go
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
package main
import (
"testing"
"os"
)
func TestHandleAddCommand(t *testing.T) {
db := setupTestDB(t)
defer teardownTestDB(db)
tests := []struct {
name string
args []string
expectError bool
}{
{"Valid Task", []string{"--name=Task1", "--estimate=3"}, false},
{"Missing Name", []string{"--estimate=3"}, true},
{"Empty Args", []string{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := handleAddCommand(db, tt.args)
if tt.expectError {
if err == nil {
t.Errorf("Expected error, got nil")
}
} else {
if err != nil {
t.Errorf("Did not expect error, got %v", err)
}
// Verify that the task was added to the database
if !tt.expectError {
var count int
err = db.QueryRow(`SELECT COUNT(*) FROM tasks WHERE name = ?`, "Task1").Scan(&count)
if err != nil {
t.Fatalf("Failed to query database: %v", err)
}
if count != 1 {
t.Errorf("Expected 1 task to be added, got %d", count)
}
}
}
})
}
}
func TestHandleLoadTasksCommand(t *testing.T) {
db := setupTestDB(t)
defer teardownTestDB(db)
// Create a temporary file with test data
fileContent := "Task1,3\nTask2,2\nTask3,1"
tmpfile, err := os.CreateTemp("", "tasks_test_*.txt")
if err != nil {
t.Fatalf("Failed to create temp file: %v", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.Write([]byte(fileContent)); err != nil {
t.Fatalf("Failed to write to temp file: %v", err)
}
if err := tmpfile.Close(); err != nil {
t.Fatalf("Failed to close temp file: %v", err)
}
// Run the handleLoadTasksCommand function
err = handleLoadTasksCommand(db, []string{"--file", tmpfile.Name()})
if err != nil {
t.Errorf("handleLoadTasksCommand returned an error: %v", err)
}
// Verify that the tasks were added to the database
var count int
err = db.QueryRow(`SELECT COUNT(*) FROM tasks`).Scan(&count)
if err != nil {
t.Fatalf("Failed to query database: %v", err)
}
if count != 3 {
t.Errorf("Expected 3 tasks to be loaded, got %d", count)
}
// Verify that specific tasks were added
tasks := []struct {
name string
estimate int
}{
{"Task1", 3},
{"Task2", 2},
{"Task3", 1},
}
for _, task := range tasks {
var estimate int
err = db.QueryRow(`SELECT estimate FROM tasks WHERE name = ?`, task.name).Scan(&estimate)
if err != nil {
t.Fatalf("Failed to query database for task %s: %v", task.name, err)
}
if estimate != task.estimate {
t.Errorf("Expected estimate for task %s to be %d, got %d", task.name, task.estimate, estimate)
}
}
}