File size: 2,273 Bytes
b110593
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//                           _       _
// __      _____  __ ___   ___  __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
//  \ V  V /  __/ (_| |\ V /| | (_| | ||  __/
//   \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
//  Copyright © 2016 - 2024 Weaviate B.V. All rights reserved.
//
//  CONTACT: [email protected]
//

package aggregator

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

const (
	DateYearMonthDayHourMinute = "2022-06-16T17:30:"
	DateNanoSecondsTimeZone    = ".451235Z"
)

func TestDateAggregator(t *testing.T) {
	tests := []struct {
		name           string
		seconds        []string
		expectedMedian string
		expectedMode   string
	}{
		{
			name:           "Single value",
			seconds:        []string{"17"},
			expectedMedian: "17",
			expectedMode:   "17",
		},
		{
			name:           "Even number of values",
			seconds:        []string{"18", "18", "20", "25"},
			expectedMedian: "19",
			expectedMode:   "18",
		},
		{
			name:           "Uneven number of values",
			seconds:        []string{"18", "18", "19", "20", "25"},
			expectedMedian: "19",
			expectedMode:   "18",
		},
	}
	names := []string{"AddTimestamp", "AddRow"}
	for _, tt := range tests {
		for _, name := range names { // test two ways of adding the value to the aggregator
			t.Run(tt.name+" "+name, func(t *testing.T) {
				agg := newDateAggregator()
				for _, second := range tt.seconds {
					fullDate := DateYearMonthDayHourMinute + second + DateNanoSecondsTimeZone
					if name == names[0] {
						err := agg.AddTimestamp(fullDate)
						assert.Nil(t, err)
					} else {
						timeParsed, err := time.Parse(time.RFC3339, fullDate)
						assert.Nil(t, err)
						ts := newTimestamp(timeParsed.UnixNano())
						err = agg.addRow(ts, 1)
						assert.Nil(t, err)
					}
				}
				agg.buildPairsFromCounts() // needed to populate all required info
				assert.Equal(t, DateYearMonthDayHourMinute+tt.expectedMedian+DateNanoSecondsTimeZone, agg.Median())
				if len(tt.expectedMode) > 0 { // if there is no value that appears more often than other values
					assert.Equal(t, DateYearMonthDayHourMinute+tt.expectedMode+DateNanoSecondsTimeZone, agg.Mode())
				}
			})
		}
	}
}