Commit 112531c1b6f41ca12da3664bbc9dac4d867108d7
1 parent
56661a84
Exists in
master
and in
29 other branches
rails3: fix ArticleCategorization unit tests
Showing
1 changed file
with
29 additions
and
15 deletions
Show diff stats
test/unit/article_categorization_test.rb
@@ -9,17 +9,21 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | @@ -9,17 +9,21 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | ||
9 | should 'belong to article' do | 9 | should 'belong to article' do |
10 | p = create_user('testuser').person | 10 | p = create_user('testuser').person |
11 | article = p.articles.build(:name => 'test article'); article.save! | 11 | article = p.articles.build(:name => 'test article'); article.save! |
12 | - assert_equal article, ArticleCategorization.new(:article => article).article | 12 | + categorization = ArticleCategorization.new |
13 | + categorization.article = article | ||
14 | + assert_equal article, categorization.article | ||
13 | end | 15 | end |
14 | 16 | ||
15 | should 'belong to category' do | 17 | should 'belong to category' do |
16 | - category = Category.create!(:name => 'one category', :environment => Environment.default) | ||
17 | - assert_equal category, ArticleCategorization.new(:category => category).category | 18 | + category = create_category('one category') |
19 | + categorization = ArticleCategorization.new | ||
20 | + categorization.category = category | ||
21 | + assert_equal category, categorization.category | ||
18 | end | 22 | end |
19 | 23 | ||
20 | should 'create instances for the entire hierarchy' do | 24 | should 'create instances for the entire hierarchy' do |
21 | - c1 = Category.create!(:name => 'c1', :environment => Environment.default) | ||
22 | - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) | 25 | + c1 = create_category('c1') |
26 | + c2 = create_category('c2', c1) | ||
23 | 27 | ||
24 | p = create_user('testuser').person | 28 | p = create_user('testuser').person |
25 | a = p.articles.create!(:name => 'test') | 29 | a = p.articles.create!(:name => 'test') |
@@ -32,9 +36,9 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | @@ -32,9 +36,9 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | ||
32 | end | 36 | end |
33 | 37 | ||
34 | should 'not duplicate entry for category that is parent of two others' do | 38 | should 'not duplicate entry for category that is parent of two others' do |
35 | - c1 = Category.create!(:name => 'c1', :environment => Environment.default) | ||
36 | - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) | ||
37 | - c3 = c1.children.create!(:name => 'c3', :environment => Environment.default) | 39 | + c1 = create_category('c1') |
40 | + c2 = create_category('c2', c1) | ||
41 | + c3 = create_category('c3', c1) | ||
38 | 42 | ||
39 | p = create_user('testuser').person | 43 | p = create_user('testuser').person |
40 | a = p.articles.create!(:name => 'test') | 44 | a = p.articles.create!(:name => 'test') |
@@ -46,9 +50,9 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | @@ -46,9 +50,9 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | ||
46 | end | 50 | end |
47 | 51 | ||
48 | should 'remove all instances for a given article' do | 52 | should 'remove all instances for a given article' do |
49 | - c1 = Category.create!(:name => 'c1', :environment => Environment.default) | ||
50 | - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) | ||
51 | - c3 = c1.children.create!(:name => 'c3', :environment => Environment.default) | 53 | + c1 = create_category('c1') |
54 | + c2 = create_category('c2', c1) | ||
55 | + c3 = create_category('c3', c1) | ||
52 | 56 | ||
53 | p = create_user('testuser').person | 57 | p = create_user('testuser').person |
54 | a = p.articles.create!(:name => 'test') | 58 | a = p.articles.create!(:name => 'test') |
@@ -62,8 +66,8 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | @@ -62,8 +66,8 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | ||
62 | end | 66 | end |
63 | 67 | ||
64 | should 'not duplicate when adding the parent of a category by witch the article is already categorized' do | 68 | should 'not duplicate when adding the parent of a category by witch the article is already categorized' do |
65 | - c1 = Category.create!(:name => 'c1', :environment => Environment.default) | ||
66 | - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) | 69 | + c1 = create_category('c1') |
70 | + c2 = create_category('c2', c1) | ||
67 | 71 | ||
68 | p = create_user('testuser').person | 72 | p = create_user('testuser').person |
69 | a = p.articles.create!(:name => 'test') | 73 | a = p.articles.create!(:name => 'test') |
@@ -75,8 +79,8 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | @@ -75,8 +79,8 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | ||
75 | end | 79 | end |
76 | 80 | ||
77 | should 'make parent real when categorized after child' do | 81 | should 'make parent real when categorized after child' do |
78 | - c1 = Category.create!(:name => 'c1', :environment => Environment.default) | ||
79 | - c2 = c1.children.create!(:name => 'c2', :environment => Environment.default) | 82 | + c1 = create_category('c1') |
83 | + c2 = create_category('c2', c1) | ||
80 | 84 | ||
81 | p = create_user('testuser').person | 85 | p = create_user('testuser').person |
82 | a = p.articles.create!(:name => 'test') | 86 | a = p.articles.create!(:name => 'test') |
@@ -85,4 +89,14 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | @@ -85,4 +89,14 @@ class ArticleCategorizationTest < ActiveSupport::TestCase | ||
85 | 89 | ||
86 | assert ArticleCategorization.find(:first, :conditions => [ 'category_id = ? and article_id = ? and not virtual', c1.id, a.id ]), 'categorization must be promoted to not virtual' | 90 | assert ArticleCategorization.find(:first, :conditions => [ 'category_id = ? and article_id = ? and not virtual', c1.id, a.id ]), 'categorization must be promoted to not virtual' |
87 | end | 91 | end |
92 | + | ||
93 | + private | ||
94 | + | ||
95 | + def create_category(name, parent = nil) | ||
96 | + c = Category.new(:name => name) | ||
97 | + c.environment = Environment.default | ||
98 | + c.parent = parent | ||
99 | + c.save! | ||
100 | + c | ||
101 | + end | ||
88 | end | 102 | end |