Skip to content

Commit bb8d6db

Browse files
authored
fix: transaction sorting now takes time of day and creation time into account (#834)
1 parent ec988ff commit bb8d6db

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

pkg/controllers/transaction_v1.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func (co Controller) GetTransactions(c *gin.Context) {
247247
}
248248

249249
var query *gorm.DB
250-
query = co.DB.Order("date(date) DESC").Where(&models.Transaction{
250+
query = co.DB.Order("datetime(date) DESC, datetime(created_at) DESC").Where(&models.Transaction{
251251
TransactionCreate: create,
252252
}, queryFields...)
253253

pkg/controllers/transaction_v1_test.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,44 @@ func (suite *TestSuiteStandard) TestOptionsTransaction() {
118118
assert.Equal(suite.T(), http.StatusNoContent, recorder.Code, "Request ID %s", recorder.Header().Get("x-request-id"))
119119
}
120120

121+
// TestGetTransactions verifies that transactions can be read from the API.
122+
// It also acts as a regression test for a bug where transactions were sorted by date(date)
123+
// instead of datetime(date), leading to transactions being correctly sorted by dates, but
124+
// not correctly sorted when multiple transactions occurred on a day. In that case, the
125+
// oldest transaction would be at the bottom and not at the top.
121126
func (suite *TestSuiteStandard) TestGetTransactions() {
122-
_ = suite.createTestTransaction(models.TransactionCreate{Amount: decimal.NewFromFloat(17.23)})
123-
_ = suite.createTestTransaction(models.TransactionCreate{Amount: decimal.NewFromFloat(23.42)})
127+
t1 := suite.createTestTransaction(models.TransactionCreate{
128+
Amount: decimal.NewFromFloat(17.23),
129+
Date: time.Date(2023, 11, 10, 10, 11, 12, 0, time.UTC),
130+
})
131+
132+
_ = suite.createTestTransaction(models.TransactionCreate{
133+
Amount: decimal.NewFromFloat(23.42),
134+
Date: time.Date(2023, 11, 10, 11, 12, 13, 0, time.UTC),
135+
})
136+
137+
// Need to sleep 1 second because SQLite datetime only has second precision
138+
time.Sleep(1 * time.Second)
139+
140+
t3 := suite.createTestTransaction(models.TransactionCreate{
141+
Amount: decimal.NewFromFloat(44.05),
142+
Date: time.Date(2023, 11, 10, 10, 11, 12, 0, time.UTC),
143+
})
124144

125145
recorder := test.Request(suite.controller, suite.T(), http.MethodGet, "http://example.com/v1/transactions", "")
126146

127147
var response controllers.TransactionListResponse
128148
suite.decodeResponse(&recorder, &response)
129149

130150
assert.Equal(suite.T(), 200, recorder.Code)
131-
assert.Len(suite.T(), response.Data, 2)
151+
assert.Len(suite.T(), response.Data, 3)
152+
153+
// Verify that the transaction with the earlier date is the last in the list
154+
assert.Equal(suite.T(), t1.Data.ID, response.Data[2].ID, t1.Data.CreatedAt)
155+
156+
// Verify that the transaction added for the same time as the first, but added later
157+
// is before the other
158+
assert.Equal(suite.T(), t3.Data.ID, response.Data[1].ID, t3.Data.CreatedAt)
132159
}
133160

134161
func (suite *TestSuiteStandard) TestGetTransactionsInvalidQuery() {

0 commit comments

Comments
 (0)