Skip to content

Commit 3f3f1b9

Browse files
authored
feat: add balance for envelopes (#104)
Resolves #40.
1 parent 0347d9c commit 3f3f1b9

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

internal/controllers/envelope.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,19 @@ func GetEnvelope(c *gin.Context) {
8787
return
8888
}
8989

90+
var allocation models.Allocation
91+
models.DB.First(&allocation, &models.Allocation{
92+
Month: uint8(month.Month.UTC().Month()),
93+
Year: uint(month.Month.UTC().Year()),
94+
})
95+
96+
balance := allocation.Amount.Add(spent)
97+
9098
c.JSON(http.StatusOK, gin.H{
9199
"data": map[string]interface{}{
92-
"spent": spent,
93-
"month": month.Month,
100+
"spent": spent,
101+
"balance": balance,
102+
"month": month.Month,
94103
},
95104
})
96105
return

internal/controllers/envelope_test.go

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ type EnvelopeDetailResponse struct {
2525
type EnvelopeMonthResponse struct {
2626
test.APIResponse
2727
Data struct {
28-
Month time.Time `json:"month"`
29-
Spent decimal.Decimal `json:"spent"`
28+
Month time.Time `json:"month"`
29+
Spent decimal.Decimal `json:"spent"`
30+
Balance decimal.Decimal `json:"balance"`
3031
}
3132
}
3233

@@ -141,29 +142,44 @@ func TestGetEnvelope(t *testing.T) {
141142
assert.Equal(t, dbEnvelope, envelope.Data)
142143
}
143144

145+
// TestEnvelopeMonth verifies that the monthly calculations are correct.
144146
func TestEnvelopeMonth(t *testing.T) {
145147
var envelopeMonth EnvelopeMonthResponse
146148

147-
r := test.Request(t, "GET", "/v1/budgets/1/categories/1/envelopes/1?month=2022-01", "")
148-
test.AssertHTTPStatus(t, http.StatusOK, &r)
149-
spent := decimal.NewFromFloat(-10)
150-
test.DecodeResponse(t, &r, &envelopeMonth)
151-
assert.True(t, envelopeMonth.Data.Spent.Equal(spent), "Month calculation for 2022-01 is wrong: should be %v, but is %v", spent, envelopeMonth.Data.Spent)
149+
tests := []struct {
150+
path string
151+
spent decimal.Decimal
152+
balance decimal.Decimal
153+
}{
154+
{
155+
"/v1/budgets/1/categories/1/envelopes/1?month=2022-01",
156+
decimal.NewFromFloat(-10),
157+
decimal.NewFromFloat(10.99),
158+
},
159+
{
160+
"/v1/budgets/1/categories/1/envelopes/1?month=2022-02",
161+
decimal.NewFromFloat(-5),
162+
decimal.NewFromFloat(42.12),
163+
},
164+
{
165+
"/v1/budgets/1/categories/1/envelopes/1?month=2022-03",
166+
decimal.NewFromFloat(-15),
167+
decimal.NewFromFloat(16.17),
168+
},
169+
}
152170

153-
r = test.Request(t, "GET", "/v1/budgets/1/categories/1/envelopes/1?month=2022-02", "")
154-
test.AssertHTTPStatus(t, http.StatusOK, &r)
155-
spent = decimal.NewFromFloat(-5)
156-
test.DecodeResponse(t, &r, &envelopeMonth)
157-
assert.True(t, envelopeMonth.Data.Spent.Equal(spent), "Month calculation for 2022-02 is wrong: should be %v, but is %v", spent, envelopeMonth.Data.Spent)
171+
for _, tt := range tests {
172+
r := test.Request(t, "GET", tt.path, "")
173+
test.AssertHTTPStatus(t, http.StatusOK, &r)
158174

159-
r = test.Request(t, "GET", "/v1/budgets/1/categories/1/envelopes/1?month=2022-03", "")
160-
test.AssertHTTPStatus(t, http.StatusOK, &r)
161-
spent = decimal.NewFromFloat(-15)
162-
test.DecodeResponse(t, &r, &envelopeMonth)
163-
assert.True(t, envelopeMonth.Data.Spent.Equal(spent), "Month calculation for 2022-03 is wrong: should be %v, but is %v", spent, envelopeMonth.Data.Spent)
175+
test.DecodeResponse(t, &r, &envelopeMonth)
176+
assert.True(t, envelopeMonth.Data.Spent.Equal(tt.spent), "Monthly spent calculation for 2022-01 is wrong: should be %v, but is %v", tt.spent, envelopeMonth.Data.Spent)
177+
}
178+
}
164179

180+
func TestEnvelopeMonthInvalid(t *testing.T) {
165181
// Test that non-parseable requests produce an error
166-
r = test.Request(t, "GET", "/v1/budgets/1/categories/1/envelopes/1?month=Stonks!", "")
182+
r := test.Request(t, "GET", "/v1/budgets/1/categories/1/envelopes/1?month=Stonks!", "")
167183
test.AssertHTTPStatus(t, http.StatusBadRequest, &r)
168184
}
169185

0 commit comments

Comments
 (0)