2
0

increase_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "single update": {
  24. updates: []update{
  25. {
  26. value: 1,
  27. timestamp: time1,
  28. },
  29. },
  30. want: []MetricValue{
  31. {
  32. Value: 0,
  33. },
  34. },
  35. },
  36. "normal increase": {
  37. updates: []update{
  38. {
  39. value: 1,
  40. timestamp: time1,
  41. },
  42. {
  43. value: 2,
  44. timestamp: time2,
  45. },
  46. },
  47. want: []MetricValue{
  48. {
  49. Value: 1,
  50. },
  51. },
  52. },
  53. "double increase": {
  54. updates: []update{
  55. {
  56. value: 1,
  57. timestamp: time1,
  58. },
  59. {
  60. value: 2,
  61. timestamp: time1,
  62. },
  63. {
  64. value: 3,
  65. timestamp: time2,
  66. },
  67. {
  68. value: 4,
  69. timestamp: time2,
  70. },
  71. },
  72. want: []MetricValue{
  73. {
  74. Value: 4,
  75. },
  76. },
  77. },
  78. }
  79. for name, tt := range tests {
  80. t.Run(name, func(t *testing.T) {
  81. agg := increaseAggregator{}
  82. for _, u := range tt.updates {
  83. agg.Update(u.value, u.timestamp, u.additionalInformation)
  84. }
  85. got := agg.Value()
  86. if !reflect.DeepEqual(got, tt.want) {
  87. t.Errorf("IncreaseAggregator.Value() = %v, want %v", got, tt.want)
  88. }
  89. })
  90. }
  91. }