avgovertime_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package aggregator
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. )
  7. func TestAvgOverTimeAggregator_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, 1, 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. "single update": {
  24. updates: []update{
  25. {
  26. value: 1,
  27. timestamp: time1,
  28. },
  29. },
  30. want: []MetricValue{
  31. {
  32. Value: 1,
  33. },
  34. },
  35. },
  36. "multiple updates": {
  37. updates: []update{
  38. {
  39. value: 2,
  40. timestamp: time1,
  41. },
  42. {
  43. value: 1,
  44. timestamp: time2,
  45. },
  46. },
  47. want: []MetricValue{
  48. {
  49. Value: 1.5,
  50. },
  51. },
  52. },
  53. "aggregated updates": {
  54. updates: []update{
  55. {
  56. value: 1,
  57. timestamp: time1,
  58. },
  59. {
  60. value: 2,
  61. timestamp: time1,
  62. },
  63. },
  64. want: []MetricValue{
  65. {
  66. Value: 3,
  67. },
  68. },
  69. },
  70. "multiple aggregated updates": {
  71. updates: []update{
  72. {
  73. value: 2,
  74. timestamp: time1,
  75. },
  76. {
  77. value: 2,
  78. timestamp: time1,
  79. },
  80. {
  81. value: 4,
  82. timestamp: time2,
  83. },
  84. {
  85. value: 4,
  86. timestamp: time2,
  87. },
  88. },
  89. want: []MetricValue{
  90. {
  91. Value: 6,
  92. },
  93. },
  94. },
  95. }
  96. for name, tt := range tests {
  97. t.Run(name, func(t *testing.T) {
  98. agg := averageOverTimeAggregator{}
  99. for _, u := range tt.updates {
  100. agg.Update(u.value, u.timestamp, u.additionalInformation)
  101. }
  102. got := agg.Value()
  103. if !reflect.DeepEqual(got, tt.want) {
  104. t.Errorf("got = %v, want %v", got, tt.want)
  105. }
  106. })
  107. }
  108. }