Skip to content

Commit 701c6e4

Browse files
authored
fix: return empty list, not null if there are no accounts (#88)
1 parent dcbe333 commit 701c6e4

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

internal/controllers/account.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,21 @@ func CreateAccount(c *gin.Context) {
6969

7070
// GetAccounts retrieves all accounts.
7171
func GetAccounts(c *gin.Context) {
72-
var accounts, apiResponses []models.Account
72+
var accounts []models.Account
7373

7474
models.DB.Where("budget_id = ?", c.Param("budgetId")).Find(&accounts)
7575

76-
for _, account := range accounts {
76+
for i, account := range accounts {
7777
response, err := account.WithCalculations()
7878
if err != nil {
7979
FetchErrorHandler(c, fmt.Errorf("could not get values for account %v: %v", account.Name, err))
8080
return
8181
}
8282

83-
apiResponses = append(apiResponses, *response)
83+
accounts[i] = *response
8484
}
8585

86-
c.JSON(http.StatusOK, gin.H{"data": apiResponses})
86+
c.JSON(http.StatusOK, gin.H{"data": accounts})
8787
}
8888

8989
// GetAccount retrieves an account by its ID.

internal/controllers/account_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,35 @@ func TestUpdateNonExistingAccount(t *testing.T) {
196196
test.AssertHTTPStatus(t, http.StatusNotFound, &recorder)
197197
}
198198

199-
func TestDeleteAccount(t *testing.T) {
199+
func TestDeleteAccountsAndEmptyList(t *testing.T) {
200200
recorder := test.Request(t, "DELETE", "/v1/budgets/1/accounts/1", "")
201201
test.AssertHTTPStatus(t, http.StatusNoContent, &recorder)
202+
203+
recorder = test.Request(t, "DELETE", "/v1/budgets/1/accounts/2", "")
204+
test.AssertHTTPStatus(t, http.StatusNoContent, &recorder)
205+
206+
recorder = test.Request(t, "DELETE", "/v1/budgets/1/accounts/3", "")
207+
test.AssertHTTPStatus(t, http.StatusNoContent, &recorder)
208+
209+
recorder = test.Request(t, "DELETE", "/v1/budgets/1/accounts/4", "")
210+
test.AssertHTTPStatus(t, http.StatusNoContent, &recorder)
211+
212+
recorder = test.Request(t, "DELETE", "/v1/budgets/1/accounts/5", "")
213+
test.AssertHTTPStatus(t, http.StatusNoContent, &recorder)
214+
215+
recorder = test.Request(t, "DELETE", "/v1/budgets/1/accounts/6", "")
216+
test.AssertHTTPStatus(t, http.StatusNoContent, &recorder)
217+
218+
recorder = test.Request(t, "GET", "/v1/budgets/1/accounts", "")
219+
var apiResponse AccountListResponse
220+
err := json.NewDecoder(recorder.Body).Decode(&apiResponse)
221+
if err != nil {
222+
assert.Fail(t, "Unable to parse response from server %q into APIListResponse, '%v'", recorder.Body, err)
223+
}
224+
225+
// Verify that the account list is an empty list, not null
226+
assert.NotNil(t, apiResponse.Data)
227+
assert.Empty(t, apiResponse.Data)
202228
}
203229

204230
func TestDeleteNonExistingAccount(t *testing.T) {

0 commit comments

Comments
 (0)