golift.io/starr@v1.0.0/lidarr/tag_test.go (about) 1 package lidarr_test 2 3 import ( 4 "net/http" 5 "path" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "golift.io/starr" 10 "golift.io/starr/lidarr" 11 "golift.io/starr/starrtest" 12 ) 13 14 func TestGetTags(t *testing.T) { 15 t.Parallel() 16 17 tests := []*starrtest.MockData{ 18 { 19 Name: "200", 20 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag"), 21 ExpectedMethod: "GET", 22 ResponseStatus: 200, 23 ResponseBody: `[{"label": "flac","id": 1},{"label": "mp3","id": 2}]`, 24 WithResponse: []*starr.Tag{ 25 { 26 Label: "flac", 27 ID: 1, 28 }, 29 { 30 Label: "mp3", 31 ID: 2, 32 }, 33 }, 34 WithError: nil, 35 }, 36 { 37 Name: "404", 38 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag"), 39 ExpectedMethod: "GET", 40 ResponseStatus: 404, 41 ResponseBody: `{"message": "NotFound"}`, 42 WithError: &starr.ReqError{Code: http.StatusNotFound}, 43 WithResponse: []*starr.Tag(nil), 44 }, 45 } 46 47 for _, test := range tests { 48 test := test 49 t.Run(test.Name, func(t *testing.T) { 50 t.Parallel() 51 mockServer := test.GetMockServer(t) 52 client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 53 output, err := client.GetTags() 54 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 55 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 56 }) 57 } 58 } 59 60 func TestGetTag(t *testing.T) { 61 t.Parallel() 62 63 tests := []*starrtest.MockData{ 64 { 65 Name: "200", 66 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"), 67 ExpectedMethod: "GET", 68 ResponseStatus: 200, 69 WithRequest: 1, 70 ResponseBody: `{"label": "flac","id": 1}`, 71 WithResponse: &starr.Tag{ 72 Label: "flac", 73 ID: 1, 74 }, 75 WithError: nil, 76 }, 77 { 78 Name: "404", 79 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"), 80 ExpectedMethod: "GET", 81 ResponseStatus: 404, 82 WithRequest: 1, 83 ResponseBody: `{"message": "NotFound"}`, 84 WithResponse: (*starr.Tag)(nil), 85 WithError: &starr.ReqError{Code: http.StatusNotFound}, 86 }, 87 } 88 89 for _, test := range tests { 90 test := test 91 t.Run(test.Name, func(t *testing.T) { 92 t.Parallel() 93 mockServer := test.GetMockServer(t) 94 client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 95 output, err := client.GetTag(test.WithRequest.(int)) 96 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 97 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 98 }) 99 } 100 } 101 102 func TestAddTag(t *testing.T) { 103 t.Parallel() 104 105 tests := []*starrtest.MockData{ 106 { 107 Name: "200", 108 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag"), 109 ExpectedMethod: "POST", 110 ResponseStatus: 200, 111 WithRequest: &starr.Tag{ 112 Label: "flac", 113 }, 114 ExpectedRequest: `{"label":"flac"}` + "\n", 115 ResponseBody: `{"label": "flac","id": 1}`, 116 WithResponse: &starr.Tag{ 117 Label: "flac", 118 ID: 1, 119 }, 120 WithError: nil, 121 }, 122 { 123 Name: "200", 124 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag"), 125 ExpectedMethod: "POST", 126 ResponseStatus: 404, 127 WithRequest: &starr.Tag{ 128 Label: "flac", 129 }, 130 ExpectedRequest: `{"label":"flac"}` + "\n", 131 ResponseBody: `{"message": "NotFound"}`, 132 WithError: &starr.ReqError{Code: http.StatusNotFound}, 133 WithResponse: (*starr.Tag)(nil), 134 }, 135 } 136 137 for _, test := range tests { 138 test := test 139 t.Run(test.Name, func(t *testing.T) { 140 t.Parallel() 141 mockServer := test.GetMockServer(t) 142 client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 143 output, err := client.AddTag(test.WithRequest.(*starr.Tag)) 144 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 145 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 146 }) 147 } 148 } 149 150 func TestUpdateTag(t *testing.T) { 151 t.Parallel() 152 153 tests := []*starrtest.MockData{ 154 { 155 Name: "200", 156 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"), 157 ExpectedMethod: "PUT", 158 ResponseStatus: 200, 159 WithRequest: &starr.Tag{ 160 ID: 1, 161 Label: "flac", 162 }, 163 ExpectedRequest: `{"id":1,"label":"flac"}` + "\n", 164 ResponseBody: `{"id": 1,"label": "flac"}`, 165 WithResponse: &starr.Tag{ 166 ID: 1, 167 Label: "flac", 168 }, 169 WithError: nil, 170 }, 171 { 172 Name: "404", 173 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"), 174 ExpectedMethod: "PUT", 175 ResponseStatus: 404, 176 WithRequest: &starr.Tag{ 177 ID: 1, 178 Label: "flac", 179 }, 180 ExpectedRequest: `{"id":1,"label":"flac"}` + "\n", 181 ResponseBody: `{"message": "NotFound"}`, 182 WithError: &starr.ReqError{Code: http.StatusNotFound}, 183 WithResponse: (*starr.Tag)(nil), 184 }, 185 } 186 187 for _, test := range tests { 188 test := test 189 t.Run(test.Name, func(t *testing.T) { 190 t.Parallel() 191 mockServer := test.GetMockServer(t) 192 client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 193 output, err := client.UpdateTag(test.WithRequest.(*starr.Tag)) 194 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 195 assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected") 196 }) 197 } 198 } 199 200 func TestDeleteTag(t *testing.T) { 201 t.Parallel() 202 203 tests := []*starrtest.MockData{ 204 { 205 Name: "200", 206 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"), 207 ExpectedMethod: "DELETE", 208 WithRequest: 1, 209 ResponseStatus: 200, 210 ResponseBody: "{}", 211 WithError: nil, 212 }, 213 { 214 Name: "404", 215 ExpectedPath: path.Join("/", starr.API, lidarr.APIver, "tag", "1"), 216 ExpectedMethod: "DELETE", 217 WithRequest: 1, 218 ResponseStatus: 404, 219 ResponseBody: `{"message": "NotFound"}`, 220 WithError: &starr.ReqError{Code: http.StatusNotFound}, 221 }, 222 } 223 224 for _, test := range tests { 225 test := test 226 t.Run(test.Name, func(t *testing.T) { 227 t.Parallel() 228 mockServer := test.GetMockServer(t) 229 client := lidarr.New(starr.New("mockAPIkey", mockServer.URL, 0)) 230 err := client.DeleteTag(test.WithRequest.(int)) 231 assert.ErrorIs(t, err, test.WithError, "error is not the same as expected") 232 }) 233 } 234 }