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

     1  package sonarr_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/sonarr"
    11  	"golift.io/starr/starrtest"
    12  )
    13  
    14  const namingBody = `{
    15  	"renameEpisodes": false,
    16  	"replaceIllegalCharacters": true,
    17  	"multiEpisodeStyle": 0,
    18  	"standardEpisodeFormat": "{Series Title} - S{season:00}E{episode:00} - {Episode Title} {Quality Full}",
    19  	"dailyEpisodeFormat": "{Series Title} - {Air-Date} - {Episode Title} {Quality Full}",
    20  	"animeEpisodeFormat": "{Series Title} - S{season:00}E{episode:00} - {Episode Title} {Quality Full}",
    21  	"seriesFolderFormat": "{Series Title}",
    22  	"seasonFolderFormat": "Season {season}",
    23  	"specialsFolderFormat": "Specials",
    24  	"includeSeriesTitle": true,
    25  	"includeEpisodeTitle": false,
    26  	"includeQuality": false,
    27  	"replaceSpaces": true,
    28  	"separator": " - ",
    29  	"numberStyle": "S{season:00}E{episode:00}",
    30  	"id": 1
    31  }`
    32  
    33  func TestGetNaming(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	tests := []*starrtest.MockData{
    37  		{
    38  			Name:           "200",
    39  			ExpectedPath:   path.Join("/", starr.API, sonarr.APIver, "config", "naming"),
    40  			ExpectedMethod: "GET",
    41  			ResponseStatus: 200,
    42  			ResponseBody:   namingBody,
    43  			WithResponse: &sonarr.Naming{
    44  				ID:                       1,
    45  				RenameEpisodes:           false,
    46  				ReplaceIllegalCharacters: true,
    47  				MultiEpisodeStyle:        0,
    48  				StandardEpisodeFormat:    "{Series Title} - S{season:00}E{episode:00} - {Episode Title} {Quality Full}",
    49  				DailyEpisodeFormat:       "{Series Title} - {Air-Date} - {Episode Title} {Quality Full}",
    50  				AnimeEpisodeFormat:       "{Series Title} - S{season:00}E{episode:00} - {Episode Title} {Quality Full}",
    51  				SeriesFolderFormat:       "{Series Title}",
    52  				SeasonFolderFormat:       "Season {season}",
    53  				SpecialsFolderFormat:     "Specials",
    54  				IncludeSeriesTitle:       true,
    55  				IncludeEpisodeTitle:      false,
    56  				IncludeQuality:           false,
    57  				ReplaceSpaces:            true,
    58  				Separator:                " - ",
    59  				NumberStyle:              "S{season:00}E{episode:00}",
    60  			},
    61  			WithError: nil,
    62  		},
    63  		{
    64  			Name:           "404",
    65  			ExpectedPath:   path.Join("/", starr.API, sonarr.APIver, "config", "naming"),
    66  			ExpectedMethod: "GET",
    67  			ResponseStatus: 404,
    68  			ResponseBody:   `{"message": "NotFound"}`,
    69  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
    70  			WithResponse:   (*sonarr.Naming)(nil),
    71  		},
    72  	}
    73  
    74  	for _, test := range tests {
    75  		test := test
    76  		t.Run(test.Name, func(t *testing.T) {
    77  			t.Parallel()
    78  			mockServer := test.GetMockServer(t)
    79  			client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
    80  			output, err := client.GetNaming()
    81  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
    82  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
    83  		})
    84  	}
    85  }
    86  
    87  func TestUpdateNaming(t *testing.T) {
    88  	t.Parallel()
    89  
    90  	tests := []*starrtest.MockData{
    91  		{
    92  			Name:           "202",
    93  			ExpectedPath:   path.Join("/", starr.API, sonarr.APIver, "config", "naming"),
    94  			ExpectedMethod: "PUT",
    95  			ResponseStatus: 202,
    96  			WithRequest: &sonarr.Naming{
    97  				ReplaceIllegalCharacters: true,
    98  			},
    99  			ExpectedRequest: `{"replaceIllegalCharacters":true}` + "\n",
   100  			ResponseBody:    namingBody,
   101  			WithResponse: &sonarr.Naming{
   102  				ID:                       1,
   103  				RenameEpisodes:           false,
   104  				ReplaceIllegalCharacters: true,
   105  				MultiEpisodeStyle:        0,
   106  				StandardEpisodeFormat:    "{Series Title} - S{season:00}E{episode:00} - {Episode Title} {Quality Full}",
   107  				DailyEpisodeFormat:       "{Series Title} - {Air-Date} - {Episode Title} {Quality Full}",
   108  				AnimeEpisodeFormat:       "{Series Title} - S{season:00}E{episode:00} - {Episode Title} {Quality Full}",
   109  				SeriesFolderFormat:       "{Series Title}",
   110  				SeasonFolderFormat:       "Season {season}",
   111  				SpecialsFolderFormat:     "Specials",
   112  				IncludeSeriesTitle:       true,
   113  				IncludeEpisodeTitle:      false,
   114  				IncludeQuality:           false,
   115  				ReplaceSpaces:            true,
   116  				Separator:                " - ",
   117  				NumberStyle:              "S{season:00}E{episode:00}",
   118  			},
   119  			WithError: nil,
   120  		},
   121  		{
   122  			Name:           "404",
   123  			ExpectedPath:   path.Join("/", starr.API, sonarr.APIver, "config", "naming"),
   124  			ExpectedMethod: "PUT",
   125  			WithRequest: &sonarr.Naming{
   126  				ReplaceIllegalCharacters: true,
   127  			},
   128  			ExpectedRequest: `{"replaceIllegalCharacters":true}` + "\n",
   129  			ResponseStatus:  404,
   130  			ResponseBody:    `{"message": "NotFound"}`,
   131  			WithError:       &starr.ReqError{Code: http.StatusNotFound},
   132  			WithResponse:    (*sonarr.Naming)(nil),
   133  		},
   134  	}
   135  
   136  	for _, test := range tests {
   137  		test := test
   138  		t.Run(test.Name, func(t *testing.T) {
   139  			t.Parallel()
   140  			mockServer := test.GetMockServer(t)
   141  			client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   142  			output, err := client.UpdateNaming(test.WithRequest.(*sonarr.Naming))
   143  			assert.ErrorIs(t, err, test.WithError, "error is not the same as expected")
   144  			assert.EqualValues(t, test.WithResponse, output, "response is not the same as expected")
   145  		})
   146  	}
   147  }