picky 1.2.4 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/lib/picky/adapters/rack/base.rb +23 -0
  2. data/lib/picky/adapters/rack/live_parameters.rb +33 -0
  3. data/lib/picky/adapters/rack/query.rb +59 -0
  4. data/lib/picky/adapters/rack.rb +28 -0
  5. data/lib/picky/alias_instances.rb +2 -0
  6. data/lib/picky/application.rb +9 -8
  7. data/lib/picky/cli.rb +25 -3
  8. data/lib/picky/frontend_adapters/rack.rb +150 -0
  9. data/lib/picky/helpers/measuring.rb +0 -2
  10. data/lib/picky/index_api.rb +1 -1
  11. data/lib/picky/indexed/categories.rb +51 -14
  12. data/lib/picky/indexers/solr.rb +1 -5
  13. data/lib/picky/indexing/indexes.rb +6 -0
  14. data/lib/picky/interfaces/live_parameters.rb +165 -0
  15. data/lib/picky/loader.rb +13 -2
  16. data/lib/picky/query/base.rb +15 -18
  17. data/lib/picky/query/combination.rb +2 -2
  18. data/lib/picky/query/solr.rb +0 -17
  19. data/lib/picky/query/token.rb +14 -27
  20. data/lib/picky/query/weights.rb +13 -1
  21. data/lib/picky/results/base.rb +9 -2
  22. data/spec/lib/adapters/rack/base_spec.rb +24 -0
  23. data/spec/lib/adapters/rack/live_parameters_spec.rb +21 -0
  24. data/spec/lib/adapters/rack/query_spec.rb +33 -0
  25. data/spec/lib/application_spec.rb +27 -8
  26. data/spec/lib/cli_spec.rb +9 -0
  27. data/spec/lib/extensions/symbol_spec.rb +1 -3
  28. data/spec/lib/{routing_spec.rb → frontend_adapters/rack_spec.rb} +69 -66
  29. data/spec/lib/indexed/categories_spec.rb +24 -0
  30. data/spec/lib/interfaces/live_parameters_spec.rb +138 -0
  31. data/spec/lib/query/base_spec.rb +10 -14
  32. data/spec/lib/query/live_spec.rb +1 -30
  33. data/spec/lib/query/token_spec.rb +72 -5
  34. data/spec/lib/query/weights_spec.rb +59 -36
  35. data/spec/lib/results/base_spec.rb +13 -1
  36. metadata +20 -7
  37. data/lib/picky/routing.rb +0 -171
@@ -2,10 +2,11 @@
2
2
  #
3
3
  require 'spec_helper'
4
4
 
5
- describe Routing do
5
+ describe FrontendAdapters::Rack do
6
6
 
7
7
  before(:each) do
8
- @routing = Routing.new
8
+ @rack_adapter = FrontendAdapters::Rack.new
9
+ @rack_adapter.stub! :exclaim
9
10
  end
10
11
 
11
12
  def rack_defaults_for url
@@ -38,64 +39,64 @@ describe Routing do
38
39
  context 'empty?' do
39
40
  context 'no routes' do
40
41
  before(:each) do
41
- @routing.reset_routes
42
+ @rack_adapter.reset_routes
42
43
  end
43
44
  it 'returns the right answer' do
44
- @routing.empty?.should == true
45
+ @rack_adapter.empty?.should == true
45
46
  end
46
47
  end
47
48
  context 'with routes' do
48
49
  before(:each) do
49
- @routing.route %r{something} => :some_query
50
+ @rack_adapter.route %r{something} => Query::Full.new
50
51
  end
51
52
  it 'returns the right answer' do
52
- @routing.empty?.should == false
53
+ @rack_adapter.empty?.should == false
53
54
  end
54
55
  end
55
56
  end
56
57
 
57
58
  context 'real routes' do
58
59
  before(:each) do
59
- @routing.reset_routes
60
+ @rack_adapter.reset_routes
60
61
  PickyLog.stub! :log
61
62
  end
62
63
  it 'should route correctly' do
63
64
  env = {}
64
65
 
65
- @routing.routes.freeze
66
- @routing.call(env).should == [404, {"Content-Type" => "text/html", "X-Cascade" => "pass"}, ["Not Found"]]
66
+ @rack_adapter.routes.freeze
67
+ @rack_adapter.call(env).should == [404, {"Content-Type" => "text/html", "X-Cascade" => "pass"}, ["Not Found"]]
67
68
  end
68
69
  it 'should route correctly' do
69
70
  env = rack_defaults_for '/'
70
71
 
71
- @routing.root 200
72
+ @rack_adapter.root 200
72
73
 
73
- @routing.routes.freeze
74
- @routing.call(env).should == [200, {"Content-Type"=>"text/html", "Content-Length"=>"0"}, ['']]
74
+ @rack_adapter.routes.freeze
75
+ @rack_adapter.call(env).should == [200, {"Content-Type"=>"text/html", "Content-Length"=>"0"}, ['']]
75
76
  end
76
77
  it 'should route correctly' do
77
78
  env = rack_defaults_for '/blarf'
78
79
 
79
- @routing.default 200
80
+ @rack_adapter.default 200
80
81
 
81
- @routing.routes.freeze
82
- @routing.call(env).should == [200, {"Content-Type"=>"text/html", "Content-Length"=>"0"}, ['']]
82
+ @rack_adapter.routes.freeze
83
+ @rack_adapter.call(env).should == [200, {"Content-Type"=>"text/html", "Content-Length"=>"0"}, ['']]
83
84
  end
84
85
  it 'should route correctly' do
85
86
  env = rack_defaults_for '/gurk'
86
87
 
87
- @routing.answer %r{/gurk}, lambda { |env| [333, {}, ['this is gurk']] }
88
+ @rack_adapter.answer %r{/gurk}, lambda { |env| [333, {}, ['this is gurk']] }
88
89
 
89
- @routing.routes.freeze
90
- @routing.call(env).should == [333, {}, ['this is gurk']]
90
+ @rack_adapter.routes.freeze
91
+ @rack_adapter.call(env).should == [333, {}, ['this is gurk']]
91
92
  end
92
93
  it 'should route correctly' do
93
94
  env = rack_defaults_for '/gurk'
94
95
 
95
- @routing.answer '/gurk', lambda { |env| [333, {}, ['this is gurk']] }
96
+ @rack_adapter.answer '/gurk', lambda { |env| [333, {}, ['this is gurk']] }
96
97
 
97
- @routing.routes.freeze
98
- @routing.call(env).should == [333, {}, ['this is gurk']]
98
+ @rack_adapter.routes.freeze
99
+ @rack_adapter.call(env).should == [333, {}, ['this is gurk']]
99
100
  end
100
101
  it 'should route correctly' do
101
102
  env = rack_defaults_for '/searches/some_route?query=some_query'
@@ -104,10 +105,10 @@ describe Routing do
104
105
  full.should_receive(:search_with_text).once.with(anything, 0).and_return(Results::Full.new)
105
106
  Query::Full.stub! :new => full
106
107
 
107
- @routing.route '/searches/some_route' => Query::Full.new(:some_index, :some_other_index)
108
+ @rack_adapter.route '/searches/some_route' => Query::Full.new(:some_index, :some_other_index)
108
109
 
109
- @routing.routes.freeze
110
- @routing.call(env).should == [200, {"Content-Type"=>"application/octet-stream", "Content-Length"=>"52"}, ["{\"allocations\":[],\"offset\":0,\"duration\":0,\"total\":0}"]]
110
+ @rack_adapter.routes.freeze
111
+ @rack_adapter.call(env).should == [200, {"Content-Type"=>"application/json", "Content-Length"=>"52"}, ["{\"allocations\":[],\"offset\":0,\"duration\":0,\"total\":0}"]]
111
112
  end
112
113
  it 'should route correctly' do
113
114
  env = rack_defaults_for '/searches/some_route?query=some_query&type=some_type'
@@ -116,10 +117,10 @@ describe Routing do
116
117
  full.should_receive(:search_with_text).once.with(anything, 0).and_return(Results::Full.new)
117
118
  Query::Full.stub! :new => full
118
119
 
119
- @routing.route '/searches/some_route' => Query::Full.new(:some_index, :some_other_index), :query => { :type => :some_type }
120
+ @rack_adapter.route '/searches/some_route' => Query::Full.new(:some_index, :some_other_index), :query => { :type => :some_type }
120
121
 
121
- @routing.routes.freeze
122
- @routing.call(env).should == [200, {"Content-Type"=>"application/octet-stream", "Content-Length"=>"52"}, ["{\"allocations\":[],\"offset\":0,\"duration\":0,\"total\":0}"]]
122
+ @rack_adapter.routes.freeze
123
+ @rack_adapter.call(env).should == [200, {"Content-Type"=>"application/json", "Content-Length"=>"52"}, ["{\"allocations\":[],\"offset\":0,\"duration\":0,\"total\":0}"]]
123
124
  end
124
125
  it 'should route correctly' do
125
126
  env = rack_defaults_for '/searches/some_wrong_route?query=some_query'
@@ -128,36 +129,31 @@ describe Routing do
128
129
  full.should_receive(:search_with_text).never
129
130
  Query::Full.stub! :new => full
130
131
 
131
- @routing.route '/searches/some_route' => Query::Full.new(:some_index, :some_other_index)
132
+ @rack_adapter.route '/searches/some_route' => Query::Full.new(:some_index, :some_other_index)
132
133
 
133
- @routing.routes.freeze
134
- @routing.call(env).should == [404, {"Content-Type"=>"text/html", "X-Cascade"=>"pass"}, ["Not Found"]]
134
+ @rack_adapter.routes.freeze
135
+ @rack_adapter.call(env).should == [404, {"Content-Type"=>"text/html", "X-Cascade"=>"pass"}, ["Not Found"]]
135
136
  end
136
137
  end
137
138
 
138
139
  context 'stubbed routes' do
139
140
  before(:each) do
140
141
  @routes = stub :routes
141
- @routing.stub! :routes => @routes
142
- end
143
- describe 'call' do
144
- it 'should description' do
145
-
146
- end
142
+ @rack_adapter.stub! :routes => @routes
147
143
  end
148
144
 
149
145
  describe 'generate_query_string' do
150
146
  it 'should not allow an empty query condition' do
151
147
  lambda {
152
- @routing.generate_query_string({}).should == 'this=must_be'
148
+ @rack_adapter.generate_query_string({}).should == 'this=must_be'
153
149
  }.should raise_error "At least one query string condition is needed."
154
150
  end
155
151
  it 'should handle a single condition' do
156
- @routing.generate_query_string(:this => :must_be).should == 'this=must_be'
152
+ @rack_adapter.generate_query_string(:this => :must_be).should == 'this=must_be'
157
153
  end
158
154
  it 'should not allow multiple query strings' do
159
155
  lambda {
160
- @routing.generate_query_string(:this => :must_be, :that => :should_be).should == 'this=must_be|that=should_be'
156
+ @rack_adapter.generate_query_string(:this => :must_be, :that => :should_be).should == 'this=must_be|that=should_be'
161
157
  }.should raise_error "Too many query param conditions (only 1 allowed): {:this=>:must_be, :that=>:should_be}"
162
158
  end
163
159
  it 'should be sanity checked' do
@@ -167,62 +163,69 @@ describe Routing do
167
163
 
168
164
  describe "route" do
169
165
  it "should delegate correctly" do
170
- @routing.should_receive(:route_one).once.with %r{regexp1}, :query1, {}
171
- @routing.should_receive(:route_one).once.with %r{regexp2}, :query2, {}
166
+ @rack_adapter.should_receive(:route_one).once.with %r{regexp1}, :query1, {}
167
+ @rack_adapter.should_receive(:route_one).once.with %r{regexp2}, :query2, {}
172
168
 
173
- @routing.route %r{regexp1} => :query1, %r{regexp2} => :query2
169
+ @rack_adapter.route %r{regexp1} => :query1, %r{regexp2} => :query2
174
170
  end
175
171
  it "should split options correctly" do
176
- @routing.should_receive(:route_one).once.with %r{regexp1}, :query1, :some => :option
177
- @routing.should_receive(:route_one).once.with %r{regexp2}, :query2, :some => :option
172
+ @rack_adapter.should_receive(:route_one).once.with %r{regexp1}, :query1, :some => :option
173
+ @rack_adapter.should_receive(:route_one).once.with %r{regexp2}, :query2, :some => :option
178
174
 
179
- @routing.route %r{regexp1} => :query1, %r{regexp2} => :query2, :some => :option
175
+ @rack_adapter.route %r{regexp1} => :query1, %r{regexp2} => :query2, :some => :option
180
176
  end
181
177
  it 'does not accept nil queries' do
182
- lambda { @routing.route %r{some/regexp} => nil }.should raise_error(Routing::TargetQueryNilError, /Routing for \/some\\\/regexp\/ was defined with a nil query object/)
178
+ lambda { @rack_adapter.route %r{some/regexp} => nil }.should raise_error(FrontendAdapters::Rack::RouteTargetNilError, /Routing for \/some\\\/regexp\/ was defined with a nil target object, i.e. \/some\\\/regexp\/ => nil./)
179
+ end
180
+ end
181
+
182
+ describe 'finalize' do
183
+ before(:each) do
184
+ @rack_adapter.routes.should_receive(:freeze).once.with
185
+
186
+ @rack_adapter.finalize
183
187
  end
184
188
  end
185
189
 
186
190
  describe 'route_one' do
187
191
  before(:each) do
188
- @some_query_app = stub :some_query_app
189
- @routing.stub! :generate_app => @some_query_app
192
+ Adapters::Rack.stub! :app_for => :some_query_app
190
193
  end
191
194
  it 'should add the right route' do
192
- @routes.should_receive(:add_route).once.with @some_query_app, { :request_method => "GET", :path_info => /some_url/ }
195
+ @routes.should_receive(:add_route).once.with :some_query_app, { :request_method => "GET", :path_info => /some_url/ }
193
196
 
194
- @routing.route_one %r{some_url}, :some_query, {}
197
+ @rack_adapter.route_one %r{some_url}, :some_query, {}
195
198
  end
196
199
  it 'should add the right route' do
197
- @routes.should_receive(:add_route).once.with @some_query_app, { :request_method => "GET", :path_info => /some_url/ }
200
+ @routes.should_receive(:add_route).once.with :some_query_app, { :request_method => "GET", :path_info => /some_url/ }
198
201
 
199
- @routing.route_one 'some_url', :some_query, {}
202
+ @rack_adapter.route_one 'some_url', :some_query, {}
200
203
  end
201
204
  it 'should add the right route' do
202
- @routes.should_receive(:add_route).once.with @some_query_app, { :request_method => "GET", :glarf => :blarf, :path_info => /some_url/ }
205
+ @routes.should_receive(:add_route).once.with :some_query_app, { :request_method => "GET", :glarf => :blarf, :path_info => /some_url/ }
203
206
 
204
- @routing.route_one 'some_url', :some_query, { :glarf => :blarf }
207
+ @rack_adapter.route_one 'some_url', :some_query, { :glarf => :blarf }
205
208
  end
206
209
  end
207
210
 
208
211
  describe 'default' do
209
212
  it 'should call answer' do
210
- @routing.should_receive(:answer).once.with nil, Routing::STATUSES[200]
213
+ @rack_adapter.should_receive(:answer).once.with nil, FrontendAdapters::Rack::STATUSES[200]
211
214
 
212
- @routing.default 200
215
+ @rack_adapter.default 200
213
216
  end
214
217
  end
215
218
 
216
219
  describe 'root' do
217
220
  it 'should call answer' do
218
- @routing.should_receive(:answer).once.with %r{^/$}, Routing::STATUSES[200]
221
+ @rack_adapter.should_receive(:answer).once.with %r{^/$}, FrontendAdapters::Rack::STATUSES[200]
219
222
 
220
- @routing.root 200
223
+ @rack_adapter.root 200
221
224
  end
222
225
  it 'should call answer' do
223
- @routing.should_receive(:answer).once.with %r{^/$}, Routing::STATUSES[404]
226
+ @rack_adapter.should_receive(:answer).once.with %r{^/$}, FrontendAdapters::Rack::STATUSES[404]
224
227
 
225
- @routing.root 404
228
+ @rack_adapter.root 404
226
229
  end
227
230
  end
228
231
 
@@ -235,30 +238,30 @@ describe Routing do
235
238
  it 'should use the app with default_options from the url' do
236
239
  @routes.should_receive(:add_route).once.with @app, { :request_method => "GET", :path_info => /some_url/ }
237
240
 
238
- @routing.answer 'some_url', @app
241
+ @rack_adapter.answer 'some_url', @app
239
242
  end
240
243
  end
241
244
  context 'without url' do
242
245
  it 'should use the app with default_options' do
243
246
  @routes.should_receive(:add_route).once.with @app, { :request_method => "GET" }
244
247
 
245
- @routing.answer nil, @app
248
+ @rack_adapter.answer nil, @app
246
249
  end
247
250
  end
248
251
  end
249
252
  context 'without app' do
250
253
  context 'with url' do
251
254
  it 'should use the 404 with default_options from the url' do
