Skip to content

Commit d29a212

Browse files
committed
feat: add Envelope ID, Envelope name and month to month calculation
1 parent a326921 commit d29a212

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

internal/controllers/envelope_test.go

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,28 +143,41 @@ func TestEnvelopeMonth(t *testing.T) {
143143
var envelopeMonth EnvelopeMonthResponse
144144

145145
tests := []struct {
146-
path string
147-
spent decimal.Decimal
148-
balance decimal.Decimal
149-
allocation decimal.Decimal
146+
path string
147+
envelopeMonth models.EnvelopeMonth
150148
}{
151149
{
152150
"/v1/budgets/1/categories/1/envelopes/1?month=2022-01",
153-
decimal.NewFromFloat(-10),
154-
decimal.NewFromFloat(10.99),
155-
decimal.NewFromFloat(20.99),
151+
models.EnvelopeMonth{
152+
ID: 1,
153+
Name: "Utilities",
154+
Month: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
155+
Spent: decimal.NewFromFloat(-10),
156+
Balance: decimal.NewFromFloat(10.99),
157+
Allocation: decimal.NewFromFloat(20.99),
158+
},
156159
},
157160
{
158161
"/v1/budgets/1/categories/1/envelopes/1?month=2022-02",
159-
decimal.NewFromFloat(-5),
160-
decimal.NewFromFloat(42.12),
161-
decimal.NewFromFloat(47.12),
162+
models.EnvelopeMonth{
163+
ID: 1,
164+
Name: "Utilities",
165+
Month: time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC),
166+
Spent: decimal.NewFromFloat(-5),
167+
Balance: decimal.NewFromFloat(42.12),
168+
Allocation: decimal.NewFromFloat(47.12),
169+
},
162170
},
163171
{
164172
"/v1/budgets/1/categories/1/envelopes/1?month=2022-03",
165-
decimal.NewFromFloat(-15),
166-
decimal.NewFromFloat(16.17),
167-
decimal.NewFromFloat(31.17),
173+
models.EnvelopeMonth{
174+
ID: 1,
175+
Name: "Utilities",
176+
Month: time.Date(2022, 3, 1, 0, 0, 0, 0, time.UTC),
177+
Spent: decimal.NewFromFloat(-15),
178+
Balance: decimal.NewFromFloat(16.17),
179+
Allocation: decimal.NewFromFloat(31.17),
180+
},
168181
},
169182
}
170183

@@ -173,11 +186,12 @@ func TestEnvelopeMonth(t *testing.T) {
173186
test.AssertHTTPStatus(t, http.StatusOK, &r)
174187

175188
test.DecodeResponse(t, &r, &envelopeMonth)
176-
assert.True(t, envelopeMonth.Data.Spent.Equal(tt.spent), "Monthly spent calculation for %v is wrong: should be %v, but is %v: %#v", envelopeMonth.Data.Month, tt.spent, envelopeMonth.Data.Spent, envelopeMonth.Data)
177-
178-
assert.True(t, envelopeMonth.Data.Balance.Equal(tt.balance), "Monthly balance calculation for %v is wrong: should be %v, but is %v: %#v", envelopeMonth.Data.Month, tt.balance, envelopeMonth.Data.Balance, envelopeMonth.Data)
179-
180-
assert.True(t, envelopeMonth.Data.Allocation.Equal(tt.allocation), "Monthly allocation fetch for %v is wrong: should be %v, but is %v: %#v", envelopeMonth.Data.Month, tt.allocation, envelopeMonth.Data.Allocation, envelopeMonth.Data)
189+
assert.Equal(t, envelopeMonth.Data.ID, tt.envelopeMonth.ID)
190+
assert.Equal(t, envelopeMonth.Data.Name, tt.envelopeMonth.Name)
191+
assert.Equal(t, envelopeMonth.Data.Month, tt.envelopeMonth.Month)
192+
assert.True(t, envelopeMonth.Data.Spent.Equal(tt.envelopeMonth.Spent), "Monthly spent calculation for %v is wrong: should be %v, but is %v: %#v", envelopeMonth.Data.Month, tt.envelopeMonth.Spent, envelopeMonth.Data.Spent, envelopeMonth.Data)
193+
assert.True(t, envelopeMonth.Data.Balance.Equal(tt.envelopeMonth.Balance), "Monthly balance calculation for %v is wrong: should be %v, but is %v: %#v", envelopeMonth.Data.Month, tt.envelopeMonth.Balance, envelopeMonth.Data.Balance, envelopeMonth.Data)
194+
assert.True(t, envelopeMonth.Data.Allocation.Equal(tt.envelopeMonth.Allocation), "Monthly allocation fetch for %v is wrong: should be %v, but is %v: %#v", envelopeMonth.Data.Month, tt.envelopeMonth.Allocation, envelopeMonth.Data.Allocation, envelopeMonth.Data)
181195
}
182196
}
183197

internal/models/envelope.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type Envelope struct {
1818

1919
// EnvelopeMonth contains data about an Envelope for a specific month.
2020
type EnvelopeMonth struct {
21+
ID uint64 `json:"id"`
22+
Name string `json:"name"`
2123
Month time.Time `json:"month"`
2224
Spent decimal.Decimal `json:"spent"`
2325
Balance decimal.Decimal `json:"balance"`
@@ -68,7 +70,9 @@ func (e Envelope) Month(t time.Time) EnvelopeMonth {
6870
balance := allocation.Amount.Add(spent)
6971

7072
return EnvelopeMonth{
71-
Month: t,
73+
ID: e.ID,
74+
Name: e.Name,
75+
Month: time.Date(t.UTC().Year(), t.UTC().Month(), 1, 0, 0, 0, 0, time.UTC),
7276
Spent: spent,
7377
Balance: balance,
7478
Allocation: allocation.Amount,

0 commit comments

Comments
 (0)