forked from getconversio/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e88e862
commit 9c5a83f
Showing
3 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package goshopify | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
const collectsBasePath = "admin/collects" | ||
|
||
// CollectService is an interface for interfacing with the collect endpoints | ||
// of the Shopify API. | ||
// See: https://help.shopify.com/api/reference/products/collect | ||
type CollectService interface { | ||
List(interface{}) ([]Collect, error) | ||
Count(interface{}) (int, error) | ||
} | ||
|
||
// CollectServiceOp handles communication with the collect related methods of | ||
// the Shopify API. | ||
type CollectServiceOp struct { | ||
client *Client | ||
} | ||
|
||
// Collect represents a Shopify collect | ||
type Collect struct { | ||
ID int `json:"id,omitempty"` | ||
CollectionID int `json:"collection_id,omitempty"` | ||
ProductID int `json:"product_id,omitempty"` | ||
Featured bool `json:"featured,omitempty"` | ||
CreatedAt *time.Time `json:"created_at,omitempty"` | ||
UpdatedAt *time.Time `json:"updated_at,omitempty"` | ||
Position int `json:"position,omitempty"` | ||
SortValue string `json:"sort_value,omitempty"` | ||
} | ||
|
||
// Represents the result from the collects/X.json endpoint | ||
type CollectResource struct { | ||
Collect *Collect `json:"collect"` | ||
} | ||
|
||
// Represents the result from the collects.json endpoint | ||
type CollectsResource struct { | ||
Collects []Collect `json:"collects"` | ||
} | ||
|
||
// List collects | ||
func (s *CollectServiceOp) List(options interface{}) ([]Collect, error) { | ||
path := fmt.Sprintf("%s.json", collectsBasePath) | ||
resource := new(CollectsResource) | ||
err := s.client.Get(path, resource, options) | ||
return resource.Collects, err | ||
} | ||
|
||
// Count collects | ||
func (s *CollectServiceOp) Count(options interface{}) (int, error) { | ||
path := fmt.Sprintf("%s/count.json", collectsBasePath) | ||
return s.client.Count(path, options) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package goshopify | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
httpmock "gopkg.in/jarcoal/httpmock.v1" | ||
) | ||
|
||
func collectTests(t *testing.T, collect Collect) { | ||
|
||
// Test a few fields | ||
cases := []struct { | ||
field string | ||
expected interface{} | ||
actual interface{} | ||
}{ | ||
{"ID", 18091352323, collect.ID}, | ||
{"CollectionID", 241600835, collect.CollectionID}, | ||
{"ProductID", 6654094787, collect.ProductID}, | ||
{"Featured", false, collect.Featured}, | ||
{"SortValue", "0000000001", collect.SortValue}, | ||
} | ||
|
||
for _, c := range cases { | ||
if c.expected != c.actual { | ||
t.Errorf("Collect.%v returned %v, expected %v", c.field, c.actual, c.expected) | ||
} | ||
} | ||
} | ||
|
||
func TestCollectList(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
httpmock.RegisterResponder("GET", "https://fooshop.myshopify.com/admin/collects.json", | ||
httpmock.NewStringResponder(200, `{"collects": [{"id":1},{"id":2}]}`)) | ||
|
||
collects, err := client.Collect.List(nil) | ||
if err != nil { | ||
t.Errorf("Collect.List returned error: %v", err) | ||
} | ||
|
||
expected := []Collect{{ID: 1}, {ID: 2}} | ||
if !reflect.DeepEqual(collects, expected) { | ||
t.Errorf("Collect.List returned %+v, expected %+v", collects, expected) | ||
} | ||
} | ||
|
||
func TestCollectCount(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
httpmock.RegisterResponder("GET", "https://fooshop.myshopify.com/admin/collects/count.json", | ||
httpmock.NewStringResponder(200, `{"count": 5}`)) | ||
|
||
params := map[string]string{"since_id": "123"} | ||
httpmock.RegisterResponderWithQuery("GET", "https://fooshop.myshopify.com/admin/collects/count.json", params, | ||
httpmock.NewStringResponder(200, `{"count": 2}`)) | ||
|
||
cnt, err := client.Collect.Count(nil) | ||
if err != nil { | ||
t.Errorf("Collect.Count returned error: %v", err) | ||
} | ||
|
||
expected := 5 | ||
if cnt != expected { | ||
t.Errorf("Collect.Count returned %d, expected %d", cnt, expected) | ||
} | ||
|
||
cnt, err = client.Collect.Count(ListOptions{SinceID: 123}) | ||
if err != nil { | ||
t.Errorf("Collect.Count returned error: %v", err) | ||
} | ||
|
||
expected = 2 | ||
if cnt != expected { | ||
t.Errorf("Collect.Count returned %d, expected %d", cnt, expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters