article_categorization_test.rb
3.19 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
require_relative "../test_helper"
class ArticleCategorizationTest < ActiveSupport::TestCase
should 'use articles_categories table' do
assert_equal 'articles_categories', ArticleCategorization.table_name
end
should 'belong to article' do
p = create_user('testuser').person
article = p.articles.build(:name => 'test article'); article.save!
categorization = ArticleCategorization.new
categorization.article = article
assert_equal article, categorization.article
end
should 'belong to category' do
category = create_category('one category')
categorization = ArticleCategorization.new
categorization.category = category
assert_equal category, categorization.category
end
should 'create instances for the entire hierarchy' do
c1 = create_category('c1')
c2 = create_category('c2', c1)
p = create_user('testuser').person
a = p.articles.create!(:name => 'test')
assert_difference 'ArticleCategorization.count(:category_id)', 2 do
ArticleCategorization.add_category_to_article(c2, a)
end
assert_equal 2, ArticleCategorization.find_all_by_article_id(a.id).size
end
should 'not duplicate entry for category that is parent of two others' do
c1 = create_category('c1')
c2 = create_category('c2', c1)
c3 = create_category('c3', c1)
p = create_user('testuser').person
a = p.articles.create!(:name => 'test')
assert_difference 'ArticleCategorization.count(:category_id)', 3 do
ArticleCategorization.add_category_to_article(c2, a)
ArticleCategorization.add_category_to_article(c3, a)
end
end
should 'remove all instances for a given article' do
c1 = create_category('c1')
c2 = create_category('c2', c1)
c3 = create_category('c3', c1)
p = create_user('testuser').person
a = p.articles.create!(:name => 'test')
ArticleCategorization.add_category_to_article(c2, a)
ArticleCategorization.add_category_to_article(c3, a)
assert_difference 'ArticleCategorization.count(:category_id)', -3 do
ArticleCategorization.remove_all_for(a)
end
end
should 'not duplicate when adding the parent of a category by witch the article is already categorized' do
c1 = create_category('c1')
c2 = create_category('c2', c1)
p = create_user('testuser').person
a = p.articles.create!(:name => 'test')
assert_difference 'ArticleCategorization.count(:category_id)', 2 do
ArticleCategorization.add_category_to_article(c2, a)
ArticleCategorization.add_category_to_article(c1, a)
end
end
should 'make parent real when categorized after child' do
c1 = create_category('c1')
c2 = create_category('c2', c1)
p = create_user('testuser').person
a = p.articles.create!(:name => 'test')
ArticleCategorization.add_category_to_article(c2, a)
ArticleCategorization.add_category_to_article(c1, a)
assert ArticleCategorization.find(:first, :conditions => [ 'category_id = ? and article_id = ? and not virtual', c1.id, a.id ]), 'categorization must be promoted to not virtual'
end
private
def create_category(name, parent = nil)
c = Category.new(:name => name)
c.environment = Environment.default
c.parent = parent
c.save!
c
end
end