category_test.rb
3.56 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
require File.dirname(__FILE__) + '/../test_helper'
class CategoryTest < Test::Unit::TestCase
def setup
@env = Environment.create!(:name => 'Enviroment for testing')
end
def test_mandatory_field_name
c = Category.new
c.organization = @env
c.save
assert c.errors.invalid?(:name)
end
def test_mandatory_field_name
c = Category.new
c.name = 'product category for testing'
assert !c.valid?
assert c.errors.invalid?(:environment_id)
end
def test_relationship_with_environment
c = Category.create!(:name => 'product category for testing', :environment_id => @env.id)
assert_equal @env, c.environment
end
def test_relation_with_parent
parent_category = Category.create!(:name => 'parent category for testing', :environment_id => @env.id)
c = Category.create!(:name => 'product category for testing', :environment_id => @env.id, :parent_id => parent_category.id)
assert_equal parent_category, c.parent
end
# def test_full_text_search
# c = Category.create!(:name => 'product category for testing', :environment_id => @env.id)
# assert @env.product_categories.full_text_search('product*').include?(c)
# end
def test_category_full_name
cat = Category.new(:name => 'category_name')
assert_equal 'category_name', cat.full_name
end
def test_subcategory_full_name
cat = Category.new(:name => 'category_name')
sub_cat = Category.new(:name => 'subcategory_name')
sub_cat.stubs(:parent).returns(cat)
sub_cat.parent = cat
assert_equal 'category_name/subcategory_name', sub_cat.full_name
end
def test_category_level
cat = Category.new(:name => 'category_name')
assert_equal 0, cat.level
end
def test_subegory_level
cat = Category.new(:name => 'category_name')
sub_cat = Category.new(:name => 'subcategory_name')
sub_cat.stubs(:parent).returns(cat)
sub_cat.parent = cat
assert_equal 1, sub_cat.level
end
def test_top_level
cat = Category.new(:name => 'category_name')
assert cat.top_level?
end
def test_not_top_level
cat = Category.new(:name => 'category_name')
sub_cat = Category.new(:name => 'subcategory_name')
sub_cat.stubs(:parent).returns(cat)
sub_cat.parent = cat
assert !sub_cat.top_level?
end
def test_leaf
cat = Category.new(:name => 'category_name')
sub_cat = Category.new(:name => 'subcategory_name')
cat.stubs(:children).returns([sub_cat])
assert !cat.leaf?
end
def test_not_leaf
cat = Category.new(:name => 'category_name')
sub_cat = Category.new(:name => 'subcategory_name')
cat.stubs(:children).returns([])
assert cat.leaf?
end
def test_top_level_for
cat = Category.create(:name => 'Category for testing', :environment_id => @env.id)
sub_cat = Category.create(:name => 'SubCategory for testing', :environment_id => @env.id, :parent_id => cat.id)
roots = Category.top_level_for(@env)
assert_equal 1, roots.size
end
def test_slug
c = Category.create(:name => 'Category name')
assert_equal 'category-name', c.slug
end
def test_path_for_toplevel
c = Category.new(:name => 'top_level')
assert_equal 'top-level', c.path
end
def test_path_for_subcategory
c1 = Category.new(:name => 'parent')
c2 = Category.new
c2.parent = c1
c2.name = 'child'
assert_equal 'parent/child', c2.path
end
def test_save_
c1 = Category.create!(:name => 'parent', :environment_id => @env.id)
c2 = Category.new(:name => 'child', :environment_id => @env.id)
c2.parent = c1
c2.save!
assert_equal 'parent/child', c2.path
end
end