|
| 1 | +package gapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/gobs/pretty" |
| 7 | +) |
| 8 | + |
| 9 | +func TestMuteTimings(t *testing.T) { |
| 10 | + t.Run("get mute timings succeeds", func(t *testing.T) { |
| 11 | + server, client := gapiTestTools(t, 200, getMuteTimingsJSON) |
| 12 | + defer server.Close() |
| 13 | + |
| 14 | + mts, err := client.MuteTimings() |
| 15 | + |
| 16 | + if err != nil { |
| 17 | + t.Error(err) |
| 18 | + } |
| 19 | + t.Log(pretty.PrettyFormat(mts)) |
| 20 | + if len(mts) != 2 { |
| 21 | + t.Errorf("wrong number of mute timings returned, got %#v", mts) |
| 22 | + } |
| 23 | + if mts[0].Name != "timing one" { |
| 24 | + t.Errorf("incorrect name - expected %s on element %d, got %#v", "timing one", 0, mts) |
| 25 | + } |
| 26 | + if mts[1].Name != "another timing" { |
| 27 | + t.Errorf("incorrect name - expected %s on element %d, got %#v", "another timing", 1, mts) |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + t.Run("get mute timing succeeds", func(t *testing.T) { |
| 32 | + server, client := gapiTestTools(t, 200, muteTimingJSON) |
| 33 | + defer server.Close() |
| 34 | + |
| 35 | + mt, err := client.MuteTiming("timing one") |
| 36 | + |
| 37 | + if err != nil { |
| 38 | + t.Error(err) |
| 39 | + } |
| 40 | + t.Log(pretty.PrettyFormat(mt)) |
| 41 | + if mt.Name != "timing one" { |
| 42 | + t.Errorf("incorrect name - expected %s, got %#v", "timing one", mt) |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + t.Run("get non-existent mute timing fails", func(t *testing.T) { |
| 47 | + server, client := gapiTestTools(t, 404, muteTimingJSON) |
| 48 | + defer server.Close() |
| 49 | + |
| 50 | + mt, err := client.MuteTiming("does not exist") |
| 51 | + |
| 52 | + if err == nil { |
| 53 | + t.Errorf("expected error but got nil") |
| 54 | + t.Log(pretty.PrettyFormat(mt)) |
| 55 | + } |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("create mute timing succeeds", func(t *testing.T) { |
| 59 | + server, client := gapiTestTools(t, 201, muteTimingJSON) |
| 60 | + defer server.Close() |
| 61 | + mt := createMuteTiming() |
| 62 | + |
| 63 | + err := client.NewMuteTiming(&mt) |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + t.Error(err) |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + t.Run("update mute timing succeeds", func(t *testing.T) { |
| 71 | + server, client := gapiTestTools(t, 200, muteTimingJSON) |
| 72 | + defer server.Close() |
| 73 | + mt := createMuteTiming() |
| 74 | + mt.TimeIntervals[0].Weekdays = []WeekdayRange{"tuesday", "thursday"} |
| 75 | + |
| 76 | + err := client.UpdateMuteTiming(&mt) |
| 77 | + |
| 78 | + if err != nil { |
| 79 | + t.Error(err) |
| 80 | + } |
| 81 | + }) |
| 82 | + |
| 83 | + t.Run("delete mute timing succeeds", func(t *testing.T) { |
| 84 | + server, client := gapiTestTools(t, 204, muteTimingJSON) |
| 85 | + defer server.Close() |
| 86 | + |
| 87 | + err := client.DeleteMuteTiming("timing two") |
| 88 | + |
| 89 | + if err != nil { |
| 90 | + t.Error(err) |
| 91 | + } |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +func createMuteTiming() MuteTiming { |
| 96 | + return MuteTiming{ |
| 97 | + Name: "timing two", |
| 98 | + TimeIntervals: []TimeInterval{ |
| 99 | + { |
| 100 | + Weekdays: []WeekdayRange{"monday", "wednesday"}, |
| 101 | + Months: []MonthRange{"1:3", "4"}, |
| 102 | + Years: []YearRange{"2022", "2023"}, |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +const getMuteTimingsJSON = ` |
| 109 | +[ |
| 110 | + { |
| 111 | + "name": "timing one", |
| 112 | + "time_intervals": [ |
| 113 | + { |
| 114 | + "times": [ |
| 115 | + { |
| 116 | + "start_time": "13:13", |
| 117 | + "end_time": "15:15" |
| 118 | + } |
| 119 | + ], |
| 120 | + "weekdays": [ |
| 121 | + "monday:wednesday" |
| 122 | + ], |
| 123 | + "months": [ |
| 124 | + "1" |
| 125 | + ] |
| 126 | + } |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "name": "another timing", |
| 131 | + "time_intervals": [ |
| 132 | + { |
| 133 | + "days_of_month": [ |
| 134 | + "1" |
| 135 | + ], |
| 136 | + "years": [ |
| 137 | + "2030" |
| 138 | + ] |
| 139 | + } |
| 140 | + ] |
| 141 | + } |
| 142 | +]` |
| 143 | + |
| 144 | +const muteTimingJSON = ` |
| 145 | +{ |
| 146 | + "name": "timing one", |
| 147 | + "time_intervals": [ |
| 148 | + { |
| 149 | + "times": [ |
| 150 | + { |
| 151 | + "start_time": "13:13", |
| 152 | + "end_time": "15:15" |
| 153 | + } |
| 154 | + ], |
| 155 | + "weekdays": [ |
| 156 | + "monday:wednesday" |
| 157 | + ], |
| 158 | + "months": [ |
| 159 | + "1" |
| 160 | + ] |
| 161 | + } |
| 162 | + ] |
| 163 | +}` |
0 commit comments