increase_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package aggregator
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. )
  7. func TestIncreaseAggregator_Value(t *testing.T) {
  8. time1 := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
  9. time2 := time.Date(1, 1, 1, 0, 15, 0, 0, time.UTC)
  10. type update struct {
  11. value float64
  12. timestamp time.Time
  13. additionalInformation map[string]string
  14. }
  15. tests := map[string]struct {
  16. updates []update
  17. want []MetricValue
  18. }{
  19. "no update": {
  20. updates: []update{},
  21. want: []MetricValue{
  22. {
  23. Value: 0,
  24. },
  25. },
  26. },
  27. "single update": {
  28. updates: []update{
  29. {
  30. value: 1,
  31. timestamp: time1,
  32. },
  33. },
  34. want: []MetricValue{
  35. {
  36. Value: 0,
  37. },
  38. },
  39. },
  40. "normal increase": {
  41. updates: []update{
  42. {
  43. value: 1,
  44. timestamp: time1,
  45. },
  46. {
  47. value: 2,
  48. timestamp: time2,
  49. },
  50. },
  51. want: []MetricValue{
  52. {
  53. Value: 1,
  54. },
  55. },
  56. },
  57. "double increase": {
  58. updates: []update{
  59. {
  60. value: 1,
  61. timestamp: time1,
  62. },
  63. {
  64. value: 2,
  65. timestamp: time1,
  66. },
  67. {
  68. value: 3,
  69. timestamp: time2,
  70. },
  71. {
  72. value: 4,
  73. timestamp: time2,
  74. },
  75. },
  76. want: []MetricValue{
  77. {
  78. Value: 4,
  79. },
  80. },
  81. },
  82. }
  83. for name, tt := range tests {
  84. t.Run(name, func(t *testing.T) {
  85. agg := increaseAggregator{}
  86. for _, u := range tt.updates {
  87. agg.Update(u.value, u.timestamp, u.additionalInformation)
  88. }
  89. got := agg.Value()
  90. if !reflect.DeepEqual(got, tt.want) {
  91. t.Errorf("IncreaseAggregator.Value() = %v, want %v", got, tt.want)
  92. }
  93. })
  94. }
  95. }