Skip to content

Commit 054f8a9

Browse files
committed
add unit test for create command
1 parent 2c40096 commit 054f8a9

File tree

3 files changed

+95
-9
lines changed

3 files changed

+95
-9
lines changed

cmd/create_command.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"flag"
66
"fmt"
7+
"io"
78
"os"
89
"strings"
910
"time"
@@ -22,7 +23,7 @@ const (
2223
JSON
2324
)
2425

25-
func RunCreateShiftPlan(arguments []string) error {
26+
func RunCreateShiftPlan(writer io.Writer, arguments []string) error {
2627
enums := map[string]Format{"CVS": CVS, "Table": Table, "json": JSON}
2728
exporters := map[Format]func() apis.Exporter{
2829
Table: func() apis.Exporter {
@@ -55,12 +56,12 @@ func RunCreateShiftPlan(arguments []string) error {
5556
createCommand.StringVar(primaryRules, "primary-rules", "vacation", "Rule to decide which employee should be on-call for the next shift")
5657
createCommand.StringVar(secondaryRules, "secondary-rules", "vacation", "Rule to decide which employee should be on-call for the next shift")
5758
createCommand.Usage = func() {
58-
fmt.Fprintf(os.Stdout, "Create on-call schedule\n")
59-
fmt.Fprintf(os.Stdout, "\nUsage\n")
60-
fmt.Fprintf(os.Stdout, " %s create [flags]\n", os.Args[0])
61-
fmt.Fprintf(os.Stdout, "\nFlags:\n")
59+
fmt.Fprintf(writer, "Create on-call schedule\n")
60+
fmt.Fprintf(writer, "\nUsage\n")
61+
fmt.Fprintf(writer, " %s create [flags]\n", os.Args[0])
62+
fmt.Fprintf(writer, "\nFlags:\n")
6263
flag.PrintDefaults()
63-
fmt.Fprintf(os.Stdout, "\nUse \"%s create -h\" for more information about a command\n", os.Args[0])
64+
fmt.Fprintf(writer, "\nUse \"%s create -h\" for more information about a command\n", os.Args[0])
6465
}
6566

6667
if err := createCommand.Parse(arguments); err != nil {
@@ -101,7 +102,7 @@ func RunCreateShiftPlan(arguments []string) error {
101102
return fmt.Errorf("can not create on-call schedule: %w", err)
102103
}
103104

104-
if err := exporters[outputFormat]().Write(plan, os.Stdout); err != nil {
105+
if err := exporters[outputFormat]().Write(plan, writer); err != nil {
105106
return fmt.Errorf("unexpecting error: %w", err)
106107
}
107108

cmd/create_command_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestRunCreateShiftPlan(t *testing.T) {
12+
dir := t.TempDir()
13+
tempFilePath := filepath.Join(dir, "team.json")
14+
content := `{"employees": [{"id": "joe@example.com", "name": "Joe"}, {"id": "jan@example.com", "name": "Jan"}]}`
15+
if err := os.WriteFile(tempFilePath, []byte(content), 0o600); err != nil {
16+
t.FailNow()
17+
}
18+
19+
type args struct {
20+
arguments []string
21+
}
22+
tests := []struct {
23+
name string
24+
args args
25+
wantWriter string
26+
wantErrMsg string
27+
}{
28+
{
29+
name: "Print Usage when required flags are missing",
30+
wantWriter: "Usage",
31+
wantErrMsg: "missing required flag: start,end,team-file",
32+
},
33+
{
34+
name: "Print Usage when use help flag",
35+
args: args{
36+
arguments: []string{
37+
"-h",
38+
},
39+
},
40+
wantWriter: "Usage",
41+
wantErrMsg: "",
42+
},
43+
{
44+
name: "Print an on-call schedule in JSON format",
45+
args: args{
46+
arguments: []string{
47+
"--start", "2024-01-01 00:00:00",
48+
"--end", "2024-01-08 00:00:00",
49+
"--team-file", tempFilePath,
50+
"--output", "json",
51+
},
52+
},
53+
wantWriter: `[{"start":"2024-01-01T00:00:00Z","end":"2024-01-08T00:00:00Z","primary":"joe@example.com","secondary":"jan@example.com"}]`,
54+
wantErrMsg: "",
55+
},
56+
{
57+
name: "Print an error if shift plan cannot be created if rules are too restricted",
58+
args: args{
59+
arguments: []string{
60+
"--start", "2024-01-01 00:00:00",
61+
"--end", "2024-01-15 00:00:00",
62+
"--team-file", tempFilePath,
63+
"--primary-rules", "minimumfourshiftgap",
64+
"--output", "json",
65+
},
66+
},
67+
wantWriter: "",
68+
wantErrMsg: "can not create on-call schedule: could not find available duty between 2024-01-08 00:00:00 +0000 UTC and 2024-01-15 00:00:00 +0000 UTC",
69+
},
70+
}
71+
for _, tt := range tests {
72+
t.Run(tt.name, func(t *testing.T) {
73+
writer := &bytes.Buffer{}
74+
err := RunCreateShiftPlan(writer, tt.args.arguments)
75+
if (err != nil) && tt.wantErrMsg != err.Error() {
76+
t.Errorf("RunCreateShiftPlan() error = %v, wantErr %v", err, tt.wantErrMsg)
77+
return
78+
}
79+
if gotWriter := writer.String(); !strings.Contains(gotWriter, tt.wantWriter) {
80+
t.Errorf("RunCreateShiftPlan() gotWriter = %v, want %v", gotWriter, tt.wantWriter)
81+
}
82+
})
83+
}
84+
}

cmd/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"flag"
66
"fmt"
7+
"io"
78
"os"
89
"slices"
910
)
@@ -13,7 +14,7 @@ var ErrCommandNotFound = errors.New("command not found")
1314
type command struct {
1415
name string
1516
description string
16-
run func([]string) error
17+
run func(io.Writer, []string) error
1718
}
1819

1920
func main() {
@@ -67,7 +68,7 @@ func runCommand(cmd string, commands []command) error {
6768
return fmt.Errorf("%w: %s", ErrCommandNotFound, cmd)
6869
}
6970

70-
if err := commands[cmdIdx].run(os.Args[2:]); err != nil {
71+
if err := commands[cmdIdx].run(os.Stdout, os.Args[2:]); err != nil {
7172
return fmt.Errorf("\n\n%w", err)
7273
}
7374

0 commit comments

Comments
 (0)