Skip to content

Commit

Permalink
Add metafields to the shopify variant struct (#60)
Browse files Browse the repository at this point in the history
Update tests to include variants with and without metafields
  • Loading branch information
sandersjessica authored and rickywiens committed Aug 29, 2019
1 parent 7c6d6f3 commit 20ffb76
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
44 changes: 44 additions & 0 deletions fixtures/variant_with_metafields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"variant": {
"id": 2,
"product_id": 1,
"title": "Blue",
"price": "2.00",
"sku": "",
"position": 2,
"inventory_policy": "deny",
"compare_at_price": null,
"fulfillment_service": "manual",
"inventory_management": null,
"inventory_item_id": 1,
"option1": "Yellow",
"option2": null,
"option3": null,
"created_at": "2017-10-18T12:22:36-04:00",
"updated_at": "2017-10-18T12:22:36-04:00",
"taxable": true,
"barcode": null,
"grams": 0,
"image_id": null,
"inventory_quantity": 1,
"weight": 0,
"weight_unit": "lb",
"old_inventory_quantity": 1,
"requires_shipping": true,
"admin_graphql_api_id": "gid://shopify/ProductVariant/1",
"metafields": [
{
"id": 721389482,
"namespace": "affiliates",
"key": "app_key",
"value": "app_value",
"value_type": "string",
"description": "description",
"owner_id": 690933842,
"created_at": "2018-04-27T15:15:25-04:00",
"updated_at": "2018-04-27T15:15:25-04:00",
"owner_resource": "shop"
}
]
}
}
1 change: 1 addition & 0 deletions variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Variant struct {
OldInventoryQuantity int `json:"old_inventory_quantity,omitempty"`
RequireShipping bool `json:"requires_shipping,omitempty"`
AdminGraphqlAPIID string `json:"admin_graphql_api_id,omitempty"`
Metafields []Metafield `json:"metafields,omitempty"`
}

// VariantResource represents the result from the variants/X.json endpoint
Expand Down
83 changes: 83 additions & 0 deletions variant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,40 @@ func variantTests(t *testing.T, variant Variant) {
if variant.InventoryItemId != expectedInventoryItemId {
t.Errorf("Variant.InventoryItemId returned %+v, expected %+v", variant.InventoryItemId, expectedInventoryItemId)
}

expectedMetafieldCount := 0
if len(variant.Metafields) != expectedMetafieldCount {
t.Errorf("Variant.Metafield returned %+v, expected %+v", variant.Metafields, expectedMetafieldCount)
}
}

func variantWithMetafieldsTests(t *testing.T, variant Variant) {
// Check that the ID is assigned to the returned variant
expectedInt := int64(2)
if variant.ID != expectedInt {
t.Errorf("Variant.ID returned %+v, expected %+v", variant.ID, expectedInt)
}

// Check that the Title is assigned to the returned variant
expectedTitle := "Blue"
if variant.Title != expectedTitle {
t.Errorf("Variant.Title returned %+v, expected %+v", variant.Title, expectedTitle)
}

expectedInventoryItemId := int64(1)
if variant.InventoryItemId != expectedInventoryItemId {
t.Errorf("Variant.InventoryItemId returned %+v, expected %+v", variant.InventoryItemId, expectedInventoryItemId)
}

expectedMetafieldCount := 1
if len(variant.Metafields) != expectedMetafieldCount {
t.Errorf("Variant.Metafield returned %+v, expected %+v", variant.Metafields, expectedMetafieldCount)
}

expectedMetafieldDescription := "description"
if variant.Metafields[0].Description != expectedMetafieldDescription {
t.Errorf("Variant.Metafield.Description returned %+v, expected %+v", variant.Metafields[0].Description, expectedMetafieldDescription)
}
}

func TestVariantList(t *testing.T) {
Expand Down Expand Up @@ -121,6 +155,26 @@ func TestVariantCreate(t *testing.T) {
variantTests(t, *result)
}

func TestVariantCreateWithMetafields(t *testing.T) {
setup()
defer teardown()

httpmock.RegisterResponder("POST", fmt.Sprintf("https://fooshop.myshopify.com/%s/products/1/variants.json", globalApiPathPrefix),
httpmock.NewBytesResponder(200, loadFixture("variant_with_metafields.json")))

price := decimal.NewFromFloat(2)

variant := Variant{
Option1: "Blue",
Price: &price,
}
result, err := client.Variant.Create(1, variant)
if err != nil {
t.Errorf("Variant.Create returned error: %v", err)
}
variantWithMetafieldsTests(t, *result)
}

func TestVariantUpdate(t *testing.T) {
setup()
defer teardown()
Expand All @@ -142,6 +196,35 @@ func TestVariantUpdate(t *testing.T) {
variantTests(t, *returnedVariant)
}

func TestVariantWithMetafieldsUpdate(t *testing.T) {
setup()
defer teardown()

httpmock.RegisterResponder("PUT", fmt.Sprintf("https://fooshop.myshopify.com/%s/variants/2.json", globalApiPathPrefix),
httpmock.NewBytesResponder(200, loadFixture("variant_with_metafields.json")))

variant := Variant{
ID: 2,
Option1: "Green",
Metafields: []Metafield{
{
ID: 123,
Description: "Original",
},
},
}

variant.Option1 = "Blue"
variant.Metafields[0].Description = "description"

returnedVariant, err := client.Variant.Update(variant)
if err != nil {
t.Errorf("Variant.Update returned error: %v", err)
}

variantWithMetafieldsTests(t, *returnedVariant)
}

func TestVariantDelete(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit 20ffb76

Please sign in to comment.