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
|
package commands
import (
"context"
"database/sql"
"testing"
"punchcard/internal/queries"
)
func TestAddProjectCommand(t *testing.T) {
tests := []struct {
name string
setupClient bool
clientName string
clientEmail string
args []string
expectedOutput string
expectError bool
}{
{
name: "add project with client name",
setupClient: true,
clientName: "TestCorp",
clientEmail: "test@testcorp.com",
args: []string{"add", "project", "Website Redesign", "-c", "TestCorp"},
expectedOutput: "Created project: Website Redesign for client TestCorp (ID: 1)\n",
expectError: false,
},
{
name: "add project with client ID",
setupClient: true,
clientName: "TechSolutions",
clientEmail: "contact@techsolutions.com",
args: []string{"add", "project", "Mobile App", "-c", "1"},
expectedOutput: "Created project: Mobile App for client TechSolutions (ID: 1)\n",
expectError: false,
},
{
name: "add project with nonexistent client name",
setupClient: false,
args: []string{"add", "project", "Test Project", "-c", "NonexistentClient"},
expectError: true,
},
{
name: "add project with nonexistent client ID",
setupClient: false,
args: []string{"add", "project", "Test Project", "-c", "999"},
expectError: true,
},
{
name: "add project with no arguments",
setupClient: false,
args: []string{"add", "project"},
expectError: true,
},
{
name: "add project with only name",
setupClient: false,
args: []string{"add", "project", "Test Project"},
expectError: true,
},
{
name: "add project with too many arguments",
setupClient: true,
clientName: "TestCorp",
args: []string{"add", "project", "name", "extra", "-c", "TestCorp"},
expectError: true,
},
{
name: "add project with billable rate",
setupClient: true,
clientName: "BillableClient",
clientEmail: "billing@client.com",
args: []string{"add", "project", "Premium Project", "-c", "BillableClient", "-r", "175.25"},
expectedOutput: "Created project: Premium Project for client BillableClient (ID: 1)\n",
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q, cleanup := setupTestDB(t)
defer cleanup()
if tt.setupClient {
_, err := q.CreateClient(context.Background(), queries.CreateClientParams{
Name: tt.clientName,
Email: sql.NullString{String: tt.clientEmail, Valid: tt.clientEmail != ""},
BillableRate: sql.NullInt64{},
})
if err != nil {
t.Fatalf("Failed to setup test client: %v", err)
}
}
output, err := executeCommandWithDB(t, q, tt.args...)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if output != tt.expectedOutput {
t.Errorf("Expected output %q, got %q", tt.expectedOutput, output)
}
})
}
}
func TestAddProjectBillableRateStorage(t *testing.T) {
tests := []struct {
name string
args []string
expectedRate *int64 // nil means NULL in database, values in cents
expectError bool
}{
{
name: "project without billable rate stores NULL",
args: []string{"add", "project", "NoRateProject", "-c", "testclient"},
expectedRate: nil,
expectError: false,
},
{
name: "project with zero billable rate stores NULL",
args: []string{"add", "project", "ZeroRateProject", "-c", "testclient", "--hourly-rate", "0"},
expectedRate: nil,
expectError: false,
},
{
name: "project with billable rate stores value",
args: []string{"add", "project", "RateProject", "-c", "testclient", "--hourly-rate", "225.50"},
expectedRate: func() *int64 { f := int64(22550); return &f }(), // $225.50 = 22550 cents
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
q, cleanup := setupTestDB(t)
defer cleanup()
_, err := q.CreateClient(context.Background(), queries.CreateClientParams{
Name: "testclient",
Email: sql.NullString{},
BillableRate: sql.NullInt64{},
})
if err != nil {
t.Fatalf("Failed to setup test client: %v", err)
}
_, err = executeCommandWithDB(t, q, tt.args...)
if tt.expectError {
if err == nil {
t.Errorf("Expected error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
projects, err := q.FindProject(context.Background(), queries.FindProjectParams{
ID: 1,
Name: "",
})
if err != nil {
t.Fatalf("Failed to query created project: %v", err)
}
if len(projects) != 1 {
t.Fatalf("Expected 1 project, got %d", len(projects))
}
project := projects[0]
if tt.expectedRate == nil {
if project.BillableRate.Valid {
t.Errorf("Expected NULL billable_rate, got %d", project.BillableRate.Int64)
}
} else {
if !project.BillableRate.Valid {
t.Errorf("Expected billable_rate %d, got NULL", *tt.expectedRate)
} else if project.BillableRate.Int64 != *tt.expectedRate {
t.Errorf("Expected billable_rate %d, got %d", *tt.expectedRate, project.BillableRate.Int64)
}
}
})
}
}
|