@@ -87,7 +87,12 @@ func TestV1UsersDeveloperApps(t *testing.T) {
8787
8888func TestV1UsersDeveloperAppsIncludeMetrics (t * testing.T ) {
8989 app := emptyTestApp (t )
90- now := time .Now ()
90+ // Use UTC so api_metrics row dates align with PostgreSQL CURRENT_DATE in tests.
91+ now := time .Now ().UTC ()
92+ // AddDate(0, -1, 0) from e.g. Mar 30 normalizes to Mar 2 (same month), which would
93+ // incorrectly include prior-month metrics in month-to-date. Use last day of prev month.
94+ firstOfMonth := time .Date (now .Year (), now .Month (), 1 , 0 , 0 , 0 , 0 , time .UTC )
95+ lastOfPrevMonth := firstOfMonth .AddDate (0 , 0 , - 1 )
9196
9297 fixtures := database.FixtureMap {
9398 "users" : []map [string ]any {
@@ -113,7 +118,7 @@ func TestV1UsersDeveloperAppsIncludeMetrics(t *testing.T) {
113118 _ , err := app .pool .Exec (t .Context (), `
114119 INSERT INTO api_metrics_apps (date, api_key, app_name, request_count)
115120 VALUES ($1, $2, $3, $4), ($5, $6, $7, $8)
116- ` , now , "app_address_1" , "app_name_1" , 7 , now . AddDate ( 0 , - 1 , 0 ) , "app_address_1" , "app_name_1" , 3 )
121+ ` , now , "app_address_1" , "app_name_1" , 7 , lastOfPrevMonth , "app_address_1" , "app_name_1" , 3 )
117122 assert .NoError (t , err )
118123
119124 status , body := testGet (t , app , "/v1/users/" + trashid .MustEncodeHashID (1 )+ "/developer-apps?include=metrics" )
0 commit comments