Commit 2ec6c0872e30a30c1d3a9e8d785a42b239f9fa69

Authored by JoenioCosta
1 parent b2fad82f

ActionItem243: updating search controller tests

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1605 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/search_controller.rb
@@ -16,29 +16,18 @@ class SearchController < ApplicationController @@ -16,29 +16,18 @@ class SearchController < ApplicationController
16 16
17 ###################################################### 17 ######################################################
18 18
19 - class Finder  
20 - attr_reader :environment  
21 - def initialize(env)  
22 - @environment = env  
23 - end  
24 -  
25 - def articles  
26 - environment.articles  
27 - end  
28 -  
29 - def comments  
30 - environment.comments  
31 - end  
32 - end  
33 -  
34 def index 19 def index
35 @query = params[:query] || '' 20 @query = params[:query] || ''
36 @filtered_query = remove_stop_words(@query) 21 @filtered_query = remove_stop_words(@query)
37 22
38 - @finder ||= SearchController::Finder.new(@environment) 23 + @finder ||= @environment
39 24
40 - @results = { :articles => search(@finder.articles, @query),  
41 - :comments => search(@finder.comments, @query) } 25 + @results = {
  26 + :articles => search(@finder.articles, @filtered_query),
  27 + :comments => search(@finder.comments, @filtered_query),
  28 + :enterprises => search(@finder.enterprises, @filtered_query),
  29 + :people => search(@finder.people, @filtered_query),
  30 + }
42 end 31 end
43 32
44 before_filter :load_category, :only => :filter 33 before_filter :load_category, :only => :filter
app/models/category.rb
@@ -26,7 +26,8 @@ class Category < ActiveRecord::Base @@ -26,7 +26,8 @@ class Category < ActiveRecord::Base
26 has_many :comments, :through => :articles 26 has_many :comments, :through => :articles
27 27
28 has_many :profile_categorizations 28 has_many :profile_categorizations
29 - has_many :profiles, :through => :profile_categorizations 29 + has_many :enterprises, :through => :profile_categorizations, :source => :profile
  30 + has_many :people, :through => :profile_categorizations, :source => :profile
30 31
31 def recent_articles(limit = 10) 32 def recent_articles(limit = 10)
32 self.articles.recent(limit) 33 self.articles.recent(limit)
test/functional/search_controller_test.rb
@@ -19,6 +19,13 @@ class SearchControllerTest < Test::Unit::TestCase @@ -19,6 +19,13 @@ class SearchControllerTest < Test::Unit::TestCase
19 assert_equal 'carne vaca', assigns('filtered_query') 19 assert_equal 'carne vaca', assigns('filtered_query')
20 end 20 end
21 21
  22 + should 'search with filtered query' do
  23 + @controller.expects(:locale).returns('pt_BR').at_least_once
  24 + @controller.expects(:search).with(anything, 'carne vaca').at_least_once
  25 + @controller.expects(:search).with(anything, 'a carne da vaca').never
  26 + get 'index', :query => 'a carne da vaca'
  27 + end
  28 +
22 should 'search only in specified types of content' do 29 should 'search only in specified types of content' do
23 get :index, :query => 'something not important', :find_in => [ 'articles' ] 30 get :index, :query => 'something not important', :find_in => [ 'articles' ]
24 assert_equal [:articles], assigns(:results).keys 31 assert_equal [:articles], assigns(:results).keys
@@ -94,24 +101,9 @@ class SearchControllerTest < Test::Unit::TestCase @@ -94,24 +101,9 @@ class SearchControllerTest < Test::Unit::TestCase
94 assert_not_includes assigns(:results)[:comments], comment2 101 assert_not_includes assigns(:results)[:comments], comment2
95 end 102 end
96 103
97 -  
98 - should 'find in environment' do  
99 - env = mock  
100 - finder = SearchController::Finder.new(env)  
101 - assert_same env, finder.environment  
102 - end  
103 -  
104 - should 'delegate to environment in default finder' do  
105 - env = mock  
106 - articles = mock  
107 - finder = SearchController::Finder.new(env)  
108 - env.expects(:articles).returns(articles)  
109 - assert_same articles, finder.articles  
110 - end  
111 -  
112 should 'find enterprises' do 104 should 'find enterprises' do
113 ent = Enterprise.create!(:name => 'teste', :identifier => 'teste') 105 ent = Enterprise.create!(:name => 'teste', :identifier => 'teste')
114 - get 'index', :query => 'teste' 106 + get 'index', :query => 'teste', :find_in => [ 'enterprises' ]
115 assert_includes assigns(:results)[:enterprises], ent 107 assert_includes assigns(:results)[:enterprises], ent
116 end 108 end
117 109
@@ -119,22 +111,34 @@ class SearchControllerTest < Test::Unit::TestCase @@ -119,22 +111,34 @@ class SearchControllerTest < Test::Unit::TestCase
119 category = Category.create!(:name => 'my category', :environment => Environment.default) 111 category = Category.create!(:name => 'my category', :environment => Environment.default)
120 112
121 # in category 113 # in category
122 - ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test1', :categories => [category]) 114 + ent1 = Enterprise.create!(:name => 'testing enterprise 1', :identifier => 'test1', :categories => [category])
123 115
124 # not in category 116 # not in category
125 - ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test1') 117 + ent2 = Enterprise.create!(:name => 'testing enterprise 2', :identifier => 'test2')
126 118
127 - get :filter, :category_path => [ 'my-category' ], :query => 'test', :find_in => [ 'enterprises' ] 119 + get :filter, :category_path => [ 'my-category' ], :query => 'testing', :find_in => [ 'enterprises' ]
128 120
129 - assert_includes ent1, assigns(:results)[:enterprises]  
130 - assert_not_includes ent2, assigns(:results)[:enterprises] 121 + assert_includes assigns(:results)[:enterprises], ent1
  122 + assert_not_includes assigns(:results)[:enterprises], ent2
131 end 123 end
132 124
133 # 'assets' menu 125 # 'assets' menu
134 should 'list enterprises in a specified category' 126 should 'list enterprises in a specified category'
135 127
136 - should 'find people'  
137 - should 'find people in a specific category' 128 + should 'find people' do
  129 + p1 = create_user('people_1').person; p1.name = 'a beatiful person'; p1.save!
  130 + get :index, :query => 'beatiful', :find_in => [ 'people' ]
  131 + assert_includes assigns(:results)[:people], p1
  132 + end
  133 +
  134 + should 'find people in a specific category' do
  135 + c = Category.create!(:name => 'my category', :environment => Environment.default)
  136 + p1 = create_user('people_1').person; p1.name = 'a beatiful person'; p1.categories << c; p1.save!
  137 + p2 = create_user('people_2').person; p2.name = 'another beatiful person'; p2.save!
  138 + get :filter, :category_path => [ 'my-category' ], :query => 'beatiful', :find_in => [ 'people' ]
  139 + assert_includes assigns(:results)[:people], p1
  140 + assert_not_includes assigns(:results)[:people], p2
  141 + end
138 142
139 # 'assets' menu 143 # 'assets' menu
140 should 'list people in a specified category' 144 should 'list people in a specified category'
test/unit/category_test.rb
@@ -276,14 +276,23 @@ class CategoryTest &lt; Test::Unit::TestCase @@ -276,14 +276,23 @@ class CategoryTest &lt; Test::Unit::TestCase
276 assert_equivalent [c1, c2, c3], c.comments 276 assert_equivalent [c1, c2, c3], c.comments
277 end 277 end
278 278
279 - should 'have profiles' do 279 + should 'have enterprises' do
280 c = @env.categories.build(:name => 'my category'); c.save! 280 c = @env.categories.build(:name => 'my category'); c.save!
281 - person1 = create_user('testuser_one').person  
282 - person1.categories << c  
283 - person2 = create_user('testuser_two').person  
284 - person2.categories << c  
285 - assert_includes c.profiles, person1  
286 - assert_includes c.profiles, person2 281 + ent1 = Enterprise.create!(:identifier => 'enterprise_1', :name => 'Enterprise one')
  282 + ent1.categories << c
  283 + ent2 = Enterprise.create!(:identifier => 'enterprise_2', :name => 'Enterprise one')
  284 + ent2.categories << c
  285 + assert_includes c.enterprises, ent1
  286 + assert_includes c.enterprises, ent2
  287 + end
  288 +
  289 + should 'have people' do
  290 + c = @env.categories.build(:name => 'my category'); c.save!
  291 + p1 = create_user('testuser_1').person
  292 + p1.categories << c
  293 + p2 = create_user('testuser_2').person
  294 + p2.categories << c
  295 + assert_equal [p1, p2], c.people
287 end 296 end
288 297
289 end 298 end