lucid_works 0.6.10 → 0.6.11

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,4 +8,6 @@ group :development do
8
8
  gem 'rspec'
9
9
  gem 'autotest'
10
10
  gem "autotest-fsevent"
11
+ gem 'timecop'
12
+ gem 'tzinfo'
11
13
  end
@@ -2,6 +2,7 @@ module LucidWorks
2
2
 
3
3
  class Collection < Base
4
4
 
5
+ belongs_to :server
5
6
  has_many :datasources, :fields, :activities
6
7
  has_one :info, :settings
7
8
  has_one :index, :has_content => false
@@ -23,21 +24,21 @@ module LucidWorks
23
24
  build_index.destroy(:params => {:key => 'iaccepttherisk'})
24
25
  end
25
26
 
26
- # Setup the Collection with an RSolr object that it can use to search.
27
- # Must be provided with the URL of a Solr instance (excluding the /solr/... path)
28
- def rsolr_connect(solr_url, default_search_params={})
29
- @default_search_params = default_search_params
30
- @path_prefix = URI.parse(solr_url).path
31
- @rsolr ||= RSolr.connect :url => solr_url.dup
27
+ def rsolr
28
+ unless @rsolr
29
+ server_uri = self.server.host
30
+ @path_prefix = URI.parse(server_uri).path # The API key
31
+ @rsolr = RSolr.connect :url => server_uri.dup
32
+ end
33
+ @rsolr
32
34
  end
33
35
 
34
36
  # Perform a Solr search using RSolr
35
37
  def search(search_params={}, options={})
36
- params = @default_search_params.merge(search_params)
37
38
  page = options[:page] ||= 1
38
39
  per_page = options[:per_page] ||= 10
39
- resp = @rsolr.paginate page, per_page, "#{@path_prefix}/solr/#{name}/select", :params => params
40
- if params[:wt] == :xml
40
+ resp = rsolr.paginate page, per_page, "#{@path_prefix}/solr/#{name}/select", :params => search_params
41
+ if search_params[:wt] == :xml
41
42
  data = Nokogiri.XML(resp)
42
43
  raise "search received bad XML" unless data.root
43
44
  else
@@ -78,6 +78,10 @@ module LucidWorks
78
78
  self.deleteAfter = days.blank? ? nil : days.to_i.days.milliseconds
79
79
  end
80
80
 
81
+ def document_count
82
+ collection.search(:wt => :ruby, :q => "data_source:#{id}", :rows => 0)['response']['numFound']
83
+ end
84
+
81
85
  def empty!
82
86
  build_index.destroy
83
87
  end
@@ -55,9 +55,9 @@ module LucidWorks
55
55
  # predict when action will occur next if active at that time
56
56
  #
57
57
  def next_start
58
- return start_time if reference_time <= start_time
58
+ return start_time if (now = Time.now) <= start_time
59
59
  # require 'ruby-debug'; debugger
60
- time_since_start = reference_time - start_time
60
+ time_since_start = now - start_time
61
61
  last_interval_num = (time_since_start / period).to_i
62
62
  next_interval_num = if (time_since_start % period) == 0
63
63
  # this is sort of a stupid condition b/c time precision is millisecond or less
@@ -70,12 +70,6 @@ module LucidWorks
70
70
  start_time + period * next_interval_num
71
71
  end
72
72
 
73
- #
74
- # Provide ability to override "now" explicitly for testing purposes
75
- #
76
- def reference_time=(time); @reference_time = time; end
77
- def reference_time; @reference_time || Time.now.localtime; end
78
-
79
73
  #
80
74
  # phantom attribute for compiling real start time and frequency from appropriate form data
81
75
  # Allows the user to specify a schedule simply with repeat interval (hourly, daily, weekly)
@@ -112,19 +106,20 @@ module LucidWorks
112
106
 
113
107
  self.active = all_attributes['active'] if all_attributes.keys.include?('active')
114
108
 
109
+ now = Time.now
115
110
  self.frequency = all_attributes['frequency']
116
111
  self.start_time =
117
112
  case all_attributes['frequency']
118
113
  when 'weekly'
119
114
  # require 'ruby-debug'; debugger
120
- start = self.reference_time.beginning_of_week.advance(all_attributes['start'])
121
- start < self.reference_time ? start.advance(:weeks => 1) : start
115
+ start = now.beginning_of_week.advance(all_attributes['start'])
116
+ start < now ? start.advance(:weeks => 1) : start
122
117
  when 'daily'
