price_rule_test.rb 2.2 KB

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