alerting_test.go 889 B

1234567891011121314151617181920212223242526272829303132333435
  1. package dtos
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestFormatShort(t *testing.T) {
  7. tcs := []struct {
  8. interval time.Duration
  9. expected string
  10. }{
  11. {interval: time.Hour, expected: "1h"},
  12. {interval: time.Hour + time.Minute, expected: "1h1m"},
  13. {interval: (time.Hour * 10) + time.Minute, expected: "10h1m"},
  14. {interval: (time.Hour * 10) + (time.Minute * 10) + time.Second, expected: "10h10m1s"},
  15. {interval: time.Minute * 10, expected: "10m"},
  16. }
  17. for _, tc := range tcs {
  18. got := formatShort(tc.interval)
  19. if got != tc.expected {
  20. t.Errorf("expected %s got %s interval: %v", tc.expected, got, tc.interval)
  21. }
  22. parsed, err := time.ParseDuration(tc.expected)
  23. if err != nil {
  24. t.Fatalf("could not parse expected duration")
  25. }
  26. if parsed != tc.interval {
  27. t.Errorf("expectes the parsed duration to equal the interval. Got %v expected: %v", parsed, tc.interval)
  28. }
  29. }
  30. }