Commit 61c01f54a07bab1c8605f30e7a033a0e1ea3d2e3
1 parent
dae370c6
Exists in
master
and in
22 other branches
rails3: fix category tests
PS: still with failing tests
Showing
2 changed files
with
79 additions
and
85 deletions
Show diff stats
app/models/category.rb
| ... | ... | @@ -77,7 +77,7 @@ class Category < ActiveRecord::Base |
| 77 | 77 | end |
| 78 | 78 | |
| 79 | 79 | def recent_comments(limit = 10) |
| 80 | - comments.paginate(:all, :order => 'created_at DESC, comments.id DESC', :page => 1, :per_page => limit) | |
| 80 | + comments.paginate(:order => 'created_at DESC, comments.id DESC', :page => 1, :per_page => limit) | |
| 81 | 81 | end |
| 82 | 82 | |
| 83 | 83 | def most_commented_articles(limit = 10) | ... | ... |
test/unit/category_test.rb
| ... | ... | @@ -33,61 +33,61 @@ class CategoryTest < ActiveSupport::TestCase |
| 33 | 33 | end |
| 34 | 34 | |
| 35 | 35 | def test_category_full_name |
| 36 | - cat = Category.new(:name => 'category_name') | |
| 36 | + cat = build(Category, :name => 'category_name') | |
| 37 | 37 | assert_equal 'category_name', cat.full_name |
| 38 | 38 | end |
| 39 | 39 | |
| 40 | 40 | def test_subcategory_full_name |
| 41 | - cat = Category.new(:name => 'category_name') | |
| 42 | - sub_cat = Category.new(:name => 'subcategory_name') | |
| 41 | + cat = build(Category, :name => 'category_name') | |
| 42 | + sub_cat = build(Category, :name => 'subcategory_name') | |
| 43 | 43 | sub_cat.stubs(:parent).returns(cat) |
| 44 | 44 | sub_cat.parent = cat |
| 45 | 45 | assert_equal 'category_name/subcategory_name', sub_cat.full_name |
| 46 | 46 | end |
| 47 | 47 | |
| 48 | 48 | should 'cope with nil name when calculating full_name' do |
| 49 | - cat = Category.new(:name => 'toplevel') | |
| 49 | + cat = build(Category, :name => 'toplevel') | |
| 50 | 50 | sub = Category.new |
| 51 | 51 | sub.parent = cat |
| 52 | 52 | assert_equal 'toplevel/?', sub.full_name |
| 53 | 53 | end |
| 54 | 54 | |
| 55 | 55 | def test_category_level |
| 56 | - cat = Category.new(:name => 'category_name') | |
| 56 | + cat = build(Category, :name => 'category_name') | |
| 57 | 57 | assert_equal 0, cat.level |
| 58 | 58 | end |
| 59 | 59 | |
| 60 | 60 | def test_subegory_level |
| 61 | - cat = Category.new(:name => 'category_name') | |
| 62 | - sub_cat = Category.new(:name => 'subcategory_name') | |
| 61 | + cat = build(Category, :name => 'category_name') | |
| 62 | + sub_cat = build(Category, :name => 'subcategory_name') | |
| 63 | 63 | sub_cat.stubs(:parent).returns(cat) |
| 64 | 64 | sub_cat.parent = cat |
| 65 | 65 | assert_equal 1, sub_cat.level |
| 66 | 66 | end |
| 67 | 67 | |
| 68 | 68 | def test_top_level |
| 69 | - cat = Category.new(:name => 'category_name') | |
| 69 | + cat = build(Category, :name => 'category_name') | |
| 70 | 70 | assert cat.top_level? |
| 71 | 71 | end |
| 72 | 72 | |
| 73 | 73 | def test_not_top_level |
| 74 | - cat = Category.new(:name => 'category_name') | |
| 75 | - sub_cat = Category.new(:name => 'subcategory_name') | |
| 74 | + cat = build(Category, :name => 'category_name') | |
| 75 | + sub_cat = build(Category, :name => 'subcategory_name') | |
| 76 | 76 | sub_cat.stubs(:parent).returns(cat) |
| 77 | 77 | sub_cat.parent = cat |
| 78 | 78 | assert !sub_cat.top_level? |
| 79 | 79 | end |
| 80 | 80 | |
| 81 | 81 | def test_leaf |
| 82 | - cat = Category.new(:name => 'category_name') | |
| 83 | - sub_cat = Category.new(:name => 'subcategory_name') | |
| 82 | + cat = build(Category, :name => 'category_name') | |
| 83 | + sub_cat = build(Category, :name => 'subcategory_name') | |
| 84 | 84 | cat.stubs(:children).returns([sub_cat]) |
| 85 | 85 | assert !cat.leaf? |
| 86 | 86 | end |
| 87 | 87 | |
| 88 | 88 | def test_not_leaf |
| 89 | - cat = Category.new(:name => 'category_name') | |
| 90 | - sub_cat = Category.new(:name => 'subcategory_name') | |
| 89 | + cat = build(Category, :name => 'category_name') | |
| 90 | + sub_cat = build(Category, :name => 'subcategory_name') | |
| 91 | 91 | cat.stubs(:children).returns([]) |
| 92 | 92 | assert cat.leaf? |
| 93 | 93 | end |
| ... | ... | @@ -102,38 +102,35 @@ class CategoryTest < ActiveSupport::TestCase |
| 102 | 102 | end |
| 103 | 103 | |
| 104 | 104 | def test_slug |
| 105 | - c = Category.new(:name => 'Category name') | |
| 106 | - assert_equal 'category-name', c.slug | |
| 105 | + c = build(Category) | |
| 106 | + assert_equal c.name.to_slug, c.slug | |
| 107 | 107 | end |
| 108 | 108 | |
| 109 | 109 | def test_path_for_toplevel |
| 110 | - c = Category.new(:name => 'top_level') | |
| 111 | - assert_equal 'top-level', c.path | |
| 110 | + c = build(Category) | |
| 111 | + assert_equal c.slug, c.path | |
| 112 | 112 | end |
| 113 | 113 | |
| 114 | 114 | def test_path_for_subcategory |
| 115 | - c1 = Category.new(:name => 'parent') | |
| 115 | + parent = create(Category) | |
| 116 | + child = create(Category, :parent => parent) | |
| 116 | 117 | |
| 117 | - c2 = Category.new | |
| 118 | - c2.parent = c1 | |
| 119 | - c2.name = 'child' | |
| 120 | - | |
| 121 | - assert_equal 'parent/child', c2.path | |
| 118 | + assert_equal "#{parent.path}/#{child.slug}", child.path | |
| 122 | 119 | end |
| 123 | 120 | |
| 124 | 121 | def test_should_set_path_correctly_before_saving |
| 125 | - c1 = Category.create!(:name => 'parent', :environment_id => @env.id) | |
| 122 | + parent = create(Category, :environment_id => @env.id) | |
| 126 | 123 | |
| 127 | - c2 = Category.new(:name => 'child', :environment_id => @env.id) | |
| 128 | - c2.parent = c1 | |
| 129 | - c2.save! | |
| 124 | + child = build(Category, :environment_id => @env.id) | |
| 125 | + child.parent = parent | |
| 126 | + child.save! | |
| 130 | 127 | |
| 131 | - assert_equal 'parent/child', c2.path | |
| 128 | + assert_equal "#{parent.path}/#{child.slug}", child.path | |
| 132 | 129 | end |
| 133 | 130 | |
| 134 | 131 | def test_should_refuse_to_duplicate_slug_under_the_same_parent |
| 135 | - c1 = Category.create!(:name => 'test category', :environment_id => @env.id) | |
| 136 | - c2 = Category.new(:name => 'Test: Category', :environment_id => @env.id) | |
| 132 | + c1 = create(Category, :environment_id => @env.id) | |
| 133 | + c2 = build(Category, :slug => c1.slug, :environment_id => @env.id) | |
| 137 | 134 | |
| 138 | 135 | assert !c2.valid? |
| 139 | 136 | assert c2.errors[:slug.to_s].present? |
| ... | ... | @@ -144,29 +141,26 @@ class CategoryTest < ActiveSupport::TestCase |
| 144 | 141 | root2 = fast_create(Category, :name => 'root category 2', :environment_id => @env.id) |
| 145 | 142 | child1 = fast_create(Category, :name => 'test category', :environment_id => @env.id, :parent_id => root1.id) |
| 146 | 143 | |
| 147 | - child2 = Category.new(:name => 'test category', :environment_id => @env.id, :parent => root2) | |
| 144 | + child2 = build(Category, :name => 'test category', :environment_id => @env.id, :parent => root2) | |
| 148 | 145 | assert child2.valid? |
| 149 | 146 | |
| 150 | - newroot = Category.new(:name => 'test category', :environment_id => @env.id, :parent => nil) | |
| 147 | + newroot = build(Category, :name => 'test category', :environment_id => @env.id, :parent => nil) | |
| 151 | 148 | assert newroot.valid? |
| 152 | 149 | end |
| 153 | 150 | |
| 154 | 151 | def test_renaming_a_category_should_change_path_of_children |
| 155 | - c1 = Category.create!(:name => 'parent', :environment_id => @env.id) | |
| 156 | - c2 = Category.create!(:name => 'child', :environment_id => @env.id, :parent_id => c1.id) | |
| 157 | - c3 = Category.create!(:name => 'grandchild', :environment_id => @env.id, :parent_id => c2.id) | |
| 158 | - | |
| 159 | - c1.name = 'parent new name' | |
| 160 | - c1.save! | |
| 152 | + parent = create(Category, :environment => @env) | |
| 153 | + child = create(Category, :environment => @env, :parent => parent) | |
| 154 | + grandchild = create(Category, :environment => @env, :parent => child) | |
| 161 | 155 | |
| 162 | - assert_equal 'parent-new-name', c1.path | |
| 163 | - assert_equal 'parent-new-name/child', Category.find(c2.id).path | |
| 164 | - assert_equal 'parent-new-name/child/grandchild', Category.find(c3.id).path | |
| 156 | + assert_equal parent.slug, parent.path | |
| 157 | + assert_equal "#{parent.path}/#{child.slug}", child.path | |
| 158 | + assert_equal "#{child.path}/#{grandchild.slug}", grandchild.path | |
| 165 | 159 | |
| 166 | 160 | end |
| 167 | 161 | |
| 168 | 162 | should "limit the possibile display colors" do |
| 169 | - c = Category.new(:name => 'test category', :environment_id => @env.id) | |
| 163 | + c = build(Category, :name => 'test category', :environment_id => @env.id) | |
| 170 | 164 | |
| 171 | 165 | c.display_color = 10 |
| 172 | 166 | c.valid? |
| ... | ... | @@ -184,7 +178,7 @@ class CategoryTest < ActiveSupport::TestCase |
| 184 | 178 | should 'avoid duplicated display colors' do |
| 185 | 179 | c1 = fast_create(Category, :name => 'test category', :environment_id => @env.id, :display_color => 1) |
| 186 | 180 | |
| 187 | - c = Category.new(:name => 'lalala', :environment_id => @env.id) | |
| 181 | + c = build(Category, :name => 'lalala', :environment_id => @env.id) | |
| 188 | 182 | c.display_color = 1 |
| 189 | 183 | assert !c.valid? |
| 190 | 184 | assert c.errors[:display_color.to_s].present? |
| ... | ... | @@ -293,12 +287,12 @@ class CategoryTest < ActiveSupport::TestCase |
| 293 | 287 | a1 = person.articles.build(:name => 'art1') |
| 294 | 288 | a1.add_category c |
| 295 | 289 | a1.save! |
| 296 | - c1 = a1.comments.build(:title => 'comm1', :body => 'khdkashd ', :author => person); c1.save! | |
| 290 | + c1 = create(Comment, :article => a1, :title => 'comm1', :body => 'khdkashd ', :author => person) | |
| 297 | 291 | |
| 298 | 292 | a2 = person.articles.build(:name => 'art2') |
| 299 | 293 | a2.add_category c |
| 300 | 294 | a2.save! |
| 301 | - c2 = a2.comments.build(:title => 'comm1', :body => 'khdkashd ', :author => person); c2.save! | |
| 295 | + c2 = create(Comment, :article => a2, :title => 'comm1', :body => 'khdkashd ', :author => person) | |
| 302 | 296 | |
| 303 | 297 | assert_equal [c2, c1], c.recent_comments |
| 304 | 298 | end |
| ... | ... | @@ -307,14 +301,14 @@ class CategoryTest < ActiveSupport::TestCase |
| 307 | 301 | c = @env.categories.build(:name => 'my category'); c.save! |
| 308 | 302 | person = create_user('testuser').person |
| 309 | 303 | |
| 310 | - a1 = person.articles.build(:name => 'art1', :category_ids => [c.id]); a1.save! | |
| 311 | - a2 = person.articles.build(:name => 'art2', :category_ids => [c.id]); a2.save! | |
| 312 | - a3 = person.articles.build(:name => 'art3', :category_ids => [c.id]); a3.save! | |
| 304 | + a1 = create(Article, :profile => person, :name => 'art1', :category_ids => [c.id]) | |
| 305 | + a2 = create(Article, :profile => person, :name => 'art2', :category_ids => [c.id]) | |
| 306 | + a3 = create(Article, :profile => person, :name => 'art3', :category_ids => [c.id]) | |
| 313 | 307 | |
| 314 | - Comment.create(:title => 'test', :body => 'asdsa', :author => person, :source => a1) | |
| 315 | - 5.times { Comment.create(:title => 'test', :body => 'asdsa', :author => person, :source => a2) } | |
| 308 | + create(Comment, :title => 'test', :body => 'asdsa', :author => person, :source => a1) | |
| 309 | + 5.times { create(Comment, :title => 'test', :body => 'asdsa', :author => person, :source => a2) } | |
| 316 | 310 | |
| 317 | - 10.times { Comment.create(:title => 'test', :body => 'kajsdsa', :author => person, :source => a3) } | |
| 311 | + 10.times { create(Comment, :title => 'test', :body => 'kajsdsa', :author => person, :source => a3) } | |
| 318 | 312 | |
| 319 | 313 | assert_equal [a3, a2], c.most_commented_articles(2) |
| 320 | 314 | end |
| ... | ... | @@ -323,13 +317,13 @@ class CategoryTest < ActiveSupport::TestCase |
| 323 | 317 | c = @env.categories.build(:name => 'my category'); c.save! |
| 324 | 318 | person = create_user('testuser').person |
| 325 | 319 | |
| 326 | - a1 = person.articles.build(:name => 'art1', :category_ids => [c.id]); a1.save! | |
| 327 | - a2 = person.articles.build(:name => 'art2', :category_ids => [c.id]); a2.save! | |
| 328 | - a3 = person.articles.build(:name => 'art3', :category_ids => [c.id]); a3.save! | |
| 320 | + a1 = create(Article, :profile => person, :name => 'art1', :category_ids => [c.id]) | |
| 321 | + a2 = create(Article, :profile => person, :name => 'art2', :category_ids => [c.id]) | |
| 322 | + a3 = create(Article, :profile => person, :name => 'art3', :category_ids => [c.id]) | |
| 329 | 323 | |
| 330 | - c1 = a1.comments.build(:title => 'test', :body => 'asdsa', :author => person); c1.save! | |
| 331 | - c2 = a2.comments.build(:title => 'test', :body => 'asdsa', :author => person); c2.save! | |
| 332 | - c3 = a3.comments.build(:title => 'test', :body => 'asdsa', :author => person); c3.save! | |
| 324 | + c1 = create(Comment, :article => a1, :title => 'test', :body => 'asdsa', :author => person) | |
| 325 | + c2 = create(Comment, :article => a2, :title => 'test', :body => 'asdsa', :author => person) | |
| 326 | + c3 = create(Comment, :article => a3, :title => 'test', :body => 'asdsa', :author => person) | |
| 333 | 327 | |
| 334 | 328 | assert_equivalent [c1, c2, c3], c.comments |
| 335 | 329 | end |
| ... | ... | @@ -407,7 +401,7 @@ class CategoryTest < ActiveSupport::TestCase |
| 407 | 401 | |
| 408 | 402 | should 'have image' do |
| 409 | 403 | assert_difference Category, :count do |
| 410 | - c = Category.create!(:name => 'test category1', :environment => Environment.default, :image_builder => { | |
| 404 | + c = create(Category, :name => 'test category1', :environment => Environment.default, :image_builder => { | |
| 411 | 405 | :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') |
| 412 | 406 | }) |
| 413 | 407 | assert_equal c.image(true).filename, 'rails.png' |
| ... | ... | @@ -425,11 +419,11 @@ class CategoryTest < ActiveSupport::TestCase |
| 425 | 419 | end |
| 426 | 420 | |
| 427 | 421 | should 'cache children count' do |
| 428 | - c = Category.create!(:name => 'test', :environment => Environment.default) | |
| 422 | + c = create(Category, :name => 'test', :environment => Environment.default) | |
| 429 | 423 | |
| 430 | 424 | # two children catagories |
| 431 | - c.children.create!(:name => 'test1', :environment => Environment.default) | |
| 432 | - c.children.create!(:name => 'test2', :environment => Environment.default) | |
| 425 | + create(Category, :parent => c, :name => 'test1', :environment => Environment.default) | |
| 426 | + create(Category, :parent => c, :name => 'test2', :environment => Environment.default) | |
| 433 | 427 | |
| 434 | 428 | c.reload |
| 435 | 429 | |
| ... | ... | @@ -442,9 +436,9 @@ class CategoryTest < ActiveSupport::TestCase |
| 442 | 436 | end |
| 443 | 437 | |
| 444 | 438 | should 'get categories by type including nil' do |
| 445 | - category = Category.create!(:name => 'test category', :environment => Environment.default) | |
| 446 | - region = Region.create!(:name => 'test region', :environment => Environment.default) | |
| 447 | - product = ProductCategory.create!(:name => 'test product', :environment => Environment.default) | |
| 439 | + category = create(Category, :name => 'test category', :environment => Environment.default) | |
| 440 | + region = create(Region, :name => 'test region', :environment => Environment.default) | |
| 441 | + product = create(ProductCategory, :name => 'test product', :environment => Environment.default) | |
| 448 | 442 | result = Category.from_types(['ProductCategory', '']).all |
| 449 | 443 | assert_equal 2, result.size |
| 450 | 444 | assert result.include?(product) |
| ... | ... | @@ -452,9 +446,9 @@ class CategoryTest < ActiveSupport::TestCase |
| 452 | 446 | end |
| 453 | 447 | |
| 454 | 448 | should 'get categories by type and not nil' do |
| 455 | - category = Category.create!(:name => 'test category', :environment => Environment.default) | |
| 456 | - region = Region.create!(:name => 'test region', :environment => Environment.default) | |
| 457 | - product = ProductCategory.create!(:name => 'test product', :environment => Environment.default) | |
| 449 | + category = create(Category, :name => 'test category', :environment => Environment.default) | |
| 450 | + region = create(Region, :name => 'test region', :environment => Environment.default) | |
| 451 | + product = create(ProductCategory, :name => 'test product', :environment => Environment.default) | |
| 458 | 452 | result = Category.from_types(['Region', 'ProductCategory']).all |
| 459 | 453 | assert_equal 2, result.size |
| 460 | 454 | assert result.include?(region) |
| ... | ... | @@ -488,7 +482,7 @@ class CategoryTest < ActiveSupport::TestCase |
| 488 | 482 | end |
| 489 | 483 | |
| 490 | 484 | should 'paginate upcoming events' do |
| 491 | - category = Category.create!(:name => 'category1', :environment_id => Environment.default.id) | |
| 485 | + category = create(Category, :name => 'category1', :environment_id => Environment.default.id) | |
| 492 | 486 | profile = fast_create(Profile) |
| 493 | 487 | event1 = category.events.build(:name => 'event1', :start_date => Time.now, :profile => profile) |
| 494 | 488 | event2 = category.events.build(:name => 'event2', :start_date => Time.now + 1.hour, :profile => profile) |
| ... | ... | @@ -498,15 +492,15 @@ class CategoryTest < ActiveSupport::TestCase |
| 498 | 492 | end |
| 499 | 493 | |
| 500 | 494 | should 'remove all article categorizations when destroyed' do |
| 501 | - cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id) | |
| 502 | - art = Article.create!(:name => 'article 1', :profile_id => fast_create(Person).id) | |
| 495 | + cat = create(Category, :name => 'category 1', :environment_id => Environment.default.id) | |
| 496 | + art = create(Article, :name => 'article 1', :profile_id => fast_create(Person).id) | |
| 503 | 497 | art.add_category cat |
| 504 | 498 | cat.destroy |
| 505 | 499 | assert art.categories.reload.empty? |
| 506 | 500 | end |
| 507 | 501 | |
| 508 | 502 | should 'remove all profile categorizations when destroyed' do |
| 509 | - cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id) | |
| 503 | + cat = create(Category, :name => 'category 1', :environment_id => Environment.default.id) | |
| 510 | 504 | p = create(Person, :user_id => fast_create(User).id) |
| 511 | 505 | p.add_category cat |
| 512 | 506 | cat.destroy |
| ... | ... | @@ -538,15 +532,15 @@ class CategoryTest < ActiveSupport::TestCase |
| 538 | 532 | end |
| 539 | 533 | |
| 540 | 534 | should 'list category sub-categories' do |
| 541 | - c1 = Category.create!(:name => 'Category 1', :environment => Environment.default) | |
| 542 | - c2 = Category.create!(:name => 'Category 2', :environment => Environment.default) | |
| 543 | - c3 = Category.create!(:name => 'Category 3', :environment => Environment.default, :parent_id => c1) | |
| 544 | - c4 = Category.create!(:name => 'Category 4', :environment => Environment.default, :parent_id => c1) | |
| 545 | - c5 = Category.create!(:name => 'Category 5', :environment => Environment.default, :parent_id => c3) | |
| 535 | + c1 = create(Category, :name => 'Category 1', :environment => Environment.default) | |
| 536 | + c2 = create(Category, :name => 'Category 2', :environment => Environment.default) | |
| 537 | + c3 = create(Category, :name => 'Category 3', :environment => Environment.default, :parent_id => c1) | |
| 538 | + c4 = create(Category, :name => 'Category 4', :environment => Environment.default, :parent_id => c1) | |
| 539 | + c5 = create(Category, :name => 'Category 5', :environment => Environment.default, :parent_id => c3) | |
| 546 | 540 | |
| 547 | 541 | sub_categories = Category.sub_categories(c1) |
| 548 | 542 | |
| 549 | - assert ActiveRecord::NamedScope::Scope, sub_categories.class | |
| 543 | + assert_equal ActiveRecord::Relation, sub_categories.class | |
| 550 | 544 | assert_not_includes sub_categories, c1 |
| 551 | 545 | assert_not_includes sub_categories, c2 |
| 552 | 546 | assert_includes sub_categories, c3 |
| ... | ... | @@ -555,15 +549,15 @@ class CategoryTest < ActiveSupport::TestCase |
| 555 | 549 | end |
| 556 | 550 | |
| 557 | 551 | should 'list category sub-tree' do |
| 558 | - c1 = Category.create!(:name => 'Category 1', :environment => Environment.default) | |
| 559 | - c2 = Category.create!(:name => 'Category 2', :environment => Environment.default) | |
| 560 | - c3 = Category.create!(:name => 'Category 3', :environment => Environment.default, :parent_id => c1) | |
| 561 | - c4 = Category.create!(:name => 'Category 4', :environment => Environment.default, :parent_id => c1) | |
| 562 | - c5 = Category.create!(:name => 'Category 5', :environment => Environment.default, :parent_id => c3) | |
| 552 | + c1 = create(Category, :name => 'Category 1', :environment => Environment.default) | |
| 553 | + c2 = create(Category, :name => 'Category 2', :environment => Environment.default) | |
| 554 | + c3 = create(Category, :name => 'Category 3', :environment => Environment.default, :parent_id => c1) | |
| 555 | + c4 = create(Category, :name => 'Category 4', :environment => Environment.default, :parent_id => c1) | |
| 556 | + c5 = create(Category, :name => 'Category 5', :environment => Environment.default, :parent_id => c3) | |
| 563 | 557 | |
| 564 | 558 | sub_tree = Category.sub_tree(c1) |
| 565 | 559 | |
| 566 | - assert ActiveRecord::NamedScope::Scope, sub_tree.class | |
| 560 | + assert_equal ActiveRecord::Relation, sub_tree.class | |
| 567 | 561 | assert_includes sub_tree, c1 |
| 568 | 562 | assert_not_includes sub_tree, c2 |
| 569 | 563 | assert_includes sub_tree, c3 | ... | ... |