forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository_naming_test.rb
146 lines (122 loc) · 4.17 KB
/
repository_naming_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
require 'test_helper'
class Elasticsearch::Persistence::RepositoryNamingTest < Test::Unit::TestCase
context "The repository naming" do
# Fake class for the naming tests
class ::Foobar; end
class ::FooBar; end
module ::Foo; class Bar; end; end
setup do
@shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Naming }.new
end
context "get Ruby class from the Elasticsearch type" do
should "get a simple class" do
assert_equal Foobar, subject.__get_klass_from_type('foobar')
end
should "get a camelcased class" do
assert_equal FooBar, subject.__get_klass_from_type('foo_bar')
end
should "get a namespaced class" do
assert_equal Foo::Bar, subject.__get_klass_from_type('foo/bar')
end
should "re-raise a NameError exception" do
assert_raise NameError do
subject.__get_klass_from_type('foobarbazbam')
end
end
end
context "get Elasticsearch type from the Ruby class" do
should "encode a simple class" do
assert_equal 'foobar', subject.__get_type_from_class(Foobar)
end
should "encode a camelcased class" do
assert_equal 'foo_bar', subject.__get_type_from_class(FooBar)
end
should "encode a namespaced class" do
assert_equal 'foo/bar', subject.__get_type_from_class(Foo::Bar)
end
end
context "get an ID from the document" do
should "get an ID from Hash" do
assert_equal 1, subject.__get_id_from_document(id: 1)
assert_equal 1, subject.__get_id_from_document(_id: 1)
assert_equal 1, subject.__get_id_from_document('id' => 1)
assert_equal 1, subject.__get_id_from_document('_id' => 1)
end
end
context "extract an ID from the document" do
should "delete the key from theHash" do
d1 = { :id => 1 }
d2 = { :_id => 1 }
d3 = { 'id' => 1 }
d4 = { '_id' => 1 }
assert_equal 1, subject.__extract_id_from_document(d1)
assert_nil d1[:id]
assert_equal 1, subject.__extract_id_from_document(d2)
assert_nil d1[:_id]
assert_equal 1, subject.__extract_id_from_document(d3)
assert_nil d1['id']
assert_equal 1, subject.__extract_id_from_document(d4)
assert_nil d1['_id']
end
end
context "document class name" do
should "be nil by default" do
assert_nil subject.klass
end
should "be settable" do
subject.klass = Foobar
assert_equal Foobar, subject.klass
end
should "be settable by DSL" do
subject.klass Foobar
assert_equal Foobar, subject.klass
end
end
context "index_name" do
should "default to the class name" do
subject.instance_eval do
def self.class
'FakeRepository'
end
end
assert_equal 'fake_repository', subject.index_name
end
should "be settable" do
subject.index_name = 'foobar1'
assert_equal 'foobar1', subject.index_name
subject.index_name 'foobar2'
assert_equal 'foobar2', subject.index_name
end
should "be aliased as `index`" do
subject.index_name = 'foobar1'
assert_equal 'foobar1', subject.index
end
should "be inferred from the host class" do
class ::MySpecialRepository; end
subject.define_singleton_method(:host) { MySpecialRepository }
assert_equal 'my_special_repository', subject.index_name
end
end
context "document_type" do
should "be nil when no klass is set" do
assert_equal nil, subject.document_type
end
should "default to klass" do
subject.klass Foobar
assert_equal 'foobar', subject.document_type
end
should "be aliased as `type`" do
subject.klass Foobar
assert_equal 'foobar', subject.type
end
should "be settable" do
subject.document_type = 'foobar'
assert_equal 'foobar', subject.document_type
end
should "be settable by DSL" do
subject.document_type 'foobar'
assert_equal 'foobar', subject.document_type
end
end
end
end