123
- start = self.reference_time.beginning_of_day.advance(all_attributes['start'])
124
- start < self.reference_time ? start.advance(:days => 1) : start
118
+ start = now.beginning_of_day.advance(all_attributes['start'])
119
+ start < now ? start.advance(:days => 1) : start
125
120
  when 'hourly'
126
- start = self.reference_time.change(:min => 0).advance(all_attributes['start'])
127
- start < self.reference_time ? start.advance(:hours => 1) : start
121
+ start = now.change(:min => 0).advance(all_attributes['start'])
122
+ start < now ? start.advance(:hours => 1) : start
128
123
  else
129
124
  puts "*** frequency: <#{all_attributes[:frequency]}>"
130
125
  raise "unexpected frequency encountered"
@@ -156,7 +151,7 @@ module LucidWorks
156
151
  def start_is_more_than_one_period_in_the_future?
157
152
  # This works fine with start times that are multiple periods in the past
158
153
  # because that gives a negative difference, and period is always >= 0
159
- (self.start_time - self.reference_time) > self.period
154
+ (self.start_time - Time.now) > self.period
160
155
  end
161
156
 
162
157
  #
@@ -1,3 +1,3 @@
1
1
  module LucidWorks
2
- VERSION = "0.6.10"
2
+ VERSION = "0.6.11"
3
3
  end
@@ -286,60 +286,51 @@ describe LucidWorks::Collection do
286
286
  end
287
287
  end
288
288
 
289
- describe "#rsolr_connect" do
289
+ describe "#rsolr" do
290
290
  before :each do
291
- @collection = LucidWorks::Collection.new(:parent => @server)
291
+ @collection = LucidWorks::Collection.new(:name => "collection_to_search", :parent => @server)
292
+ mock_server = double('server', :host => 'fake_url')
293
+ @collection.stub(:server) { mock_server }
292
294
  end
293
295
 
294
296
  it "should call RSolr.connect" do
295
297
  RSolr.should_receive(:connect).with(:url => 'fake_url')
296
- @collection.rsolr_connect('fake_url')
298
+ @collection.rsolr
297
299
  end
298
300
 
299
301
  it "should return an RSolr::Client" do
300
- @collection.rsolr_connect('fake_url').should be_a(RSolr::Client)
302
+ @collection.rsolr.should be_a(RSolr::Client)
301
303
  end
302
-
304
+
303
305
  it "should not call RSolr.connect again if it is called again" do
304
306
  mock_rsolr_client = double("RSolr::Client")
305
307
  RSolr.should_receive(:connect).once.and_return(mock_rsolr_client)
306
- @collection.rsolr_connect('fake_uri')
307
- @collection.rsolr_connect('fake_uri')
308
+ @collection.rsolr
309
+ @collection.rsolr
308
310
  end
309
311
  end
310
312
 
311
- describe "search" do
313
+ describe "#search" do
312
314
  context "using faked out Solr" do
313
315
  before do
314
316
  @collection = LucidWorks::Collection.new(:name => "collection_to_search", :parent => @server)
317
+ mock_server = double('server', :host => 'http://fake-solr-host.com/fake_access_path')
318
+ @collection.stub(:server) { mock_server }
315
319
  @mock_rsolr_client = double("RSolr::Client")
316
320
  RSolr.stub(:connect) { @mock_rsolr_client }
317
321
  end
318
322
 
319
- it "should use default search params stored by #rsolr_connect" do
320
- @mock_rsolr_client.should_receive(:paginate)
321
- .with(1, 10, '/fake_access_path/solr/collection_to_search/select',
322
- :params => {:q => 'baz', :foo => :bar})
323
- .and_return({})
324
-
325
- @collection.rsolr_connect('http://fake-solr-host.com/fake_access_path', :foo => :bar)
326
- @collection.search :q => 'baz'
327
- end
328
-
329
323
  it "should respect pagination variables" do
330
324
  @mock_rsolr_client.should_receive(:paginate)
331
325
  .with(33, 44, '/fake_access_path/solr/collection_to_search/select',
332
326
  :params => {:q => 'fake_query', :wt => :ruby})
333
327
  .and_return({})
334
328
 
335
- @collection.rsolr_connect('http://fake-solr-host.com/fake_access_path', :wt => :ruby)
336
- @collection.search({:q => 'fake_query'}, :page => 33, :per_page => 44)
329
+ @collection.search({:q => 'fake_query', :wt => :ruby}, :page => 33, :per_page => 44)
337
330
  end
338
331
 
339
332
  context "when requesting an XML response" do
340
333
  before do
341
- @default_search_params = {:wt => :xml}
342
- @collection.rsolr_connect('http://fake-solr-host.com/fake_access_path', @default_search_params)
343
334
  @good_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<response>FooBar</response>\n</xml>"
344
335
  end
345
336
 
@@ -349,12 +340,12 @@ describe LucidWorks::Collection do
349
340
  :params => {:q => 'fake_query', :wt => :xml})
350
341
  .and_return(@good_xml)
351
342
 
352
- @collection.search(:q => 'fake_query')
343
+ @collection.search(:q => 'fake_query', :wt => :xml)
353
344
  end
354
345
 
355
346
  it "should parse the XML results" do
356
347
  @mock_rsolr_client.stub(:paginate) { @good_xml }
357
- results = @collection.search(:q => 'fake_query')
348
+ results = @collection.search(:q => 'fake_query', :wt => :xml)
358
349
  results.should be_a(Nokogiri::XML::Document)
359
350
  results.root.children.first.text.should == "FooBar"
360
351
  end
@@ -366,7 +357,7 @@ describe LucidWorks::Collection do
366
357
 
367
358
  it "should raise an error" do
368
359
  lambda {
369
- @collection.search
360
+ @collection.search(:wt => :xml)
370
361
  }.should raise_error
371
362
  end
372
363
  end
@@ -384,10 +375,8 @@ describe LucidWorks::Collection do
384
375
  end
385
376
 
386
377
  it "should raise an error (until I decide what the right answer is)" do
387
- collection = LucidWorks::Collection.new(:name => "collection1", :parent => @server)
388
- collection.rsolr_connect(@server.instance_variable_get(:'@host'))
389
378
  lambda {
390
- collection.search @query_params
379
+ @collection.search @query_params
391
380
  }.should raise_error
392
381
  end
393
382
  end
@@ -14,6 +14,10 @@ require 'spec_helper'
14
14
  end
15
15
 
16
16
  describe LucidWorks::Datasource::Schedule do
17
+ before :all do
18
+ ENV['TZ'] = 'UTC'
19
+ end
20
+
17
21
  before do
18
22
  @server = connect_to_live_server
19
23
  @schedule = LucidWorks::Datasource::Schedule.new(:parent => @server)
@@ -49,9 +53,10 @@ describe LucidWorks::Datasource::Schedule do
49
53
  describe '#next_start' do
50
54
  context 'start_time is in the future' do
51
55
  it 'should return start_time' do
52
- @schedule.reference_time = Time.now.utc
53
- @schedule.start_time = @schedule.reference_time + 30.minutes
54
- @schedule.next_start.should == @schedule.start_time
56
+ Timecop.freeze(now = Time.now.utc) do
57
+ @schedule.start_time = now + 30.minutes
58
+ @schedule.next_start.should == @schedule.start_time
59
+ end
55
60
  end
56
61
  end
57
62
 
@@ -69,10 +74,11 @@ describe LucidWorks::Datasource::Schedule do
69
74
  describe 'and it is an exact multiple of self.periods until now' do
70
75
  it 'should return now' do
71
76
  # get current time after rounding error
72
- @schedule.reference_time = @now
73
- @schedule.start_time = @now - 30.minutes
74
- @schedule.period = 10.minutes
75
- @schedule.next_start.should == @now
77
+ Timecop.freeze(@now) do
78
+ @schedule.start_time = @now - 30.minutes
79
+ @schedule.period = 10.minutes
80
+ @schedule.next_start.should == @now
81
+ end
76
82
  end
77
83
  end
78
84
 
@@ -155,29 +161,30 @@ describe LucidWorks::Datasource::Schedule do
155
161
  context 'supplied start time has elapsed w/rt current interval' do
156
162
  describe 'with hourly frequency' do
157
163
  it "should advance the start time" do
158
- half_past_midnight = Time.iso8601("2011-05-09T00:30:00Z")
164
+ half_past_midnight = Time.iso8601("2011-05-09T00:30:00Z").in_time_zone('Eastern Time (US & Canada)')
159
165
  hourly_at_quarter_past = {"frequency"=>"hourly", "start"=>{"minutes"=>"15"}}
