forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscenario_test.go
More file actions
177 lines (150 loc) · 4.75 KB
/
scenario_test.go
File metadata and controls
177 lines (150 loc) · 4.75 KB
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
package codehost_testing
import (
"context"
"errors"
"testing"
)
func testAction(name string) *Action {
return &Action{
Name: name,
Apply: func(ctx context.Context) error {
return nil
},
Teardown: func(ctx context.Context) error {
return nil
},
}
}
func TestScenarioApplyAndTeardown(t *testing.T) {
t.Run("Applied actions updates nextActionIdx", func(t *testing.T) {
scenario := &GitHubScenario{
id: "testing-id",
t: t,
client: nil,
actions: []*Action{},
reporter: NoopReporter{},
nextActionIdx: 0,
}
scenario.Append(testAction("t1"), testAction("t2"))
if len(scenario.actions) != 2 {
t.Errorf("actions not appended - got %d wanted %d", len(scenario.actions), 2)
}
err := scenario.Apply(context.TODO())
if err != nil {
t.Fatalf("failed to apply test scenario with mock actions: %v", err)
}
if scenario.nextActionIdx != 2 {
t.Errorf("actions applied count mismatch - got %d wanted %d", scenario.nextActionIdx, 2)
}
if !scenario.IsApplied() {
t.Error("all actions have been applied thus IsApplied should be true")
}
})
t.Run("Next Action Idx at 1 if remaining action errors", func(t *testing.T) {
scenario := &GitHubScenario{
id: "testing-id",
t: t,
client: nil,
actions: []*Action{},
reporter: NoopReporter{},
nextActionIdx: 0,
}
errAction := testAction("err1")
fakeErr := errors.New("fake error")
errAction.Apply = func(ctx context.Context) error {
return fakeErr
}
scenario.Append(testAction("t1"), errAction)
if len(scenario.actions) != 2 {
t.Errorf("actions not appended - got %d wanted %d", len(scenario.actions), 2)
}
err := scenario.Apply(context.TODO())
if err != nil && !errors.Is(err, fakeErr) {
t.Fatalf("failed to apply test scenario with mock actions: %v", err)
}
if scenario.nextActionIdx != 1 {
t.Errorf("actions applied count mismatch - got %d wanted %d", scenario.nextActionIdx, 1)
}
if scenario.IsApplied() {
t.Error("not all actions have been applied thus IsApplied should be false")
}
err = scenario.Teardown(context.TODO())
if err != nil {
t.Fatalf("teardown not expected to fail here: %v", err)
}
if scenario.IsApplied() {
t.Error("after teardown, IsApplied should be false")
}
if scenario.nextActionIdx != 0 {
t.Errorf("after teardown nextActionIdx should be 0 - got %d", scenario.nextActionIdx)
}
})
t.Run("3 Actions with 1 skipped teardown", func(t *testing.T) {
scenario := &GitHubScenario{
id: "testing-id",
t: t,
client: nil,
actions: []*Action{},
reporter: NoopReporter{},
nextActionIdx: 0,
}
skipAction := testAction("s2")
skipAction.Teardown = nil
scenario.Append(testAction("t1"), skipAction, testAction("t3"))
if len(scenario.actions) != 3 {
t.Errorf("actions not appended - got %d wanted %d", len(scenario.actions), 2)
}
scenario.Apply(context.TODO())
if scenario.nextActionIdx != 3 {
t.Errorf("actions applied count mismatch - got %d wanted %d", scenario.nextActionIdx, 1)
}
if !scenario.IsApplied() {
t.Error("all actions should be applied")
}
err := scenario.Teardown(context.TODO())
if err != nil {
t.Fatalf("teardown not expected to fail here: %v", err)
}
if scenario.IsApplied() {
t.Error("after teardown, scenario should not be Applied")
}
if scenario.nextActionIdx != 0 {
t.Errorf("after teardown nextActionIdx should be 0 - got %d", scenario.nextActionIdx)
}
})
t.Run("4 Actions with 2 failed teardown", func(t *testing.T) {
scenario := &GitHubScenario{
id: "testing-id",
t: t,
client: nil,
actions: []*Action{},
reporter: NoopReporter{},
nextActionIdx: 0,
}
errTeardown := func(_ context.Context) error { return errors.New("fake") }
errAction := testAction("e2")
errAction.Teardown = errTeardown
scenario.Append(testAction("t1"), errAction, testAction("t3"))
errTeardown = func(_ context.Context) error { return errors.New("fake") }
errAction = testAction("e4")
errAction.Teardown = errTeardown
scenario.Append(errAction)
if len(scenario.actions) != 4 {
t.Errorf("actions not appended - got %d wanted %d", len(scenario.actions), 2)
}
scenario.Apply(context.TODO())
if scenario.nextActionIdx != 4 {
t.Errorf("actions applied count mismatch - got %d wanted %d", scenario.nextActionIdx, 4)
}
if !scenario.IsApplied() {
t.Error("all actions should be applied")
}
scenario.Teardown(context.TODO())
if scenario.IsApplied() {
t.Error("after teardown, scenario should not be Applied")
}
if scenario.nextActionIdx != 0 {
t.Errorf("after teardown nextActionIdx should be 0 - got %d", scenario.nextActionIdx)
}
})
}