From 20ffb769c1ea52bb3b078393177356003bd32738 Mon Sep 17 00:00:00 2001 From: sandersjessica Date: Thu, 29 Aug 2019 09:01:24 -0500 Subject: [PATCH] Add metafields to the shopify variant struct (#60) Update tests to include variants with and without metafields --- fixtures/variant_with_metafields.json | 44 ++++++++++++++ variant.go | 1 + variant_test.go | 83 +++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 fixtures/variant_with_metafields.json diff --git a/fixtures/variant_with_metafields.json b/fixtures/variant_with_metafields.json new file mode 100644 index 00000000..3ea434b7 --- /dev/null +++ b/fixtures/variant_with_metafields.json @@ -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" + } + ] + } +} diff --git a/variant.go b/variant.go index cad40623..2d5b6061 100644 --- a/variant.go +++ b/variant.go @@ -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 diff --git a/variant_test.go b/variant_test.go index 8609b78b..8549ade5 100644 --- a/variant_test.go +++ b/variant_test.go @@ -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) { @@ -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() @@ -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()