(QoL) Kline fifteen second support & improved test coverage (#524)

* added fiftesensecond to word output increased TotalCandlesPerInterval test coverage

* moved end date forward to next day

* reworded help output for extended command
This commit is contained in:
Andrew
2020-07-10 16:00:40 +10:00
committed by GitHub
parent 6df19bfaf7
commit 409fc6478e
3 changed files with 140 additions and 15 deletions

View File

@@ -162,6 +162,8 @@ func (i Interval) Short() string {
// durationToWord returns english version of interval
func durationToWord(in Interval) string {
switch in {
case FifteenSecond:
return "fifteensecond"
case OneMin:
return "onemin"
case ThreeMin:
@@ -208,6 +210,8 @@ func durationToWord(in Interval) string {
// TotalCandlesPerInterval turns total candles per period for interval
func TotalCandlesPerInterval(start, end time.Time, interval Interval) (out uint32) {
switch interval {
case FifteenSecond:
out = uint32(end.Sub(start).Seconds() / 15)
case OneMin:
out = uint32(end.Sub(start).Minutes())
case ThreeMin:
@@ -242,6 +246,8 @@ func TotalCandlesPerInterval(start, end time.Time, interval Interval) (out uint3
out = uint32(end.Sub(start).Hours()) / (24 * 7)
case TwoWeek:
out = uint32(end.Sub(start).Hours() / (24 * 14))
case OneMonth:
out = uint32(end.Sub(start).Hours() / (24 * 30))
case OneYear:
out = uint32(end.Sub(start).Hours() / 8760)
}

View File

@@ -138,6 +138,10 @@ func TestDurationToWord(t *testing.T) {
name string
interval Interval
}{
{
"FifteenSecond",
FifteenSecond,
},
{
"OneMin",
OneMin,
@@ -210,6 +214,10 @@ func TestDurationToWord(t *testing.T) {
"OneMonth",
OneMonth,
},
{
"OneYear",
OneYear,
},
{
"notfound",
Interval(time.Hour * 1337),
@@ -241,16 +249,123 @@ func TestKlineErrors(t *testing.T) {
}
func TestTotalCandlesPerInterval(t *testing.T) {
end := time.Now()
start := end.AddDate(-1, 0, 0)
start := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
v := TotalCandlesPerInterval(start, end, OneYear)
if v != 1 {
t.Fatalf("unexpected result expected 1 received %v", v)
testCases := []struct {
name string
interval Interval
expected uint32
}{
{
"FifteenSecond",
FifteenSecond,
2102400,
},
{
"OneMin",
OneMin,
525600,
},
{
"ThreeMin",
ThreeMin,
175200,
},
{
"FiveMin",
FiveMin,
105120,
},
{
"TenMin",
TenMin,
52560,
},
{
"FifteenMin",
FifteenMin,
35040,
},
{
"ThirtyMin",
ThirtyMin,
17520,
},
{
"OneHour",
OneHour,
8760,
},
{
"TwoHour",
TwoHour,
4380,
},
{
"FourHour",
FourHour,
2190,
},
{
"SixHour",
SixHour,
1460,
},
{
"EightHour",
OneHour * 8,
1095,
},
{
"TwelveHour",
TwelveHour,
730,
},
{
"OneDay",
OneDay,
365,
},
{
"ThreeDay",
ThreeDay,
121,
},
{
"FifteenDay",
FifteenDay,
24,
},
{
"OneWeek",
OneWeek,
52,
},
{
"TwoWeek",
TwoWeek,
26,
},
{
"OneMonth",
OneMonth,
12,
},
{
"OneYear",
OneYear,
1,
},
}
v = TotalCandlesPerInterval(start, end, FifteenDay)
if v != 24 {
t.Fatalf("unexpected result expected 24 received %v", v)
for x := range testCases {
test := testCases[x]
t.Run(test.name, func(t *testing.T) {
v := TotalCandlesPerInterval(start, end, test.interval)
if v != test.expected {
t.Fatalf("%v: received %v expected %v", test.name, v, test.expected)
}
})
}
}