Skip to content

Commit 2f9f7a5

Browse files
committed
[MODEL] Cleaned up the tests for multiple fields/properties in mapping DSL
This removes the dynamic definitions of tests from 3792616 and makes the tests more physical. Related: elastic#138
1 parent 3792616 commit 2f9f7a5

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

elasticsearch-model/lib/elasticsearch/model/indexing.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def initialize(type, options={})
4747
@mapping = {}
4848
end
4949

50-
def indexes(name, options = {}, &block)
50+
def indexes(name, options={}, &block)
5151
@mapping[name] = options
5252

5353
if block_given?
@@ -66,7 +66,6 @@ def indexes(name, options = {}, &block)
6666
end
6767

6868
# Set the type to `string` by default
69-
#
7069
@mapping[name][:type] ||= 'string'
7170

7271
self

elasticsearch-model/test/unit/indexing_test.rb

+41-21
Original file line numberDiff line numberDiff line change
@@ -90,36 +90,56 @@ class NotFound < Exception; end
9090
assert_equal 'string', mappings.to_hash[:mytype][:properties][:bar][:type]
9191
end
9292

93-
%w(string long double boolean multi_field).each do |outer_type|
94-
should "define embedded fields for #{outer_type}" do
95-
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
96-
97-
mappings.indexes :foo, type: outer_type do
98-
indexes :raw, analyzer: 'keyword'
99-
end
93+
should "define multiple fields" do
94+
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
10095

101-
foo_mapping = mappings.to_hash[:mytype][:properties][:foo]
96+
mappings.indexes :foo_1, type: 'string' do
97+
indexes :raw, analyzer: 'keyword'
98+
end
10299

103-
assert_equal outer_type, foo_mapping[:type]
104-
assert_nil foo_mapping[:properties]
105-
assert_equal 'string', foo_mapping[:fields][:raw][:type]
100+
mappings.indexes :foo_2, type: 'multi_field' do
101+
indexes :raw, analyzer: 'keyword'
106102
end
103+
104+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_1][:type]
105+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_1][:fields][:raw][:type]
106+
assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:foo_1][:fields][:raw][:analyzer]
107+
assert_nil mappings.to_hash[:mytype][:properties][:foo_1][:properties]
108+
109+
assert_equal 'multi_field', mappings.to_hash[:mytype][:properties][:foo_2][:type]
110+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_2][:fields][:raw][:type]
111+
assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:foo_2][:fields][:raw][:analyzer]
112+
assert_nil mappings.to_hash[:mytype][:properties][:foo_2][:properties]
107113
end
108114

109-
%w(object nested).each do |outer_type|
110-
should "define embedded properties for #{outer_type}" do
111-
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
115+
should "define embedded properties" do
116+
mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
112117

113-
mappings.indexes :foo, type: outer_type do
114-
indexes :bar
115-
end
118+
mappings.indexes :foo do
119+
indexes :bar
120+
end
116121

117-
foo_mapping = mappings.to_hash[:mytype][:properties][:foo]
122+
mappings.indexes :foo_object, type: 'object' do
123+
indexes :bar
124+
end
118125

119-
assert_equal outer_type, foo_mapping[:type]
120-
assert_nil foo_mapping[:fields]
121-
assert_equal 'string', foo_mapping[:properties][:bar][:type]
126+
mappings.indexes :foo_nested, type: 'nested' do
127+
indexes :bar
122128
end
129+
130+
# Object is the default when `type` is missing and there's a block passed
131+
#
132+
assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo][:type]
133+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo][:properties][:bar][:type]
134+
assert_nil mappings.to_hash[:mytype][:properties][:foo][:fields]
135+
136+
assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo_object][:type]
137+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_object][:properties][:bar][:type]
138+
assert_nil mappings.to_hash[:mytype][:properties][:foo_object][:fields]
139+
140+
assert_equal 'nested', mappings.to_hash[:mytype][:properties][:foo_nested][:type]
141+
assert_equal 'string', mappings.to_hash[:mytype][:properties][:foo_nested][:properties][:bar][:type]
142+
assert_nil mappings.to_hash[:mytype][:properties][:foo_nested][:fields]
123143
end
124144
end
125145

0 commit comments

Comments
 (0)