This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for currency in Cart and Product
- Loading branch information
Showing
5 changed files
with
238 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
describe Cart do | ||
|
||
describe 'when product is not an upgrade' do | ||
|
||
describe 'ex_gst_total' do | ||
|
||
it 'delegates to the cart item, passing currency and gst_charged' do | ||
p = Product.new | ||
cart = Cart.new('XTS', :gst_charged => true) | ||
|
||
cart_item = mock(CartItem) | ||
CartItem.stub!(:new).and_return(cart_item) | ||
|
||
cart_item.stub!(:quantity).and_return(1) | ||
cart_item.should_receive(:subtotal).with('XTS', false).and_return(88.95) | ||
|
||
cart.add_product_or_increase_quantity(p, 1) | ||
cart.ex_gst_total.should == 88.95 | ||
end | ||
|
||
end | ||
|
||
describe 'total' do | ||
|
||
it 'delegates to the cart item, passing currency and gst_charged' do | ||
p = Product.new | ||
cart = Cart.new('XTS', :gst_charged => true) | ||
|
||
cart_item = mock(CartItem) | ||
CartItem.stub!(:new).and_return(cart_item) | ||
|
||
cart_item.stub!(:quantity).and_return(1) | ||
cart_item.should_receive(:subtotal).with('XTS', true).and_return(89.95) | ||
|
||
cart.add_product_or_increase_quantity(p, 1) | ||
cart.total.should == 89.95 | ||
end | ||
|
||
end | ||
|
||
describe 'gst_amount' do | ||
|
||
it 'is total minus ex_gst_total' do | ||
p = Product.new | ||
cart = Cart.new('XTS', :gst_charged => true) | ||
|
||
cart.should_receive(:ex_gst_total).with(no_args()).and_return(20) | ||
cart.should_receive(:total).with(no_args()).and_return(30) | ||
|
||
cart.add_product_or_increase_quantity(p, 1) | ||
cart.gst_amount.should == 10 | ||
end | ||
|
||
end | ||
|
||
end | ||
|
||
describe 'when product is an upgrade' do | ||
|
||
it "uses product.upgrade to calculate price when product is an upgrade" do | ||
p = Product.new | ||
cart = Cart.new('XTS', :gst_charged => false) | ||
|
||
p.should_receive(:upgrade_price).with('XTS').twice.and_return(99.95) | ||
|
||
cart.add_product_or_increase_quantity(p, 1, true) | ||
cart.total.should == 99.95 | ||
end | ||
|
||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,113 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
describe Product do | ||
|
||
describe 'price_for_quantity' do | ||
|
||
it 'should be product_price.price when the supplied currency is the base currency' do | ||
p = Product.new | ||
product_price = stub(ProductPrice, :price => 99.95) | ||
p.stub!(:product_price_for_quantity).and_return(product_price) | ||
p.price_for_quantity(1, p.base_currency).should == 99.95 | ||
end | ||
|
||
it 'should perform currency conversion and rounding when supplied currency is not the base currency' do | ||
p = Product.new | ||
product_price = stub(ProductPrice, :price => 50) | ||
p.stub!(:product_price_for_quantity).and_return(product_price) | ||
Radiant::Config.should_receive(:[]).with('currency.usd-aud').and_return('2') | ||
p.price_for_quantity(1, 'AUD').should == 99 | ||
end | ||
|
||
end | ||
|
||
describe 'upgrade_price' do | ||
|
||
it 'should be product_price.price when the supplied currency is the base currency' do | ||
p = Product.new | ||
product_price = stub(ProductPrice, :price => 24.95) | ||
product_prices = mock('product_prices') | ||
p.should_receive(:product_prices).and_return(product_prices) | ||
product_prices.should_receive(:find).with( | ||
:first, | ||
:conditions => {:upgrade => true} | ||
).and_return(product_price) | ||
|
||
p.upgrade_price(p.base_currency).should == 24.95 | ||
end | ||
|
||
it 'should perform currency conversion and rounding when supplied currency is not the base currency' do | ||
p = Product.new | ||
product_price = stub(ProductPrice, :price => 24.95) | ||
product_prices = mock('product_prices') | ||
p.should_receive(:product_prices).and_return(product_prices) | ||
product_prices.should_receive(:find).with( | ||
:first, | ||
:conditions => {:upgrade => true} | ||
).and_return(product_price) | ||
Radiant::Config.should_receive(:[]).with('currency.usd-aud').and_return('2') | ||
|
||
p.upgrade_price('AUD').should == 49.0 | ||
end | ||
|
||
end | ||
|
||
describe 'total_for_quantity' do | ||
it 'should multiply price_for_quantity by qty' do | ||
p = Product.new | ||
p.stub!(:price_for_quantity).and_return(12.05) | ||
p.total_for_quantity(3).should == 12.05*3 | ||
p.total_for_quantity(3, p.base_currency).should == 12.05*3 | ||
end | ||
end | ||
|
||
def self.should_snap(amount, expected_amount) | ||
it "should snap #{amount} to #{expected_amount}" do | ||
Product.new.snap_to_round_amount(amount).should == expected_amount | ||
end | ||
end | ||
|
||
describe 'snap_to_round_amount' do | ||
|
||
should_snap 1, 4.0 | ||
should_snap 2.25, 4.0 | ||
should_snap 3.9, 4.0 | ||
should_snap 100.12345, 99.0 | ||
should_snap 8.9823, 9.0 | ||
should_snap 480.39, 479.0 | ||
should_snap 7.39, 4.0 | ||
should_snap 83.398, 84.0 | ||
should_snap 47.19, 44.0 | ||
should_snap 7, 4 | ||
should_snap -1, -4 | ||
should_snap -3, -4 | ||
should_snap -7, -4 | ||
should_snap -7.6, -9 | ||
should_snap -11, -9 | ||
|
||
end | ||
|
||
describe 'exchange_rate_for_currency' do | ||
|
||
it 'should should be nil when currency is base_currency' do | ||
Radiant::Config.should_not_receive(:[]).with(anything()) | ||
Product.exchange_rate_for_currency(Product::BASE_CURRENCY).should be_nil | ||
end | ||
|
||
end | ||
it 'should be a float when currency is not base_currency' do | ||
Radiant::Config.should_receive(:[]).with('currency.usd-aud').and_return('1.31') | ||
Product.exchange_rate_for_currency('AUD').should be_close(1.31, 0.01) | ||
end | ||
|
||
it 'should raise a CurrencyException if the rate is not available' do | ||
Radiant::Config.should_receive(:[]).with('currency.usd-aud').and_return(nil) | ||
lambda { Product.exchange_rate_for_currency('AUD') }.should raise_error(CurrencyException) | ||
end | ||
|
||
it 'should raise a CurrencyException if the rate is very small' do | ||
Radiant::Config.should_receive(:[]).with('currency.usd-aud').and_return('0.09') | ||
lambda { Product.exchange_rate_for_currency('AUD') }.should raise_error(CurrencyException) | ||
end | ||
|
||
end | ||
|
||
end |