Skip to content

Instantly share code, notes, and snippets.

@lastobelus
Last active September 9, 2015 17:58
Show Gist options
  • Save lastobelus/1501dd6fb1dde248bce0 to your computer and use it in GitHub Desktop.
Save lastobelus/1501dd6fb1dde248bce0 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe "shopify api nested resources", unless: ENV['LIVE_SHOPIFY_SPECS_TOKEN'].nil? do
before(:all) do
session = ShopifyAPI::Session.new(ENV['LIVE_SHOPIFY_SPECS_DOMAIN'], ENV['LIVE_SHOPIFY_SPECS_TOKEN'])
ShopifyAPI::Base.activate_session(session)
@old_ar_logger = ActiveResource::Base.logger
@created = []
ActiveResource::Base.logger = Logger.new STDOUT if ENV['LOG_SHOPIFY']
WebMock.disable!
end
after(:all) do
@created.each{|obj| obj.destroy}
ActiveResource::Base.logger = @old_ar_logger
WebMock.enable!
end
it "creates a product" do
product = ShopifyAPI::Product.create( {
title: "Product",
vendor: '',
body_html: 'the body',
product_type: 'Widget',
})
@created << product
expect(product).to be_persisted
end
it "creates product with metafields" do
product = ShopifyAPI::Product.create( {
title: "Product with Options, Metafields, Variants and Product Images nested",
vendor: '',
body_html: 'the body',
product_type: 'Widget',
metafields: [
{key: 'custom1', value: "some value", value_type: "string", namespace: "test"},
{key: 'custom2', value: "some value", value_type: "string", namespace: "test"}
]
})
expect(product).to be_persisted
metafields = product.metafields
expect(metafields.length).to eq(2)
expect(metafields.first).to be_persisted
end
it "creates product with options, variants, images, and metafields" do
product = ShopifyAPI::Product.create( {
title: "Product with Options, Metafields, Variants and Product Images nested",
vendor: '',
body_html: 'the body',
options: [{name: "Colour"}, {name: "Size"}],
product_type: 'Widget',
variants: [
{title: "WidgetOne", option1: "Blue", option2: "small", price: '10.0', sku: 100},
{title: "WidgetOne", option1: "Blue", option2: "large", price: '10.0', sku: 100},
{title: "WidgetTwo", option1: "Red", option2: "small", price: '20.0', sku: 200},
{title: "WidgetTwo", option1: "Red", option2: "very_big", price: '20.0', sku: 200}
],
metafields: [
{key: 'custom1', value: "some value", value_type: "string", namespace: "test"},
{key: 'custom2', value: "some value", value_type: "string", namespace: "test"}
],
images: [
{src: "https://s3.amazonaws.com/teesforthepeople.com/uploads/23/9f545d21-a9dd-4bc9-912a-946889540974/"}
]
})
@created << product
expect(product).to be_persisted
expect(product.variants.length).to eq(4)
expect(product.variants.first).to be_persisted
metafields = product.metafields
expect(metafields.length).to eq(2)
expect(metafields.first).to be_persisted
expect(product.options.length).to eq(2)
expect(product.images.length).to eq(1)
expect(product.images.first).to be_persisted
end
it "creates variant with metafield as child of product" do
product = ShopifyAPI::Product.create( {
title: "Product",
vendor: '',
options: [{name: "Colour"}, {name: "Size"}],
variants: [
{option1: "Blue", option2: "small", price: '10.0'}
],
body_html: 'the body',
product_type: 'Widget',
})
@created << product
product.save
variant = ShopifyAPI::Variant.create( {
product_id: product.id,
title: "Standalone Variant",
option1: 'Pink',
option2: 'Vast',
sku: 'test',
price: '40.1',
metafields: [
{key: 'variant_1', value: "some value", value_type: "string", namespace: "test"},
{key: 'variant_2', value: "some value", value_type: "string", namespace: "test"},
{key: 'variant_3', value: "some value", value_type: "string", namespace: "test"}
]
})
expect(variant).to be_persisted
metafields = variant.metafields
expect(metafields.length).to eq(3)
expect(ShopifyAPI::Product.find(product.id).variants.length).to eq(2)
expect(metafields.first).to be_persisted
end
it "adds a metafield to a product" do
product = ShopifyAPI::Product.create( {
title: "Product",
vendor: '',
body_html: 'the body',
product_type: 'Widget',
})
@created << product
metafield = ShopifyAPI::Metafield.new({key: 'test_add_metafield_to_product', value: "some value", value_type: "string", namespace: "test"})
expect {
product.add_metafield(metafield)
}.to change{product.metafields.length}.by(1)
expect(metafield).to be_persisted
end
it "updates dirty product" do
product = ShopifyAPI::Product.create( {
title: "Product",
vendor: '',
body_html: 'the body',
product_type: 'Widget',
})
@created << product
expect(product).to be_persisted
product.title += " changed"
product.save
expect(product.errors).to be_empty
expect(ShopifyAPI::Product.find(product.id).title).to eq("Product changed")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment