Commit 81cc92ba15a8ed70bd70e462c0b6a9546257c6ff
1 parent
61c01f54
Exists in
master
and in
22 other branches
rails3: fix product tests
PS: still failing due to image mass-assignment
Showing
2 changed files
with
47 additions
and
37 deletions
Show diff stats
app/models/product.rb
| ... | ... | @@ -164,13 +164,21 @@ class Product < ActiveRecord::Base |
| 164 | 164 | def qualifiers_list=(qualifiers) |
| 165 | 165 | self.product_qualifiers.destroy_all |
| 166 | 166 | qualifiers.each do |qualifier_id, certifier_id| |
| 167 | - self.product_qualifiers.create(:qualifier_id => qualifier_id, :certifier_id => certifier_id) if qualifier_id != 'nil' | |
| 167 | + if qualifier_id != 'nil' | |
| 168 | + product_qualifier = ProductQualifier.new | |
| 169 | + product_qualifier.product = self | |
| 170 | + product_qualifier.qualifier_id = qualifier_id | |
| 171 | + product_qualifier.certifier_id = certifier_id | |
| 172 | + product_qualifier.save! | |
| 173 | + end | |
| 168 | 174 | end |
| 169 | 175 | end |
| 170 | 176 | |
| 171 | 177 | def order_inputs!(order = []) |
| 172 | 178 | order.each_with_index do |input_id, array_index| |
| 173 | - self.inputs.find(input_id).update_attributes(:position => array_index + 1) | |
| 179 | + input = self.inputs.find(input_id) | |
| 180 | + input.position = array_index + 1 | |
| 181 | + input.save! | |
| 174 | 182 | end |
| 175 | 183 | end |
| 176 | 184 | ... | ... |
test/unit/product_test.rb
| ... | ... | @@ -25,7 +25,7 @@ class ProductTest < ActiveSupport::TestCase |
| 25 | 25 | |
| 26 | 26 | should 'create product' do |
| 27 | 27 | assert_difference Product, :count do |
| 28 | - p = Product.new(:name => 'test product1', :product_category => @product_category, :enterprise_id => @profile.id) | |
| 28 | + p = build(Product, :name => 'test product1', :product_category => @product_category, :enterprise_id => @profile.id) | |
| 29 | 29 | assert p.save |
| 30 | 30 | end |
| 31 | 31 | end |
| ... | ... | @@ -82,7 +82,7 @@ class ProductTest < ActiveSupport::TestCase |
| 82 | 82 | |
| 83 | 83 | should 'save image on create product' do |
| 84 | 84 | assert_difference Product, :count do |
| 85 | - p = Product.create!(:name => 'test product1', :product_category => @product_category, :image_builder => { | |
| 85 | + p = create(Product, :name => 'test product1', :product_category => @product_category, :image_builder => { | |
| 86 | 86 | :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') |
| 87 | 87 | }, :enterprise_id => @profile.id) |
| 88 | 88 | assert_equal p.image(true).filename, 'rails.png' |
| ... | ... | @@ -158,7 +158,7 @@ class ProductTest < ActiveSupport::TestCase |
| 158 | 158 | ["12.345.678", 12345678.00], |
| 159 | 159 | ["12,345,678", 12345678.00] |
| 160 | 160 | ].each do |input, output| |
| 161 | - product = Product.new(:price => input) | |
| 161 | + product = build(Product, :price => input) | |
| 162 | 162 | assert_equal output, product.price |
| 163 | 163 | end |
| 164 | 164 | end |
| ... | ... | @@ -173,13 +173,13 @@ class ProductTest < ActiveSupport::TestCase |
| 173 | 173 | ["12.345.678", 12345678.00], |
| 174 | 174 | ["12,345,678", 12345678.00] |
| 175 | 175 | ].each do |input, output| |
| 176 | - product = Product.new(:discount => input) | |
| 176 | + product = build(Product, :discount => input) | |
| 177 | 177 | assert_equal output, product.discount |
| 178 | 178 | end |
| 179 | 179 | end |
| 180 | 180 | |
| 181 | 181 | should 'strip name with malformed HTML when sanitize' do |
| 182 | - product = Product.new(:product_category => @product_category) | |
| 182 | + product = build(Product, :product_category => @product_category) | |
| 183 | 183 | product.name = "<h1 Bla </h1>" |
| 184 | 184 | product.valid? |
| 185 | 185 | |
| ... | ... | @@ -187,7 +187,7 @@ class ProductTest < ActiveSupport::TestCase |
| 187 | 187 | end |
| 188 | 188 | |
| 189 | 189 | should 'escape malformed html tags' do |
| 190 | - product = Product.new(:product_category => @product_category) | |
| 190 | + product = build(Product, :product_category => @product_category) | |
| 191 | 191 | product.name = "<h1 Malformed >> html >< tag" |
| 192 | 192 | product.description = "<h1 Malformed</h1>><<<a>> >> html >< tag" |
| 193 | 193 | product.valid? |
| ... | ... | @@ -197,21 +197,23 @@ class ProductTest < ActiveSupport::TestCase |
| 197 | 197 | end |
| 198 | 198 | |
| 199 | 199 | should 'use name of category when has no name yet' do |
| 200 | - product = Product.new(:product_category => @product_category, :enterprise_id => @profile.id) | |
| 200 | + product = Product.new | |
| 201 | + product.product_category = @product_category | |
| 202 | + product.enterprise = @profile | |
| 201 | 203 | assert product.valid? |
| 202 | - assert_equal product.name, @product_category.name | |
| 204 | + assert_equal @product_category.name, product.name | |
| 203 | 205 | end |
| 204 | 206 | |
| 205 | 207 | should 'not save without category' do |
| 206 | - product = Product.new(:name => 'A product without category') | |
| 208 | + product = build(Product, :name => 'A product without category') | |
| 207 | 209 | product.valid? |
| 208 | 210 | assert product.errors[:product_category_id.to_s].present? |
| 209 | 211 | end |
| 210 | 212 | |
| 211 | 213 | should 'not save with a invalid category' do |
| 212 | - category = Category.new(:name => 'Region', :environment => Environment.default) | |
| 214 | + category = build(Category, :name => 'Region', :environment => Environment.default) | |
| 213 | 215 | assert_raise ActiveRecord::AssociationTypeMismatch do |
| 214 | - Product.new(:name => 'Invalid category product', :product_category => category) | |
| 216 | + build(Product, :name => 'Invalid category product', :product_category => category) | |
| 215 | 217 | end |
| 216 | 218 | end |
| 217 | 219 | |
| ... | ... | @@ -282,13 +284,13 @@ class ProductTest < ActiveSupport::TestCase |
| 282 | 284 | product = Product.new |
| 283 | 285 | assert !product.has_basic_info? |
| 284 | 286 | |
| 285 | - product = Product.new(:unit => Unit.new) | |
| 287 | + product = build(Product, :unit => Unit.new) | |
| 286 | 288 | assert product.has_basic_info? |
| 287 | 289 | |
| 288 | - product = Product.new(:price => 1) | |
| 290 | + product = build(Product, :price => 1) | |
| 289 | 291 | assert product.has_basic_info? |
| 290 | 292 | |
| 291 | - product = Product.new(:discount => 1) | |
| 293 | + product = build(Product, :discount => 1) | |
| 292 | 294 | assert product.has_basic_info? |
| 293 | 295 | end |
| 294 | 296 | |
| ... | ... | @@ -307,9 +309,9 @@ class ProductTest < ActiveSupport::TestCase |
| 307 | 309 | |
| 308 | 310 | should 'save order of inputs' do |
| 309 | 311 | product = fast_create(Product) |
| 310 | - first = Input.create!(:product => product, :product_category => fast_create(ProductCategory)) | |
| 311 | - second = Input.create!(:product => product, :product_category => fast_create(ProductCategory)) | |
| 312 | - third = Input.create!(:product => product, :product_category => fast_create(ProductCategory)) | |
| 312 | + first = create(Input, :product => product, :product_category => fast_create(ProductCategory)) | |
| 313 | + second = create(Input, :product => product, :product_category => fast_create(ProductCategory)) | |
| 314 | + third = create(Input, :product => product, :product_category => fast_create(ProductCategory)) | |
| 313 | 315 | |
| 314 | 316 | assert_equal [first, second, third], product.inputs |
| 315 | 317 | |
| ... | ... | @@ -319,9 +321,9 @@ class ProductTest < ActiveSupport::TestCase |
| 319 | 321 | end |
| 320 | 322 | |
| 321 | 323 | should 'format name with unit' do |
| 322 | - product = Product.new(:name => "My product") | |
| 324 | + product = build(Product, :name => "My product") | |
| 323 | 325 | assert_equal "My product", product.name_with_unit |
| 324 | - product.unit = Unit.new(:name => 'litre') | |
| 326 | + product.unit = build(Unit, :name => 'litre') | |
| 325 | 327 | assert_equal "My product - litre", product.name_with_unit |
| 326 | 328 | end |
| 327 | 329 | |
| ... | ... | @@ -427,7 +429,7 @@ class ProductTest < ActiveSupport::TestCase |
| 427 | 429 | |
| 428 | 430 | env_production_cost = fast_create(ProductionCost, :owner_id => ent.environment.id, :owner_type => 'Environment') |
| 429 | 431 | ent_production_cost = fast_create(ProductionCost, :owner_id => ent.id, :owner_type => 'Profile') |
| 430 | - product.price_details.create(:production_cost => env_production_cost, :product => product) | |
| 432 | + create(PriceDetail, :product => product, :production_cost => env_production_cost, :product => product) | |
| 431 | 433 | assert_equal [env_production_cost, ent_production_cost], product.available_production_costs |
| 432 | 434 | end |
| 433 | 435 | |
| ... | ... | @@ -436,7 +438,7 @@ class ProductTest < ActiveSupport::TestCase |
| 436 | 438 | product = fast_create(Product, :enterprise_id => ent.id) |
| 437 | 439 | |
| 438 | 440 | env_production_cost = fast_create(ProductionCost, :owner_id => ent.environment.id, :owner_type => 'Environment') |
| 439 | - price_detail = product.price_details.create(:production_cost => env_production_cost, :price => 10) | |
| 441 | + price_detail = create(PriceDetail, :product => product, :production_cost => env_production_cost, :price => 10) | |
| 440 | 442 | |
| 441 | 443 | input = fast_create(Input, :product_id => product.id, :product_category_id => fast_create(ProductCategory).id, :price_per_unit => 20.0, :amount_used => 2) |
| 442 | 444 | |
| ... | ... | @@ -493,31 +495,31 @@ class ProductTest < ActiveSupport::TestCase |
| 493 | 495 | prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :enterprise_id => @profile.id) |
| 494 | 496 | assert_equal 0, prod.percentage_from_solidarity_economy.first |
| 495 | 497 | |
| 496 | - Input.create!(:product_id => prod.id, :product_category_id => @product_category.id, | |
| 498 | + create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | |
| 497 | 499 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) |
| 498 | 500 | assert_equal 0, prod.percentage_from_solidarity_economy.first |
| 499 | 501 | |
| 500 | - Input.create!(:product_id => prod.id, :product_category_id => @product_category.id, | |
| 502 | + create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | |
| 501 | 503 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
| 502 | 504 | assert_equal 50, prod.percentage_from_solidarity_economy.first |
| 503 | 505 | |
| 504 | - Input.create!(:product_id => prod.id, :product_category_id => @product_category.id, | |
| 506 | + create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | |
| 505 | 507 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) |
| 506 | 508 | assert_equal 25, prod.percentage_from_solidarity_economy.first |
| 507 | 509 | |
| 508 | 510 | prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :enterprise_id => @profile.id) |
| 509 | - Input.create!(:product_id => prod.id, :product_category_id => @product_category.id, | |
| 511 | + create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | |
| 510 | 512 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
| 511 | - Input.create!(:product_id => prod.id, :product_category_id => @product_category.id, | |
| 513 | + create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | |
| 512 | 514 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
| 513 | - Input.create!(:product_id => prod.id, :product_category_id => @product_category.id, | |
| 515 | + create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | |
| 514 | 516 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
| 515 | - Input.create!(:product_id => prod.id, :product_category_id => @product_category.id, | |
| 517 | + create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | |
| 516 | 518 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) |
| 517 | 519 | assert_equal 75, prod.percentage_from_solidarity_economy.first |
| 518 | 520 | |
| 519 | 521 | prod = fast_create(Product, :name => 'test product', :product_category_id => @product_category.id, :enterprise_id => @profile.id) |
| 520 | - Input.create!(:product_id => prod.id, :product_category_id => @product_category.id, | |
| 522 | + create(Input, :product_id => prod.id, :product_category_id => @product_category.id, | |
| 521 | 523 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
| 522 | 524 | assert_equal 100, prod.percentage_from_solidarity_economy.first |
| 523 | 525 | end |
| ... | ... | @@ -543,9 +545,9 @@ class ProductTest < ActiveSupport::TestCase |
| 543 | 545 | should 'return more recent products' do |
| 544 | 546 | Product.destroy_all |
| 545 | 547 | |
| 546 | - prod1 = Product.create!(:name => 'Damaged LP', :enterprise_id => @profile.id, :product_category_id => @product_category.id) | |
| 547 | - prod2 = Product.create!(:name => 'Damaged CD', :enterprise_id => @profile.id, :product_category_id => @product_category.id) | |
| 548 | - prod3 = Product.create!(:name => 'Damaged DVD', :enterprise_id => @profile.id, :product_category_id => @product_category.id) | |
| 548 | + prod1 = create(Product, :name => 'Damaged LP', :enterprise_id => @profile.id, :product_category_id => @product_category.id) | |
| 549 | + prod2 = create(Product, :name => 'Damaged CD', :enterprise_id => @profile.id, :product_category_id => @product_category.id) | |
| 550 | + prod3 = create(Product, :name => 'Damaged DVD', :enterprise_id => @profile.id, :product_category_id => @product_category.id) | |
| 549 | 551 | |
| 550 | 552 | prod1.update_attribute :created_at, Time.now-2.days |
| 551 | 553 | prod2.update_attribute :created_at, Time.now-1.days |
| ... | ... | @@ -555,9 +557,9 @@ class ProductTest < ActiveSupport::TestCase |
| 555 | 557 | end |
| 556 | 558 | |
| 557 | 559 | should 'return products from a category' do |
| 558 | - pc1 = ProductCategory.create!(:name => 'PC1', :environment => Environment.default) | |
| 559 | - pc2 = ProductCategory.create!(:name => 'PC2', :environment => Environment.default) | |
| 560 | - pc3 = ProductCategory.create!(:name => 'PC3', :environment => Environment.default, :parent => pc1) | |
| 560 | + pc1 = create(ProductCategory, :name => 'PC1', :environment => Environment.default) | |
| 561 | + pc2 = create(ProductCategory, :name => 'PC2', :environment => Environment.default) | |
| 562 | + pc3 = create(ProductCategory, :name => 'PC3', :environment => Environment.default, :parent => pc1) | |
| 561 | 563 | p1 = fast_create(Product, :product_category_id => pc1) |
| 562 | 564 | p2 = fast_create(Product, :product_category_id => pc1) |
| 563 | 565 | p3 = fast_create(Product, :product_category_id => pc2) | ... | ... |