|
| 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 | +} |
0 commit comments