forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter_active_record_test.rb
147 lines (115 loc) · 5.1 KB
/
adapter_active_record_test.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
require 'test_helper'
class Elasticsearch::Model::AdapterActiveRecordTest < Test::Unit::TestCase
context "Adapter ActiveRecord module: " do
class ::DummyClassForActiveRecord
RESPONSE = Struct.new('DummyActiveRecordResponse') do
def response
{ 'hits' => {'hits' => [ {'_id' => 2}, {'_id' => 1} ]} }
end
end.new
def response
RESPONSE
end
def ids
[2, 1]
end
end
RESPONSE = { 'hits' => { 'total' => 123, 'max_score' => 456, 'hits' => [] } }
setup do
@records = [ stub(id: 1, inspect: '<Model-1>'), stub(id: 2, inspect: '<Model-2>') ]
@records.stubs(:load).returns(true)
@records.stubs(:exec_queries).returns(true)
end
should "have the register condition" do
assert_not_nil Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::ActiveRecord]
assert_equal false, Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::ActiveRecord].call(DummyClassForActiveRecord)
end
context "Records" do
setup do
DummyClassForActiveRecord.__send__ :include, Elasticsearch::Model::Adapter::ActiveRecord::Records
end
should "have the implementation" do
assert_instance_of Module, Elasticsearch::Model::Adapter::ActiveRecord::Records
instance = DummyClassForActiveRecord.new
instance.expects(:klass).returns(mock('class', primary_key: :some_key, where: @records)).at_least_once
assert_equal @records, instance.records
end
should "load the records" do
instance = DummyClassForActiveRecord.new
instance.expects(:records).returns(@records)
instance.load
end
should "reorder the records based on hits order" do
@records.instance_variable_set(:@records, @records)
instance = DummyClassForActiveRecord.new
instance.expects(:klass).returns(mock('class', primary_key: :some_key, where: @records)).at_least_once
assert_equal [1, 2], @records. to_a.map(&:id)
assert_equal [2, 1], instance.records.to_a.map(&:id)
end
should "not reorder records when SQL order is present" do
@records.instance_variable_set(:@records, @records)
instance = DummyClassForActiveRecord.new
instance.expects(:klass).returns(stub('class', primary_key: :some_key, where: @records)).at_least_once
instance.records.expects(:order).returns(@records)
assert_equal [2, 1], instance.records. to_a.map(&:id)
assert_equal [1, 2], instance.order(:foo).to_a.map(&:id)
end
end
context "Callbacks" do
should "register hooks for automatically updating the index" do
DummyClassForActiveRecord.expects(:after_commit).times(3)
Elasticsearch::Model::Adapter::ActiveRecord::Callbacks.included(DummyClassForActiveRecord)
end
end
context "Importing" do
setup do
DummyClassForActiveRecord.__send__ :extend, Elasticsearch::Model::Adapter::ActiveRecord::Importing
end
should "raise an exception when passing an invalid scope" do
assert_raise NoMethodError do
DummyClassForActiveRecord.__find_in_batches(scope: :not_found_method) do; end
end
end
should "implement the __find_in_batches method" do
DummyClassForActiveRecord.expects(:find_in_batches).returns([])
DummyClassForActiveRecord.__find_in_batches do; end
end
should "limit the relation to a specific scope" do
DummyClassForActiveRecord.expects(:find_in_batches).returns([])
DummyClassForActiveRecord.expects(:published).returns(DummyClassForActiveRecord)
DummyClassForActiveRecord.__find_in_batches(scope: :published) do; end
end
should "limit the relation to a specific query" do
DummyClassForActiveRecord.expects(:find_in_batches).returns([])
DummyClassForActiveRecord.expects(:where).returns(DummyClassForActiveRecord)
DummyClassForActiveRecord.__find_in_batches(query: -> { where(color: "red") }) do; end
end
should "preprocess the batch if option provided" do
class << DummyClassForActiveRecord
# Updates/transforms the batch while fetching it from the database
# (eg. with information from an external system)
#
def update_batch(batch)
batch.collect { |b| b.to_s + '!' }
end
end
DummyClassForActiveRecord.expects(:__find_in_batches).returns( [:a, :b] )
DummyClassForActiveRecord.__find_in_batches(preprocess: :update_batch) do |batch|
assert_same_elements ["a!", "b!"], batch
end
end
context "when transforming models" do
setup do
@transform = DummyClassForActiveRecord.__transform
end
should "provide an object that responds to #call" do
assert_respond_to @transform, :call
end
should "provide default transformation" do
model = mock("model", id: 1, __elasticsearch__: stub(as_indexed_json: {}))
assert_equal @transform.call(model), { index: { _id: 1, data: {} } }
end
end
end
end
end