id_list_utils_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //go:build unit
  2. package admin
  3. import (
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func TestNormalizeInt64IDList(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. in []int64
  11. want []int64
  12. }{
  13. {"nil input", nil, nil},
  14. {"empty input", []int64{}, nil},
  15. {"single element", []int64{5}, []int64{5}},
  16. {"already sorted unique", []int64{1, 2, 3}, []int64{1, 2, 3}},
  17. {"duplicates removed", []int64{3, 1, 3, 2, 1}, []int64{1, 2, 3}},
  18. {"zero filtered", []int64{0, 1, 2}, []int64{1, 2}},
  19. {"negative filtered", []int64{-5, -1, 3}, []int64{3}},
  20. {"all invalid", []int64{0, -1, -2}, []int64{}},
  21. {"sorted output", []int64{9, 3, 7, 1}, []int64{1, 3, 7, 9}},
  22. }
  23. for _, tc := range tests {
  24. t.Run(tc.name, func(t *testing.T) {
  25. got := normalizeInt64IDList(tc.in)
  26. if tc.want == nil {
  27. require.Nil(t, got)
  28. } else {
  29. require.Equal(t, tc.want, got)
  30. }
  31. })
  32. }
  33. }
  34. func TestBuildAccountTodayStatsBatchCacheKey(t *testing.T) {
  35. tests := []struct {
  36. name string
  37. ids []int64
  38. want string
  39. }{
  40. {"empty", nil, "accounts_today_stats_empty"},
  41. {"single", []int64{42}, "accounts_today_stats:42"},
  42. {"multiple", []int64{1, 2, 3}, "accounts_today_stats:1,2,3"},
  43. }
  44. for _, tc := range tests {
  45. t.Run(tc.name, func(t *testing.T) {
  46. got := buildAccountTodayStatsBatchCacheKey(tc.ids)
  47. require.Equal(t, tc.want, got)
  48. })
  49. }
  50. }