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

     1  package readarr_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/readarr"
    11  	"golift.io/starr/starrtest"
    12  )
    13  
    14  var testCalendarJSON = `{
    15  	"title": "The Invitation",
    16  	"authorTitle": "foley, lucy The Invitation",
    17  	"seriesTitle": "",
    18  	"disambiguation": "",
    19  	"authorId": 1,
    20  	"foreignBookId": "46232124",
    21  	"titleSlug": "46232124",
    22  	"monitored": true,
    23  	"anyEditionOk": true,
    24  	"ratings": {
    25  	  "votes": 4797,
    26  	  "value": 3.67,
    27  	  "popularity": 17604.989999999998
    28  	},
    29  	"releaseDate": "2016-08-02T07:00:00Z",
    30  	"pageCount": 432,
    31  	"genres": [
    32  	  "historical-fiction",
    33  	  "fiction",
    34  	  "romance",
    35  	  "italy"
    36  	],
    37  	"images": [
    38  	  {
    39  		"url": "/readarr/MediaCover/Books/1/cover.jpg?lastWrite=637735196543077695",
    40  		"coverType": "cover",
    41  		"extension": ".jpg"
    42  	  }
    43  	],
    44  	"links": [
    45  	  {
    46  		"url": "https://www.goodreads.com/work/editions/46232124",
    47  		"name": "Goodreads Editions"
    48  	  },
    49  	  {
    50  		"url": "https://www.goodreads.com/book/show/28118525-the-invitation",
    51  		"name": "Goodreads Book"
    52  	  }
    53  	],
    54  	"statistics": {
    55  	  "bookFileCount": 0,
    56  	  "bookCount": 1,
    57  	  "totalBookCount": 1,
    58  	  "sizeOnDisk": 0,
    59  	  "percentOfBooks": 0
    60  	},
    61  	"added": "2020-09-29T04:47:05Z",
    62  	"grabbed": false,
    63  	"id": 1
    64    }`
    65  
    66  // This matches the json above.
    67  var testCalendarStruct = readarr.Book{
    68  	Added:          time.Date(2020, time.September, 29, 4, 47, 5, 0, time.UTC),
    69  	AnyEditionOk:   true,
    70  	Author:         nil,
    71  	AuthorID:       1,
    72  	AuthorTitle:    "foley, lucy The Invitation",
    73  	Disambiguation: "",
    74  	ForeignBookID:  "46232124",
    75  	Genres:         []string{"historical-fiction", "fiction", "romance", "italy"},
    76  	Grabbed:        false,
    77  	ID:             1,
    78  	Images: []*starr.Image{{
    79  		URL:       "/readarr/MediaCover/Books/1/cover.jpg?lastWrite=637735196543077695",
    80  		CoverType: "cover",
    81  		Extension: ".jpg",
    82  	}},
    83  	Links: []*starr.Link{{
    84  		URL:  "https://www.goodreads.com/work/editions/46232124",
    85  		Name: "Goodreads Editions",
    86  	}, {
    87  		URL:  "https://www.goodreads.com/book/show/28118525-the-invitation",
    88  		Name: "Goodreads Book",
    89  	}},
    90  	Monitored: true,
    91  	PageCount: 432,
    92  	Ratings: &starr.Ratings{
    93  		Votes:      4797,
    94  		Value:      3.67,
    95  		Popularity: 17604.989999999998,
    96  	},
    97  	ReleaseDate: time.Date(2016, time.August, 2, 7, 0, 0, 0, time.UTC),
    98  	SeriesTitle: "",
    99  	Statistics: &readarr.Statistics{
   100  		BookFileCount:      0,
   101  		BookCount:          1,
   102  		AvailableBookCount: 0,
   103  		TotalBookCount:     1,
   104  		SizeOnDisk:         0,
   105  		PercentOfBooks:     0,
   106  	},
   107  	Title:     "The Invitation",
   108  	TitleSlug: "46232124",
   109  }
   110  
   111  func TestGetCalendar(t *testing.T) {
   112  	t.Parallel()
   113  
   114  	tests := []*starrtest.MockData{
   115  		{
   116  			Name: "200",
   117  			ExpectedPath: "/api/v1/calendar" +
   118  				"?end=2020-02-20T04%3A20%3A20.000Z" +
   119  				"&includeAuthor=false" +
   120  				"&start=2020-02-20T04%3A20%3A20.000Z" +
   121  				"&unmonitored=true",
   122  			ResponseStatus: http.StatusOK,
   123  			ResponseBody:   `[` + testCalendarJSON + `]`,
   124  			WithRequest: readarr.Calendar{
   125  				Start:         time.Unix(1582172420, 0),
   126  				End:           time.Unix(1582172420, 0),
   127  				Unmonitored:   true,
   128  				IncludeAuthor: false,
   129  			},
   130  			WithError:      nil,
   131  			ExpectedMethod: http.MethodGet,
   132  			WithResponse:   []*readarr.Book{&testCalendarStruct},
   133  		},
   134  		{
   135  			Name: "404",
   136  			ExpectedPath: "/api/v1/calendar" +
   137  				"?end=2020-02-20T04%3A20%3A20.000Z" +
   138  				"&includeAuthor=false" +
   139  				"&start=2020-02-20T04%3A20%3A20.000Z" +
   140  				"&unmonitored=true",
   141  			ResponseStatus: http.StatusNotFound,
   142  			ResponseBody:   `{"message": "NotFound"}`,
   143  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   144  			ExpectedMethod: http.MethodGet,
   145  			WithRequest: readarr.Calendar{
   146  				Start:       time.Unix(1582172420, 0),
   147  				End:         time.Unix(1582172420, 0),
   148  				Unmonitored: true,
   149  			},
   150  			WithResponse: []*readarr.Book(nil),
   151  		},
   152  	}
   153  
   154  	for _, test := range tests {
   155  		test := test
   156  		t.Run(test.Name, func(t *testing.T) {
   157  			t.Parallel()
   158  			mockServer := test.GetMockServer(t)
   159  			client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   160  			output, err := client.GetCalendar(test.WithRequest.(readarr.Calendar))
   161  			assert.ErrorIs(t, err, test.WithError, "the wrong error was returned")
   162  			assert.EqualValues(t, test.WithResponse, output, "make sure ResponseBody and WithResponse are a match")
   163  		})
   164  	}
   165  }
   166  
   167  func TestGetCalendarID(t *testing.T) {
   168  	t.Parallel()
   169  
   170  	tests := []*starrtest.MockData{
   171  		{
   172  			Name:           "200",
   173  			ExpectedPath:   "/api/v1/calendar/1",
   174  			ResponseStatus: http.StatusOK,
   175  			ResponseBody:   testCalendarJSON,
   176  			WithRequest:    int64(1),
   177  			WithError:      nil,
   178  			ExpectedMethod: http.MethodGet,
   179  			WithResponse:   &testCalendarStruct,
   180  		},
   181  		{
   182  			Name:           "404",
   183  			ExpectedPath:   "/api/v1/calendar/1",
   184  			ResponseStatus: http.StatusNotFound,
   185  			ResponseBody:   `{"message": "NotFound"}`,
   186  			WithError:      &starr.ReqError{Code: http.StatusNotFound},
   187  			ExpectedMethod: http.MethodGet,
   188  			WithRequest:    int64(1),
   189  			WithResponse:   (*readarr.Book)(nil),
   190  		},
   191  	}
   192  
   193  	for _, test := range tests {
   194  		test := test
   195  		t.Run(test.Name, func(t *testing.T) {
   196  			t.Parallel()
   197  			mockServer := test.GetMockServer(t)
   198  			client := readarr.New(starr.New("mockAPIkey", mockServer.URL, 0))
   199  			output, err := client.GetCalendarID(test.WithRequest.(int64))
   200  			assert.ErrorIs(t, err, test.WithError, "the wrong error was returned")
   201  			assert.EqualValues(t, test.WithResponse, output, "make sure ResponseBody and WithResponse are a match")
   202  		})
   203  	}
   204  }