product_test.rb
2.46 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
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 'find by initial' do
p1 = Product.create!(:name => 'a test product')
p2 = Product.create!(:name => 'A Capitalize Product')
p3 = Product.create!(:name => 'b-class test product')
list = Product.find_by_initial('a')
assert_includes list, p1
assert_includes list, p2
assert_not_includes list, p3
end
should 'calculate catagory full name' do
cat = mock
cat.expects(:full_name).returns('A B C')
p = Product.new
p.expects(:product_category).returns(cat)
assert_equal 'A B C', 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
end