forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter_mongoid_test.rb
102 lines (79 loc) · 3.23 KB
/
adapter_mongoid_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
require 'test_helper'
class Elasticsearch::Model::AdapterMongoidTest < Test::Unit::TestCase
context "Adapter Mongoid module: " do
class ::DummyClassForMongoid
RESPONSE = Struct.new('DummyMongoidResponse') do
def response
{ 'hits' => {'hits' => [ {'_id' => 2}, {'_id' => 1} ]} }
end
end.new
def response
RESPONSE
end
def ids
[2, 1]
end
end
setup do
@records = [ stub(id: 1, inspect: '<Model-1>'), stub(id: 2, inspect: '<Model-2>') ]
::Symbol.any_instance.stubs(:in).returns(@records)
end
should "have the register condition" do
assert_not_nil Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::Mongoid]
assert_equal false, Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::Mongoid].call(DummyClassForMongoid)
end
context "Records" do
setup do
DummyClassForMongoid.__send__ :include, Elasticsearch::Model::Adapter::Mongoid::Records
end
should "have the implementation" do
assert_instance_of Module, Elasticsearch::Model::Adapter::Mongoid::Records
instance = DummyClassForMongoid.new
instance.expects(:klass).returns(mock('class', where: @records))
assert_equal @records, instance.records
end
should "reorder the records based on hits order" do
@records.instance_variable_set(:@records, @records)
instance = DummyClassForMongoid.new
instance.expects(:klass).returns(mock('class', where: @records))
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 = DummyClassForMongoid.new
instance.expects(:klass).returns(stub('class', where: @records)).at_least_once
instance.records.expects(:asc).returns(@records)
assert_equal [2, 1], instance.records.to_a.map(&:id)
assert_equal [1, 2], instance.asc.to_a.map(&:id)
end
end
context "Callbacks" do
should "register hooks for automatically updating the index" do
DummyClassForMongoid.expects(:after_create)
DummyClassForMongoid.expects(:after_update)
DummyClassForMongoid.expects(:after_destroy)
Elasticsearch::Model::Adapter::Mongoid::Callbacks.included(DummyClassForMongoid)
end
end
context "Importing" do
should "implement the __find_in_batches method" do
DummyClassForMongoid.expects(:all).returns([])
DummyClassForMongoid.__send__ :extend, Elasticsearch::Model::Adapter::Mongoid::Importing
DummyClassForMongoid.__find_in_batches do; end
end
context "when transforming models" do
setup do
@transform = DummyClassForMongoid.__transform
end
should "provide an object that responds to #call" do
assert_respond_to @transform, :call
end
should "provide basic transformation" do
model = mock("model", id: 1, as_indexed_json: {})
assert_equal @transform.call(model), { index: { _id: "1", data: {} } }
end
end
end
end
end