Navigation Menu

Skip to content

Commit

Permalink
Add Chargify::Coupon.find_all_by_product_family_id
Browse files Browse the repository at this point in the history
- Add coupon specs to cover some existing functionality
  • Loading branch information
shayfrendt committed Oct 20, 2011
1 parent f61987a commit 5b8ef66
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/chargify_api_ares.rb
Expand Up @@ -298,6 +298,10 @@ class PaymentProfile < Base
end end


class Coupon < Base class Coupon < Base
def self.find_all_by_product_family_id(product_family_id)
Coupon.find(:all, :params => { :product_family_id => product_family_id })
end

def self.find_by_product_family_id_and_code(product_family_id, code) def self.find_by_product_family_id_and_code(product_family_id, code)
Coupon.new get(:lookup, :product_family_id => product_family_id, :code => code) Coupon.new get(:lookup, :product_family_id => product_family_id, :code => code)
end end
Expand Down
35 changes: 35 additions & 0 deletions spec/coupon_spec.rb
@@ -0,0 +1,35 @@
require 'spec_helper'

describe Chargify::Coupon do
context '.find_by_product_family_id_and_code' do
let(:existing_coupon) { Factory.build(:coupon, :code => '20OFF') }

before do
FakeWeb.register_uri(:get, "#{test_domain}/product_families/10/coupons/lookup.xml?code=#{existing_coupon.code}", :body => existing_coupon.attributes.to_xml)
end

it "finds the correct coupon by product family and code" do
Chargify::Coupon.find_by_product_family_id_and_code(10, '20OFF').should == existing_coupon
end

it "is an instance of Chargify::Coupon" do
coupon = Chargify::Coupon.find_by_product_family_id_and_code(10, '20OFF')
coupon.should be_instance_of(Chargify::Coupon)
end
end

context '.find_all_by_product_family_id' do
let(:coupon_1) { Factory.build(:coupon, :product_family_id => 5) }
let(:coupon_2) { Factory.build(:coupon, :product_family_id => 5) }

before do
FakeWeb.register_uri(:get, "#{test_domain}/product_families/5/coupons.xml", :body => [coupon_1.attributes, coupon_2.attributes].to_xml)
end

it "returns all of the coupons for a product family" do
coupons = Chargify::Coupon.find_all_by_product_family_id(5)
coupons.count.should == 2
coupons.map{|c| c.should be_instance_of(Chargify::Coupon)}
end
end
end
10 changes: 10 additions & 0 deletions spec/factories.rb
Expand Up @@ -61,4 +61,14 @@
f.unit_name 'unit' f.unit_name 'unit'
f.component_type 'quantity_based_component' f.component_type 'quantity_based_component'
end end

factory :coupon, :class => Chargify::Coupon do |f|
f.name { '15% off' }
f.code { '15OFF' }
f.description { '15% off for life' }
f.percentage { '14' }
f.allow_negative_balance { false }
f.recurring { false }
f.end_date { 1.month.from_now }
end
end end

0 comments on commit 5b8ef66

Please sign in to comment.