stats_test.go 921 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package kubemodel
  2. import (
  3. "errors"
  4. "math"
  5. "testing"
  6. )
  7. func TestStats_Sanitize(t *testing.T) {
  8. type testCase struct {
  9. stats Stats
  10. exp error
  11. }
  12. testCases := []testCase{
  13. {
  14. nil,
  15. nil,
  16. },
  17. {
  18. Stats{},
  19. nil,
  20. },
  21. {
  22. Stats{
  23. StatAvg: 0.1,
  24. StatMax: 1.0,
  25. },
  26. nil,
  27. },
  28. {
  29. Stats{
  30. StatAvg: math.Inf(0),
  31. StatMax: 1.0,
  32. },
  33. errors.New("1 errors: [avg is Inf]"),
  34. },
  35. {
  36. Stats{
  37. StatAvg: math.Inf(0),
  38. StatMax: math.NaN(),
  39. },
  40. errors.New("2 errors: [avg is Inf] [max is NaN]"),
  41. },
  42. }
  43. for _, tc := range testCases {
  44. err := tc.stats.Sanitize()
  45. if err != nil && tc.exp == nil {
  46. t.Errorf("unexpected error: %s", err)
  47. }
  48. if err == nil && tc.exp != nil {
  49. t.Errorf("expected error: %s", tc.exp)
  50. }
  51. if err != nil && tc.exp != nil && err.Error()[0] != tc.exp.Error()[0] {
  52. t.Errorf("expected error: %s; received error: %s", tc.exp, err)
  53. }
  54. }
  55. }