forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_gateway_test.rb
99 lines (74 loc) · 3.37 KB
/
model_gateway_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
require 'test_helper'
require 'elasticsearch/persistence/model'
require 'elasticsearch/persistence/model/rails'
class Elasticsearch::Persistence::ModelGatewayTest < Test::Unit::TestCase
context "The model gateway" do
setup do
class DummyGatewayModel
include Elasticsearch::Persistence::Model
end
end
teardown do
Elasticsearch::Persistence::ModelGatewayTest.__send__ :remove_const, :DummyGatewayModel \
rescue NameError; nil
end
should "be accessible" do
assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyGatewayModel.gateway
$a = 0
DummyGatewayModel.gateway { $a += 1 }
assert_equal 1, $a
@b = 0
def run!; DummyGatewayModel.gateway { |g| @b += 1 }; end
run!
assert_equal 1, @b
assert_equal DummyGatewayModel, DummyGatewayModel.gateway.klass
end
should "define common attributes" do
d = DummyGatewayModel.new
assert_respond_to d, :updated_at
assert_respond_to d, :created_at
end
should "allow to configure settings" do
DummyGatewayModel.settings(number_of_shards: 1)
assert_equal 1, DummyGatewayModel.settings.to_hash[:number_of_shards]
end
should "allow to configure mappings" do
DummyGatewayModel.mapping { indexes :name, analyzer: 'snowball' }
assert_equal 'snowball',
DummyGatewayModel.mapping.to_hash[:dummy_gateway_model][:properties][:name][:analyzer]
end
should "configure the mapping via attribute" do
DummyGatewayModel.attribute :name, String, mapping: { analyzer: 'snowball' }
assert_respond_to DummyGatewayModel, :name
assert_equal 'snowball',
DummyGatewayModel.mapping.to_hash[:dummy_gateway_model][:properties][:name][:analyzer]
end
should "configure the mapping via an attribute block" do
DummyGatewayModel.attribute :name, String do
indexes :name, analyzer: 'custom'
end
assert_respond_to DummyGatewayModel, :name
assert_equal 'custom',
DummyGatewayModel.mapping.to_hash[:dummy_gateway_model][:properties][:name][:analyzer]
end
should "properly look up types for classes" do
assert_equal 'string', Elasticsearch::Persistence::Model::Utils::lookup_type(String)
assert_equal 'integer', Elasticsearch::Persistence::Model::Utils::lookup_type(Integer)
assert_equal 'float', Elasticsearch::Persistence::Model::Utils::lookup_type(Float)
assert_equal 'date', Elasticsearch::Persistence::Model::Utils::lookup_type(Date)
assert_equal 'boolean', Elasticsearch::Persistence::Model::Utils::lookup_type(Virtus::Attribute::Boolean)
end
should "remove IDs from hash when serializing" do
assert_equal( {foo: 'bar'}, DummyGatewayModel.gateway.serialize(id: '123', foo: 'bar') )
end
should "set IDs from hash when deserializing" do
assert_equal 'abc123', DummyGatewayModel.gateway.deserialize('_id' => 'abc123', '_source' => {}).id
end
should "set @persisted variable from hash when deserializing" do
assert DummyGatewayModel.gateway.deserialize('_id' => 'abc123', '_source' => {}).instance_variable_get(:@persisted)
end
should "allow to access the raw hit from results as Hashie::Mash" do
assert_equal 0.42, DummyGatewayModel.gateway.deserialize('_score' => 0.42, '_source' => {}).hit._score
end
end
end