@@ -2454,28 +2454,26 @@ func TestGetUserStatusCounts(t *testing.T) {
2454
2454
UpdatedAt : createdAt ,
2455
2455
})
2456
2456
2457
- _ , offset := today .Zone ()
2458
2457
userStatusChanges , err := db .GetUserStatusCounts (ctx , database.GetUserStatusCountsParams {
2459
- StartTime : startOfDay (createdAt ),
2460
- EndTime : startOfDay (today ),
2461
- TzOffset : int32 (offset ),
2458
+ StartTime : dbtime .StartOfDay (createdAt ),
2459
+ EndTime : dbtime .StartOfDay (today ),
2462
2460
})
2463
2461
require .NoError (t , err )
2464
2462
2465
- numDays := int (startOfDay (today ).Sub (startOfDay (createdAt )).Hours () / 24 )
2463
+ numDays := int (dbtime . StartOfDay (today ).Sub (dbtime . StartOfDay (createdAt )).Hours () / 24 )
2466
2464
require .Len (t , userStatusChanges , numDays + 1 , "should have 1 entry per day between the start and end time, including the end time" )
2467
2465
2468
2466
for i , row := range userStatusChanges {
2469
2467
require .Equal (t , tc .status , row .Status , "should have the correct status" )
2470
2468
require .True (
2471
2469
t ,
2472
- row .Date .In (location ).Equal (startOfDay (createdAt ).AddDate (0 , 0 , i )),
2470
+ row .Date .In (location ).Equal (dbtime . StartOfDay (createdAt ).AddDate (0 , 0 , i )),
2473
2471
"expected date %s, but got %s for row %n" ,
2474
- startOfDay (createdAt ).AddDate (0 , 0 , i ),
2472
+ dbtime . StartOfDay (createdAt ).AddDate (0 , 0 , i ),
2475
2473
row .Date .In (location ).String (),
2476
2474
i ,
2477
2475
)
2478
- if row .Date .Before (startOfDay ( createdAt ) ) {
2476
+ if row .Date .Before (createdAt ) {
2479
2477
require .Equal (t , int64 (0 ), row .Count , "should have 0 users before creation" )
2480
2478
} else {
2481
2479
require .Equal (t , int64 (1 ), row .Count , "should have 1 user after creation" )
@@ -2634,24 +2632,38 @@ func TestGetUserStatusCounts(t *testing.T) {
2634
2632
2635
2633
// Query for the last 5 days
2636
2634
userStatusChanges , err := db .GetUserStatusCounts (ctx , database.GetUserStatusCountsParams {
2637
- StartTime : createdAt ,
2638
- EndTime : today ,
2635
+ StartTime : dbtime . StartOfDay ( createdAt ) ,
2636
+ EndTime : dbtime . StartOfDay ( today ) ,
2639
2637
})
2640
2638
require .NoError (t , err )
2641
- require .NotEmpty (t , userStatusChanges , "should return results" )
2642
2639
2643
- gotCounts := map [time.Time ]map [database.UserStatus ]int64 {}
2644
- for _ , row := range userStatusChanges {
2645
- gotDateInLocation := row .Date .In (location )
2646
- if _ , ok := gotCounts [gotDateInLocation ]; ! ok {
2647
- gotCounts [gotDateInLocation ] = map [database.UserStatus ]int64 {}
2648
- }
2649
- if _ , ok := gotCounts [gotDateInLocation ][row.Status ]; ! ok {
2650
- gotCounts [gotDateInLocation ][row.Status ] = 0
2640
+ for i , row := range userStatusChanges {
2641
+ require .True (
2642
+ t ,
2643
+ row .Date .In (location ).Equal (dbtime .StartOfDay (createdAt ).AddDate (0 , 0 , i / 2 )),
2644
+ "expected date %s, but got %s for row %n" ,
2645
+ dbtime .StartOfDay (createdAt ).AddDate (0 , 0 , i / 2 ),
2646
+ row .Date .In (location ).String (),
2647
+ i ,
2648
+ )
2649
+ if row .Date .Before (createdAt ) {
2650
+ require .Equal (t , int64 (0 ), row .Count )
2651
+ } else if row .Date .Before (firstTransitionTime ) {
2652
+ if row .Status == tc .initialStatus {
2653
+ require .Equal (t , int64 (1 ), row .Count )
2654
+ } else if row .Status == tc .targetStatus {
2655
+ require .Equal (t , int64 (0 ), row .Count )
2656
+ }
2657
+ } else if ! row .Date .After (today ) {
2658
+ if row .Status == tc .initialStatus {
2659
+ require .Equal (t , int64 (0 ), row .Count )
2660
+ } else if row .Status == tc .targetStatus {
2661
+ require .Equal (t , int64 (1 ), row .Count )
2662
+ }
2663
+ } else {
2664
+ t .Errorf ("date %q beyond expected range end %q" , row .Date , today )
2651
2665
}
2652
- gotCounts [gotDateInLocation ][row.Status ] += row .Count
2653
2666
}
2654
- require .Equal (t , tc .expectedCounts , gotCounts )
2655
2667
})
2656
2668
}
2657
2669
})
@@ -2840,16 +2852,23 @@ func TestGetUserStatusCounts(t *testing.T) {
2840
2852
})
2841
2853
2842
2854
userStatusChanges , err := db .GetUserStatusCounts (ctx , database.GetUserStatusCountsParams {
2843
- StartTime : createdAt .Add (time .Hour * 24 ),
2844
- EndTime : today ,
2855
+ StartTime : dbtime . StartOfDay ( createdAt .Add (time .Hour * 24 ) ),
2856
+ EndTime : dbtime . StartOfDay ( today ) ,
2845
2857
})
2846
2858
require .NoError (t , err )
2847
2859
2848
- require .Len (t , userStatusChanges , 2 )
2849
- require .Equal (t , userStatusChanges [0 ].Count , int64 (1 ))
2850
- require .Equal (t , userStatusChanges [0 ].Status , database .UserStatusActive )
2851
- require .Equal (t , userStatusChanges [1 ].Count , int64 (1 ))
2852
- require .Equal (t , userStatusChanges [1 ].Status , database .UserStatusActive )
2860
+ for i , row := range userStatusChanges {
2861
+ require .True (
2862
+ t ,
2863
+ row .Date .In (location ).Equal (dbtime .StartOfDay (createdAt ).AddDate (0 , 0 , 1 + i )),
2864
+ "expected date %s, but got %s for row %n" ,
2865
+ dbtime .StartOfDay (createdAt ).AddDate (0 , 0 , 1 + i ),
2866
+ row .Date .In (location ).String (),
2867
+ i ,
2868
+ )
2869
+ require .Equal (t , database .UserStatusActive , row .Status )
2870
+ require .Equal (t , int64 (1 ), row .Count )
2871
+ }
2853
2872
})
2854
2873
2855
2874
t .Run ("User deleted before query range" , func (t * testing.T ) {
@@ -2889,16 +2908,28 @@ func TestGetUserStatusCounts(t *testing.T) {
2889
2908
require .NoError (t , err )
2890
2909
2891
2910
userStatusChanges , err := db .GetUserStatusCounts (ctx , database.GetUserStatusCountsParams {
2892
- StartTime : createdAt ,
2893
- EndTime : today .Add (time .Hour * 24 ),
2911
+ StartTime : dbtime . StartOfDay ( createdAt ) ,
2912
+ EndTime : dbtime . StartOfDay ( today .Add (time .Hour * 24 ) ),
2894
2913
})
2895
2914
require .NoError (t , err )
2896
- require .Equal (t , userStatusChanges [0 ].Count , int64 (1 ))
2897
- require .Equal (t , userStatusChanges [0 ].Status , database .UserStatusActive )
2898
- require .Equal (t , userStatusChanges [1 ].Count , int64 (0 ))
2899
- require .Equal (t , userStatusChanges [1 ].Status , database .UserStatusActive )
2900
- require .Equal (t , userStatusChanges [2 ].Count , int64 (0 ))
2901
- require .Equal (t , userStatusChanges [2 ].Status , database .UserStatusActive )
2915
+ for i , row := range userStatusChanges {
2916
+ require .True (
2917
+ t ,
2918
+ row .Date .In (location ).Equal (dbtime .StartOfDay (createdAt ).AddDate (0 , 0 , i )),
2919
+ "expected date %s, but got %s for row %n" ,
2920
+ dbtime .StartOfDay (createdAt ).AddDate (0 , 0 , i ),
2921
+ row .Date .In (location ).String (),
2922
+ i ,
2923
+ )
2924
+ require .Equal (t , database .UserStatusActive , row .Status )
2925
+ if row .Date .Before (createdAt ) {
2926
+ require .Equal (t , int64 (0 ), row .Count )
2927
+ } else if i == len (userStatusChanges )- 1 {
2928
+ require .Equal (t , int64 (0 ), row .Count )
2929
+ } else {
2930
+ require .Equal (t , int64 (1 ), row .Count )
2931
+ }
2932
+ }
2902
2933
})
2903
2934
})
2904
2935
}
@@ -2908,8 +2939,3 @@ func requireUsersMatch(t testing.TB, expected []database.User, found []database.
2908
2939
t .Helper ()
2909
2940
require .ElementsMatch (t , expected , database .ConvertUserRows (found ), msg )
2910
2941
}
2911
-
2912
- func startOfDay (t time.Time ) time.Time {
2913
- year , month , day := t .Date ()
2914
- return time .Date (year , month , day , 0 , 0 , 0 , 0 , t .Location ())
2915
- }
0 commit comments