To edit pages or tickets please login with username/password: aaf/aaf

root/trunk/demo/test/unit/comment_test.rb

Revision 350, 7.8 kB (checked in by jk, 4 months ago)

fixing #220

Line 
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class CommentTest < Test::Unit::TestCase
4   fixtures :comments
5
6   def setup
7     Comment.rebuild_index
8   end
9
10   # Replace this with your real tests.
11   def test_truth
12     assert_kind_of Comment, comments(:first)
13   end
14  
15   def test_issue_220_index_false_as_false
16     c = Comment.new :content => false
17     assert_equal false, c.content
18     assert_equal 'false', c.content_for_field_name(:content)
19     assert_equal 'false', c.to_doc[:content]
20     c.save
21     assert_equal c, Comment.find_with_ferret('content:false').first
22   end
23
24   def test_if_option
25     c = Comment.new :content => 'do not index'
26     c.save
27     assert_equal 0, Comment.find_with_ferret('do not index').total_hits
28   end
29
30   def test_analyzer
31     c = Comment.create! :content => 'a fax modem'
32     assert_equal 0, Comment.find_with_ferret('fax').size
33     assert_equal 1, Comment.find_with_ferret('modem').size
34   end
35
36   def test_class_index_dir
37     assert Comment.aaf_configuration[:index_dir] =~ %r{^#{RAILS_ROOT}/index/test/comment}
38   end
39
40   def test_aliased_field
41     res = Comment.find_with_ferret 'aliased:regarding'
42     assert_equal 1, res.total_hits
43     assert_equal comments(:comment_for_c2), res.first
44   end
45
46   def test_search_for_id
47     # don't search the id field by default:
48     assert Comment.find_with_ferret('3').empty?
49     # explicit query for id field works:
50     assert_equal 3, Comment.find_with_ferret('id:3').first.id
51   end
52
53   #def test_reloadable
54   #  assert ! Comment.reloadable?
55   #end
56
57   # tests the automatic building of an index when none exists
58   # delete index/test/* before running rake to make this useful
59   def test_automatic_index_build
60     # TODO: check why this fails, but querying for 'comment fixture' works.
61     # maybe different analyzers at index creation and searching time ?
62     #comments_from_ferret = Comment.find_with_ferret('"comment from fixture"')
63     comments_from_ferret = Comment.find_with_ferret('comment fixture')
64     assert_equal 2, comments_from_ferret.size
65     assert comments_from_ferret.include?(comments(:first))
66     assert comments_from_ferret.include?(comments(:another))
67   end
68
69   def test_rebuild_index
70     Comment.aaf_index.ferret_index.query_delete('comment')
71     comments_from_ferret = Comment.find_with_ferret('comment AND fixture')
72     assert comments_from_ferret.empty?
73     Comment.rebuild_index
74     comments_from_ferret = Comment.find_with_ferret('comment AND fixture')
75     assert_equal 2, comments_from_ferret.size
76   end
77
78   def test_total_hits
79     comments_from_ferret = Comment.find_with_ferret('comment AND fixture', :limit => 1)
80     assert_equal 1, comments_from_ferret.size
81     assert_equal 2, comments_from_ferret.total_hits
82
83     comments_from_ferret = Comment.find_with_ferret('comment AND fixture', {}, :conditions => 'id != 1')
84     assert_equal 1, comments_from_ferret.size
85     assert_equal 1, comments_from_ferret.total_hits
86   end
87
88   def test_score
89     comments_from_ferret = Comment.find_with_ferret('comment AND fixture', :limit => 1)
90     assert comments_from_ferret.first
91     assert comments_from_ferret.first.ferret_score > 0
92   end
93
94   def test_find_all
95     20.times do |i|
96       Comment.create( :author => 'multi-commenter', :content => "This is multicomment no #{i}" )
97     end
98     assert_equal 10, (res = Comment.find_with_ferret('multicomment')).size
99     assert_equal 20, res.total_hits
100     assert_equal 15, (res = Comment.find_with_ferret('multicomment', :limit => 15)).size
101     assert_equal 20, res.total_hits
102     assert_equal 20, (res = Comment.find_with_ferret('multicomment', :limit => :all)).size
103     assert_equal 20, res.total_hits
104   end
105
106   # tests the custom to_doc method defined in comment.rb
107   def test_custom_to_doc
108     top_docs = Comment.aaf_index.ferret_index.search('"from fixture"')
109     #top_docs = Comment.ferret_index.search('"comment from fixture"')
110     assert_equal 2, top_docs.total_hits
111     doc = Comment.aaf_index.ferret_index.doc(top_docs.hits[0].doc)
112     # check for the special field added by the custom to_doc method
113     assert_not_nil doc[:added]
114     # still a valid int ?
115     assert doc[:added].to_i > 0
116   end
117
118   def test_find_with_ferret
119     comment = Comment.create( :author => 'john doe', :content => 'This is a useless comment' )
120     comment2 = Comment.create( :author => 'another', :content => 'content' )
121
122     comments_from_ferret = Comment.find_with_ferret('anoth* OR jo*')
123     assert_equal 2, comments_from_ferret.size
124     assert comments_from_ferret.include?(comment)
125     assert comments_from_ferret.include?(comment2)
126    
127     # find options
128     comments_from_ferret = Comment.find_with_ferret('anoth* OR jo*', {}, :conditions => ["id=?",comment2.id])
129     assert_equal 1, comments_from_ferret.size
130     assert comments_from_ferret.include?(comment2)
131    
132     comments_from_ferret = Comment.find_with_ferret('lorem ipsum not here')
133     assert comments_from_ferret.empty?
134
135     comments_from_ferret = Comment.find_with_ferret('another')
136     assert_equal 1, comments_from_ferret.size
137     assert_equal comment2.id, comments_from_ferret.first.id
138    
139     comments_from_ferret = Comment.find_with_ferret('doe')
140     assert_equal 1, comments_from_ferret.size
141     assert_equal comment.id, comments_from_ferret.first.id
142    
143     comments_from_ferret = Comment.find_with_ferret('useless')
144     assert_equal 1, comments_from_ferret.size
145     assert_equal comment.id, comments_from_ferret.first.id
146  
147     # no monkeys here
148     comments_from_ferret = Comment.find_with_ferret('monkey')
149     assert comments_from_ferret.empty?
150    
151     # multiple terms are ANDed by default...
152     comments_from_ferret = Comment.find_with_ferret('monkey comment')
153     assert comments_from_ferret.empty?
154     # ...unless you connect them by OR
155     comments_from_ferret = Comment.find_with_ferret('monkey OR comment')
156     assert_equal 3, comments_from_ferret.size
157     assert comments_from_ferret.include?(comment)
158     assert comments_from_ferret.include?(comments(:first))
159     assert comments_from_ferret.include?(comments(:another))
160
161     # multiple terms, each term has to occur in a document to be found,
162     # but they may occur in different fields
163     comments_from_ferret = Comment.find_with_ferret('useless john')
164     assert_equal 1, comments_from_ferret.size
165     assert_equal comment.id, comments_from_ferret.first.id
166    
167
168     # search for an exact string by enclosing it in "
169     comments_from_ferret = Comment.find_with_ferret('"useless john"')
170     assert comments_from_ferret.empty?
171     comments_from_ferret = Comment.find_with_ferret('"useless comment"')
172     assert_equal 1, comments_from_ferret.size
173     assert_equal comment.id, comments_from_ferret.first.id
174
175     comment.destroy
176     comment2.destroy
177   end
178
179   # fixed with Ferret 0.9.6
180   def test_stopwords_ferret_bug
181     i = Ferret::I.new(:or_default => false, :default_field => '*' )
182     d = Ferret::Document.new
183     d[:id] = '1'
184     d[:content] = 'Move or shake'
185     i << d
186     hits = i.search 'move AND or AND shake'
187     assert_equal 1, hits.total_hits
188     hits = i.search 'move AND nothere AND shake'
189     assert_equal 0, hits.total_hits
190     hits = i.search 'move AND shake'
191     assert_equal 1, hits.total_hits
192     hits = i.search 'move OR shake'
193     assert_equal 1, hits.total_hits
194     hits = i.search '+move +the +shake'
195     assert_equal 1, hits.total_hits
196
197     hits = i.search 'move nothere'
198     assert_equal 0, hits.total_hits
199   end
200
201   def test_stopwords
202     comment = Comment.create( :author => 'john doe', :content => 'Move or shake' )
203     ['move shake', 'Move shake', 'move Shake', 'move or shake', 'move the shake'].each do |q|
204       comments_from_ferret = Comment.find_with_ferret(q)
205       assert_equal comment, comments_from_ferret.first, "query #{q} failed"
206     end
207     comment.destroy
208   end
209
210   def test_array_conditions_combining
211     comments_from_ferret = Comment.find_with_ferret('comment AND fixture', {}, :conditions => [ 'id IN (?)', [ 2, 3 ] ])
212     assert_equal 1, comments_from_ferret.size
213     assert_equal 1, comments_from_ferret.total_hits
214   end
215
216
217 end
Note: See TracBrowser for help on using the browser.

To edit pages or tickets please login with username/password: aaf/aaf