metafield_test.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class MetafieldTest < Test::Unit::TestCase
  4. def test_get_metafields
  5. fake("metafields", method: :get, body: load_fixture('metafields'))
  6. ShopifyAPI::Metafield.find(:all)
  7. end
  8. def test_get_metafield
  9. fake("metafields/721389482", method: :get, body: load_fixture('metafield'))
  10. assert(ShopifyAPI::Metafield.find(721389482))
  11. end
  12. def test_get_metafield_of_a_blog
  13. fake("blogs/1008414260/metafields/721389482", method: :get, body: load_fixture('metafield'))
  14. metafield = ShopifyAPI::Metafield.find(721389482, params: { resource: "blogs", resource_id: 1008414260 })
  15. assert_equal(1008414260, metafield.prefix_options[:resource_id])
  16. end
  17. def test_create_metafield_for_a_blog
  18. fake("blogs/1008414260", method: :get, body: load_fixture('blog'))
  19. fake("blogs/1008414260/metafields", method: :post, status: 201, body: load_fixture('metafield'))
  20. blog = ShopifyAPI::Blog.find(1008414260)
  21. metafield = blog.add_metafield(
  22. ShopifyAPI::Metafield.new(
  23. namespace: "summaries", key: "First Summary", value: "Make commerce better", value_type: "string"
  24. )
  25. )
  26. assert_equal(
  27. ActiveSupport::JSON.decode(
  28. '{"metafield":{"namespace":"summaries","key":"First Summary",' \
  29. '"value":"Make commerce better","value_type":"string"}}'
  30. ),
  31. ActiveSupport::JSON.decode(WebMock.last_request.body)
  32. )
  33. assert(!metafield.new_record?)
  34. end
  35. def test_update_metafield
  36. fake("metafields/721389482", method: :get, body: load_fixture('metafield'))
  37. fake("metafields/721389482", method: :put, status: 200, body: load_fixture('metafield'))
  38. metafield = ShopifyAPI::Metafield.find(721389482)
  39. metafield.namespace = "teaser"
  40. assert(metafield.save)
  41. end
  42. def test_delete_metafield
  43. fake("metafields/721389482", method: :get, body: load_fixture('metafield'))
  44. fake("metafields/721389482", method: :delete, body: 'destroyed')
  45. metafield = ShopifyAPI::Metafield.find(721389482)
  46. assert(metafield.destroy)
  47. end
  48. end