golift.io/starr@v1.0.0/radarr/naming_test.go (about)

     1  package radarr_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/radarr"
    11  	"golift.io/starr/starrtest"
    12  )
    13  
    14  const namingBody = `{
    15  	"renameMovies": true,
    16  	"replaceIllegalCharacters": true,
    17  	"colonReplacementFormat": "delete",
    18  	"standardMovieFormat": "{Movie.Title}.{Release.Year}.{Quality.Title}",
    19  	"movieFolderFormat": "{Movie Title} ({Release Year})",
    20  	"includeQuality": true,
    21  	"replaceSpaces": true,
    22  	"id": 1
    23    }`
    24  
    25  func TestGetNaming(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	tests := []*starrtest.MockData{
    29  		{
    30  			Name:           "200",
    31  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "config", "naming"),
    32  			ExpectedMethod: "GET",
    33  			ResponseStatus: 200,
    34  			ResponseBody:   namingBody,
    35  			WithResponse: &radarr.Naming{
    36  				ID:                       1,
    37  				ReplaceIllegalCharacters: true,
    38  				IncludeQuality:           true,
    39  				ReplaceSpaces:            true,
    40  				RenameMovies:             true,
    41  				ColonReplacementFormat:   "delete",
    42  				StandardMovieFormat:      "{Movie.Title}.{Release.Year}.{Quality.Title}",
    43  				MovieFolderFormat:        "{Movie Title} ({Release Year})",
    44  			},
    45  			WithError: nil,
    46  		},
    47  		{
    48  			Name:           "404",
    49  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "config", "naming"),
    50  			ExpectedMethod: "GET",
    51  			ResponseStatus: 404,
    52  			ResponseBody:   `{"message": "NotFound"}`,
    53  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
    54  			WithResponse:   (*radarr.Naming)(nil),
    55  		},
    56  	}
    57  
    58  	for _, test := range tests {
    59  		test := test
    60  		t.Run(test.Name, func(t *testing.T) {
    61  			t.Parallel()
    62  			mockServer := test.GetMockServer(t)
    63  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
    64  			output, err := client.GetNaming()
    65  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
    66  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
    67  		})
    68  	}
    69  }
    70  
    71  func TestUpdateNaming(t *testing.T) {
    72  	t.Parallel()
    73  
    74  	tests := []*starrtest.MockData{
    75  		{
    76  			Name:           "202",
    77  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "config", "naming"),
    78  			ExpectedMethod: "PUT",
    79  			ResponseStatus: 202,
    80  			WithRequest: &radarr.Naming{
    81  				ID:                       1,
    82  				ReplaceIllegalCharacters: true,
    83  				RenameMovies:             false,
    84  			},
    85  			ExpectedRequest: `{"replaceIllegalCharacters":true,"id":1,"standardMovieFormat":"","movieFolderFormat":""}` + "\n",
    86  			ResponseBody:    namingBody,
    87  			WithResponse: &radarr.Naming{
    88  				ID:                       1,
    89  				ReplaceIllegalCharacters: true,
    90  				IncludeQuality:           true,
    91  				ReplaceSpaces:            true,
    92  				RenameMovies:             true,
    93  				ColonReplacementFormat:   "delete",
    94  				StandardMovieFormat:      "{Movie.Title}.{Release.Year}.{Quality.Title}",
    95  				MovieFolderFormat:        "{Movie Title} ({Release Year})",
    96  			},
    97  			WithError: nil,
    98  		},
    99  		{
   100  			Name:           "404",
   101  			ExpectedPath:   path.Join("/", starr.API, radarr.APIver, "config", "naming"),
   102  			ExpectedMethod: "PUT",
   103  			WithRequest: &radarr.Naming{
   104  				ID:                       1,
   105  				ReplaceIllegalCharacters: true,
   106  				RenameMovies:             false,
   107  			},
   108  			ExpectedRequest: `{"replaceIllegalCharacters":true,"id":1,"standardMovieFormat":"","movieFolderFormat":""}` + "\n",
   109  			ResponseStatus:  404,
   110  			ResponseBody:    `{"message": "NotFound"}`,
   111  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   112  			WithResponse:    (*radarr.Naming)(nil),
   113  		},
   114  	}
   115  
   116  	for _, test := range tests {
   117  		test := test
   118  		t.Run(test.Name, func(t *testing.T) {
   119  			t.Parallel()
   120  			mockServer := test.GetMockServer(t)
   121  			client := radarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   122  			output, err := client.UpdateNaming(test.WithRequest.(*radarr.Naming))
   123  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   124  			assert.EqualValues(t, test.WithResponse, output, "test.WithResponse does not match the actual response")
   125  		})
   126  	}
   127  }