Skip to content

Commit

Permalink
Add SetExclude function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry-Sarabia committed Feb 21, 2019
1 parent d6da624 commit e64124e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
33 changes: 30 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ const (
OrderDescending order = "desc"
)

// SetOrder is a functional option used to sort the results from an API call.
// The default order is by relevance.
//
// For more information, visit: https://api-docs.igdb.com/#sorting
func SetOrder(field string, order order) Option {
return func() (apicalypse.Option, error) {
if whitespace.IsBlank(field) {
return nil, ErrEmptyFields
}

return apicalypse.Sort(field, string(order)), nil
}
}
Expand Down Expand Up @@ -115,24 +120,45 @@ func SetOffset(off int) Option {
// match an IGDB object's JSON field tag exactly, not the Go struct field
// name.
//
// The default for Get and List functions is set to all available fields.
// The default for Search functions is set to solely the ID field.
//
// For more information, visit: https://api-docs.igdb.com/#fields
func SetFields(fields ...string) Option {
return func() (apicalypse.Option, error) {
if len(fields) <= 0 {
return nil, ErrEmptyFields
}

for _, f := range fields {
if whitespace.IsBlank(f) {
return nil, ErrEmptyFields
}
}

return apicalypse.Fields(fields...), nil
}
}

// SetExclude is a functional option used to specify which fields of the
// requested IGDB object you want the API to exclude. Note that the field
// string must match an IGDB object's JSON field tag exactly, not the Go struct
// name.
//
// For more information, visit: https://api-docs.igdb.com/#exclude
func SetExclude(fields ...string) Option {
return func() (apicalypse.Option, error) {
if len(fields) <= 0 {
return nil, ErrEmptyFields
}

for _, f := range fields {
if whitespace.IsBlank(f) {
return nil, ErrEmptyFields
}
}

return apicalypse.Exclude(fields...), nil
}
}

// operator represents the postfix operation used to filter the results from
// an API call using the provided field value. For the list of postfix
// operators, visit: https://api-docs.igdb.com/#filters
Expand Down Expand Up @@ -201,6 +227,7 @@ func setSearch(qry string) Option {
if whitespace.IsBlank(qry) {
return nil, ErrEmptyQry
}

return apicalypse.Search("", qry), nil
}
}
38 changes: 38 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,44 @@ func TestSetFields(t *testing.T) {
}
}

func TestSetExclude(t *testing.T) {
var tests = []struct {
name string
fields []string
wantFields string
wantErr error
}{
{"Single non-empty field", []string{"name"}, "name", nil},
{"Multiple non-empty fields", []string{"name", "popularity", "rating"}, "name,popularity,rating", nil},
{"Empty fields slice", []string{}, "", ErrEmptyFields},
{"Single empty field", []string{" "}, "", ErrEmptyFields},
{"Multiple empty fields", []string{"", " ", "", ""}, "", ErrEmptyFields},
{"Mixed empty and non-empty fields", []string{"", "id", " ", "url"}, "", ErrEmptyFields},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
fn, err := SetExclude(test.fields...)()
if errors.Cause(err) != test.wantErr {
t.Errorf("got: <%v>, want: <%v>", errors.Cause(err), test.wantErr)
}

if test.wantErr != nil {
return
}

q, err := apicalypse.Query(fn)
if err != nil {
t.Fatal(err)
}

if !strings.Contains(q, test.wantFields) {
t.Errorf("got: <%v>, want: <%v>", q, test.wantFields)
}
})
}
}

func TestSetFilter(t *testing.T) {
var tests = []struct {
name string
Expand Down

0 comments on commit e64124e

Please sign in to comment.