Skip to content

Instantly share code, notes, and snippets.

@bogdan
Created April 23, 2011 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bogdan/63f67a02b75d06f30056 to your computer and use it in GitHub Desktop.
Save bogdan/63f67a02b75d06f30056 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe Product do
subject { Factory.build(:product)}
it_should_behave_like "Traits::Content::Slug"
it_should_behave_like "Traits::Content::Leveled"
it_should_behave_like "Traits::Blobs::Avatar"
it {should accept_values_for(:isbn, nil, "1234567890", "1234567890123")}
it {should_not accept_values_for(:isbn, "123456789", "12345678901234", "123456789012")}
context "if product with same isbn exists" do
before(:each) do
Factory.create(:product, :isbn => "1234567890")
end
it {should_not accept_values_for(:isbn, "1234567890")}
end
context "if product with empty isbn exists" do
before(:each) do
Factory.create(:product, :isbn => nil)
end
it {should accept_values_for(:isbn, nil )}
end
it { should accept_values_for(:title, 'L') }
it { should_not accept_values_for(:title, '', nil) }
it { should_not accept_values_for(:group, nil) }
it {should_not accept_values_for(:ean, nil)}
context "if product with same ean exists" do
before(:each) do
Factory.create(:product, :ean => subject.ean)
end
it {should_not be_valid}
end
context "if product with this ean is blocked" do
before(:each) do
BlockedProduct.create!(:ean => subject.ean)
end
it { should_not be_valid }
context "and drop_block is set and saved" do
before(:each) do
subject.drop_block = true
subject.save!
end
it { should be_valid }
it "should drop block" do
BlockedProduct.find_by_ean(subject.ean).should be_nil
end
end
end
describe "#formatted_summary" do
before(:each) do
subject.summary = "h<p>z<br/>sdfs</p>gg"
end
it "should replace invalid p and br tags with <br/> in summary" do
subject.formatted_summary.should == "<p>h<br />\nz<br />\nsdfs<br />\ngg</p>"
end
context "if summary is too long" do
before(:each) do
subject.summary = "ab" * Product::MAX_SHORT_SUMMARY
end
it "should not exceed #{Product::MAX_SHORT_SUMMARY} for more then 15 signs" do
subject.formatted_summary(true).size.should < (Product::MAX_SHORT_SUMMARY + 15) end
end
end
context "Successfully fetching book data from Amazon" do
before(:each) do
sample_data = [{:list_price=>"$43.95", :features=>"ISBN13: 9781934356166;Condition: NEW;Notes: Brand New from Publisher. No Remainder Mark.;&lt;a title='Condition Guide' href='/content/Condition_and_Shipping_Guide.htm' target='_blank'&gt;Click here to view our Condition Guide and Shipping Prices&lt;/a&gt;", :total_new=>9, :binding_type=>"Paperback", :authors=>"Sam Ruby;Dave Thomas;David Heinemeier Hansson", :manufacturer=>"Pragmatic Bookshelf", :total_used=>6, :edition=>"3", :number_of_pages=>850, :publisher=>"Pragmatic Bookshelf", :lowest_new_price=>"$29.01", :ean=>9781934356166, :asin=>1934356166, :lowest_used_price=>"$34.85", :title=>"Agile Web Development with Rails, Third Edition", :external_url=>"http://www.amazon.com/Agile-Web-Development-Rails-Third/dp/1934356166%3FSubscriptionId%3DAKIAJ7OK2T5JEB42YQRQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D1934356166"}]
#AmazonFetcher.stubs(:find_by_isbn).returns(sample_data)
AmazonFetcher.any_instance.expects(:find_by_isbn).returns(sample_data)
subject.save!
end
it "It should sucessfully get data and create/update the AmazonBook model" do
subject.fetch_amazon_book!
subject.amazon_book.should_not be_nil
end
end
context "Handling exceptions while fetching book data" do
before(:each) do
AmazonFetcher.any_instance.expects(:find_by_isbn).raises(Amazon::RequestError)
subject.save!
end
it "Should not rescue exceptions raised" do
lambda { subject.fetch_amazon_book! }.should raise_error()
end
end
context "Scheduling amazon book fetching" do
before(:each) do
Resque.stubs(:enqueue).returns(true)
end
it "Should not raise exceptions on book update scheduling" do
sample_amazon_book = Factory(:amazon_book)
AmazonBook.record_timestamps = false
sample_amazon_book.update_attributes :updated_at => (Settings.amazon.book_expire_days.to_i + 1).days.ago
AmazonBook.record_timestamps = true
Product.schedule_amazon_updates!
#lambda { Product.schedule_amazon_updates! }.should_not raise_error()
end
end
context "Named scopes" do
before(:each) { subject.save! }
it "should have a expired_amazon_books named scope that returns books without AmazonModel attached or with expired one" do
# Dummy book without AmazonBook model exists
Product.expired_amazon_books.size.should == 1
#Fetching AmazonBook, namespace should provide different results now
sample_data = [{:list_price=>"$43.95", :features=>"ISBN13: 9781934356166;Condition: NEW;Notes: Brand New from Publisher. No Remainder Mark.;&lt;a title='Condition Guide' href='/content/Condition_and_Shipping_Guide.htm' target='_blank'&gt;Click here to view our Condition Guide and Shipping Prices&lt;/a&gt;", :total_new=>9, :binding_type=>"Paperback", :authors=>"Sam Ruby;Dave Thomas;David Heinemeier Hansson", :manufacturer=>"Pragmatic Bookshelf", :total_used=>6, :edition=>"3", :number_of_pages=>850, :publisher=>"Pragmatic Bookshelf", :lowest_new_price=>"$29.01", :ean=>9781934356166, :asin=>1934356166, :lowest_used_price=>"$34.85", :title=>"Agile Web Development with Rails, Third Edition", :external_url=>"http://www.amazon.com/Agile-Web-Development-Rails-Third/dp/1934356166%3FSubscriptionId%3DAKIAJ7OK2T5JEB42YQRQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D1934356166"}]
AmazonFetcher.any_instance.expects(:find_by_isbn).returns(sample_data)
subject.fetch_amazon_book!
Product.expired_amazon_books.size.should == 0
end
end
describe "#order_by_representativeness" do
it "should order products with amazon book first" do
Product.count.should eql(0)
@product1 = Factory.create(:product, :amazon_book => Factory.create(:amazon_book))
@product2 = Factory.create(:product, :amazon_book => nil)
result = Product.order_by_representativeness
result[0].amazon_book.should_not be_nil
result[1].amazon_book.should be_nil
end
it "should order products with hight edition first" do
@product1 = Factory.create(:product, :edition_number => 1)
@product2 = Factory.create(:product, :edition_number => 2)
result = Product.order_by_representativeness
result[0].edition_number.should eql(2)
result[1].edition_number.should eql(1)
end
it "should order 'hardcover' product_packaging before others" do
@product1 = Factory.create(:product, :product_packaging => "Paperback edition")
@product2 = Factory.create(:product, :product_packaging => "Hardcover edition")
result = Product.order_by_representativeness
result[0].product_packaging.should eql("Hardcover edition")
result[1].product_packaging.should eql("Paperback edition")
end
end
describe "#by_category named scope" do
let(:category) { Factory.create(:category) }
before(:each) do
subject.categories << category
subject.save!
end
it "should find products that has given category" do
Product.by_category(category.id).should_not be_empty
end
it "should not find products that doesn't have given category" do
Product.by_category(Factory.create(:category).id).should be_empty
end
end
it "should provide list of product's authors" do
product_contributor = Factory(:authored_product_contributor, :product => subject)
subject.authors.should_not be_empty
end
it "should be able to show product group recommendations" do
subject.recommendations.should be_empty
product_contributor = Factory(:product_contributor, :product => subject)
contributor_who_recommended = Factory(:contributor)
recommendation = Factory(:recommendation, :user => contributor_who_recommended.user, :group => subject.group)
subject.recommendations.should_not be_empty
end
it "should be able to show product group reviews" do
subject.reviews.should be_empty
product_contributor = Factory(:product_contributor, :product => subject)
contributor_who_reviewed = Factory(:contributor)
review = Factory(:review, :user => contributor_who_reviewed.user, :group => subject.group)
subject.reviews.should_not be_empty
end
it "should be able to show book product groups inspired by this product group" do
subject.inspirations.should be_empty
product_contributor = Factory(:product_contributor, :product => subject)
inspired_product_contributor = Factory(:product_contributor)
inspiration = Factory(:inspiration, :group => subject.group)
subject.inspirations.should_not be_empty
end
it "should be stringified" do
subject.to_s.should_not be_nil
end
context "after #destroy" do
before(:each) do
subject.save!
subject.destroy
end
it "should block product with this EAN" do
BlockedProduct.blocked?(subject.ean).should be_true
end
end
end
# == Schema Information
#
# Table name: products
#
# id :integer not null, primary key
# group_id :integer
# ean :string(255)
# isbn :string(255)
# short_title :string(255)
# title :string(1000)
# summary :text
# product_type :string(255)
# product_format :string(255)
# product_packaging :string(255)
# edition_number :integer
# series :string(1000)
# series_number :string(255)
# abridged_edition :boolean
# large_print :boolean
# audience_age_minimum :integer
# audience_age_maximum :integer
# audience_grade_minimum :integer
# audience_grade_maximum :integer
# lexical_reading_level :integer
# illustrated :boolean
# content_language :string(255)
# publisher :string(1000)
# imprint :string(1000)
# ingram_family_id :integer
# slug :string(255)
# created_at :datetime
# updated_at :datetime
# avatar_id :integer
# title_level :integer default(0)
# summary_level :integer default(0)
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment