Skip to content

Commit

Permalink
tests: wrote imdb client unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cecobask committed Nov 29, 2023
1 parent f1d9203 commit 59ea39b
Show file tree
Hide file tree
Showing 9 changed files with 719 additions and 11 deletions.
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ go 1.21
require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.8.4
)

require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.17.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAc
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down Expand Up @@ -36,3 +42,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16 changes: 9 additions & 7 deletions pkg/client/imdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ type ImdbClient struct {
}

type ImdbConfig struct {
BasePath string
CookieAtMain string
CookieUbidMain string
UserId string
WatchlistId string
}

func NewImdbClient(config ImdbConfig, logger *slog.Logger) (ImdbClientInterface, error) {
config.BasePath = imdbPathBase
jar, err := setupCookieJar(config)
if err != nil {
return nil, err
Expand All @@ -66,9 +68,9 @@ func NewImdbClient(config ImdbConfig, logger *slog.Logger) (ImdbClientInterface,
}

func setupCookieJar(config ImdbConfig) (http.CookieJar, error) {
imdbUrl, err := url.Parse(imdbPathBase)
imdbUrl, err := url.Parse(config.BasePath)
if err != nil {
return nil, fmt.Errorf("failure parsing %s as url: %w", imdbPathBase, err)
return nil, fmt.Errorf("failure parsing %s as url: %w", config.BasePath, err)
}
jar, err := cookiejar.New(nil)
if err != nil {
Expand Down Expand Up @@ -133,7 +135,7 @@ func (c *ImdbClient) doRequest(requestFields requestFields) (*http.Response, err
func (c *ImdbClient) ListGet(listId string) (*entities.ImdbList, error) {
response, err := c.doRequest(requestFields{
Method: http.MethodGet,
BasePath: imdbPathBase,
BasePath: c.config.BasePath,
Endpoint: fmt.Sprintf(imdbPathListExport, listId),
Body: http.NoBody,
})
Expand Down Expand Up @@ -163,7 +165,7 @@ func (c *ImdbClient) WatchlistGet() (*entities.ImdbList, error) {
func (c *ImdbClient) ListsGetAll() ([]entities.ImdbList, error) {
response, err := c.doRequest(requestFields{
Method: http.MethodGet,
BasePath: imdbPathBase,
BasePath: c.config.BasePath,
Endpoint: fmt.Sprintf(imdbPathLists, c.config.UserId),
Body: http.NoBody,
})
Expand Down Expand Up @@ -232,7 +234,7 @@ func (c *ImdbClient) ListsGet(listIds []string) ([]entities.ImdbList, error) {
func (c *ImdbClient) UserIdScrape() error {
response, err := c.doRequest(requestFields{
Method: http.MethodGet,
BasePath: imdbPathBase,
BasePath: c.config.BasePath,
Endpoint: imdbPathProfile,
Body: http.NoBody,
})
Expand All @@ -250,7 +252,7 @@ func (c *ImdbClient) UserIdScrape() error {
func (c *ImdbClient) WatchlistIdScrape() error {
response, err := c.doRequest(requestFields{
Method: http.MethodGet,
BasePath: imdbPathBase,
BasePath: c.config.BasePath,
Endpoint: imdbPathWatchlist,
Body: http.NoBody,
})
Expand All @@ -268,7 +270,7 @@ func (c *ImdbClient) WatchlistIdScrape() error {
func (c *ImdbClient) RatingsGet() ([]entities.ImdbItem, error) {
response, err := c.doRequest(requestFields{
Method: http.MethodGet,
BasePath: imdbPathBase,
BasePath: c.config.BasePath,
Endpoint: fmt.Sprintf(imdbPathRatingsExport, c.config.UserId),
Body: http.NoBody,
})
Expand Down
Loading

0 comments on commit 59ea39b

Please sign in to comment.