forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeperiods_test.go
188 lines (182 loc) · 4.7 KB
/
timeperiods_test.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package timeperiods
import (
"testing"
"time"
)
func TestFindTimeRangesContainingData(t *testing.T) {
// validation issues
_, err := FindTimeRangesContainingData(
time.Time{},
time.Time{},
0,
nil,
)
if err != nil && err.Error() != "invalid start time, invalid end time, invalid period" {
t.Fatal(err)
}
// empty trade times
searchStartTime := time.Date(2020, 1, 1, 1, 0, 0, 0, time.UTC)
searchEndTime := time.Date(2020, 1, 1, 10, 0, 0, 0, time.UTC)
var tradeTimes []time.Time
var ranges []TimeRange
ranges, err = FindTimeRangesContainingData(
searchStartTime,
searchEndTime,
time.Hour,
tradeTimes,
)
if err != nil {
t.Error(err)
}
if len(ranges) != 1 {
t.Errorf("expected 1 time range, received %v", len(ranges))
}
// 1 trade with 3 periods
tradeTimes = append(tradeTimes, time.Date(2020, 1, 1, 2, 0, 0, 0, time.UTC))
ranges, err = FindTimeRangesContainingData(
searchStartTime,
searchEndTime,
time.Hour,
tradeTimes,
)
if err != nil {
t.Error(err)
}
if len(ranges) != 3 {
t.Errorf("expected 3 time ranges, received %v", len(ranges))
}
// 2 trades with 3 periods
tradeTimes = append(tradeTimes, time.Date(2020, 1, 1, 3, 0, 0, 0, time.UTC))
ranges, err = FindTimeRangesContainingData(
searchStartTime,
searchEndTime,
time.Hour,
tradeTimes,
)
if err != nil {
t.Error(err)
}
if len(ranges) != 3 {
t.Errorf("expected 3 time ranges, received %v", len(ranges))
}
// 3 trades with 5 periods
tradeTimes = append(tradeTimes, time.Date(2020, 1, 1, 5, 0, 0, 0, time.UTC))
ranges, err = FindTimeRangesContainingData(
searchStartTime,
searchEndTime,
time.Hour,
tradeTimes,
)
if err != nil {
t.Error(err)
}
if len(ranges) != 5 {
t.Errorf("expected 5 time ranges, received %v", len(ranges))
}
// 4 trades with 5 periods
tradeTimes = append(tradeTimes, time.Date(2020, 1, 1, 6, 0, 0, 0, time.UTC))
ranges, err = FindTimeRangesContainingData(
searchStartTime,
searchEndTime,
time.Hour,
tradeTimes,
)
if err != nil {
t.Error(err)
}
if len(ranges) != 5 {
t.Errorf("expected 5 time ranges, received %v", len(ranges))
}
// 5 trades with 6 periods
tradeTimes = append(tradeTimes, time.Date(2020, 1, 1, 9, 0, 0, 0, time.UTC))
ranges, err = FindTimeRangesContainingData(
searchStartTime,
searchEndTime,
time.Hour,
tradeTimes,
)
if err != nil {
t.Error(err)
}
if len(ranges) != 6 {
t.Errorf("expected 6 time ranges, received %v", len(ranges))
}
}
func TestCalculateTimePeriodsInRange(t *testing.T) {
// validation issues
_, err := CalculateTimePeriodsInRange(time.Time{}, time.Time{}, 0)
if err != nil && err.Error() != "invalid start time, invalid end time, invalid period" {
t.Fatal(err)
}
// start after end
var intervals []TimePeriod
timeStart := time.Date(2020, 1, 1, 1, 0, 0, 0, time.UTC)
timeEnd := time.Date(2020, 1, 2, 1, 0, 0, 0, time.UTC)
intervals, err = CalculateTimePeriodsInRange(timeEnd, timeStart, time.Hour)
if err != nil {
t.Error(err)
}
if len(intervals) != 0 {
t.Errorf("expected 0 interval(s), received %v", len(intervals))
}
// 1 interval
intervals, err = CalculateTimePeriodsInRange(timeStart, timeStart.Add(time.Hour), time.Hour)
if err != nil {
t.Error(err)
}
if len(intervals) != 1 {
t.Errorf("expected 1 interval(s), received %v", len(intervals))
}
// multiple intervals
intervals, err = CalculateTimePeriodsInRange(timeStart, timeEnd, time.Hour)
if err != nil {
t.Error(err)
}
if len(intervals) != 24 {
t.Errorf("expected 24 interval(s), received %v", len(intervals))
}
// odd time
intervals, err = CalculateTimePeriodsInRange(timeStart.Add(-time.Minute*30), timeEnd, time.Hour)
if err != nil {
t.Error(err)
}
if len(intervals) != 25 {
t.Errorf("expected 25 interval(s), received %v", len(intervals))
}
// truncate always goes to zero, no mid rounding
intervals, err = CalculateTimePeriodsInRange(timeStart, timeStart.Add(time.Minute), time.Hour)
if err != nil {
t.Error(err)
}
if len(intervals) != 0 {
t.Errorf("expected 0 interval(s), received %v", len(intervals))
}
}
func TestValidateCalculatePeriods(t *testing.T) {
var tpc TimePeriodCalculator
tpc.calculatePeriods()
if len(tpc.TimePeriods) > 0 {
t.Error("validation has been removed")
}
}
func TestSort(t *testing.T) {
var tpc TimePeriodCalculator
date1 := time.Date(2020, 1, 1, 1, 1, 1, 1, time.UTC)
date2 := time.Date(1901, 1, 1, 1, 1, 1, 1, time.UTC)
tpc.TimePeriods = append(tpc.TimePeriods,
TimePeriod{
Time: date1,
},
TimePeriod{
Time: date2,
},
)
tpc.Sort(false)
if !tpc.TimePeriods[0].Time.Equal(date2) {
t.Errorf("expected %v, received %v", date2, tpc.TimePeriods[0].Time)
}
tpc.Sort(true)
if !tpc.TimePeriods[0].Time.Equal(date1) {
t.Errorf("expected %v, received %v", date1, tpc.TimePeriods[0].Time)
}
}