search_controller_test.rb
5.13 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require File.dirname(__FILE__) + '/../test_helper'
require 'search_controller'
# Re-raise errors caught by the controller.
class SearchController; def rescue_action(e) raise e end; end
class SearchControllerTest < Test::Unit::TestCase
def setup
@controller = SearchController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
should 'filter stop words' do
@controller.expects(:locale).returns('pt_BR').at_least_once
get 'index', :query => 'a carne da vaca'
assert_response :success
assert_template 'index'
assert_equal 'carne vaca', assigns('filtered_query')
end
should 'search only in specified types of content' do
get :index, :query => 'something not important', :find_in => [ 'articles' ]
assert_equal [:articles], assigns(:results).keys
end
should 'search in more than one specified types of content' do
get :index, :query => 'something not important', :find_in => [ 'articles', 'comments' ]
assert_equivalent [:articles, :comments ], assigns(:results).keys
end
should 'render success in search' do
get :index, :query => 'something not important'
assert_response :success
end
should 'search for articles' do
person = create_user('teste').person
art = person.articles.build(:name => 'an article to be found'); art.save!
get 'index', :query => 'article found', :find_in => [ 'articles' ]
assert_includes assigns(:results)[:articles], art
end
should 'search for articles in a specific category' do
person = create_user('teste').person
category = Category.create!(:name => 'my category', :environment => Environment.default)
# in category
art1 = person.articles.build(:name => 'an article to be found')
art1.categories << category
art1.save!
# not in category
art2 = person.articles.build(:name => 'another article to be found')
art2.save!
get :filter, :category_path => [ 'my-category' ], :query => 'article found', :find_in => [ 'articles' ]
assert_includes assigns(:results)[:articles], art1
assert_not_includes assigns(:results)[:articles], art2
end
# 'assets' menu
should 'list articles in a specific category'
should 'search in comments' do
person = create_user('teste').person
art = person.articles.build(:name => 'an article to be found'); art.save!
comment = art.comments.build(:title => 'comment to be found', :body => 'hfyfyh', :author => person); comment.save!
get 'index', :query => 'found', :find_in => [ 'comments' ]
assert_includes assigns(:results)[:comments], comment
end
should 'search in comments in a specific category' do
person = create_user('teste').person
category = Category.create!(:name => 'my category', :environment => Environment.default)
# in category
art1 = person.articles.build(:name => 'an article to be found')
art1.categories << category
art1.save!
comment1 = art1.comments.build(:title => 'comment to be found', :body => 'hfyfyh', :author => person); comment1.save!
# not in category
art2 = person.articles.build(:name => 'another article to be found')
art2.save!
comment2 = art2.comments.build(:title => 'comment to be found', :body => 'hfyfyh', :author => person); comment2.save!
get 'filter', :category_path => ['my-category'], :query => 'found', :find_in => [ 'comments' ]
assert_includes assigns(:results)[:comments], comment1
assert_not_includes assigns(:results)[:comments], comment2
end
should 'find in environment' do
env = mock
finder = SearchController::Finder.new(env)
assert_same env, finder.environment
end
should 'delegate to environment in default finder' do
env = mock
articles = mock
finder = SearchController::Finder.new(env)
env.expects(:articles).returns(articles)
assert_same articles, finder.articles
end
should 'find enterprises' do
ent = Enterprise.create!(:name => 'teste', :identifier => 'teste')
get 'index', :query => 'teste'
assert_includes assigns(:results)[:enterprises], ent
end
should 'find enterprises in a specified category' do
category = Category.create!(:name => 'my category', :environment => Environment.default)
# in category
ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test1', :categories => [category])
# not in category
ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test1')
get :filter, :category_path => [ 'my-category' ], :query => 'test', :find_in => [ 'enterprises' ]
assert_includes ent1, assigns(:results)[:enterprises]
assert_not_includes ent2, assigns(:results)[:enterprises]
end
# 'assets' menu
should 'list enterprises in a specified category'
should 'find people'
should 'find people in a specific category'
# 'assets' menu
should 'list people in a specified category'
should 'find communities'
should 'find communities in a specified category'
# 'assets' menu
should 'list communities in a specified category'
should 'find products'
should 'find products in a specific category'
# 'assets' menu
should 'list products in a specific category'
end