price_rule_test.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # frozen_string_literal: true
  2. require 'test_helper'
  3. class PriceRuleTest < Test::Unit::TestCase
  4. def setup
  5. super
  6. fake('price_rules/102586120', body: load_fixture('price_rule'))
  7. @price_rule = ShopifyAPI::PriceRule.find(102586120)
  8. end
  9. def test_get_price_rule
  10. fake('price_rules/102586120', method: :get, status: 200, body: load_fixture('price_rule'))
  11. price_rule = ShopifyAPI::PriceRule.find(102586120)
  12. assert_equal(102586120, price_rule.id)
  13. end
  14. def test_get_all_price_rules
  15. fake('price_rules', method: :get, status: 200, body: load_fixture('price_rules'))
  16. price_rules = ShopifyAPI::PriceRule.all
  17. assert_equal(1, price_rules.length)
  18. assert_equal(102586120, price_rules.first.id)
  19. end
  20. def test_get_all_discount_codes_for_a_price_rule
  21. fake('price_rules/102586120/discount_codes', method: :get, status: 200, body: load_fixture('discount_codes'))
  22. price_rule = ShopifyAPI::PriceRule.find(102586120)
  23. assert_equal("SUMMERSALE10", price_rule.discount_codes.first.code)
  24. end
  25. def test_create_price_rule
  26. fake('price_rules', method: :post, status: 201, body: load_fixture('price_rule'))
  27. price_rule = ShopifyAPI::PriceRule.create(
  28. target_type: "line_item",
  29. allocation_method: "across",
  30. value_type: "fixed_amount",
  31. value: -10.0,
  32. customer_selection: "all",
  33. starts_at: "2017-01-19T00:00:00Z"
  34. )
  35. assert_equal(
  36. '{"price_rule":{"target_type":"line_item","allocation_method":"across","value_type":"fixed_amount",' \
  37. '"value":-10.0,"customer_selection":"all","starts_at":"2017-01-19T00:00:00Z"}}',
  38. WebMock.last_request.body
  39. )
  40. assert_equal(-10, price_rule.value)
  41. end
  42. def test_update_price_rule
  43. price_rule_response = ActiveSupport::JSON.decode(load_fixture('price_rule'))
  44. price_rule_response['price_rule']['value'] = -50.0
  45. @price_rule.value = -50.0
  46. fake('price_rules/102586120', method: :put, status: 200, body: ActiveSupport::JSON.encode(price_rule_response))
  47. @price_rule.save
  48. assert_equal(price_rule_response['price_rule']['value'], @price_rule.value)
  49. end
  50. def test_delete_price_rule
  51. fake('price_rules/102586120', method: :delete, body: 'destroyed')
  52. assert(@price_rule.destroy)
  53. end
  54. end