ops_cleanup_service_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package service
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestOpsCleanupPlan(t *testing.T) {
  7. now := time.Date(2026, 4, 29, 12, 0, 0, 0, time.UTC)
  8. cases := []struct {
  9. name string
  10. days int
  11. wantOK bool
  12. wantTruncate bool
  13. wantCutoff time.Time
  14. }{
  15. {name: "negative skips", days: -1, wantOK: false},
  16. {name: "zero truncates", days: 0, wantOK: true, wantTruncate: true},
  17. {name: "positive yields past cutoff", days: 7, wantOK: true, wantCutoff: now.AddDate(0, 0, -7)},
  18. }
  19. for _, tc := range cases {
  20. t.Run(tc.name, func(t *testing.T) {
  21. cutoff, truncate, ok := opsCleanupPlan(now, tc.days)
  22. if ok != tc.wantOK {
  23. t.Fatalf("ok = %v, want %v", ok, tc.wantOK)
  24. }
  25. if !ok {
  26. return
  27. }
  28. if truncate != tc.wantTruncate {
  29. t.Fatalf("truncate = %v, want %v", truncate, tc.wantTruncate)
  30. }
  31. if !tc.wantTruncate && !cutoff.Equal(tc.wantCutoff) {
  32. t.Fatalf("cutoff = %v, want %v", cutoff, tc.wantCutoff)
  33. }
  34. })
  35. }
  36. }
  37. func TestIsMissingRelationError(t *testing.T) {
  38. cases := []struct {
  39. name string
  40. err error
  41. want bool
  42. }{
  43. {name: "nil is not missing", err: nil, want: false},
  44. {name: "match relation does not exist", err: fakeErr(`pq: relation "ops_error_logs" does not exist`), want: true},
  45. {name: "match case-insensitive", err: fakeErr(`ERROR: Relation "x" Does Not Exist`), want: true},
  46. {name: "non-matching error", err: fakeErr("connection refused"), want: false},
  47. }
  48. for _, tc := range cases {
  49. t.Run(tc.name, func(t *testing.T) {
  50. if got := isMissingRelationError(tc.err); got != tc.want {
  51. t.Fatalf("got %v, want %v", got, tc.want)
  52. }
  53. })
  54. }
  55. }
  56. type fakeErr string
  57. func (e fakeErr) Error() string { return string(e) }