@@ -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.
121126func (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
134161func (suite * TestSuiteStandard ) TestGetTransactionsInvalidQuery () {
0 commit comments