Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 185 lines (146 sloc) 5.204 kB
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
1 require 'helper'
2
3 class Note
4 include MongoMapper::Document
5
6 plugin Hunt
7
c3f7ff7 @jnunemaker Ensuring that search always returns scope and works fine chained on a…
authored
8 scope :by_user, lambda { |user| where(:user_id => user.id) }
9
10 key :title, String
11 key :body, String
12 key :tags, Array
13 key :user_id, ObjectId
14
15 belongs_to :user
16 end
17
18 class User
19 include MongoMapper::Document
20 many :notes
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
21 end
22
23 describe Hunt do
24 it "adds searches key to model to store search terms" do
25 Note.searches(:title)
26 Note.new.should respond_to(:searches)
27 Note.new.should respond_to(:searches=)
28 end
29
82f0229 @jnunemaker Added search scope.
authored
30 describe ".search" do
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
31 before(:each) do
32 Note.searches(:title)
33 end
34
c3f7ff7 @jnunemaker Ensuring that search always returns scope and works fine chained on a…
authored
35 it "returns query that matches nothing if nil" do
36 Note.create(:title => 'Mongo')
37 Note.search(nil).count.should == 0
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
38 end
39
c3f7ff7 @jnunemaker Ensuring that search always returns scope and works fine chained on a…
authored
40 it "returns query that matches nothing if blank" do
41 Note.create(:title => 'Mongo')
42 Note.search('').count.should == 0
43 end
44
45 context "chained on scope" do
46 before(:each) do
47 @user = User.create
48 @note = Note.create(:title => 'Mongo', :user_id => @user.id)
49 end
50
51 it "works" do
52 Note.by_user(@user).search('Mongo').all.should == [@note]
53 end
54 end
55
56 context "chained on association" do
57 before(:each) do
58 @user = User.create
59 @note = Note.create(:title => 'Mongo', :user_id => @user.id)
60 end
61
62 it "works" do
63 @user.notes.search('Mongo').all.should == [@note]
64 @user.notes.search('Frank').all.should == []
65 end
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
66 end
67
82f0229 @jnunemaker Added search scope.
authored
68 context "with one search term" do
69 before(:each) do
c3f7ff7 @jnunemaker Ensuring that search always returns scope and works fine chained on a…
authored
70 @note = Note.create(:title => 'MongoDB is awesome!')
82f0229 @jnunemaker Added search scope.
authored
71 @result = Note.search('mongodb')
72 end
73
74 let(:note) { @note }
75 let(:result) { @result }
76
77 it "returns plucky query" do
78 result.should be_instance_of(Plucky::Query)
79 end
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
80
82f0229 @jnunemaker Added search scope.
authored
81 it "scopes query to searches.default in stemmed terms" do
82 result['searches.default'].should == {'$in' => %w(mongodb)}
83 end
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
84
82f0229 @jnunemaker Added search scope.
authored
85 it "does return matched documents" do
86 result.all.should include(note)
87 end
88
89 it "does not query unmatched documents" do
90 not_found = Note.create(:title => 'Something different')
91 result.all.should_not include(not_found)
92 end
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
93 end
94
82f0229 @jnunemaker Added search scope.
authored
95 context "with multiple search terms" do
96 before(:each) do
c3f7ff7 @jnunemaker Ensuring that search always returns scope and works fine chained on a…
authored
97 @note = Note.create(:title => 'MongoDB is awesome!')
82f0229 @jnunemaker Added search scope.
authored
98 @result = Note.search('mongodb is awesome')
99 end
100
101 let(:note) { @note }
102 let(:result) { @result }
103
104 it "returns plucky query" do
105 result.should be_instance_of(Plucky::Query)
106 end
107
108 it "scopes query to searches.default in stemmed terms" do
109 result['searches.default'].should == {'$in' => Hunt::Util.to_stemmed_words(note.concatted_search_values)}
110 end
111
131e995 @jnunemaker Added test to ensure that it was basically an any match.
authored
112 it "returns documents that match both terms" do
82f0229 @jnunemaker Added search scope.
authored
113 result.all.should include(note)
114 end
115
131e995 @jnunemaker Added test to ensure that it was basically an any match.
authored
116 it "returns documents that match any of the terms" do
117 awesome = Note.create(:title => 'Something awesome')
118 mongodb = Note.create(:title => 'Something MongoDB')
119 result.all.should include(awesome)
120 result.all.should include(mongodb)
121 end
122
82f0229 @jnunemaker Added search scope.
authored
123 it "does not query unmatched documents" do
124 not_found = Note.create(:title => 'Something different')
125 result.all.should_not include(not_found)
126 end
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
127 end
128 end
129
82f0229 @jnunemaker Added search scope.
authored
130 context "Search indexing" do
131 context "on one field" do
132 before(:each) do
133 Note.searches(:title)
134 @note = Note.create(:title => 'Woot for MongoDB!')
135 end
136
137 let(:note) { @note }
138
139 it "indexes terms on create" do
140 note.searches['default'].should == Hunt::Util.to_stemmed_words(note.concatted_search_values)
141 end
142
143 it "indexes terms on update" do
144 note.update_attributes(:title => 'Another woot')
145 note.searches['default'].should == Hunt::Util.to_stemmed_words(note.concatted_search_values)
146 end
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
147 end
148
82f0229 @jnunemaker Added search scope.
authored
149 context "on multiple fields" do
150 before(:each) do
151 Note.searches(:title, :body)
152 @note = Note.create(:title => 'Woot for MongoDB!', :body => 'This is my body.')
153 end
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
154
82f0229 @jnunemaker Added search scope.
authored
155 let(:note) { @note }
156
157 it "indexes merged terms on create" do
158 note.searches['default'].should == Hunt::Util.to_stemmed_words(note.concatted_search_values)
159 end
160
161 it "indexes merged terms on update" do
162 note.update_attributes(:title => 'Another woot', :body => 'An updated body.')
163 note.searches['default'].should == Hunt::Util.to_stemmed_words(note.concatted_search_values)
164 end
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
165 end
166
82f0229 @jnunemaker Added search scope.
authored
167 context "on multiple fields one of which is array key" do
168 before(:each) do
169 Note.searches(:title, :tags)
170 @note = Note.create(:title => 'Woot for MongoDB!', :tags => %w(mongo nosql))
171 end
172
173 let(:note) { @note }
174
175 it "indexes merged terms on create" do
176 note.searches['default'].should == Hunt::Util.to_stemmed_words(note.concatted_search_values)
177 end
178
179 it "indexes merged terms on update" do
180 note.update_attributes(:title => 'Another woot', :tags => %w(mongo))
181 note.searches['default'].should == Hunt::Util.to_stemmed_words(note.concatted_search_values)
182 end
173efd0 @jnunemaker First commit. Basic indexing of stemmed terms in.
authored
183 end
184 end
185 end
Something went wrong with that request. Please try again.