forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_base_test.rb
72 lines (58 loc) · 2.01 KB
/
model_base_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
require 'test_helper'
require 'elasticsearch/persistence/model'
require 'elasticsearch/persistence/model/rails'
class Elasticsearch::Persistence::ModelBaseTest < Test::Unit::TestCase
context "The model" do
setup do
class DummyBaseModel
include Elasticsearch::Persistence::Model
attribute :name, String
end
end
should "respond to id, _id, _index, _type and _version" do
model = DummyBaseModel.new
[:id, :_id, :_index, :_type, :_version].each { |method| assert_respond_to model, method }
end
should "set the ID from attributes during initialization" do
model = DummyBaseModel.new id: 1
assert_equal 1, model.id
model = DummyBaseModel.new 'id' => 2
assert_equal 2, model.id
end
should "set the ID using setter method" do
model = DummyBaseModel.new id: 1
assert_equal 1, model.id
model.id = 2
assert_equal 2, model.id
end
should "have ID in attributes" do
model = DummyBaseModel.new id: 1, name: 'Test'
assert_equal 1, model.attributes[:id]
end
should "have the customized inspect method" do
model = DummyBaseModel.new name: 'Test'
assert_match /name\: "Test"/, model.inspect
end
context "with custom document_type" do
setup do
@model = DummyBaseModel
@gateway = mock()
@mapping = mock()
@model.stubs(:gateway).returns(@gateway)
@gateway.stubs(:mapping).returns(@mapping)
@document_type = 'dummybase'
end
should "forward the argument to mapping" do
@gateway.expects(:document_type).with(@document_type).once
@mapping.expects(:type=).with(@document_type).once
@model.document_type @document_type
end
should "return the value from the gateway" do
@gateway.expects(:document_type).once.returns(@document_type)
@mapping.expects(:type=).never
returned_type = @model.document_type
assert_equal @document_type, returned_type
end
end
end
end