Commit 81cc92ba15a8ed70bd70e462c0b6a9546257c6ff
1 parent
61c01f54
Exists in
master
and in
29 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,13 +164,21 @@ class Product < ActiveRecord::Base | ||
164 | def qualifiers_list=(qualifiers) | 164 | def qualifiers_list=(qualifiers) |
165 | self.product_qualifiers.destroy_all | 165 | self.product_qualifiers.destroy_all |
166 | qualifiers.each do |qualifier_id, certifier_id| | 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 | end | 174 | end |
169 | end | 175 | end |
170 | 176 | ||
171 | def order_inputs!(order = []) | 177 | def order_inputs!(order = []) |
172 | order.each_with_index do |input_id, array_index| | 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 | end | 182 | end |
175 | end | 183 | end |
176 | 184 |
test/unit/product_test.rb
@@ -25,7 +25,7 @@ class ProductTest < ActiveSupport::TestCase | @@ -25,7 +25,7 @@ class ProductTest < ActiveSupport::TestCase | ||
25 | 25 | ||
26 | should 'create product' do | 26 | should 'create product' do |
27 | assert_difference Product, :count do | 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 | assert p.save | 29 | assert p.save |
30 | end | 30 | end |
31 | end | 31 | end |
@@ -82,7 +82,7 @@ class ProductTest < ActiveSupport::TestCase | @@ -82,7 +82,7 @@ class ProductTest < ActiveSupport::TestCase | ||
82 | 82 | ||
83 | should 'save image on create product' do | 83 | should 'save image on create product' do |
84 | assert_difference Product, :count do | 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 | :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') | 86 | :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') |
87 | }, :enterprise_id => @profile.id) | 87 | }, :enterprise_id => @profile.id) |
88 | assert_equal p.image(true).filename, 'rails.png' | 88 | assert_equal p.image(true).filename, 'rails.png' |
@@ -158,7 +158,7 @@ class ProductTest < ActiveSupport::TestCase | @@ -158,7 +158,7 @@ class ProductTest < ActiveSupport::TestCase | ||
158 | ["12.345.678", 12345678.00], | 158 | ["12.345.678", 12345678.00], |
159 | ["12,345,678", 12345678.00] | 159 | ["12,345,678", 12345678.00] |
160 | ].each do |input, output| | 160 | ].each do |input, output| |
161 | - product = Product.new(:price => input) | 161 | + product = build(Product, :price => input) |
162 | assert_equal output, product.price | 162 | assert_equal output, product.price |
163 | end | 163 | end |
164 | end | 164 | end |
@@ -173,13 +173,13 @@ class ProductTest < ActiveSupport::TestCase | @@ -173,13 +173,13 @@ class ProductTest < ActiveSupport::TestCase | ||
173 | ["12.345.678", 12345678.00], | 173 | ["12.345.678", 12345678.00], |
174 | ["12,345,678", 12345678.00] | 174 | ["12,345,678", 12345678.00] |
175 | ].each do |input, output| | 175 | ].each do |input, output| |
176 | - product = Product.new(:discount => input) | 176 | + product = build(Product, :discount => input) |
177 | assert_equal output, product.discount | 177 | assert_equal output, product.discount |
178 | end | 178 | end |
179 | end | 179 | end |
180 | 180 | ||
181 | should 'strip name with malformed HTML when sanitize' do | 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 | product.name = "<h1 Bla </h1>" | 183 | product.name = "<h1 Bla </h1>" |
184 | product.valid? | 184 | product.valid? |
185 | 185 | ||
@@ -187,7 +187,7 @@ class ProductTest < ActiveSupport::TestCase | @@ -187,7 +187,7 @@ class ProductTest < ActiveSupport::TestCase | ||
187 | end | 187 | end |
188 | 188 | ||
189 | should 'escape malformed html tags' do | 189 | should 'escape malformed html tags' do |
190 | - product = Product.new(:product_category => @product_category) | 190 | + product = build(Product, :product_category => @product_category) |
191 | product.name = "<h1 Malformed >> html >< tag" | 191 | product.name = "<h1 Malformed >> html >< tag" |
192 | product.description = "<h1 Malformed</h1>><<<a>> >> html >< tag" | 192 | product.description = "<h1 Malformed</h1>><<<a>> >> html >< tag" |
193 | product.valid? | 193 | product.valid? |
@@ -197,21 +197,23 @@ class ProductTest < ActiveSupport::TestCase | @@ -197,21 +197,23 @@ class ProductTest < ActiveSupport::TestCase | ||
197 | end | 197 | end |
198 | 198 | ||
199 | should 'use name of category when has no name yet' do | 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 | assert product.valid? | 203 | assert product.valid? |
202 | - assert_equal product.name, @product_category.name | 204 | + assert_equal @product_category.name, product.name |
203 | end | 205 | end |
204 | 206 | ||
205 | should 'not save without category' do | 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 | product.valid? | 209 | product.valid? |
208 | assert product.errors[:product_category_id.to_s].present? | 210 | assert product.errors[:product_category_id.to_s].present? |
209 | end | 211 | end |
210 | 212 | ||
211 | should 'not save with a invalid category' do | 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 | assert_raise ActiveRecord::AssociationTypeMismatch do | 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 | end | 217 | end |
216 | end | 218 | end |
217 | 219 | ||
@@ -282,13 +284,13 @@ class ProductTest < ActiveSupport::TestCase | @@ -282,13 +284,13 @@ class ProductTest < ActiveSupport::TestCase | ||
282 | product = Product.new | 284 | product = Product.new |
283 | assert !product.has_basic_info? | 285 | assert !product.has_basic_info? |
284 | 286 | ||
285 | - product = Product.new(:unit => Unit.new) | 287 | + product = build(Product, :unit => Unit.new) |
286 | assert product.has_basic_info? | 288 | assert product.has_basic_info? |
287 | 289 | ||
288 | - product = Product.new(:price => 1) | 290 | + product = build(Product, :price => 1) |
289 | assert product.has_basic_info? | 291 | assert product.has_basic_info? |
290 | 292 | ||
291 | - product = Product.new(:discount => 1) | 293 | + product = build(Product, :discount => 1) |
292 | assert product.has_basic_info? | 294 | assert product.has_basic_info? |
293 | end | 295 | end |
294 | 296 | ||
@@ -307,9 +309,9 @@ class ProductTest < ActiveSupport::TestCase | @@ -307,9 +309,9 @@ class ProductTest < ActiveSupport::TestCase | ||
307 | 309 | ||
308 | should 'save order of inputs' do | 310 | should 'save order of inputs' do |
309 | product = fast_create(Product) | 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 | assert_equal [first, second, third], product.inputs | 316 | assert_equal [first, second, third], product.inputs |
315 | 317 | ||
@@ -319,9 +321,9 @@ class ProductTest < ActiveSupport::TestCase | @@ -319,9 +321,9 @@ class ProductTest < ActiveSupport::TestCase | ||
319 | end | 321 | end |
320 | 322 | ||
321 | should 'format name with unit' do | 323 | should 'format name with unit' do |
322 | - product = Product.new(:name => "My product") | 324 | + product = build(Product, :name => "My product") |
323 | assert_equal "My product", product.name_with_unit | 325 | assert_equal "My product", product.name_with_unit |
324 | - product.unit = Unit.new(:name => 'litre') | 326 | + product.unit = build(Unit, :name => 'litre') |
325 | assert_equal "My product - litre", product.name_with_unit | 327 | assert_equal "My product - litre", product.name_with_unit |
326 | end | 328 | end |
327 | 329 | ||
@@ -427,7 +429,7 @@ class ProductTest < ActiveSupport::TestCase | @@ -427,7 +429,7 @@ class ProductTest < ActiveSupport::TestCase | ||
427 | 429 | ||
428 | env_production_cost = fast_create(ProductionCost, :owner_id => ent.environment.id, :owner_type => 'Environment') | 430 | env_production_cost = fast_create(ProductionCost, :owner_id => ent.environment.id, :owner_type => 'Environment') |
429 | ent_production_cost = fast_create(ProductionCost, :owner_id => ent.id, :owner_type => 'Profile') | 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 | assert_equal [env_production_cost, ent_production_cost], product.available_production_costs | 433 | assert_equal [env_production_cost, ent_production_cost], product.available_production_costs |
432 | end | 434 | end |
433 | 435 | ||
@@ -436,7 +438,7 @@ class ProductTest < ActiveSupport::TestCase | @@ -436,7 +438,7 @@ class ProductTest < ActiveSupport::TestCase | ||
436 | product = fast_create(Product, :enterprise_id => ent.id) | 438 | product = fast_create(Product, :enterprise_id => ent.id) |
437 | 439 | ||
438 | env_production_cost = fast_create(ProductionCost, :owner_id => ent.environment.id, :owner_type => 'Environment') | 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 | input = fast_create(Input, :product_id => product.id, :product_category_id => fast_create(ProductCategory).id, :price_per_unit => 20.0, :amount_used => 2) | 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,31 +495,31 @@ class ProductTest < ActiveSupport::TestCase | ||
493 | prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :enterprise_id => @profile.id) | 495 | prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :enterprise_id => @profile.id) |
494 | assert_equal 0, prod.percentage_from_solidarity_economy.first | 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 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) | 499 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) |
498 | assert_equal 0, prod.percentage_from_solidarity_economy.first | 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 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) | 503 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
502 | assert_equal 50, prod.percentage_from_solidarity_economy.first | 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 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) | 507 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) |
506 | assert_equal 25, prod.percentage_from_solidarity_economy.first | 508 | assert_equal 25, prod.percentage_from_solidarity_economy.first |
507 | 509 | ||
508 | prod = fast_create(Product, :name => 'test product1', :product_category_id => @product_category.id, :enterprise_id => @profile.id) | 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 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) | 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 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) | 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 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) | 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 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) | 518 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => false) |
517 | assert_equal 75, prod.percentage_from_solidarity_economy.first | 519 | assert_equal 75, prod.percentage_from_solidarity_economy.first |
518 | 520 | ||
519 | prod = fast_create(Product, :name => 'test product', :product_category_id => @product_category.id, :enterprise_id => @profile.id) | 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 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) | 523 | :amount_used => 10, :price_per_unit => 10, :is_from_solidarity_economy => true) |
522 | assert_equal 100, prod.percentage_from_solidarity_economy.first | 524 | assert_equal 100, prod.percentage_from_solidarity_economy.first |
523 | end | 525 | end |
@@ -543,9 +545,9 @@ class ProductTest < ActiveSupport::TestCase | @@ -543,9 +545,9 @@ class ProductTest < ActiveSupport::TestCase | ||
543 | should 'return more recent products' do | 545 | should 'return more recent products' do |
544 | Product.destroy_all | 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 | prod1.update_attribute :created_at, Time.now-2.days | 552 | prod1.update_attribute :created_at, Time.now-2.days |
551 | prod2.update_attribute :created_at, Time.now-1.days | 553 | prod2.update_attribute :created_at, Time.now-1.days |
@@ -555,9 +557,9 @@ class ProductTest < ActiveSupport::TestCase | @@ -555,9 +557,9 @@ class ProductTest < ActiveSupport::TestCase | ||
555 | end | 557 | end |
556 | 558 | ||
557 | should 'return products from a category' do | 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 | p1 = fast_create(Product, :product_category_id => pc1) | 563 | p1 = fast_create(Product, :product_category_id => pc1) |
562 | p2 = fast_create(Product, :product_category_id => pc1) | 564 | p2 = fast_create(Product, :product_category_id => pc1) |
563 | p3 = fast_create(Product, :product_category_id => pc2) | 565 | p3 = fast_create(Product, :product_category_id => pc2) |