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

     1  package sonarr_test
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"golift.io/starr"
    10  	"golift.io/starr/sonarr"
    11  	"golift.io/starr/starrtest"
    12  )
    13  
    14  var testCalendarJSON = `{
    15  	"seriesId": 1,
    16  	"tvdbId": 8484175,
    17  	"episodeFileId": 0,
    18  	"seasonNumber": 0,
    19  	"episodeNumber": 1,
    20  	"title": "Dead Zone",
    21  	"airDate": "1989-07-15",
    22  	"airDateUtc": "1989-07-14T15:00:00Z",
    23  	"overview": "...",
    24  	"hasFile": false,
    25  	"monitored": false,
    26  	"unverifiedSceneNumbering": false,
    27  	"series": {
    28  	  "title": "Dragon Ball Z",
    29  	  "sortTitle": "dragon ball z",
    30  	  "status": "ended",
    31  	  "ended": true,
    32  	  "overview": "...",
    33  	  "network": "Fuji TV",
    34  	  "images": [
    35  		{
    36  		  "coverType": "fanart",
    37  		  "url": "https://artworks.thetvdb.com/banners/fanart/original/81472-26.jpg"
    38  		}
    39  	  ],
    40  	  "seasons": [
    41  		{
    42  		  "seasonNumber": 0,
    43  		  "monitored": false
    44  		}
    45  	  ],
    46  	  "year": 1989,
    47  	  "path": "/tv/[DBNL] Dragon Ball Z Digitally Remastered",
    48  	  "qualityProfileId": 1,
    49  	  "languageProfileId": 1,
    50  	  "seasonFolder": true,
    51  	  "monitored": false,
    52  	  "useSceneNumbering": true,
    53  	  "runtime": 25,
    54  	  "tvdbId": 81472,
    55  	  "tvRageId": 3373,
    56  	  "tvMazeId": 2103,
    57  	  "firstAired": "1989-04-26T00:00:00Z",
    58  	  "seriesType": "standard",
    59  	  "cleanTitle": "dragonballz",
    60  	  "imdbId": "tt0214341",
    61  	  "titleSlug": "dragon-ball-z",
    62  	  "certification": "TV-PG",
    63  	  "genres": [
    64  		"Action",
    65  		"Adventure",
    66  		"Animation",
    67  		"Anime",
    68  		"Fantasy",
    69  		"Science Fiction",
    70  		"Thriller"
    71  	  ],
    72  	  "tags": [],
    73  	  "added": "2018-04-19T08:46:03.367045Z",
    74  	  "ratings": {
    75  		"votes": 4659,
    76  		"value": 8.7
    77  	  },
    78  	  "id": 1
    79  	},
    80  	"images": [],
    81  	"id": 1
    82    }
    83    `
    84  
    85  // This matches the json above.
    86  var testCalendarStruct = sonarr.Episode{
    87  	ID:                       1,
    88  	SeriesID:                 1,
    89  	TvdbID:                   8484175,
    90  	EpisodeFileID:            0,
    91  	SeasonNumber:             0,
    92  	EpisodeNumber:            1,
    93  	Title:                    "Dead Zone",
    94  	AirDate:                  "1989-07-15",
    95  	AirDateUtc:               time.Date(1989, 7, 14, 15, 0, 0, 0, time.UTC),
    96  	Overview:                 "...",
    97  	HasFile:                  false,
    98  	Monitored:                false,
    99  	UnverifiedSceneNumbering: false,
   100  	Series: &sonarr.Series{
   101  		Ended:             true,
   102  		Monitored:         false,
   103  		SeasonFolder:      true,
   104  		UseSceneNumbering: true,
   105  		Runtime:           25,
   106  		Year:              1989,
   107  		ID:                1,
   108  		LanguageProfileID: 1,
   109  		QualityProfileID:  1,
   110  		TvdbID:            81472,
   111  		TvMazeID:          2103,
   112  		TvRageID:          3373,
   113  		AirTime:           "",
   114  		Certification:     "TV-PG",
   115  		CleanTitle:        "dragonballz",
   116  		ImdbID:            "tt0214341",
   117  		Network:           "Fuji TV",
   118  		Overview:          "...",
   119  		Path:              "/tv/[DBNL] Dragon Ball Z Digitally Remastered",
   120  		SeriesType:        "standard",
   121  		SortTitle:         "dragon ball z",
   122  		Status:            "ended",
   123  		Title:             "Dragon Ball Z",
   124  		TitleSlug:         "dragon-ball-z",
   125  		Added:             time.Date(2018, 4, 19, 8, 46, 3, 367045000, time.UTC),
   126  		FirstAired:        time.Date(1989, 4, 26, 0, 0, 0, 0, time.UTC),
   127  		Ratings: &starr.Ratings{
   128  			Votes: 4659,
   129  			Value: 8.7,
   130  		},
   131  		Tags:            []int{},
   132  		Genres:          []string{"Action", "Adventure", "Animation", "Anime", "Fantasy", "Science Fiction", "Thriller"},
   133  		AlternateTitles: []*sonarr.AlternateTitle(nil),
   134  		Seasons:         []*sonarr.Season{{SeasonNumber: 0, Monitored: false}},
   135  		Images: []*starr.Image{
   136  			{CoverType: "fanart", URL: "https://artworks.thetvdb.com/banners/fanart/original/81472-26.jpg"},
   137  		},
   138  	},
   139  	Images: []*starr.Image{},
   140  }
   141  
   142  func TestGetCalendar(t *testing.T) {
   143  	t.Parallel()
   144  
   145  	tests := []*starrtest.MockData{
   146  		{
   147  			Name: "200",
   148  			ExpectedPath: "/api/v3/calendar" +
   149  				"?end=2020-02-20T04%3A20%3A20.000Z" +
   150  				"&includeEpisodeFile=false" +
   151  				"&includeEpisodeImages=false" +
   152  				"&includeSeries=true" +
   153  				"&start=2020-02-20T04%3A20%3A20.000Z" +
   154  				"&unmonitored=true",
   155  			ResponseStatus: http.StatusOK,
   156  			ResponseBody:   `[` + testCalendarJSON + `]`,
   157  			WithRequest: sonarr.Calendar{
   158  				Start:                time.Unix(1582172420, 0),
   159  				End:                  time.Unix(1582172420, 0),
   160  				Unmonitored:          true,
   161  				IncludeSeries:        true,
   162  				IncludeEpisodeFile:   false,
   163  				IncludeEpisodeImages: false,
   164  			},
   165  			WithError:      nil,
   166  			ExpectedMethod: http.MethodGet,
   167  			WithResponse:   []*sonarr.Episode{&testCalendarStruct},
   168  		},
   169  		{
   170  			Name: "404",
   171  			ExpectedPath: "/api/v3/calendar" +
   172  				"?end=2020-02-20T04%3A20%3A20.000Z" +
   173  				"&includeEpisodeFile=false" +
   174  				"&includeEpisodeImages=false" +
   175  				"&includeSeries=false" +
   176  				"&start=2020-02-20T04%3A20%3A20.000Z" +
   177  				"&unmonitored=true",
   178  			ResponseStatus: http.StatusNotFound,
   179  			ResponseBody:   `{"message": "NotFound"}`,
   180  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   181  			ExpectedMethod: http.MethodGet,
   182  			WithRequest: sonarr.Calendar{
   183  				Start:       time.Unix(1582172420, 0),
   184  				End:         time.Unix(1582172420, 0),
   185  				Unmonitored: true,
   186  			},
   187  			WithResponse: []*sonarr.Episode(nil),
   188  		},
   189  	}
   190  
   191  	for _, test := range tests {
   192  		test := test
   193  		t.Run(test.Name, func(t *testing.T) {
   194  			t.Parallel()
   195  			mockServer := test.GetMockServer(t)
   196  			client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   197  			output, err := client.GetCalendar(test.WithRequest.(sonarr.Calendar))
   198  			assert.ErrorIs(t, err, test.WithError, "the wrong error was returned")
   199  			assert.EqualValues(t, test.WithResponse, output, "make sure ResponseBody and WithResponse are a match")
   200  		})
   201  	}
   202  }
   203  
   204  func TestGetCalendarID(t *testing.T) {
   205  	t.Parallel()
   206  
   207  	tests := []*starrtest.MockData{
   208  		{
   209  			Name:           "200",
   210  			ExpectedPath:   "/api/v3/calendar/1",
   211  			ResponseStatus: http.StatusOK,
   212  			ResponseBody:   testCalendarJSON,
   213  			WithRequest:    int64(1),
   214  			WithError:      nil,
   215  			ExpectedMethod: http.MethodGet,
   216  			WithResponse:   &testCalendarStruct,
   217  		},
   218  		{
   219  			Name:           "404",
   220  			ExpectedPath:   "/api/v3/calendar/1",
   221  			ResponseStatus: http.StatusNotFound,
   222  			ResponseBody:   `{"message": "NotFound"}`,
   223  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   224  			ExpectedMethod: http.MethodGet,
   225  			WithRequest:    int64(1),
   226  			WithResponse:   (*sonarr.Episode)(nil),
   227  		},
   228  	}
   229  
   230  	for _, test := range tests {
   231  		test := test
   232  		t.Run(test.Name, func(t *testing.T) {
   233  			t.Parallel()
   234  			mockServer := test.GetMockServer(t)
   235  			client := sonarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   236  			output, err := client.GetCalendarID(test.WithRequest.(int64))
   237  			assert.ErrorIs(t, err, test.WithError, "the wrong error was returned")
   238  			assert.EqualValues(t, test.WithResponse, output, "make sure ResponseBody and WithResponse are a match")
   239  		})
   240  	}
   241  }