/
real_time_callbacks_spec.rb
236 lines (181 loc) · 8.25 KB
/
real_time_callbacks_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
require 'spec_helper'
describe ThinkingSphinx::RealTime::Callbacks::RealTimeCallbacks do
let(:callbacks) {
ThinkingSphinx::RealTime::Callbacks::RealTimeCallbacks.new :article
}
let(:instance) { double('instance', :id => 12, :persisted? => true) }
let(:config) { double('config', :indices_for_references => [index],
:settings => {}) }
let(:index) { double('index', :name => 'my_index', :is_a? => true,
:document_id_for_key => 123, :fields => [], :attributes => [],
:conditions => []) }
let(:connection) { double('connection', :execute => true) }
before :each do
allow(ThinkingSphinx::Configuration).to receive_messages :instance => config
allow(ThinkingSphinx::Connection).to receive_message_chain(:pool, :take).and_yield connection
end
describe '#after_save, #after_commit' do
let(:insert) { double('insert', :to_sql => 'REPLACE INTO my_index') }
let(:time) { 1.day.ago }
let(:field) { double('field', :name => 'name', :translate => 'Foo') }
let(:attribute) { double('attribute', :name => 'created_at',
:translate => time) }
before :each do
allow(ThinkingSphinx::Configuration).to receive_messages :instance => config
allow(Riddle::Query::Insert).to receive_messages :new => insert
allow(insert).to receive_messages :replace! => insert
allow(index).to receive_messages :fields => [field], :attributes => [attribute]
end
it "creates an insert statement with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)
callbacks.after_save instance
end
it "switches the insert to a replace statement" do
expect(insert).to receive(:replace!).and_return(insert)
callbacks.after_save instance
end
it "sends the insert through to the server" do
expect(connection).to receive(:execute).with('REPLACE INTO my_index')
callbacks.after_save instance
end
it "creates an insert statement with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)
callbacks.after_commit instance
end
it "switches the insert to a replace statement" do
expect(insert).to receive(:replace!).and_return(insert)
callbacks.after_commit instance
end
it "sends the insert through to the server" do
expect(connection).to receive(:execute).with('REPLACE INTO my_index')
callbacks.after_commit instance
end
context 'with a given path' do
let(:callbacks) {
ThinkingSphinx::RealTime::Callbacks::RealTimeCallbacks.new(
:article, [:user]
)
}
let(:instance) { double('instance', :id => 12, :user => user) }
let(:user) { double('user', :id => 13, :persisted? => true) }
it "creates an insert statement with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)
callbacks.after_save instance
end
it "gets the document id for the user object" do
expect(index).to receive(:document_id_for_key).with(13).and_return(123)
callbacks.after_save instance
end
it "translates values for the user object" do
expect(field).to receive(:translate).with(user).and_return('Foo')
callbacks.after_save instance
end
it "creates an insert statement with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)
callbacks.after_commit instance
end
it "gets the document id for the user object" do
expect(index).to receive(:document_id_for_key).with(13).and_return(123)
callbacks.after_commit instance
end
it "translates values for the user object" do
expect(field).to receive(:translate).with(user).and_return('Foo')
callbacks.after_commit instance
end
end
context 'with a path returning multiple objects' do
let(:callbacks) {
ThinkingSphinx::RealTime::Callbacks::RealTimeCallbacks.new(
:article, [:readers]
)
}
let(:instance) { double('instance', :id => 12,
:readers => [user_a, user_b]) }
let(:user_a) { double('user', :id => 13, :persisted? => true) }
let(:user_b) { double('user', :id => 14, :persisted? => true) }
it "creates insert statements with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).twice.
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)
callbacks.after_save instance
end
it "gets the document id for each reader" do
expect(index).to receive(:document_id_for_key).with(13).and_return(123)
expect(index).to receive(:document_id_for_key).with(14).and_return(123)
callbacks.after_save instance
end
it "translates values for each reader" do
expect(field).to receive(:translate).with(user_a).and_return('Foo')
expect(field).to receive(:translate).with(user_b).and_return('Foo')
callbacks.after_save instance
end
it "creates insert statements with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).twice.
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)
callbacks.after_commit instance
end
it "gets the document id for each reader" do
expect(index).to receive(:document_id_for_key).with(13).and_return(123)
expect(index).to receive(:document_id_for_key).with(14).and_return(123)
callbacks.after_commit instance
end
it "translates values for each reader" do
expect(field).to receive(:translate).with(user_a).and_return('Foo')
expect(field).to receive(:translate).with(user_b).and_return('Foo')
callbacks.after_commit instance
end
end
context 'with a block instead of a path' do
let(:callbacks) {
ThinkingSphinx::RealTime::Callbacks::RealTimeCallbacks.new(
:article
) { |object| object.readers }
}
let(:instance) { double('instance', :id => 12,
:readers => [user_a, user_b]) }
let(:user_a) { double('user', :id => 13, :persisted? => true) }
let(:user_b) { double('user', :id => 14, :persisted? => true) }
it "creates insert statements with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).twice.
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)
callbacks.after_save instance
end
it "gets the document id for each reader" do
expect(index).to receive(:document_id_for_key).with(13).and_return(123)
expect(index).to receive(:document_id_for_key).with(14).and_return(123)
callbacks.after_save instance
end
it "translates values for each reader" do
expect(field).to receive(:translate).with(user_a).and_return('Foo')
expect(field).to receive(:translate).with(user_b).and_return('Foo')
callbacks.after_save instance
end
it "creates insert statements with all fields and attributes" do
expect(Riddle::Query::Insert).to receive(:new).twice.
with('my_index', ['id', 'name', 'created_at'], [[123, 'Foo', time]]).
and_return(insert)
callbacks.after_commit instance
end
it "gets the document id for each reader" do
expect(index).to receive(:document_id_for_key).with(13).and_return(123)
expect(index).to receive(:document_id_for_key).with(14).and_return(123)
callbacks.after_commit instance
end
it "translates values for each reader" do
expect(field).to receive(:translate).with(user_a).and_return('Foo')
expect(field).to receive(:translate).with(user_b).and_return('Foo')
callbacks.after_commit instance
end
end
end
end