price_detail_test.rb
2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require_relative "../test_helper"
class PriceDetailTest < ActiveSupport::TestCase
should 'have price 0 by default' do
p = PriceDetail.new
assert p.price.zero?
end
should 'return zero on price if it is blank' do
p = PriceDetail.new(:price => '')
assert p.price.zero?
end
should 'accept price 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|
new_price_detail = PriceDetail.new(:price => input)
assert_equal output, new_price_detail.price
end
end
should 'belongs to a product' do
p = PriceDetail.new
assert_respond_to p, :product
end
should 'product be mandatory' do
p = PriceDetail.new
p.valid?
assert p.errors[:product_id].any?
end
should 'have production cost' do
product = fast_create(Product)
cost = fast_create(ProductionCost, :owner_id => Environment.default.id, :owner_type => 'Environment')
detail = product.price_details.create(:production_cost_id => cost.id, :price => 10)
assert_equal cost, PriceDetail.find(detail.id).production_cost
end
should 'production cost not be mandatory' do
product = fast_create(Product)
price = PriceDetail.new
price.product = product
price.valid?
assert price.errors.empty?
end
should 'the production cost be unique on scope of product' do
product = fast_create(Product)
cost = fast_create(ProductionCost, :owner_id => Environment.default.id, :owner_type => 'environment')
detail1 = product.price_details.create(:production_cost_id => cost.id, :price => 10)
detail2 = product.price_details.build(:production_cost_id => cost.id, :price => 10)
detail2.valid?
assert detail2.errors[:production_cost_id].any?
end
should 'format values to float with 2 decimals' do
enterprise = fast_create(Enterprise)
product = fast_create(Product, :profile_id => enterprise.id)
cost = fast_create(ProductionCost, :owner_id => Environment.default.id, :owner_type => 'environment')
price_detail = product.price_details.create(:production_cost_id => cost.id, :price => 10)
assert_equal "10.00", price_detail.formatted_value(:price)
end
should 'have the production cost name as name' do
product = fast_create(Product)
cost = fast_create(ProductionCost, :name => 'Energy',:owner_id => Environment.default.id, :owner_type => 'environment')
detail = product.price_details.create(:production_cost_id => cost.id, :price => 10)
assert_equal 'Energy', detail.name
end
end