160
- quarter_past_1_am = Time.iso8601('2011-05-09T01:15:00Z')
166
+ quarter_past_1_am = Time.iso8601('2011-05-09T01:15:00Z').in_time_zone('Eastern Time (US & Canada)')
161
167
 
162
- @schedule.reference_time = half_past_midnight
163
- @schedule.start_time.should_not == quarter_past_1_am
168
+ Timecop.freeze half_past_midnight do
169
+ @schedule.start_time.should_not == quarter_past_1_am
164
170
 
165
- @schedule.schedule = hourly_at_quarter_past
166
- @schedule.start_time.should == quarter_past_1_am
171
+ @schedule.schedule = hourly_at_quarter_past
172
+ @schedule.start_time.should == quarter_past_1_am
173
+ end
167
174
  end
168
175
  end
169
176
 
170
177
  describe 'with daily frequency' do
171
178
  it "should advance the start time" do
172
- today_at_noon = Time.iso8601("2011-05-09T12:00:00Z")
173
- tomorrow_at_eleven_am = Time.iso8601('2011-05-10T11:00:00Z')
179
+ today_at_noon = Time.iso8601("2011-05-09T12:00:00Z").in_time_zone('Eastern Time (US & Canada)')
180
+ tomorrow_at_eleven_am = Time.iso8601('2011-05-10T11:00:00Z').in_time_zone('Eastern Time (US & Canada)')
174
181
  daily_at_11_am = {"frequency"=>"daily", "start"=>{"hours" => "11", "minutes"=>"00"}}
182
+ Timecop.freeze today_at_noon do
183
+ @schedule.start_time.should_not == tomorrow_at_eleven_am
175
184
 
176
- @schedule.reference_time = today_at_noon
177
- @schedule.start_time.should_not == tomorrow_at_eleven_am
178
-
179
- @schedule.schedule = daily_at_11_am
180
- @schedule.start_time.should == tomorrow_at_eleven_am
185
+ @schedule.schedule = daily_at_11_am
186
+ @schedule.start_time.should == tomorrow_at_eleven_am
187
+ end
181
188
  end
182
189
  end
183
190
 
@@ -192,11 +199,12 @@ describe LucidWorks::Datasource::Schedule do
192
199
  friday_of_this_week.weekday.should == 'Friday'
193
200
  wednesday_of_next_week.weekday.should == 'Wednesday'
194
201
 
195
- @schedule.reference_time = friday_of_this_week
196
- @schedule.start_time.should_not == wednesday_of_next_week
202
+ Timecop.freeze friday_of_this_week do
203
+ @schedule.start_time.should_not == wednesday_of_next_week
197
204
 
198
- @schedule.schedule = weekly_on_wednesday
199
- @schedule.start_time.should == wednesday_of_next_week
205
+ @schedule.schedule = weekly_on_wednesday
206
+ @schedule.start_time.should == wednesday_of_next_week
207
+ end
200
208
  end
201
209
  end
202
210
  end
@@ -208,14 +216,14 @@ describe LucidWorks::Datasource::Schedule do
208
216
  three_quarters_past_midnight = Time.iso8601('2011-05-09T00:45:00Z')
209
217
  hourly_at_three_quarters_past = {"frequency"=>"hourly", "start"=>{"minutes"=>"45"}}
210
218
 
211
- @schedule.reference_time = half_past_midnight
219
+ Timecop.freeze half_past_midnight do
220
+ @schedule.start_time.should_not == three_quarters_past_midnight
221
+ @schedule.frequency.should_not == 'hourly'
212
222
 
213
- @schedule.start_time.should_not == three_quarters_past_midnight
214
- @schedule.frequency.should_not == 'hourly'
215
-
216
- @schedule.schedule = hourly_at_three_quarters_past
217
- @schedule.start_time.should == three_quarters_past_midnight
218
- @schedule.frequency.should == 'hourly'
223
+ @schedule.schedule = hourly_at_three_quarters_past
224
+ @schedule.start_time.should == three_quarters_past_midnight
225
+ @schedule.frequency.should == 'hourly'
226
+ end
219
227
  end
220
228
  end
221
229
 
@@ -225,13 +233,14 @@ describe LucidWorks::Datasource::Schedule do
225
233
  today_at_1pm = Time.iso8601('2011-05-09T13:00:00Z')
226
234
  daily_at_1pm = {"frequency"=>"daily", "start"=>{"hours" => "13", "minutes"=>"00"}}
227
235
 
