sunspot_index_queue 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,148 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe Sunspot::IndexQueue::Entry do
4
+
5
+ context "implementation" do
6
+ after :each do
7
+ Sunspot::IndexQueue::Entry.implementation = nil
8
+ end
9
+
10
+ it "should use the active record implementation by default" do
11
+ Sunspot::IndexQueue::Entry.implementation = nil
12
+ Sunspot::IndexQueue::Entry.implementation.should == Sunspot::IndexQueue::Entry::ActiveRecordImpl
13
+ end
14
+
15
+ it "should be able to set the implementation with a class" do
16
+ Sunspot::IndexQueue::Entry.implementation = Sunspot::IndexQueue::Entry::MockImpl
17
+ Sunspot::IndexQueue::Entry.implementation.should == Sunspot::IndexQueue::Entry::MockImpl
18
+ end
19
+
20
+ it "should be able to set the implementation with a class name" do
21
+ Sunspot::IndexQueue::Entry.implementation = "Sunspot::IndexQueue::Entry::MockImpl"
22
+ Sunspot::IndexQueue::Entry.implementation.should == Sunspot::IndexQueue::Entry::MockImpl
23
+ end
24
+
25
+ it "should be able to set the implementation with a symbol" do
26
+ Sunspot::IndexQueue::Entry.implementation = :mock
27
+ Sunspot::IndexQueue::Entry.implementation.should == Sunspot::IndexQueue::Entry::MockImpl
28
+ end
29
+ end
30
+
31
+ context "proxy class methods" do
32
+
33
+ before :all do
34
+ Sunspot::IndexQueue::Entry.implementation = :mock
35
+ end
36
+
37
+ after :all do
38
+ Sunspot::IndexQueue::Entry.implementation = nil
39
+ end
40
+
41
+ let(:implementation) { Sunspot::IndexQueue::Entry.implementation }
42
+ let(:queue) { Sunspot::IndexQueue.new }
43
+ let(:entry) { Sunspot::IndexQueue::Entry.implementation.new }
44
+
45
+ it "should proxy the total_count method to the implementation" do
46
+ implementation.should_receive(:total_count).with(queue).and_return(100)
47
+ Sunspot::IndexQueue::Entry.total_count(queue).should == 100
48
+ end
49
+
50
+ it "should proxy the ready_count method to the implementation" do
51
+ implementation.should_receive(:ready_count).with(queue).and_return(100)
52
+ Sunspot::IndexQueue::Entry.ready_count(queue).should == 100
53
+ end
54
+
55
+ it "should proxy the error_count method to the implementation" do
56
+ implementation.should_receive(:error_count).with(queue).and_return(100)
57
+ Sunspot::IndexQueue::Entry.error_count(queue).should == 100
58
+ end
59
+
60
+ it "should proxy the errors method to the implementation" do
61
+ implementation.should_receive(:errors).with(queue, 2, 1).and_return([entry])
62
+ Sunspot::IndexQueue::Entry.errors(queue, 2, 1).should == [entry]
63
+ end
64
+
65
+ it "should proxy the reset! method to the implementation" do
66
+ implementation.should_receive(:reset!).with(queue)
67
+ Sunspot::IndexQueue::Entry.reset!(queue)
68
+ end
69
+
70
+ it "should proxy the next_batch! method to the implementation" do
71
+ implementation.should_receive(:next_batch!).with(queue).and_return([entry])
72
+ Sunspot::IndexQueue::Entry.next_batch!(queue).should == [entry]
73
+ end
74
+ end
75
+
76
+ context "class methods" do
77
+ before :all do
78
+ Sunspot::IndexQueue::Entry.implementation = :mock
79
+ end
80
+
81
+ after :all do
82
+ Sunspot::IndexQueue::Entry.implementation = nil
83
+ end
84
+
85
+ let(:implementation) { Sunspot::IndexQueue::Entry.implementation }
86
+ let(:queue) { Sunspot::IndexQueue.new }
87
+
88
+ it "should enqueue an entry to the implementation" do
89
+ implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 1, false, 2)
90
+ Sunspot::IndexQueue::Entry.enqueue(queue, Sunspot::IndexQueue::Test::Searchable, 1, false, 2)
91
+ end
92
+
93
+ it "should enqueue an entry to the implementation given a class name" do
94
+ implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 1, false, 2)
95
+ Sunspot::IndexQueue::Entry.enqueue(queue, "Sunspot::IndexQueue::Test::Searchable", 1, false, 2)
96
+ end
97
+
98
+ it "should enqueue multiple entries to the implementation" do
99
+ implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 1, false, 2)
100
+ implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 2, false, 2)
101
+ Sunspot::IndexQueue::Entry.enqueue(queue, Sunspot::IndexQueue::Test::Searchable, [1, 2], false, 2)
102
+ end
103
+
104
+ it "should not enqueue an entry for an object to the implementation" do
105
+ queue.class_names << "Sunspot::IndexQueue::Test::Searchable"
106
+ queue.class_names << "Sunspot::IndexQueue::Test::Searchable::Subclass"
107
+ implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 1, true, 0)
108
+ Sunspot::IndexQueue::Entry.enqueue(queue, Sunspot::IndexQueue::Test::Searchable, 1, true, 0)
109
+ implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable::Subclass, 2, true, 0)
110
+ Sunspot::IndexQueue::Entry.enqueue(queue, Sunspot::IndexQueue::Test::Searchable::Subclass, 2, true, 0)
111
+ lambda{ Sunspot::IndexQueue::Entry.enqueue(queue, Object, 1, false) }.should raise_error(ArgumentError)
112
+ end
113
+
114
+ it "should load all records for an array of entries at once" do
115
+ entry_1 = Sunspot::IndexQueue::Entry.implementation.new(:record_class_name => "Sunspot::IndexQueue::Test::Searchable", :record_id => 1)
116
+ entry_2 = Sunspot::IndexQueue::Entry.implementation.new(:record_class_name => "Sunspot::IndexQueue::Test::Searchable", :record_id => 2)
117
+ entry_3 = Sunspot::IndexQueue::Entry.implementation.new(:record_class_name => "Sunspot::IndexQueue::Test::Searchable::Subclass", :record_id => 3)
118
+ Sunspot::IndexQueue::Entry.load_all_records([entry_1, entry_2, entry_3])
119
+ record_1 = entry_1.instance_variable_get(:@record)
120
+ record_1.should == Sunspot::IndexQueue::Test::Searchable.new("1")
121
+ entry_1.record.object_id.should == record_1.object_id
122
+ record_2 = entry_2.instance_variable_get(:@record)
123
+ record_2.should == Sunspot::IndexQueue::Test::Searchable.new("2")
124
+ entry_2.record.object_id.should == record_2.object_id
125
+ record_3 = entry_3.instance_variable_get(:@record)
126
+ record_3.should == Sunspot::IndexQueue::Test::Searchable::Subclass.new("3")
127
+ entry_3.record.object_id.should == record_3.object_id
128
+ end
129
+ end
130
+
131
+ context "instance methods" do
132
+ it "should get a record for the entry using the Sunspot DataAccessor" do
133
+ entry = Sunspot::IndexQueue::Entry::MockImpl.new(:record_class_name => "Sunspot::IndexQueue::Test::Searchable", :record_id => "1")
134
+ entry.record.class.should == Sunspot::IndexQueue::Test::Searchable
135
+ entry.record.id.should == "1"
136
+ end
137
+
138
+ it "should set if an entry has been processed" do
139
+ entry = Sunspot::IndexQueue::Entry::MockImpl.new(:record_class_name => "Sunspot::IndexQueue::Test::Searchable", :record_id => "1")
140
+ entry.processed?.should == false
141
+ entry.processed = true
142
+ entry.processed?.should == true
143
+ entry.processed = false
144
+ entry.processed?.should == false
145
+ end
146
+ end
147
+
148
+ end
@@ -0,0 +1,150 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe Sunspot::IndexQueue do
4
+
5
+ before :all do
6
+ Sunspot::IndexQueue::Entry.implementation = :mock
7
+ end
8
+
9
+ after :all do
10
+ Sunspot::IndexQueue::Entry.implementation = nil
11
+ end
12
+
13
+ context "enqueing entries" do
14
+ let(:queue) { Sunspot::IndexQueue.new }
15
+
16
+ it "should be able to index a record" do
17
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 1, false, 0)
18
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 2, false, 1)
19
+ queue.index(Sunspot::IndexQueue::Test::Searchable.new(1))
20
+ queue.index(Sunspot::IndexQueue::Test::Searchable.new(2), :priority => 1)
21
+ end
22
+
23
+ it "should be able to index a class and id" do
24
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 1, false, 0)
25
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 2, false, 1)
26
+ queue.index(:class => Sunspot::IndexQueue::Test::Searchable, :id => 1)
27
+ queue.index({:class => Sunspot::IndexQueue::Test::Searchable, :id => 2}, :priority => 1)
28
+ end
29
+
30
+ it "should be able to index multiple records" do
31
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, [1, 2], false, 0)
32
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, [3, 4], false, 1)
33
+ queue.index_all(Sunspot::IndexQueue::Test::Searchable, [1, 2])
34
+ queue.index_all(Sunspot::IndexQueue::Test::Searchable, [3, 4], :priority => 1)
35
+ end
36
+
37
+ it "should be able to remove a record" do
38
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 1, true, 0)
39
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 2, true, 1)
40
+ queue.remove(Sunspot::IndexQueue::Test::Searchable.new(1))
41
+ queue.remove(Sunspot::IndexQueue::Test::Searchable.new(2), :priority => 1)
42
+ end
43
+
44
+ it "should be able to remove a class and id" do
45
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 1, true, 0)
46
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 2, true, 1)
47
+ queue.remove(:class => Sunspot::IndexQueue::Test::Searchable, :id => 1)
48
+ queue.remove({:class => Sunspot::IndexQueue::Test::Searchable, :id => 2}, :priority => 1)
49
+ end
50
+
51
+ it "should be able to remove multiple records" do
52
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, [1, 2], true, 0)
53
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, [3, 4], true, 1)
54
+ queue.remove_all(Sunspot::IndexQueue::Test::Searchable, [1, 2])
55
+ queue.remove_all(Sunspot::IndexQueue::Test::Searchable, [3, 4], :priority => 1)
56
+ end
57
+
58
+ it "should be able to set the priority for indexing or removing records in a block" do
59
+ Sunspot::IndexQueue.default_priority.should == 0
60
+
61
+ Sunspot::IndexQueue.set_priority(1) do
62
+ Sunspot::IndexQueue.default_priority.should == 1
63
+
64
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 1, false, 1)
65
+ queue.index(Sunspot::IndexQueue::Test::Searchable.new(1))
66
+
67
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 2, false, 1)
68
+ queue.index(:class => Sunspot::IndexQueue::Test::Searchable, :id => 2)
69
+
70
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, [3], false, 1)
71
+ queue.index_all(Sunspot::IndexQueue::Test::Searchable, [3])
72
+
73
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 1, true, 1)
74
+ queue.remove(Sunspot::IndexQueue::Test::Searchable.new(1))
75
+
76
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, 2, true, 1)
77
+ queue.remove(:class => Sunspot::IndexQueue::Test::Searchable, :id => 2)
78
+
79
+ Sunspot::IndexQueue::Entry.should_receive(:enqueue).with(queue, Sunspot::IndexQueue::Test::Searchable, [3], true, 1)
80
+ queue.remove_all(Sunspot::IndexQueue::Test::Searchable, [3])
81
+ end
82
+
83
+ Sunspot::IndexQueue.default_priority.should == 0
84
+ end
85
+ end
86
+
87
+ context "processing" do
88
+ let(:queue) { Sunspot::IndexQueue.new(:batch_size => 2, :session => mock(:session)) }
89
+ let(:entry_1) { Sunspot::IndexQueue::Entry::MockImpl.new(:record => record_1, :delete => true) }
90
+ let(:entry_2) { Sunspot::IndexQueue::Entry::MockImpl.new(:record => record_2, :delete => true) }
91
+ let(:entry_3) { Sunspot::IndexQueue::Entry::MockImpl.new(:record => record_3, :delete => true) }
92
+ let(:record_1) { Sunspot::IndexQueue::Test::Searchable.new(1) }
93
+ let(:record_2) { Sunspot::IndexQueue::Test::Searchable.new(2) }
94
+ let(:record_3) { Sunspot::IndexQueue::Test::Searchable.new(3) }
95
+
96
+ it "should process all entries in the queue in batch of batch_size" do
97
+ Sunspot::IndexQueue::Entry.should_receive(:next_batch!).with(queue).and_return([entry_1, entry_2], [entry_3], [])
98
+ Sunspot::IndexQueue::Entry::MockImpl.should_receive(:ready_count).with(queue).and_return(0)
99
+ queue.session.should_receive(:batch).twice.and_yield
100
+ queue.session.should_receive(:remove_by_id).with("Sunspot::IndexQueue::Test::Searchable", "1")
101
+ queue.session.should_receive(:remove_by_id).with("Sunspot::IndexQueue::Test::Searchable", "2")
102
+ queue.session.should_receive(:remove_by_id).with("Sunspot::IndexQueue::Test::Searchable", "3")
103
+ queue.session.should_receive(:commit).twice
104
+ Sunspot::IndexQueue::Entry::MockImpl.should_receive(:delete_entries).with([entry_1, entry_2])
105
+ Sunspot::IndexQueue::Entry::MockImpl.should_receive(:delete_entries).with([entry_3])
106
+ queue.process.should == 3
107
+ end
108
+
109
+ it "should process all entries in the queue and call a batch wrapper if defined" do
110
+ Sunspot::IndexQueue::Entry.should_receive(:next_batch!).with(queue).and_return([entry_1, entry_2], [entry_3], [])
111
+ Sunspot::IndexQueue::Entry::MockImpl.should_receive(:ready_count).with(queue).and_return(0)
112
+ queue.session.should_receive(:batch).twice.and_yield
113
+ queue.session.should_receive(:remove_by_id).with("Sunspot::IndexQueue::Test::Searchable", "1")
114
+ queue.session.should_receive(:remove_by_id).with("Sunspot::IndexQueue::Test::Searchable", "2")
115
+ queue.session.should_receive(:remove_by_id).with("Sunspot::IndexQueue::Test::Searchable", "3")
116
+ queue.session.should_receive(:commit).twice
117
+ Sunspot::IndexQueue::Entry::MockImpl.should_receive(:delete_entries).with([entry_1, entry_2])
118
+ Sunspot::IndexQueue::Entry::MockImpl.should_receive(:delete_entries).with([entry_3])
119
+ wrapper_count = 0
120
+ queue.batch_handler{|batch| wrapper_count += 1; batch.submit!}
121
+ queue.process.should == 3
122
+ wrapper_count.should == 2
123
+ end
124
+ end
125
+
126
+ context "maintenance" do
127
+ let(:queue) { Sunspot::IndexQueue.new }
128
+
129
+ it "should be able to reset all entries to clear errors and set them to be processed immediately" do
130
+ Sunspot::IndexQueue::Entry.should_receive(:reset!).with(queue)
131
+ queue.reset!
132
+ end
133
+
134
+ it "should get the total number of entries in the queue" do
135
+ Sunspot::IndexQueue::Entry.should_receive(:total_count).with(queue).and_return(10)
136
+ queue.total_count.should == 10
137
+ end
138
+
139
+ it "should get the number of entries in the queue ready to be processed" do
140
+ Sunspot::IndexQueue::Entry.should_receive(:ready_count).with(queue).and_return(10)
141
+ queue.ready_count.should == 10
142
+ end
143
+
144
+ it "should get the number of entries with errors in the queue" do
145
+ Sunspot::IndexQueue::Entry.should_receive(:error_count).with(queue).and_return(10)
146
+ queue.error_count.should == 10
147
+ end
148
+ end
149
+
150
+ end
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+ require 'active_record'
3
+
4
+ describe "Sunspot::IndexQueue integration tests" do
5
+ before :all do
6
+ db_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp'))
7
+ Dir.mkdir(db_dir) unless File.exist?(db_dir)
8
+ Dir.chdir(db_dir) do
9
+ FileUtils.rm_rf('data') if File.exist?('data')
10
+ Dir.mkdir('data')
11
+ `sunspot-solr start --port=18983 --data-directory=data --pid-dir=data --log-file=data/solr.log --max-memory=64m`
12
+ raise "Failed to start Solr on port 18983" unless $? == 0
13
+ # Wait until the server is responding
14
+ ping_uri = URI.parse("http://127.0.0.1:18983/solr/ping")
15
+ solr_started = false
16
+ 100.times do
17
+ begin
18
+ Net::HTTP.get(ping_uri)
19
+ solr_started = true
20
+ break
21
+ rescue
22
+ sleep(0.1)
23
+ end
24
+ end
25
+ raise "Solr failed to start after 10 seconds" unless solr_started
26
+ end
27
+
28
+ db = File.join(db_dir, 'sunspot_index_queue_test.sqlite3')
29
+ File.delete(db) if File.exist?(db)
30
+ ActiveRecord::Base.establish_connection("adapter" => "sqlite3", "database" => db)
31
+ Sunspot::IndexQueue::Entry.implementation = :active_record
32
+ Sunspot::IndexQueue::Entry::ActiveRecordImpl.create_table
33
+
34
+ @solr_session = Sunspot::Session.new do |config|
35
+ config.solr.url = 'http://127.0.0.1:18983/solr'
36
+ end
37
+ end
38
+
39
+ after :all do
40
+ db_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'tmp'))
41
+ Dir.chdir(db_dir) do
42
+ `sunspot-solr stop --pid-dir=data`
43
+ end
44
+ db = File.join(db_dir, 'sunspot_index_queue_test.sqlite3')
45
+ data = File.join(db_dir, 'data')
46
+ FileUtils.rm_rf(data) if File.exist?(data)
47
+ ActiveRecord::Base.connection.disconnect!
48
+ File.delete(db) if File.exist?(db)
49
+ Dir.delete(db_dir) if File.exist?(db_dir) and Dir.entries(db_dir).reject{|f| f.match(/^\.+$/)}.empty?
50
+ Sunspot::IndexQueue::Entry.implementation = nil
51
+ end
52
+
53
+ let(:session) { Sunspot::IndexQueue::SessionProxy.new(queue) }
54
+ let(:queue) { Sunspot::IndexQueue.new(:session => @solr_session, :batch_size => 2) }
55
+
56
+ it "should actually work" do
57
+ Sunspot::IndexQueue::Test::Searchable.mock_db do
58
+ record_1 = Sunspot::IndexQueue::Test::Searchable.new("1", "one")
59
+ record_2 = Sunspot::IndexQueue::Test::Searchable.new("2", "two")
60
+ record_3 = Sunspot::IndexQueue::Test::Searchable::Subclass.new("3", "three")
61
+ Sunspot::IndexQueue::Test::Searchable.save(record_1, record_2, record_3)
62
+
63
+ # Enqueue records
64
+ queue.index(record_1)
65
+ queue.index(record_2)
66
+ queue.index(record_3)
67
+ queue.index(record_2)
68
+ queue.total_count.should == 3
69
+ queue.ready_count.should == 3
70
+ queue.error_count.should == 0
71
+ queue.errors.should == []
72
+
73
+ # Should not be found
74
+ session.search(Sunspot::IndexQueue::Test::Searchable){with :value, "three"}.results.should == []
75
+
76
+ # Process queue
77
+ queue.process
78
+ queue.total_count.should == 0
79
+ queue.ready_count.should == 0
80
+ queue.error_count.should == 0
81
+ queue.errors.should == []
82
+
83
+ # Should be found
84
+ session.search(Sunspot::IndexQueue::Test::Searchable){with :value, "two"}.results.should == [record_2]
85
+ session.search(Sunspot::IndexQueue::Test::Searchable){with :value, "three"}.results.should == [record_3]
86
+
87
+ # Subclass should be found
88
+ session.search(Sunspot::IndexQueue::Test::Searchable::Subclass){with :value, "two"}.results.should == []
89
+ session.search(Sunspot::IndexQueue::Test::Searchable::Subclass){with :value, "three"}.results.should == [record_3]
90
+
91
+ # Update record
92
+ record_3.value = "four"
93
+
94
+ queue.index(record_3)
95
+ Sunspot::IndexQueue::Test::Searchable.save(record_3)
96
+ session.search(Sunspot::IndexQueue::Test::Searchable){with :value, "three"}.results.should == [record_3]
97
+ session.search(Sunspot::IndexQueue::Test::Searchable){with :value, "four"}.results.should == []
98
+ queue.process
99
+ session.search(Sunspot::IndexQueue::Test::Searchable){with :value, "three"}.results.should == []
100
+ session.search(Sunspot::IndexQueue::Test::Searchable){with :value, "four"}.results.should == [record_3]
101
+
102
+ # Remove record
103
+ queue.remove(record_3)
104
+ session.search(Sunspot::IndexQueue::Test::Searchable){with :value, "four"}.results.should == [record_3]
105
+ queue.process
106
+ session.search(Sunspot::IndexQueue::Test::Searchable){with :value, "four"}.results.should == []
107
+ end
108
+ end
109
+
110
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+ require File.expand_path('../entry_impl_examples', __FILE__)
3
+
4
+ describe Sunspot::IndexQueue::Entry::MongoImpl do
5
+
6
+ before :all do
7
+ Sunspot::IndexQueue::Entry.implementation = :mongo
8
+ Sunspot::IndexQueue::Entry::MongoImpl.connection = 'localhost'
9
+ Sunspot::IndexQueue::Entry::MongoImpl.database_name = "sunspot_index_queue_test"
10
+ end
11
+
12
+ after :all do
13
+ Sunspot::IndexQueue::Entry.implementation = nil
14
+ end
15
+
16
+ let(:factory) do
17
+ factory = Object.new
18
+ def factory.create (attributes)
19
+ Sunspot::IndexQueue::Entry::MongoImpl.create(attributes)
20
+ end
21
+
22
+ def factory.delete_all
23
+ Sunspot::IndexQueue::Entry::MongoImpl.collection.remove
24
+ end
25
+
26
+ def factory.find (id)
27
+ doc = Sunspot::IndexQueue::Entry::MongoImpl.find_one(id)
28
+ end
29
+
30
+ factory
31
+ end
32
+
33
+ it_should_behave_like "Entry implementation"
34
+
35
+ end
@@ -0,0 +1,174 @@
1
+ require File.expand_path('../spec_helper', __FILE__)
2
+
3
+ describe Sunspot::IndexQueue::SessionProxy do
4
+
5
+ before :all do
6
+ Sunspot::IndexQueue::Entry.implementation = :mock
7
+ end
8
+
9
+ after :all do
10
+ Sunspot::IndexQueue::Entry.implementation = nil
11
+ end
12
+
13
+ context "initialization" do
14
+ let(:queue) { Sunspot::IndexQueue.new }
15
+
16
+ it "should use the default queue by default" do
17
+ proxy = Sunspot::IndexQueue::SessionProxy.new
18
+ proxy.queue.session == proxy.session
19
+ end
20
+
21
+ it "should use the queue's session by default" do
22
+ proxy = Sunspot::IndexQueue::SessionProxy.new(queue)
23
+ proxy.session.should == queue.session
24
+ proxy.queue.should == queue
25
+ end
26
+
27
+ it "should be able to specify the underlying session" do
28
+ session = Sunspot::Session.new
29
+ proxy = Sunspot::IndexQueue::SessionProxy.new(queue, session)
30
+ proxy.session.should == session
31
+ proxy.queue.session.should_not == proxy.session
32
+ end
33
+ end
34
+
35
+ context "delgated methods" do
36
+
37
+ subject { Sunspot::IndexQueue::SessionProxy.new(queue, session) }
38
+ let(:session) { Sunspot::Session.new }
39
+ let(:queue) { Sunspot::IndexQueue.new }
40
+
41
+ it "should delegate new_search" do
42
+ session.should_receive(:new_search).with(String, Symbol)
43
+ subject.new_search(String, Symbol)
44
+ end
45
+
46
+ it "should delegate search" do
47
+ session.should_receive(:search).with(String, Symbol)
48
+ subject.search(String, Symbol)
49
+ end
50
+
51
+ it "should delegate new_more_like_this" do
52
+ session.should_receive(:new_more_like_this).with(:test, String, Symbol)
53
+ subject.new_more_like_this(:test, String, Symbol)
54
+ end
55
+
56
+ it "should delegate more_like_this" do
57
+ session.should_receive(:more_like_this).with(:test, String, Symbol)
58
+ subject.more_like_this(:test, String, Symbol)
59
+ end
60
+
61
+ it "should delegate config" do
62
+ subject.config.should == session.config
63
+ end
64
+ end
65
+
66
+ context "indexing methods" do
67
+
68
+ subject { Sunspot::IndexQueue::SessionProxy.new(queue, session) }
69
+ let(:session) { mock(:session) }
70
+ let(:queue) { Sunspot::IndexQueue.new(:session => mock(:queue_session)) }
71
+
72
+ it "should yield the block to batch" do
73
+ executed = false
74
+ subject.batch do
75
+ executed = true
76
+ end
77
+ executed.should == true
78
+ end
79
+
80
+ it "should not do anything on commit" do
81
+ subject.commit
82
+ end
83
+
84
+ it "should not do anything on commit_if_delete_dirty" do
85
+ subject.commit_if_delete_dirty
86
+ end
87
+
88
+ it "should not do anything on commit_if_dirty" do
89
+ subject.commit_if_dirty
90
+ end
91
+
92
+ it "should not mark deletes as dirty" do
93
+ Sunspot::IndexQueue::Entry.implementation.stub!(:add)
94
+ subject.remove(Sunspot::IndexQueue::Test::Searchable.new(1))
95
+ subject.delete_dirty?.should == false
96
+ end
97
+
98
+ it "should not mark the session as dirty" do
99
+ Sunspot::IndexQueue::Entry.implementation.stub!(:add)
100
+ subject.index(Sunspot::IndexQueue::Test::Searchable.new(1))
101
+ subject.delete_dirty?.should == false
102
+ end
103
+
104
+ it "should queue up objects being indexed" do
105
+ Sunspot::IndexQueue::Entry.implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 1, false, 0)
106
+ Sunspot::IndexQueue::Entry.implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 2, false, 0)
107
+ subject.index(Sunspot::IndexQueue::Test::Searchable.new(1), [Sunspot::IndexQueue::Test::Searchable.new(2)])
108
+ end
109
+
110
+ it "should queue up objects being indexed and committed" do
111
+ Sunspot::IndexQueue::Entry.implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 1, false, 0)
112
+ Sunspot::IndexQueue::Entry.implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 2, false, 0)
113
+ subject.index!(Sunspot::IndexQueue::Test::Searchable.new(1), [Sunspot::IndexQueue::Test::Searchable.new(2)])
114
+ end
115
+
116
+ it "should queue up objects being removed" do
117
+ Sunspot::IndexQueue::Entry.implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 1, true, 0)
118
+ Sunspot::IndexQueue::Entry.implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 2, true, 0)
119
+ subject.remove(Sunspot::IndexQueue::Test::Searchable.new(1), [Sunspot::IndexQueue::Test::Searchable.new(2)])
120
+ end
121
+
122
+ it "should queue up objects being removed and committed" do
123
+ Sunspot::IndexQueue::Entry.implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 1, true, 0)
124
+ Sunspot::IndexQueue::Entry.implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 2, true, 0)
125
+ subject.remove!(Sunspot::IndexQueue::Test::Searchable.new(1), [Sunspot::IndexQueue::Test::Searchable.new(2)])
126
+ end
127
+
128
+ it "should queue up objects being removed by id" do
129
+ Sunspot::IndexQueue::Entry.implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 1, true, 0)
130
+ subject.remove_by_id(Sunspot::IndexQueue::Test::Searchable, 1)
131
+ end
132
+
133
+ it "should queue up objects being removed by id and committed" do
134
+ Sunspot::IndexQueue::Entry.implementation.should_receive(:add).with(Sunspot::IndexQueue::Test::Searchable, 1, true, 0)
135
+ subject.remove_by_id(Sunspot::IndexQueue::Test::Searchable, 1)
136
+ end
137
+
138
+ context "not queueable" do
139
+
140
+ subject { Sunspot::IndexQueue::SessionProxy.new(queue, session) }
141
+ let(:session) { Sunspot::Session.new }
142
+ let(:queue) { Sunspot::IndexQueue.new }
143
+
144
+ it "should immediately remove objects using the queue session if the method takes a block" do
145
+ executed = false
146
+ queue.session.should_receive(:remove).with(:test).and_yield
147
+ subject.remove(:test) do
148
+ executed = true
149
+ end
150
+ executed.should == true
151
+ end
152
+
153
+ it "should immediately remove objects and commit using the queue session if the method takes a block" do
154
+ executed = false
155
+ queue.session.should_receive(:remove!).with(:test).and_yield
156
+ subject.remove!(:test) do
157
+ executed = true
158
+ end
159
+ executed.should == true
160
+ end
161
+
162
+ it "should immediately remove all classes using the queue session" do
163
+ queue.session.should_receive(:remove_all).with(Sunspot::IndexQueue::Test::Searchable)
164
+ subject.remove_all(Sunspot::IndexQueue::Test::Searchable)
165
+ end
166
+
167
+ it "should immediately remove all classes and and commit using the queue session" do
168
+ queue.session.should_receive(:remove_all!).with(Sunspot::IndexQueue::Test::Searchable)
169
+ subject.remove_all!(Sunspot::IndexQueue::Test::Searchable)
170
+ end
171
+ end
172
+ end
173
+
174
+ end