-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidx_test.go
166 lines (137 loc) · 5.44 KB
/
idx_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
package indeks
import (
"reflect"
"testing"
"time"
)
var (
testIdx *Idx
testAction *Action
)
func init() {
testIdx = &Idx{Name: "idx1", Desc: "One of idx", DefaultDuration: time.Duration(24) * time.Hour, DefaultPoint: 1, Actions: nil}
testAction = &Action{Idx: testIdx}
}
func TestInit(t *testing.T) {
idx := &Idx{Name: "idx1", Desc: "One of idx", DefaultDuration: time.Duration(24) * time.Hour, DefaultPoint: 1, Actions: nil}
if !reflect.DeepEqual(testIdx, idx) {
t.Fatalf("Idx Init Failed\nhave: %v\nwant: %v", idx, testIdx)
}
}
func TestCreateAction(t *testing.T) {
idx := &Idx{Name: "idx1", Desc: "One of idx", DefaultPoint: 1, Actions: nil}
action := CreateAction(idx, time.Time{})
action.Idx.Name = "changed"
if !reflect.DeepEqual(idx, action.Idx) {
t.Fatalf("Failed action links idx\nhave: %v\nwant: %v", action.Idx, idx)
}
if !reflect.DeepEqual(idx.Actions[0], action) {
t.Fatalf("Failed add action to idx\nhave: %v\nwant: %v", idx.Actions[0], action)
}
}
func TestCheckAction(t *testing.T) {
action := &Action{Idx: testIdx}
action.Idx = nil
action.TargetTime = time.Date(2018, 1, 13, 0, 0, 0, 0, time.UTC)
if action.Result != ResultUncheck {
t.Fatalf("Failed check unknown action\nhave: %v\nwant: %v", action.Result, ResultUncheck)
}
d := time.Date(2018, 1, 12, 0, 0, 0, 0, time.UTC)
action1 := Do(action, &d)
if action1.Result != ResultOK {
t.Fatalf("Failed check ok action\nhave: %v\nwant: %v", action1.Result, ResultOK)
}
d = time.Date(2018, 1, 14, 0, 0, 0, 0, time.UTC)
action2 := Do(action, &d)
if action2.Result != ResultNG {
t.Fatalf("Failed check ok action\nhave: %v\nwant: %v", action2.Result, ResultOK)
}
if !reflect.DeepEqual(action2.ActualTime, d) {
t.Fatalf("ActualTime is not Matched\nhave: %v\nwant: %v", action2.ActualTime, d)
}
}
func TestChangeTargetTimeAction(t *testing.T) {
action := &Action{Idx: testIdx}
action.TargetTime = time.Date(2018, 1, 13, 0, 0, 0, 0, time.UTC)
d := time.Date(2018, 1, 12, 0, 0, 0, 0, time.UTC)
changed := ChangeTargetTime(action, &d, "reschedule")
if reflect.DeepEqual(d, changed.TargetTime) {
t.Fatalf("Failed Changed TargetTime\nhave: %v\nwant: %v", d, changed.TargetTime)
}
if changed.Result != ResultUncheck {
t.Fatalf("changed ResultStatus is not Uncheck: %v", changed.Result)
}
if action.Result != ResultChanged {
t.Fatalf("original ResultStatus is not ResultChanged: %v", action.Result)
}
}
func TestRemoveAction(t *testing.T) {
raIdx := &Idx{Name: "idxra", Desc: "idxra", DefaultPoint: 1, Actions: nil}
action := CreateAction(raIdx, time.Date(2018, 1, 13, 0, 0, 0, 0, time.UTC))
action2 := CreateAction(raIdx, time.Date(2018, 1, 14, 0, 0, 0, 0, time.UTC))
raIdx = RemoveAction(raIdx, action)
if len(raIdx.Actions) != 1 {
t.Fatalf("Removed raIdx not 1: %v", len(raIdx.Actions))
}
if !reflect.DeepEqual(action2, raIdx.Actions[0]) {
t.Fatalf("Failed match remained action\nhave: %v\nwant: %v", raIdx.Actions[0], action2)
}
}
func TestSumActualPointIdx(t *testing.T) {
spIdx := &Idx{Name: "spidx", Desc: "spidx", DefaultPoint: 1, Actions: nil}
action1 := CreateAction(spIdx, time.Date(2018, 1, 13, 0, 0, 0, 0, time.UTC))
action1.Point = 2
action2 := CreateAction(spIdx, time.Date(2018, 1, 14, 0, 0, 0, 0, time.UTC))
CreateAction(spIdx, time.Date(2018, 1, 15, 0, 0, 0, 0, time.UTC))
t1 := time.Date(2018, 1, 12, 0, 0, 0, 0, time.UTC)
t2 := time.Date(2018, 1, 13, 0, 0, 0, 0, time.UTC)
Do(action1, &t1)
Do(action2, &t2)
start := time.Date(2018, 1, 10, 0, 0, 0, 0, time.UTC)
expected := 3
actual := SumActualPoint(spIdx, start, nil)
if expected != actual {
t.Fatal("Sum result not match\nhave %v\nwant %v", actual, expected)
}
}
func TestSumTargetPointIdx(t *testing.T) {
spIdx := &Idx{Name: "spidx", Desc: "spidx", DefaultPoint: 1, Actions: nil}
action1 := CreateAction(spIdx, time.Date(2018, 1, 13, 0, 0, 0, 0, time.UTC))
action1.Point = 2
action2 := CreateAction(spIdx, time.Date(2018, 1, 14, 0, 0, 0, 0, time.UTC))
action3 := CreateAction(spIdx, time.Date(2018, 1, 15, 0, 0, 0, 0, time.UTC))
t1 := time.Date(2018, 1, 12, 0, 0, 0, 0, time.UTC)
t2 := time.Date(2018, 1, 13, 0, 0, 0, 0, time.UTC)
t3 := time.Date(2018, 1, 16, 0, 0, 0, 0, time.UTC)
Do(action1, &t1)
Do(action2, &t2)
ChangeTargetTime(action3, &t3, "test")
start := time.Date(2018, 1, 13, 1, 0, 0, 0, time.UTC)
end := time.Date(2018, 1, 17, 0, 0, 0, 0, time.UTC)
expected := 2
actual := SumTargetPoint(spIdx, start, &end)
if expected != actual {
t.Fatalf("Sum result not match\nhave %v\nwant %v", actual, expected)
}
}
func TestSummaryPointIdx(t *testing.T) {
spIdx := &Idx{Name: "spidx", Desc: "spidx", DefaultPoint: 1, Actions: nil}
action1 := CreateAction(spIdx, time.Date(2018, 1, 13, 0, 0, 0, 0, time.UTC))
action1.Point = 2
action2 := CreateAction(spIdx, time.Date(2018, 1, 15, 0, 0, 0, 0, time.UTC))
action2.Point = 3
action3 := CreateAction(spIdx, time.Date(2018, 1, 16, 0, 0, 0, 0, time.UTC))
t1 := time.Date(2018, 1, 12, 0, 0, 0, 0, time.UTC)
t2 := time.Date(2018, 1, 14, 0, 0, 0, 0, time.UTC)
t3 := time.Date(2018, 1, 17, 0, 0, 0, 0, time.UTC)
Do(action1, &t1)
Do(action2, &t2)
ChangeTargetTime(action3, &t3, "test")
start := time.Date(2018, 1, 13, 1, 0, 0, 0, time.UTC)
end := time.Date(2018, 1, 18, 0, 0, 0, 0, time.UTC)
expected := Summary{Target: 4, Actual: 3, Ratio: 0.75, TargetC: 2, ActualC: 1, RatioC: 0.5}
actual := SummaryPoint(spIdx, start, &end)
if reflect.DeepEqual(expected, actual) {
t.Fatalf("Sum result not match\nhave %v\nwant %v", actual, expected)
}
}