228
- @schedule.reference_time = today_at_noon
229
- @schedule.start_time.should_not == today_at_1pm
230
- @schedule.frequency.should_not == 'daily'
236
+ Timecop.freeze today_at_noon do
237
+ @schedule.start_time.should_not == today_at_1pm
238
+ @schedule.frequency.should_not == 'daily'
231
239
 
232
- @schedule.schedule = daily_at_1pm
233
- @schedule.start_time.should == today_at_1pm
234
- @schedule.frequency.should == 'daily'
240
+ @schedule.schedule = daily_at_1pm
241
+ @schedule.start_time.should == today_at_1pm
242
+ @schedule.frequency.should == 'daily'
243
+ end
235
244
  end
236
245
  end
237
246
 
@@ -246,13 +255,14 @@ describe LucidWorks::Datasource::Schedule do
246
255
  wednesday_of_this_week.weekday.should == 'Wednesday'
247
256
  thursday_of_this_week.weekday.should == 'Thursday'
248
257
 
249
- @schedule.reference_time = wednesday_of_this_week
250
- @schedule.start_time.should_not == thursday_of_this_week
251
- @schedule.frequency.should_not == 'weekly'
258
+ Timecop.freeze wednesday_of_this_week do
259
+ @schedule.start_time.should_not == thursday_of_this_week
260
+ @schedule.frequency.should_not == 'weekly'
252
261
 
253
- @schedule.schedule = weekly_on_thursday
254
- @schedule.start_time.should == thursday_of_this_week
255
- @schedule.frequency.should == 'weekly'
262
+ @schedule.schedule = weekly_on_thursday
263
+ @schedule.start_time.should == thursday_of_this_week
264
+ @schedule.frequency.should == 'weekly'
265
+ end
256
266
  end
257
267
  end
258
268
  end
@@ -307,11 +317,12 @@ describe LucidWorks::Datasource::Schedule do
307
317
  some_random_time = Time.new(2011, 4, 15, 13, 11, 56)
308
318
  more_than_a_day_later = some_random_time.advance(:days => 2)
309
319
 
310
- @schedule.reference_time = some_random_time
311
- @schedule.frequency = 'daily'
312
- @schedule.start_time = more_than_a_day_later
320
+ Timecop.freeze some_random_time do
321
+ @schedule.frequency = 'daily'
322
+ @schedule.start_time = more_than_a_day_later
313
323
 
314
- @schedule.should be_custom
324
+ @schedule.should be_custom
325
+ end
315
326
  end
316
327
  end
317
328
 
@@ -319,22 +330,24 @@ describe LucidWorks::Datasource::Schedule do
319
330
  it 'should be false if period is standard and start_time is within period from now' do
320
331
  half_past_midnight = Time.iso8601("2011-05-09T00:30:00Z")
321
332
  a_standard_schedule = {"frequency"=>"hourly", "start"=>{"minutes"=>"15"}}
322
- a_standard_reference_time = Time.iso8601('2011-05-09T01:15:00Z')
333
+ some_random_specific_time = Time.iso8601('2011-05-09T01:15:00Z')
323
334
 
324
- @schedule.reference_time = a_standard_reference_time
325
- @schedule.schedule = a_standard_schedule
335
+ Timecop.freeze some_random_specific_time do
336
+ @schedule.schedule = a_standard_schedule
326
337
 
327
- @schedule.should_not be_custom
338
+ @schedule.should_not be_custom
339
+ end
328
340
  end
329
341
 
330
342
  it 'should be false if period is standard and start time is > 1 period in the past' do
331
343
  some_random_time = Time.new(2011, 4, 15, 13, 11, 56)
332
344
  more_than_a_day_ago = some_random_time.advance(:days => -2)
333
- @schedule.reference_time = some_random_time
334
- @schedule.frequency = 'daily'
335
- @schedule.start_time = more_than_a_day_ago
345
+ Timecop.freeze some_random_time do
346
+ @schedule.frequency = 'daily'
347
+ @schedule.start_time = more_than_a_day_ago
336
348
 
337
- @schedule.should_not be_custom
349
+ @schedule.should_not be_custom
350
+ end
338
351
  end
339
352
 
340
353
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'lucid_works'
2
+ require 'timecop'
2
3
 
3
4
  def connect_to_live_server
4
5
  server_uri = ENV['LUCIDWORKS_SERVER_URI']
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: lucid_works
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.6.10
5
+ version: 0.6.11
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sam Pierson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-19 00:00:00 -07:00
13
+ date: 2011-05-24 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency