avgovertime_test.go 1.8 KB

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