require File.dirname(__FILE__) + '/../test_helper' class ProductTest < ActiveSupport::TestCase def setup super @product_category = fast_create(ProductCategory, :name => 'Products') @profile = fast_create(Enterprise) end should 'validate the presence of enterprise' do p = Product.new assert_raise ActiveRecord::RecordInvalid do p.save! end end should 'return associated enterprise region' do @profile.region = fast_create Region, :name => 'Salvador' @profile.save! p = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :enterprise_id => @profile.id) assert_equal @profile.region, p.region end should 'create product' do assert_difference Product, :count do p = Product.new(:name => 'test product1', :product_category => @product_category, :enterprise_id => @profile.id) assert p.save end end should 'destroy product' do p = fast_create(Product, :name => 'test product2', :product_category_id => @product_category.id) assert_difference Product, :count, -1 do p.destroy end end should 'display category name if name is nil' do p = fast_create(Product, :name => nil) p.expects(:category_name).returns('Software') assert_equal 'Software', p.name end should 'display category name if name is blank' do p = fast_create(Product, :name => '') p.expects(:category_name).returns('Software') assert_equal 'Software', p.name end should 'set nil to name if name is equal to category name' do p = fast_create(Product) p.expects(:category_name).returns('Software').at_least_once p.name = 'Software' p.save assert_equal 'Software', p.name assert_equal nil, p[:name] end should 'list recent products' do enterprise = fast_create(Enterprise, :name => "My enterprise", :identifier => 'my-enterprise') Product.delete_all p1 = enterprise.products.create!(:name => 'product 1', :product_category => @product_category) p2 = enterprise.products.create!(:name => 'product 2', :product_category => @product_category) p3 = enterprise.products.create!(:name => 'product 3', :product_category => @product_category) assert_equal [p3, p2, p1], Product.recent end should 'list recent products with limit' do enterprise = fast_create(Enterprise, :name => "My enterprise", :identifier => 'my-enterprise') Product.delete_all p1 = enterprise.products.create!(:name => 'product 1', :product_category => @product_category) p2 = enterprise.products.create!(:name => 'product 2', :product_category => @product_category) p3 = enterprise.products.create!(:name => 'product 3', :product_category => @product_category) assert_equal [p3, p2], Product.recent(2) end should 'save image on create product' do assert_difference Product, :count do p = Product.create!(:name => 'test product1', :product_category => @product_category, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }, :enterprise_id => @profile.id) assert_equal p.image(true).filename, 'rails.png' end end should 'calculate category full name' do cat = mock cat.expects(:full_name).returns('A/B/C') p = Product.new p.stubs(:product_category).returns(cat) assert_equal ['A','B','C'], p.category_full_name end should 'return a nil cateory full name when not categorized' do p = Product.new p.stubs(:product_category).returns(nil) assert_equal nil, p.category_full_name end should 'have same lat and lng of its enterprise' do ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_enterprise', :lat => 30.0, :lng => 30.0) prod = ent.products.create!(:name => 'test product', :product_category => @product_category) prod = Product.find(prod.id) assert_equal ent.lat, prod.lat assert_equal ent.lng, prod.lng end should 'update lat and lng of product afer update enterprise' do ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_enterprise', :lat => 30.0, :lng => 30.0) prod = ent.products.create!(:name => 'test product', :product_category => @product_category) ent.lat = 45.0; ent.lng = 45.0; ent.save! process_delayed_job_queue prod.reload assert_in_delta 45.0, prod.lat, 0.0001 assert_in_delta 45.0, prod.lng, 0.0001 end should 'provide url' do product = Product.new enterprise = Enterprise.new enterprise.expects(:public_profile_url).returns({}) product.expects(:id).returns(999) product.expects(:enterprise).returns(enterprise) assert_equal({:controller => 'manage_products', :action => 'show', :id => 999}, product.url) end should 'respond to public? as its enterprise public?' do e1 = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1') p1 = fast_create(Product, :name => 'test product 1', :enterprise_id => e1.id, :product_category_id => @product_category.id) assert p1.public? e1.public_profile = false e1.save!; p1.reload; assert !p1.public? end should 'accept prices in american\'s or brazilian\'s currency format' do [ [12.34, 12.34], ["12.34", 12.34], ["12,34", 12.34], ["12.345.678,90", 12345678.90], ["12,345,678.90", 12345678.90], ["12.345.678", 12345678.00], ["12,345,678", 12345678.00] ].each do |input, output| product = Product.new(:price => input) assert_equal output, product.price end end should 'accept discount in american\'s or brazilian\'s currency format' do [ [12.34, 12.34], ["12.34", 12.34], ["12,34", 12.34], ["12.345.678,90", 12345678.90], ["12,345,678.90", 12345678.90], ["12.345.678", 12345678.00], ["12,345,678", 12345678.00] ].each do |input, output| product = Product.new(:discount => input) assert_equal output, product.discount end end should 'strip name with malformed HTML when sanitize' do product = Product.new(:product_category => @product_category) product.name = "