@@ -2258,6 +2258,10 @@ func TestGroupRemovalTrigger(t *testing.T) {
2258
2258
func TestGetUserStatusChanges (t * testing.T ) {
2259
2259
t .Parallel ()
2260
2260
2261
+ now := dbtime .Now ()
2262
+ createdAt := now .Add (- 5 * 24 * time .Hour ) // 5 days ago
2263
+ firstTransitionTime := createdAt .Add (2 * 24 * time .Hour ) // 3 days ago
2264
+ secondTransitionTime := firstTransitionTime .Add (2 * 24 * time .Hour ) // 1 days ago
2261
2265
t .Run ("No Users" , func (t * testing.T ) {
2262
2266
t .Parallel ()
2263
2267
db , _ := dbtestutil .NewDB (t )
@@ -2307,8 +2311,6 @@ func TestGetUserStatusChanges(t *testing.T) {
2307
2311
ctx := testutil .Context (t , testutil .WaitShort )
2308
2312
2309
2313
// Create a user that's been in the specified status for the past 30 days
2310
- now := dbtime .Now ()
2311
- createdAt := now .Add (- 29 * 24 * time .Hour )
2312
2314
dbgen .User (t , db , database.User {
2313
2315
Status : tc .status ,
2314
2316
CreatedAt : createdAt ,
@@ -2324,8 +2326,10 @@ func TestGetUserStatusChanges(t *testing.T) {
2324
2326
require .NotEmpty (t , userStatusChanges , "should return results" )
2325
2327
2326
2328
// We should have an entry for each status change
2327
- require .Len (t , userStatusChanges , 1 , "should have 1 status change" )
2328
- require .Equal (t , userStatusChanges [0 ].NewStatus , tc .status , "should have the correct status" )
2329
+ require .Len (t , userStatusChanges , 5 , "should have 1 user * 5 days = 5 rows" )
2330
+ for _ , row := range userStatusChanges {
2331
+ require .Equal (t , row .NewStatus , tc .status , "should have the correct status" )
2332
+ }
2329
2333
})
2330
2334
}
2331
2335
})
@@ -2393,20 +2397,17 @@ func TestGetUserStatusChanges(t *testing.T) {
2393
2397
ctx := testutil .Context (t , testutil .WaitShort )
2394
2398
2395
2399
// Create a user that starts with initial status
2396
- now := dbtime .Now ()
2397
- createdAt := now .Add (- 5 * 24 * time .Hour ) // 5 days ago
2398
2400
user := dbgen .User (t , db , database.User {
2399
2401
Status : tc .initialStatus ,
2400
2402
CreatedAt : createdAt ,
2401
2403
UpdatedAt : createdAt ,
2402
2404
})
2403
2405
2404
2406
// After 2 days, change status to target status
2405
- statusChangeTime := createdAt .Add (2 * 24 * time .Hour )
2406
2407
user , err := db .UpdateUserStatus (ctx , database.UpdateUserStatusParams {
2407
2408
ID : user .ID ,
2408
2409
Status : tc .targetStatus ,
2409
- UpdatedAt : statusChangeTime ,
2410
+ UpdatedAt : firstTransitionTime ,
2410
2411
})
2411
2412
require .NoError (t , err )
2412
2413
@@ -2418,10 +2419,15 @@ func TestGetUserStatusChanges(t *testing.T) {
2418
2419
require .NoError (t , err )
2419
2420
require .NotEmpty (t , userStatusChanges , "should return results" )
2420
2421
2421
- // We should have an entry for each status change, including the initial status
2422
- require .Len (t , userStatusChanges , 2 , "should have 2 status changes" )
2423
- require .Equal (t , userStatusChanges [0 ].NewStatus , tc .initialStatus , "should have the initial status" )
2424
- require .Equal (t , userStatusChanges [1 ].NewStatus , tc .targetStatus , "should have the target status" )
2422
+ // We should have an entry for each status (active, dormant, suspended) for each day
2423
+ require .Len (t , userStatusChanges , 5 , "should have 1 user * 5 days = 5 rows" )
2424
+ for _ , row := range userStatusChanges {
2425
+ if row .ChangedAt .Before (firstTransitionTime ) {
2426
+ require .Equal (t , row .NewStatus , tc .initialStatus , "should have the initial status" )
2427
+ } else {
2428
+ require .Equal (t , row .NewStatus , tc .targetStatus , "should have the target status" )
2429
+ }
2430
+ }
2425
2431
})
2426
2432
}
2427
2433
})
@@ -2606,20 +2612,18 @@ func TestGetUserStatusChanges(t *testing.T) {
2606
2612
})
2607
2613
2608
2614
// First transition at 2 days
2609
- user1TransitionTime := createdAt .Add (2 * 24 * time .Hour )
2610
2615
user1 , err := db .UpdateUserStatus (ctx , database.UpdateUserStatusParams {
2611
2616
ID : user1 .ID ,
2612
2617
Status : tc .user1Transition .to ,
2613
- UpdatedAt : user1TransitionTime ,
2618
+ UpdatedAt : firstTransitionTime ,
2614
2619
})
2615
2620
require .NoError (t , err )
2616
2621
2617
2622
// Second transition at 4 days
2618
- user2TransitionTime := createdAt .Add (4 * 24 * time .Hour )
2619
2623
user2 , err = db .UpdateUserStatus (ctx , database.UpdateUserStatusParams {
2620
2624
ID : user2 .ID ,
2621
2625
Status : tc .user2Transition .to ,
2622
- UpdatedAt : user2TransitionTime ,
2626
+ UpdatedAt : secondTransitionTime ,
2623
2627
})
2624
2628
require .NoError (t , err )
2625
2629
@@ -2631,16 +2635,17 @@ func TestGetUserStatusChanges(t *testing.T) {
2631
2635
require .NoError (t , err )
2632
2636
require .NotEmpty (t , userStatusChanges )
2633
2637
2634
- // We should have an entry with the correct status changes for each user, including the initial status
2635
- require .Len (t , userStatusChanges , 4 , "should have 4 status changes" )
2636
- require .Equal (t , userStatusChanges [0 ].UserID , user1 .ID , "should have the first user" )
2637
- require .Equal (t , userStatusChanges [0 ].NewStatus , tc .user1Transition .from , "should have the first user's initial status" )
2638
- require .Equal (t , userStatusChanges [1 ].UserID , user1 .ID , "should have the first user" )
2639
- require .Equal (t , userStatusChanges [1 ].NewStatus , tc .user1Transition .to , "should have the first user's target status" )
2640
- require .Equal (t , userStatusChanges [2 ].UserID , user2 .ID , "should have the second user" )
2641
- require .Equal (t , userStatusChanges [2 ].NewStatus , tc .user2Transition .from , "should have the second user's initial status" )
2642
- require .Equal (t , userStatusChanges [3 ].UserID , user2 .ID , "should have the second user" )
2643
- require .Equal (t , userStatusChanges [3 ].NewStatus , tc .user2Transition .to , "should have the second user's target status" )
2638
+ // Expected counts before, between and after the transitions should match:
2639
+ for _ , row := range userStatusChanges {
2640
+ switch {
2641
+ case row .ChangedAt .Before (firstTransitionTime ):
2642
+ require .Equal (t , row .Count , tc .expectedCounts ["initial" ][row .NewStatus ], "should have the correct count before the first transition" )
2643
+ case row .ChangedAt .Before (secondTransitionTime ):
2644
+ require .Equal (t , row .Count , tc .expectedCounts ["between" ][row .NewStatus ], "should have the correct count between the transitions" )
2645
+ case row .ChangedAt .Before (now ):
2646
+ require .Equal (t , row .Count , tc .expectedCounts ["final" ][row .NewStatus ], "should have the correct count after the second transition" )
2647
+ }
2648
+ }
2644
2649
})
2645
2650
}
2646
2651
})
0 commit comments