-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_test.go
266 lines (244 loc) · 6.84 KB
/
cli_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package vote_test
import (
"context"
"io"
"os"
"time"
"github.com/inklabs/cqrs/cqrstest"
"github.com/inklabs/vote"
)
func ExampleApp_cliRoot_help() {
app := newTestApp()
defer app.Stop()
cmd := vote.GetCobraRootCommand(app)
cmd.SetArgs([]string{"-h"})
_ = cmd.Execute()
// Output:
// CLI application
//
// Usage:
// cli [flags]
// cli [command]
//
// Available Commands:
// async-command-status Async Command Status
// completion Generate the autocompletion script for the specified shell
// election 9 actions: [CastVote, CloseElectionByOwner, CommenceElection, GetElection, GetElectionResults, GetProposalDetails, ListOpenElections, ListProposals, MakeProposal]
// help Help about any command
//
// Flags:
// -h, --help help for cli
//
// Use "cli [command] --help" for more information about a command.
}
func ExampleApp_cliElection_help() {
app := newTestApp()
defer app.Stop()
cmd := vote.GetCobraRootCommand(app)
cmd.SetOut(NewTrimmingWriter(os.Stdout))
cmd.SetArgs([]string{"election", "-h"})
_ = cmd.Execute()
// Output:
// election subdomain actions
//
// Usage:
// cli election [command]
//
// Available Commands:
// CastVote
// CloseElectionByOwner
// CommenceElection
// GetElection
// GetElectionResults
// GetProposalDetails
// ListOpenElections
// ListProposals
// MakeProposal
//
// Flags:
// -h, --help help for election
//
// Use "cli election [command] --help" for more information about a command.
}
func ExampleApp_cliElectionCastVote_help() {
app := newTestApp()
defer app.Stop()
cmd := vote.GetCobraRootCommand(app)
cmd.SetOut(NewTrimmingWriter(os.Stdout))
cmd.SetArgs([]string{"election", "CastVote", "-h"})
_ = cmd.Execute()
// Output:
// CastVote casts a ballot for a given ElectionID. RankedProposalIDs contains the
// ranked candidates in order of preference: first, second, third and so forth. If your
// first choice doesn’t have a chance to win, your ballot counts for your next choice.
//
// Usage:
// cli election CastVote [flags]
//
// Flags:
// --ElectionID string
// --RankedProposalIDs strings
// --UserID string
// --VoteID string
// -h, --help help for CastVote
}
func ExampleApp_cliElectionListOpenElections_help() {
app := newTestApp()
defer app.Stop()
cmd := vote.GetCobraRootCommand(app)
cmd.SetOut(NewTrimmingWriter(os.Stdout))
cmd.SetArgs([]string{"election", "ListOpenElections", "-h"})
_ = cmd.Execute()
// Output:
// ListOpenElections returns a paginated result of elections that are still open.
//
// Returns:
// election.ListOpenElectionsResponse {
// OpenElections []OpenElection
// TotalResults int
// }
//
// Usage:
// cli election ListOpenElections [flags]
//
// Flags:
// --ItemsPerPage int (optional) 1 - 50
// --Page int (optional) >= 1
// --SortBy string (optional) Name, CommencedAt
// --SortDirection string (optional) ascending, descending
// -h, --help help for ListOpenElections
}
func ExampleApp_cliElectionCloseElectionByOwner() {
recordingEventDispatcher := cqrstest.NewRecordingEventDispatcher()
app := newTestApp(
vote.WithEventDispatcher(recordingEventDispatcher),
)
defer app.Stop()
recordingEventDispatcher.Add(4)
cmd := vote.GetCobraRootCommand(app)
cmd.SetOut(NewTrimmingWriter(os.Stdout))
cmd.SetArgs([]string{"election", "CommenceElection",
"--ElectionID", "E1",
"--Name", "Election Name",
"--Description", "Election Description",
"--OrganizerUserID", "U1",
})
_ = cmd.Execute()
cmd.SetArgs([]string{"election", "MakeProposal",
"--ElectionID", "E1",
"--ProposalID", "P1",
"--Name", "Proposal Name",
"--Description", "Proposal Description",
"--OwnerUserID", "U2",
})
_ = cmd.Execute()
cmd.SetArgs([]string{"election", "CastVote",
"--VoteID", "V1",
"--ElectionID", "E1",
"--UserID", "U3",
"--RankedProposalIDs", "P1",
})
_ = cmd.Execute()
cmd.SetArgs([]string{"election", "CloseElectionByOwner", "--ID", "AC1", "--ElectionID", "E1"})
_ = cmd.Execute()
ctx, done := context.WithTimeout(context.Background(), 5*time.Second)
defer done()
recordingEventDispatcher.Wait(ctx)
cmd.SetArgs([]string{"async-command-status", "--ID", "AC1", "--logs"})
_ = cmd.Execute()
// Output:
// Response: cqrs.CommandResponse {
// "Status": "OK"
// }
// Response: cqrs.CommandResponse {
// "Status": "OK"
// }
// Response: cqrs.CommandResponse {
// "Status": "OK"
// }
// Response: cqrs.AsyncCommandResponse {
// "ID": "AC1",
// "Status": "QUEUED",
// "HasBeenQueued": true
// }
// AsyncCommandStatus: *cqrs.AsyncCommandStatus {
// "Command": {
// "ID": "AC1",
// "ElectionID": "E1"
// },
// "CreatedAt": 1699900003,
// "ModifiedAt": 1699900007,
// "StartedAtMicro": 1699900004000000,
// "FinishedAtMicro": 1699900007000000,
// "ExecutionDuration": "3s",
// "TotalToProcess": 1,
// "TotalProcessed": 1,
// "PercentDone": 100,
// "IsSuccess": true,
// "IsFinished": true
// }
// AsyncCommandLogs: []cqrs.AsyncCommandLog [
// {
// "Type": "INFO",
// "CreatedAtMicro": 1699900006000000,
// "Message": "Closing election with winner: P1"
// }
// ]
}
func ExampleApp_cliListOpenElections() {
recordingEventDispatcher := cqrstest.NewRecordingEventDispatcher()
app := newTestApp(
vote.WithEventDispatcher(recordingEventDispatcher),
)
defer app.Stop()
recordingEventDispatcher.Add(4)
cmd := vote.GetCobraRootCommand(app)
cmd.SetOut(io.Discard)
cmd.SetArgs([]string{"election", "CommenceElection",
"--ElectionID", "E1",
"--Name", "Election Name 1",
"--Description", "Election Description 1",
"--OrganizerUserID", "U1",
})
_ = cmd.Execute()
cmd.SetArgs([]string{"election", "CommenceElection",
"--ElectionID", "E2",
"--Name", "Election Name 2",
"--Description", "Election Description 2",
"--OrganizerUserID", "U1",
})
_ = cmd.Execute()
cmd.SetArgs([]string{"election", "CommenceElection",
"--ElectionID", "E3",
"--Name", "Election Name 3",
"--Description", "Election Description 3",
"--OrganizerUserID", "U1",
})
_ = cmd.Execute()
cmd.SetOut(NewTrimmingWriter(os.Stdout))
cmd.SetArgs([]string{"election", "ListOpenElections",
"--ItemsPerPage", "2",
"--Page", "1",
})
_ = cmd.Execute()
// Output:
// Response: election.ListOpenElectionsResponse {
// "OpenElections": [
// {
// "ElectionID": "E1",
// "OrganizerUserID": "U1",
// "Name": "Election Name 1",
// "Description": "Election Description 1",
// "CommencedAt": 1699900000
// },
// {
// "ElectionID": "E2",
// "OrganizerUserID": "U1",
// "Name": "Election Name 2",
// "Description": "Election Description 2",
// "CommencedAt": 1699900001
// }
// ],
// "TotalResults": 3
// }
}