price_rule_test.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 '{"price_rule":{"target_type":"line_item","allocation_method":"across","value_type":"fixed_amount","value":-10.0,"customer_selection":"all","starts_at":"2017-01-19T00:00:00Z"}}', WebMock.last_request.body
  35. assert_equal(-10, price_rule.value)
  36. end
  37. def test_update_price_rule
  38. price_rule_response = ActiveSupport::JSON.decode(load_fixture('price_rule'))
  39. price_rule_response['price_rule']['value'] = -50.0
  40. @price_rule.value = -50.0
  41. fake 'price_rules/102586120', method: :put, status: 200, body: ActiveSupport::JSON.encode(price_rule_response)
  42. @price_rule.save
  43. assert_equal price_rule_response['price_rule']['value'], @price_rule.value
  44. end
  45. def test_delete_price_rule
  46. fake 'price_rules/102586120', method: :delete, body: 'destroyed'
  47. assert @price_rule.destroy
  48. end
  49. end