|
7 | 7 | "database/sql"
|
8 | 8 | "encoding/json"
|
9 | 9 | "fmt"
|
10 |
| - "maps" |
11 | 10 | "sort"
|
12 | 11 | "testing"
|
13 | 12 | "time"
|
@@ -2743,8 +2742,8 @@ func TestGetUserStatusCounts(t *testing.T) {
|
2743 | 2742 | for _, tc := range testCases {
|
2744 | 2743 | tc := tc
|
2745 | 2744 | t.Run(tc.name, func(t *testing.T) {
|
2746 |
| - t.SkipNow() |
2747 | 2745 | t.Parallel()
|
| 2746 | + |
2748 | 2747 | db, _ := dbtestutil.NewDB(t)
|
2749 | 2748 | ctx := testutil.Context(t, testutil.WaitShort)
|
2750 | 2749 |
|
@@ -2776,66 +2775,48 @@ func TestGetUserStatusCounts(t *testing.T) {
|
2776 | 2775 | require.NoError(t, err)
|
2777 | 2776 |
|
2778 | 2777 | userStatusChanges, err := db.GetUserStatusCounts(ctx, database.GetUserStatusCountsParams{
|
2779 |
| - StartTime: createdAt, |
2780 |
| - EndTime: today, |
| 2778 | + StartTime: dbtime.StartOfDay(createdAt), |
| 2779 | + EndTime: dbtime.StartOfDay(today), |
2781 | 2780 | })
|
2782 | 2781 | require.NoError(t, err)
|
2783 | 2782 | require.NotEmpty(t, userStatusChanges)
|
2784 |
| - gotCounts := map[time.Time]map[database.UserStatus]int64{ |
2785 |
| - createdAt.In(location): {}, |
2786 |
| - firstTransitionTime.In(location): {}, |
2787 |
| - secondTransitionTime.In(location): {}, |
2788 |
| - today.In(location): {}, |
2789 |
| - } |
| 2783 | + gotCounts := map[time.Time]map[database.UserStatus]int64{} |
2790 | 2784 | for _, row := range userStatusChanges {
|
2791 | 2785 | dateInLocation := row.Date.In(location)
|
2792 |
| - switch { |
2793 |
| - case dateInLocation.Equal(createdAt.In(location)): |
2794 |
| - gotCounts[createdAt][row.Status] = row.Count |
2795 |
| - case dateInLocation.Equal(firstTransitionTime.In(location)): |
2796 |
| - gotCounts[firstTransitionTime][row.Status] = row.Count |
2797 |
| - case dateInLocation.Equal(secondTransitionTime.In(location)): |
2798 |
| - gotCounts[secondTransitionTime][row.Status] = row.Count |
2799 |
| - case dateInLocation.Equal(today.In(location)): |
2800 |
| - gotCounts[today][row.Status] = row.Count |
2801 |
| - default: |
2802 |
| - t.Fatalf("unexpected date %s", row.Date) |
| 2786 | + if gotCounts[dateInLocation] == nil { |
| 2787 | + gotCounts[dateInLocation] = map[database.UserStatus]int64{} |
2803 | 2788 | }
|
| 2789 | + gotCounts[dateInLocation][row.Status] = row.Count |
2804 | 2790 | }
|
2805 | 2791 |
|
2806 | 2792 | expectedCounts := map[time.Time]map[database.UserStatus]int64{}
|
2807 |
| - for _, status := range []database.UserStatus{ |
2808 |
| - tc.user1Transition.from, |
2809 |
| - tc.user1Transition.to, |
2810 |
| - tc.user2Transition.from, |
2811 |
| - tc.user2Transition.to, |
2812 |
| - } { |
2813 |
| - if _, ok := expectedCounts[createdAt]; !ok { |
2814 |
| - expectedCounts[createdAt] = map[database.UserStatus]int64{} |
| 2793 | + for d := dbtime.StartOfDay(createdAt); !d.After(dbtime.StartOfDay(today)); d = d.AddDate(0, 0, 1) { |
| 2794 | + expectedCounts[d] = map[database.UserStatus]int64{} |
| 2795 | + |
| 2796 | + // Default values |
| 2797 | + expectedCounts[d][tc.user1Transition.from] = 0 |
| 2798 | + expectedCounts[d][tc.user1Transition.to] = 0 |
| 2799 | + expectedCounts[d][tc.user2Transition.from] = 0 |
| 2800 | + expectedCounts[d][tc.user2Transition.to] = 0 |
| 2801 | + |
| 2802 | + // Counted Values |
| 2803 | + if d.Before(createdAt) { |
| 2804 | + continue |
| 2805 | + } else if d.Before(firstTransitionTime) { |
| 2806 | + expectedCounts[d][tc.user1Transition.from]++ |
| 2807 | + expectedCounts[d][tc.user2Transition.from]++ |
| 2808 | + } else if d.Before(secondTransitionTime) { |
| 2809 | + expectedCounts[d][tc.user1Transition.to]++ |
| 2810 | + expectedCounts[d][tc.user2Transition.from]++ |
| 2811 | + } else if d.Before(today) { |
| 2812 | + expectedCounts[d][tc.user1Transition.to]++ |
| 2813 | + expectedCounts[d][tc.user2Transition.to]++ |
| 2814 | + } else { |
| 2815 | + t.Fatalf("date %q beyond expected range end %q", d, today) |
2815 | 2816 | }
|
2816 |
| - expectedCounts[createdAt][status] = 0 |
2817 | 2817 | }
|
2818 | 2818 |
|
2819 |
| - expectedCounts[createdAt][tc.user1Transition.from]++ |
2820 |
| - expectedCounts[createdAt][tc.user2Transition.from]++ |
2821 |
| - |
2822 |
| - expectedCounts[firstTransitionTime] = map[database.UserStatus]int64{} |
2823 |
| - maps.Copy(expectedCounts[firstTransitionTime], expectedCounts[createdAt]) |
2824 |
| - expectedCounts[firstTransitionTime][tc.user1Transition.from]-- |
2825 |
| - expectedCounts[firstTransitionTime][tc.user1Transition.to]++ |
2826 |
| - |
2827 |
| - expectedCounts[secondTransitionTime] = map[database.UserStatus]int64{} |
2828 |
| - maps.Copy(expectedCounts[secondTransitionTime], expectedCounts[firstTransitionTime]) |
2829 |
| - expectedCounts[secondTransitionTime][tc.user2Transition.from]-- |
2830 |
| - expectedCounts[secondTransitionTime][tc.user2Transition.to]++ |
2831 |
| - |
2832 |
| - expectedCounts[today] = map[database.UserStatus]int64{} |
2833 |
| - maps.Copy(expectedCounts[today], expectedCounts[secondTransitionTime]) |
2834 |
| - |
2835 |
| - require.Equal(t, expectedCounts[createdAt], gotCounts[createdAt]) |
2836 |
| - require.Equal(t, expectedCounts[firstTransitionTime], gotCounts[firstTransitionTime]) |
2837 |
| - require.Equal(t, expectedCounts[secondTransitionTime], gotCounts[secondTransitionTime]) |
2838 |
| - require.Equal(t, expectedCounts[today], gotCounts[today]) |
| 2819 | + require.Equal(t, expectedCounts, gotCounts) |
2839 | 2820 | })
|
2840 | 2821 | }
|
2841 | 2822 | })
|
|
0 commit comments