252
- @routes.should_receive(:add_route).once.with Routing::STATUSES[200], { :request_method => "GET", :path_info => /some_url/ }
255
+ @routes.should_receive(:add_route).once.with FrontendAdapters::Rack::STATUSES[200], { :request_method => "GET", :path_info => /some_url/ }
253
256
 
254
- @routing.answer 'some_url'
257
+ @rack_adapter.answer 'some_url'
255
258
  end
256
259
  end
257
260
  context 'without url' do
258
261
  it 'should use the 404 with default_options' do
259
- @routes.should_receive(:add_route).once.with Routing::STATUSES[200], { :request_method => "GET" }
262
+ @routes.should_receive(:add_route).once.with FrontendAdapters::Rack::STATUSES[200], { :request_method => "GET" }
260
263
 
261
- @routing.answer
264
+ @rack_adapter.answer
262
265
  end
263
266
  end
264
267
  end
@@ -39,6 +39,30 @@ describe Indexed::Categories do
39
39
  end
40
40
  end
41
41
 
42
+ describe 'clear' do
43
+ before(:each) do
44
+ @categories = Indexed::Categories.new
45
+ end
46
+ it 'is clear right at the beginning' do
47
+ @categories.categories.should be_empty
48
+ @categories.category_hash.should be_empty
49
+ end
50
+ it "isn't clear anymore after adding" do
51
+ @categories << stub(:category, :name => :some_name)
52
+
53
+ @categories.categories.should_not be_empty
54
+ @categories.category_hash.should_not be_empty
55
+ end
56
+ it "is clear again after clearing" do
57
+ @categories << stub(:category, :name => :some_name)
58
+
59
+ @categories.clear
60
+
61
+ @categories.categories.should be_empty
62
+ @categories.category_hash.should be_empty
63
+ end
64
+ end
65
+
42
66
  context 'without options' do
43
67
  before(:each) do
44
68
  @index1 = stub :index1, :name => :some_index
@@ -0,0 +1,138 @@
1
+ # encoding: utf-8
2
+ #
3
+ require 'spec_helper'
4
+
5
+ describe LiveParameters do
6
+
7
+ before(:each) do
8
+ @parent = stub :parent
9
+ @child = stub :child
10
+ IO.stub! :pipe => [@child, @parent]
11
+ @parameters = LiveParameters.new
12
+ @parameters.stub! :exclaim
13
+ end
14
+
15
+ describe Interfaces::LiveParameters::CouldNotUpdateConfigurationError do
16
+ before(:each) do
17
+ @error = Interfaces::LiveParameters::CouldNotUpdateConfigurationError.new :some_key, 'some message'
18
+ end
19
+ end
20
+
21
+ describe 'parameters' do
22
+ context 'all goes well' do
23
+ it 'does a few things in order' do
24
+ @parameters.should_receive(:close_child).once.with().ordered
25
+ @parameters.should_receive(:try_updating_configuration_with).once.with(:a => :b).ordered
26
+ @parameters.should_receive(:write_parent).once.with(:a => :b).ordered
27
+ @parameters.should_receive(:extract_configuration).once.with().ordered
28
+
29
+ @parameters.parameters :a => :b
30
+ end
31
+ end
32
+ context 'updating failed' do
33
+ before(:each) do
34
+ @parameters.should_receive(:try_updating_configuration_with).and_raise Interfaces::LiveParameters::CouldNotUpdateConfigurationError.new(:a, 'hello')
35
+ end
36
+ it 'kills itself and returns' do
37
+ @parameters.should_receive(:close_child).once.ordered
38
+ @parameters.should_receive(:harakiri).once.ordered
39
+
40
+ @parameters.parameters( :a => :b ).should == { :a => :ERROR }
41
+ end
42
+ end
43
+ end
44
+
45
+ describe 'harakiri' do
46
+ before(:each) do
47
+ Process.stub! :pid => :some_pid
48
+ end
49
+ it 'kills itself' do
50
+ Process.should_receive(:kill).once.with :QUIT, :some_pid
51
+
52
+ @parameters.harakiri
53
+ end
54
+ end
55
+
56
+ describe 'write_parent' do
57
+ before(:each) do
58
+ Process.stub! :pid => :some_pid
59
+ end
60
+ it 'calls the parent' do
61
+ @parent.should_receive(:write).once.with "[:some_pid, {:a=>:b}];;;"
62
+
63
+ @parameters.write_parent :a => :b
64
+ end
65
+ end
66
+
67
+ describe 'close_child' do
68
+ context 'child is closed' do
69
+ before(:each) do
70
+ @child.stub! :closed? => true
71
+ end
72
+ it 'does not receive close' do
73
+ @child.should_receive(:close).never
74
+
75
+ @parameters.close_child
76
+ end
77
+ end
78
+ context 'child not yet closed' do
79
+ before(:each) do
80
+ @child.stub! :closed? => false
81
+ end
82
+ it 'does receives close' do
83
+ @child.should_receive(:close).once.with()
84
+
85
+ @parameters.close_child
86
+ end
87
+ end
88
+ end
89
+
90
+ describe 'kill_worker' do
91
+ context 'all goes well' do
92
+ it 'uses Process.kill' do
93
+ Process.should_receive(:kill).once.with :some_signal, :some_pid
94
+
95
+ @parameters.kill_worker :some_signal, :some_pid
96
+ end
97
+ end
98
+ context 'there is no such process' do
99
+ it 'uses Process.kill' do
100
+ Process.should_receive(:kill).and_raise Errno::ESRCH.new
101
+
102
+ @parameters.should_receive(:remove_worker).once.with :some_pid
103
+
104
+ @parameters.kill_worker :some_signal, :some_pid
105
+ end
106
+ end
107
+ end
108
+
109
+ describe 'kill_each_worker_except' do
110
+ context 'worker pid in the worker pids' do
111
+ before(:each) do
112
+ @parameters.stub! :worker_pids => [1,2,3,4]
113
+ end
114
+ it 'kills each except the one' do
115
+ @parameters.should_receive(:kill_worker).once.with(:KILL, 1)
116
+ @parameters.should_receive(:kill_worker).once.with(:KILL, 2)
117
+
118
+ @parameters.should_receive(:kill_worker).once.with(:KILL, 4)
119
+
120
+ @parameters.kill_each_worker_except 3
121
+ end
122
+ end
123
+ context 'worker pid not in the worker pids (unrealistic, but...)' do
124
+ before(:each) do
125
+ @parameters.stub! :worker_pids => [1,2,3,4]
126
+ end
127
+ it 'kills each except the one' do
128
+ @parameters.should_receive(:kill_worker).once.with(:KILL, 1)
129
+ @parameters.should_receive(:kill_worker).once.with(:KILL, 2)
130
+ @parameters.should_receive(:kill_worker).once.with(:KILL, 3)
131
+ @parameters.should_receive(:kill_worker).once.with(:KILL, 4)
132
+
133
+ @parameters.kill_each_worker_except 5
134
+ end
135
+ end
136
+ end
137
+
138
+ end
@@ -123,26 +123,22 @@ describe 'Query::Base' do
123
123
  end
124
124
  end
125
125
  end
126
-
127
- describe "results_from" do
128
- describe 'Full' do
126
+
127
+ describe 'to_s' do
128
+ context 'with weights' do
129
129
  before(:each) do
130
- @query = Query::Full.new @index
130
+ @query = Query::Full.new @index, weights: :some_weights
131
131
  end
132
- it "should work" do
133
- allocations = stub :allocations, :process! => true
134
-
135
- @query.results_from allocations
132
+ it 'works correctly' do
133
+ @query.to_s.should == 'Query::Full, weights: some_weights'
136
134
  end
137
135
  end
138
- describe 'Live' do
136
+ context 'without weights' do
139
137
  before(:each) do
140
- @query = Query::Live.new @index
138
+ @query = Query::Full.new @index
141
139
  end
142
- it "should work" do
143
- allocations = stub :allocations, :process! => true
144
-
145
- @query.results_from allocations
140
+ it 'works correctly' do
141
+ @query.to_s.should == 'Query::Full'
146
142
  end
147
143
  end
148
144
  end
@@ -21,40 +21,11 @@ describe Query::Live do
21
21
  @query = Query::Live.new @index
22
22
  end
23
23
  it "should get allocations" do
24
- @query.should_receive(:results_from).and_return stub(:results, :prepare! => true)
24
+ @query.result_type.should_receive :from
25
25
  @query.should_receive(:sorted_allocations).once
26
26
 
27
27
  @query.execute 'some_query', 0
28
28
  end
29
- it "should generate a max amount of results from allocations" do
30
- allocations = stub :allocations
31
- @query.should_receive(:sorted_allocations).and_return allocations
32
-
33
- @query.should_receive(:results_from).once.with(0, allocations).and_return stub(:results, :prepare! => true)
34
-
35
- @query.execute 'some query', 0
36
- end
37
- end
38
-
39
- describe "results_from" do
40
- describe "with empty ids" do
41
- before(:each) do
42
- @query = Query::Live.new @index
43
- @allocations = stub :allocations, :total => 0, :ids => [], :to_result => :some_results, :process! => nil
44
- end
45
- it "should generate a result" do
46
- @query.results_from(@allocations).should be_kind_of Results::Live
47
- end
48
- it "should generate a result with 0 result count" do
49
- @query.results_from(@allocations).total.should == 0
50
- end
51
- it "should generate a result with 0 duration" do
52
- @query.results_from(@allocations).duration.should == 0
53
- end
54
- it "should generate a result with the allocations" do
55
- @query.results_from(0, @allocations).allocations.should == @allocations
56
- end
57
- end
58
29
  end
59
30
 
60
31
  end
@@ -8,6 +8,73 @@ describe Query::Token do
8
8
  Query::Qualifiers.instance.prepare
9
9
  end
10
10
 
11
+ describe 'next_similar_token' do
12
+ before(:each) do
13
+ @bundle = stub :bundle, :similar => [:array, :of, :similar]
14
+ @category = stub :category, :bundle_for => @bundle
15
+
16
+ @token = Query::Token.processed 'similar~'
17
+ end
18
+ it 'returns the right next tokens' do
19
+ next_token = @token.next_similar_token @category
20
+ next_token.text.should == :array
21
+ next_token = next_token.next_similar_token @category
22
+ next_token.text.should == :of
23
+ next_token = next_token.next_similar_token @category
24
+ next_token.text.should == :similar
25
+ next_token = next_token.next_similar_token @category
26
+ next_token.should == nil
27
+ end
28
+ end
29
+
30
+ describe 'next_similar' do
31
+ before(:each) do
32
+ @bundle = stub :bundle
33
+ end
34
+ context 'similar' do
35
+ context 'with stub' do
36
+ before(:each) do
37
+ @bundle.stub! :similar => [:array, :of, :similar]
38
+
39
+ @token = Query::Token.processed 'similar~'
40
+ end
41
+ it 'generates all similar' do
42
+ @token.next_similar(@bundle).should == :array
43
+ @token.next_similar(@bundle).should == :of
44
+ @token.next_similar(@bundle).should == :similar
45
+ @token.next_similar(@bundle).should == nil
46
+ end
47
+ it 'should have a certain text' do
48
+ @token.next_similar @bundle
49
+ @token.next_similar @bundle
50
+ @token.next_similar @bundle
51
+ @token.next_similar @bundle
52
+
53
+ @token.text.should == :similar
54
+ end
55
+ end
56
+ end
57
+ context 'non-similar' do
58
+ context 'with stub' do
59
+ before(:each) do
60
+ @bundle.stub! :similar => [:array, :of, :similar]
61
+
62
+ @token = Query::Token.processed 'nonsimilar'
63
+ end
64
+ it 'generates all similar' do
65
+ @token.next_similar(@bundle).should == nil
66
+ end
67
+ # TODO
68
+ #
69
+ # it 'should have a certain text' do
70
+ # @token.next_similar @bundle
71
+ #
72
+ # @token.text.should == :nonsimilar
73
+ # end
74
+ end
75
+ end
76
+ end
77
+
11
78
  describe "generate_similarity_for" do
12
79
  before(:each) do
13
80
  @bundle = stub :bundle
@@ -18,16 +85,16 @@ describe Query::Token do
18
85
  before(:each) do
19
86
  @bundle.stub! :similar => [:array, :of, :similar]
20
87
  end
21
- it "returns an enumerator" do
22
- @token.generate_similarity_for(@bundle).to_a.size.should == 3
88
+ it "returns an array of the right size" do
89
+ @token.generate_similarity_for(@bundle).size.should == 3
23
90
  end
24
91
  end
25
92
  context "without similar" do
26
93
  before(:each) do
27
- @bundle.stub! :similar => nil
94
+ @bundle.stub! :similar => []
28
95
  end
29
- it "returns an enumerator with 0 entries" do
30
- @token.generate_similarity_for(@bundle).to_a.size.should == 0
96
+ it "returns an array of the right size" do
97
+ @token.generate_similarity_for(@bundle).size.should == 0
31
98
  end
32
99
  end
33
100
  end