Commit dc22707ba1b2f198f7e65b3aa1d057eafd4c75ff
1 parent
71559296
Exists in
master
and in
22 other branches
ActionItem501: fixed bugs that prevented tests to pass
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2141 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
10 changed files
with
50 additions
and
22 deletions
Show diff stats
app/models/category_finder.rb
| @@ -20,7 +20,7 @@ class CategoryFinder | @@ -20,7 +20,7 @@ class CategoryFinder | ||
| 20 | asset_class(asset).paginate(:all, options_for_find(asset_class(asset), {:order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options))) | 20 | asset_class(asset).paginate(:all, options_for_find(asset_class(asset), {:order => "created_at desc, #{asset_table(asset)}.id desc"}.merge(options))) |
| 21 | else | 21 | else |
| 22 | ferret_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} | 22 | ferret_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)} |
| 23 | - asset_class(asset).find_by_contents(query, ferret_options, options_for_find(asset_class(asset), options)).uniq | 23 | + asset_class(asset).find_by_contents(query, ferret_options, options_for_find(asset_class(asset), options)) |
| 24 | end | 24 | end |
| 25 | end | 25 | end |
| 26 | 26 | ||
| @@ -63,7 +63,7 @@ class CategoryFinder | @@ -63,7 +63,7 @@ class CategoryFinder | ||
| 63 | 63 | ||
| 64 | case klass.name | 64 | case klass.name |
| 65 | when 'Comment' | 65 | when 'Comment' |
| 66 | - {:select => 'distinct comments.*', :joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options) | 66 | + {:joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options) |
| 67 | when 'Product' | 67 | when 'Product' |
| 68 | if prod_cat_ids | 68 | if prod_cat_ids |
| 69 | {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id = (?) and products.product_category_id in (?)', category_id, prod_cat_ids]}.merge!(options) | 69 | {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id = (?) and products.product_category_id in (?)', category_id, prod_cat_ids]}.merge!(options) |
app/models/enterprise.rb
| @@ -9,7 +9,7 @@ class Enterprise < Organization | @@ -9,7 +9,7 @@ class Enterprise < Organization | ||
| 9 | extra_data_for_index :product_categories | 9 | extra_data_for_index :product_categories |
| 10 | 10 | ||
| 11 | def product_categories | 11 | def product_categories |
| 12 | - products.map{|p| p.category_full_name} | 12 | + products.map{|p| p.category_full_name}.compact |
| 13 | end | 13 | end |
| 14 | 14 | ||
| 15 | def product_updated | 15 | def product_updated |
app/models/product.rb
| @@ -26,7 +26,7 @@ class Product < ActiveRecord::Base | @@ -26,7 +26,7 @@ class Product < ActiveRecord::Base | ||
| 26 | acts_as_mappable | 26 | acts_as_mappable |
| 27 | 27 | ||
| 28 | def category_full_name | 28 | def category_full_name |
| 29 | - product_category.full_name.split('/') | 29 | + product_category ? product_category.full_name.split('/') : nil |
| 30 | end | 30 | end |
| 31 | 31 | ||
| 32 | acts_as_having_image | 32 | acts_as_having_image |
app/models/profile_categorization.rb
| @@ -4,7 +4,7 @@ class ProfileCategorization < ActiveRecord::Base | @@ -4,7 +4,7 @@ class ProfileCategorization < ActiveRecord::Base | ||
| 4 | belongs_to :category | 4 | belongs_to :category |
| 5 | 5 | ||
| 6 | def self.add_category_to_profile(category, profile) | 6 | def self.add_category_to_profile(category, profile) |
| 7 | - | 7 | + return if self.find_by_category_id_and_profile_id(category.id, profile.id) |
| 8 | connection.execute("insert into categories_profiles (category_id, profile_id) values(#{category.id}, #{profile.id})") | 8 | connection.execute("insert into categories_profiles (category_id, profile_id) values(#{category.id}, #{profile.id})") |
| 9 | 9 | ||
| 10 | c = category.parent | 10 | c = category.parent |
app/views/search/_product_categories_menu.rhtml
| @@ -3,7 +3,11 @@ | @@ -3,7 +3,11 @@ | ||
| 3 | <div id="product-categories-menu"> | 3 | <div id="product-categories-menu"> |
| 4 | <ul> | 4 | <ul> |
| 5 | <% if product_categories_menu.empty? %> | 5 | <% if product_categories_menu.empty? %> |
| 6 | - <li class="cat-empty"> <%= _('There is no sub-categories for %s.') % @product_category.name %> </li> | 6 | + <% if @product_category %> |
| 7 | + <li class="cat-empty"> <%= _('There is no sub-categories for %s.') % @product_category.name %> </li> | ||
| 8 | + <% else %> | ||
| 9 | + <li class="cat-empty"> <%= _('There is no categories.') %> </li> | ||
| 10 | + <% end %> | ||
| 7 | <% end %> | 11 | <% end %> |
| 8 | <% product_categories_menu.each do |cat, hits, childs| %> | 12 | <% product_categories_menu.each do |cat, hits, childs| %> |
| 9 | <li class="cat-parent" onmouseover="prodCatMenuOver(this)" onmouseout="prodCatMenuOut(this)"> | 13 | <li class="cat-parent" onmouseover="prodCatMenuOver(this)" onmouseout="prodCatMenuOut(this)"> |
test/functional/search_controller_test.rb
| @@ -244,11 +244,11 @@ class SearchControllerTest < Test::Unit::TestCase | @@ -244,11 +244,11 @@ class SearchControllerTest < Test::Unit::TestCase | ||
| 244 | end | 244 | end |
| 245 | 245 | ||
| 246 | should 'find products in a specific category' do | 246 | should 'find products in a specific category' do |
| 247 | - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1'); ent1.add_category @category | 247 | + ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1', :category_ids => [@category.id]) |
| 248 | ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') | 248 | ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') |
| 249 | prod1 = ent1.products.create!(:name => 'a beautiful product') | 249 | prod1 = ent1.products.create!(:name => 'a beautiful product') |
| 250 | prod2 = ent2.products.create!(:name => 'another beautiful product') | 250 | prod2 = ent2.products.create!(:name => 'another beautiful product') |
| 251 | - get :index, :category_path => ['my-category'], :query => 'beautiful', :find_in => ['products'] | 251 | + get :index, :category_path => @category.path.split('/'), :query => 'beautiful', :find_in => ['products'] |
| 252 | assert_includes assigns(:results)[:products], prod1 | 252 | assert_includes assigns(:results)[:products], prod1 |
| 253 | assert_not_includes assigns(:results)[:products], prod2 | 253 | assert_not_includes assigns(:results)[:products], prod2 |
| 254 | end | 254 | end |
test/unit/category_finder_test.rb
| @@ -167,6 +167,8 @@ class CategoryFinderTest < ActiveSupport::TestCase | @@ -167,6 +167,8 @@ class CategoryFinderTest < ActiveSupport::TestCase | ||
| 167 | p1 = @finder.find('enterprises', 'teste', :per_page => 1, :page => 1) | 167 | p1 = @finder.find('enterprises', 'teste', :per_page => 1, :page => 1) |
| 168 | p2 = @finder.find('enterprises', 'teste', :per_page => 1, :page => 2) | 168 | p2 = @finder.find('enterprises', 'teste', :per_page => 1, :page => 2) |
| 169 | 169 | ||
| 170 | + assert_respond_to p1, :total_entries | ||
| 171 | + assert_respond_to p2, :total_entries | ||
| 170 | assert (p1 == [ent1] && p2 == [ent2]) || (p1 == [ent2] && p2 == [ent1]) # consistent paging | 172 | assert (p1 == [ent1] && p2 == [ent2]) || (p1 == [ent2] && p2 == [ent1]) # consistent paging |
| 171 | end | 173 | end |
| 172 | 174 | ||
| @@ -209,25 +211,23 @@ class CategoryFinderTest < ActiveSupport::TestCase | @@ -209,25 +211,23 @@ class CategoryFinderTest < ActiveSupport::TestCase | ||
| 209 | end | 211 | end |
| 210 | 212 | ||
| 211 | should 'count enterprises with query and options' do | 213 | should 'count enterprises with query and options' do |
| 212 | - options = mock | ||
| 213 | results = mock | 214 | results = mock |
| 214 | 215 | ||
| 215 | - @finder.expects(:find).with('people', 'my query', options).returns(results) | 216 | + @finder.expects(:find).with('people', 'my query', kind_of(Hash)).returns(results) |
| 216 | 217 | ||
| 217 | results.expects(:total_hits).returns(99) | 218 | results.expects(:total_hits).returns(99) |
| 218 | 219 | ||
| 219 | - assert_equal 99, @finder.count('people', 'my query', options) | 220 | + assert_equal 99, @finder.count('people', 'my query', {}) |
| 220 | end | 221 | end |
| 221 | 222 | ||
| 222 | should 'count enterprises without query but with options' do | 223 | should 'count enterprises without query but with options' do |
| 223 | - options = mock | ||
| 224 | results = mock | 224 | results = mock |
| 225 | 225 | ||
| 226 | - @finder.expects(:find).with('people', nil, options).returns(results) | 226 | + @finder.expects(:find).with('people', nil, kind_of(Hash)).returns(results) |
| 227 | 227 | ||
| 228 | - results.expects(:size).returns(99) | 228 | + results.expects(:total_entries).returns(99) |
| 229 | 229 | ||
| 230 | - assert_equal 99, @finder.count('people', nil, options) | 230 | + assert_equal 99, @finder.count('people', nil, {}) |
| 231 | end | 231 | end |
| 232 | 232 | ||
| 233 | should 'not list more people than limit' do | 233 | should 'not list more people than limit' do |
| @@ -252,13 +252,14 @@ class CategoryFinderTest < ActiveSupport::TestCase | @@ -252,13 +252,14 @@ class CategoryFinderTest < ActiveSupport::TestCase | ||
| 252 | should 'not return the same result twice' do | 252 | should 'not return the same result twice' do |
| 253 | parent = Category.create!(:name => 'parent category', :environment => Environment.default) | 253 | parent = Category.create!(:name => 'parent category', :environment => Environment.default) |
| 254 | child = Category.create!(:name => 'child category', :environment => Environment.default, :parent => parent) | 254 | child = Category.create!(:name => 'child category', :environment => Environment.default, :parent => parent) |
| 255 | - p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.add_category(child); p1.save! | ||
| 256 | - p1.add_category(parent); p1.save! | 255 | + p1 = create_user('people_1').person |
| 256 | + p1.name = 'a beautiful person' | ||
| 257 | + p1.category_ids = [child.id, parent.id]; p1.save! | ||
| 257 | 258 | ||
| 258 | f = CategoryFinder.new(parent) | 259 | f = CategoryFinder.new(parent) |
| 259 | result = f.find(:people, 'beautiful') | 260 | result = f.find(:people, 'beautiful') |
| 260 | 261 | ||
| 261 | - assert_equivalent [p1], result | 262 | + assert_equal [p1], result |
| 262 | assert_equal 1, result.size | 263 | assert_equal 1, result.size |
| 263 | end | 264 | end |
| 264 | 265 |
test/unit/enterprise_test.rb
| @@ -173,4 +173,22 @@ class EnterpriseTest < Test::Unit::TestCase | @@ -173,4 +173,22 @@ class EnterpriseTest < Test::Unit::TestCase | ||
| 173 | end | 173 | end |
| 174 | end | 174 | end |
| 175 | 175 | ||
| 176 | + should 'list product categories full name' do | ||
| 177 | + full_name = mock | ||
| 178 | + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') | ||
| 179 | + p = ent.products.create!(:name => 'test prod') | ||
| 180 | + p.expects(:category_full_name).returns(full_name) | ||
| 181 | + | ||
| 182 | + assert_equal [full_name], ent.product_categories | ||
| 183 | + end | ||
| 184 | + | ||
| 185 | + should 'not return nil values when have uncategorized products' do | ||
| 186 | + full_name = mock | ||
| 187 | + ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') | ||
| 188 | + p1 = ent.products.create!(:name => 'test prod 1') | ||
| 189 | + p1.expects(:category_full_name).returns(full_name) | ||
| 190 | + p2 = ent.products.create!(:name => 'test prod 2') | ||
| 191 | + | ||
| 192 | + assert_equal [full_name], ent.product_categories | ||
| 193 | + end | ||
| 176 | end | 194 | end |
test/unit/environment_finder_test.rb
| @@ -116,14 +116,13 @@ class EnvironmentFinderTest < ActiveSupport::TestCase | @@ -116,14 +116,13 @@ class EnvironmentFinderTest < ActiveSupport::TestCase | ||
| 116 | should 'count enterprises with query and options' do | 116 | should 'count enterprises with query and options' do |
| 117 | env = Environment.default | 117 | env = Environment.default |
| 118 | finder = EnvironmentFinder.new(env) | 118 | finder = EnvironmentFinder.new(env) |
| 119 | - options = mock | ||
| 120 | results = mock | 119 | results = mock |
| 121 | 120 | ||
| 122 | - finder.expects(:find).with('people', 'my query', options).returns(results) | 121 | + finder.expects(:find).with('people', 'my query', kind_of(Hash)).returns(results) |
| 123 | 122 | ||
| 124 | results.expects(:total_hits).returns(99) | 123 | results.expects(:total_hits).returns(99) |
| 125 | 124 | ||
| 126 | - assert_equal 99, finder.count('people', 'my query', options) | 125 | + assert_equal 99, finder.count('people', 'my query', {}) |
| 127 | end | 126 | end |
| 128 | 127 | ||
| 129 | should 'find articles by initial' do | 128 | should 'find articles by initial' do |
test/unit/product_test.rb
| @@ -72,10 +72,16 @@ class ProductTest < Test::Unit::TestCase | @@ -72,10 +72,16 @@ class ProductTest < Test::Unit::TestCase | ||
| 72 | cat.expects(:full_name).returns('A/B/C') | 72 | cat.expects(:full_name).returns('A/B/C') |
| 73 | 73 | ||
| 74 | p = Product.new | 74 | p = Product.new |
| 75 | - p.expects(:product_category).returns(cat) | 75 | + p.stubs(:product_category).returns(cat) |
| 76 | assert_equal ['A','B','C'], p.category_full_name | 76 | assert_equal ['A','B','C'], p.category_full_name |
| 77 | end | 77 | end |
| 78 | 78 | ||
| 79 | + should 'return a nil cateory full name when not categorized' do | ||
| 80 | + p = Product.new | ||
| 81 | + p.stubs(:product_category).returns(nil) | ||
| 82 | + assert_equal nil, p.category_full_name | ||
| 83 | + end | ||
| 84 | + | ||
| 79 | should 'be indexed by category full name' do | 85 | should 'be indexed by category full name' do |
| 80 | p = Product.new(:name => 'a test product') | 86 | p = Product.new(:name => 'a test product') |
| 81 | p.expects(:category_full_name).returns('interesting category') | 87 | p.expects(:category_full_name).returns('interesting category') |