This commit is contained in:
Captain Future
2011-04-17 16:26:36 +02:00
commit e6134722d4
39 changed files with 1199 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
require 'factory_girl'
Dir["#{File.dirname(__FILE__)}/factories/**"].each do |f|
fp = File.expand_path(f)
require fp
end
+3
View File
@@ -0,0 +1,3 @@
Factory.define :digital do |f|
f.variant { |p| p.association(:variant) }
end
+4
View File
@@ -0,0 +1,4 @@
Factory.define :digital_link do |f|
f.digital { |p| p.association(:digital) }
f.line_item { |p| p.association(:line_item) }
end
+98
View File
@@ -0,0 +1,98 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe DigitalLink do
context 'validation' do
it { should belong_to(:digital) }
it { should belong_to(:line_item) }
it { should have_valid_factory(:digital_link) }
end
context "#create" do
it "should have an appropriately long secret" do
Factory(:digital_link).secret.length.should == 30
end
it "should have the access counter being an Integer on zero" do
Factory(:digital_link).access_counter.should == 0
end
end
context "#update" do
it "should not change the secret when updated" do
digital_link = Factory(:digital_link)
secret = digital_link.secret
digital_link.increment(:access_counter).save
digital_link.secret.should == secret
end
it "should enforce to have an associated digital" do
link = Factory(:digital_link)
lambda { link.update_attributes!(:digital => nil) }.should raise_error(ActiveRecord::RecordInvalid)
end
it "should not allow an empty or too short secret" do
link = Factory(:digital_link)
lambda { link.update_attributes!(:secret => nil) }.should raise_error(ActiveRecord::RecordInvalid)
lambda { link.update_attributes!(:secret => 'x' * 25) }.should raise_error(ActiveRecord::RecordInvalid)
end
end
#context "authorization" do
# it "should increment the counter using #authorize!" do
# link = Factory(:digital_link)
# link.authorize!
# link.access_counter.should == 1
# end
#
# it "should not be #authorized? when the access_counter is too high" do
# link = Factory(:digital_link)
# link.stub(:access_counter => 2)
# link.authorized?.should be_true
# link.stub(:access_counter => 3)
# link.authorized?.should be_false
# end
#
# it "should not be #authorize! when the access_counter is too high" do
# link = Factory(:digital_link)
# link.stub(:access_counter => 2)
# link.authorize!.should be_true
# link.stub(:access_counter => 3)
# link.authorize!.should be_false
# end
#
# it "should not be #authorized? when the created_at date is too far in the past" do
# link = Factory(:digital_link)
# link.authorized?.should be_true
# link.stub(:created_at => 23.hours.ago)
# link.authorized?.should be_true
# link.stub(:created_at => 25.hours.ago)
# link.authorized?.should be_false
# end
#
# it "should not be #authorize! when the created_at date is too far in the past" do
# link = Factory(:digital_link)
# link.authorize!.should be_true
# link.stub(:created_at => 23.hours.ago)
# link.authorize!.should be_true
# link.stub(:created_at => 25.hours.ago)
# link.authorize!.should be_false
# end
#
# it "should not be #authorized? when both access_counter and created_at are invalid" do
# link = Factory(:digital_link)
# link.authorized?.should be_true
# link.stub(:access_counter => 3, :created_at => 25.hours.ago)
# link.authorized?.should be_false
# end
#
# it "should not be #authorize! when both access_counter and created_at are invalid" do
# link = Factory(:digital_link)
# link.authorize!.should be_true
# link.stub(:access_counter => 3, :created_at => 25.hours.ago)
# link.authorize!.should be_false
# end
#end
end
+25
View File
@@ -0,0 +1,25 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe Digital do
context 'validation' do
it { should belong_to(:variant) }
it { should have_valid_factory(:digital) }
end
context "#create" do
end
context "#destroy" do
#it "should destroy associated digital_links" do
# digital = Factory(:digital)
# 3.times { digital.digital_links.create! :order => Factory(:order) }
# DigitalLink.count.should == 3
# digital.destroy
# DigitalLink.count.should == 0
#end
end
end
+33
View File
@@ -0,0 +1,33 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe LineItem do
context "#save" do
it "should create one link for a single digital Variant" do
digital_variant = Factory(:variant, :digital => Factory(:digital))
line_item = Factory(:line_item, :variant => digital_variant)
links = digital_variant.digital.digital_links
links.all.size.should == 1
links.first.line_item.should == line_item
end
it "should create a link for each quantity of a digital Variant, even when quantity changes later" do
digital_variant = Factory(:variant, :digital => Factory(:digital))
line_item = Factory(:line_item, :variant => digital_variant, :quantity => 5)
links = digital_variant.digital.digital_links
links.all.size.should == 5
links.each { |link| link.line_item.should == line_item }
# quantity update
line_item.quantity = 8
line_item.save
links = digital_variant.digital.digital_links
links.all.size.should == 8
links.each { |link| link.line_item.should == line_item }
end
end
end
+54
View File
@@ -0,0 +1,54 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe Order do
context 'validation' do
it { should have_valid_factory(:order) }
end
context "#add_variant" do
it "should add digital Variants of quantity 1 to an order" do
order = Factory(:order)
order.add_variant variant1 = Factory(:variant, :digital => Factory(:digital))
order.add_variant variant2 = Factory(:variant, :digital => Factory(:digital))
order.add_variant variant3 = Factory(:variant, :digital => Factory(:digital))
order.line_items.first.variant.should == variant1
order.line_items.second.variant.should == variant2
order.line_items.third.variant.should == variant3
end
it "should handle quantity higher than 1 when adding one specific digital Variant" do
order = Factory(:order)
digital_variant = Factory(:variant, :digital => Factory(:digital))
order.add_variant digital_variant, 3
order.line_items.first.quantity.should == 3
order.add_variant digital_variant, 2
order.line_items.first.quantity.should == 5
end
end
context "line_item analysis" do
it "should understand that all products are digital" do
order = Factory(:order)
3.times do
order.add_variant Factory(:variant, :digital => Factory(:digital))
end
order.digital?.should be_true
order.add_variant Factory(:variant, :digital => Factory(:digital)), 4
order.digital?.should be_true
end
it "should understand that not all products are digital" do
order = Factory(:order)
3.times do
order.add_variant Factory(:variant, :digital => Factory(:digital))
end
order.add_variant Factory(:variant) # this is the analog product
order.digital?.should be_false
order.add_variant Factory(:variant, :digital => Factory(:digital)), 4
order.digital?.should be_false
end
end
end
+61
View File
@@ -0,0 +1,61 @@
require File.expand_path("../factories", __FILE__)
########################################################################
# NOTE: The rest of file is the same as spree/core/spec/spec_helper.rb #
########################################################################
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../test_app/config/environment", __FILE__)
require 'rspec/rails'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
require 'spree_core/testing_support/factories'
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
#config.include Devise::TestHelpers, :type => :controller
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, comment the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
end
@configuration ||= AppConfiguration.find_or_create_by_name("Default configuration")
PAYMENT_STATES = Payment.state_machine.states.keys unless defined? PAYMENT_STATES
SHIPMENT_STATES = Shipment.state_machine.states.keys unless defined? SHIPMENT_STATES
ORDER_STATES = Order.state_machine.states.keys unless defined? ORDER_STATES
# Usage:
#
# context "factory" do
# it { should have_valid_factory(:address) }
# end
RSpec::Matchers.define :have_valid_factory do |factory_name|
match do |model|
Factory(factory_name).new_record?.should be_false
end
end