product_test.rb
6.29 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require File.dirname(__FILE__) + '/../test_helper'
class ProductTest < Test::Unit::TestCase
should 'create product' do
assert_difference Product, :count do
p = Product.new(:name => 'test product1')
assert p.save
end
end
should 'destroy product' do
p = Product.create(:name => 'test product2')
assert_difference Product, :count, -1 do
p.destroy
end
end
should 'name be unique' do
Product.create(:name => 'test product3')
assert_no_difference Product, :count do
p = Product.new(:name => 'test product3')
assert !p.save
end
end
should 'list recent products' do
enterprise = Enterprise.create!(:name => "My enterprise", :identifier => 'my-enterprise')
Product.delete_all
p1 = enterprise.products.create!(:name => 'product 1')
p2 = enterprise.products.create!(:name => 'product 2')
p3 = enterprise.products.create!(:name => 'product 3')
assert_equal [p3, p2, p1], Product.recent
end
should 'list recent products with limit' do
enterprise = Enterprise.create!(:name => "My enterprise", :identifier => 'my-enterprise')
Product.delete_all
p1 = enterprise.products.create!(:name => 'product 1')
p2 = enterprise.products.create!(:name => 'product 2')
p3 = enterprise.products.create!(:name => 'product 3')
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', :image_builder => {
:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
})
assert_equal p.image(true).filename, 'rails.png'
end
end
should 'calculate catagory 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 'be indexed by category full name' do
p = Product.new(:name => 'a test product')
p.expects(:category_full_name).returns('interesting category')
p.save!
assert_includes Product.find_by_contents('interesting'), p
end
should 'have same lat and lng of its enterprise' do
ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_enterprise', :lat => 30.0, :lng => 30.0 )
prod = ent.products.create!(:name => 'test product')
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 = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_enterprise', :lat => 30.0, :lng => 30.0 )
prod = ent.products.create!(:name => 'test product')
ent.lat = 45.0; ent.lng = 45.0; ent.save!
prod.reload
assert_in_delta 45.0, prod.lat, 0.0001
assert_in_delta 45.0, prod.lng, 0.0001
end
should 'be searched by radius and distance' do
prod1 = Product.create!(:name => 'prod test 1', :lat => 30.0, :lng => 30.0)
prod2 = Product.create!(:name => 'prod test 2', :lat => 45.0, :lng => 45.0)
prods = Product.find(:all, :within => 10, :origin => [30.0, 30.0])
assert_includes prods, prod1
assert_not_includes prods, prod2
end
should 'provide url' do
product = Product.new
result = mock
enterprise = Enterprise.new
enterprise.expects(:generate_url).with(:controller => 'catalog', :action => 'show', :id => 999).returns(result)
product.expects(:id).returns(999)
product.expects(:enterprise).returns(enterprise)
assert_same result, product.url
end
should 'categorize also with product categorization' do
cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default)
ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
p = ent.products.create!(:name => 'test product')
p.product_category = cat
p.save!
assert ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat})
end
should 'categorize parent cateogries with product categorization' do
parent_cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default)
child_cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default, :parent => parent_cat)
ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
p = ent.products.create!(:name => 'test product')
p.product_category = child_cat
p.save!
assert ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => parent_cat})
assert ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => child_cat})
end
should 'change product categorization when product category changes' do
cat1 = ProductCategory.create(:name => 'test cat 1', :environment => Environment.default)
cat2 = ProductCategory.create(:name => 'test cat 2', :environment => Environment.default)
ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
p = ent.products.create!(:name => 'test product', :product_category => cat1)
p.product_category = cat2
p.save!
assert ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat2}), 'must include the new category'
assert !ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat1}), 'must exclude the old category'
end
should 'remove categorization when product category is removed' do
cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default)
ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent')
p = ent.products.create!(:name => 'test product', :product_category => cat)
p.product_category = nil
p.save!
assert !ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat})
end
should 'respond to public? as its enterprise public?' do
e1 = Enterprise.create!(:name => 'test ent 1', :identifier => 'test_ent1')
p1 = Product.create!(:name => 'test product 1', :enterprise => e1)
assert p1.public?
e1.public_profile = false
e1.save!
assert !p1.public?
end
end