rsolr 1.0.8 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/spec/api/xml_spec.rb CHANGED
@@ -1,121 +1,221 @@
1
1
  require 'spec_helper'
2
+ require 'builder'
3
+ require 'nokogiri'
2
4
  describe "RSolr::Xml" do
3
5
 
4
6
  let(:generator){ RSolr::Xml::Generator.new }
7
+
8
+ builder_engines = {
9
+ :builder => { :val => false, :class => Builder::XmlMarkup, :engine => Builder::XmlMarkup.new(:indent => 0, :margin => 0, :encoding => 'UTF-8') },
10
+ :nokogiri => { :val => true, :class => Nokogiri::XML::Builder, :engine => Nokogiri::XML::Builder.new }
11
+ }
12
+
13
+ [:builder,:nokogiri].each do |engine_name|
14
+ describe engine_name do
15
+ before :all do
16
+ @engine = builder_engines[engine_name]
17
+ @old_ng_setting = RSolr::Xml::Generator.use_nokogiri
18
+ RSolr::Xml::Generator.use_nokogiri = @engine[:val]
19
+ end
20
+
21
+ after :all do
22
+ RSolr::Xml::Generator.use_nokogiri = @old_ng_setting
23
+ end
5
24
 
6
- # call all of the simple methods...
7
- # make sure the xml string is valid
8
- # ensure the class is actually Solr::XML
9
- [:optimize, :rollback, :commit].each do |meth|
10
- it "#{meth} should generator xml" do
11
- result = generator.send(meth)
12
- result.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?><#{meth}/>"
13
- end
14
- end
25
+ before :each do
26
+ builder_engines.each_pair do |name,spec|
27
+ expect(spec[:class]).not_to receive(:new) unless name == engine_name
28
+ end
29
+ end
30
+
31
+ context :xml_engine do
32
+ it "should use #{engine_name}" do
33
+ expect(@engine[:class]).to receive(:new).and_return(@engine[:engine])
34
+ generator.send(:commit)
35
+ end
36
+ end
37
+
38
+ # call all of the simple methods...
39
+ # make sure the xml string is valid
40
+ # ensure the class is actually Solr::XML
41
+ [:optimize, :rollback, :commit].each do |meth|
42
+ it "#{meth} should generator xml" do
43
+ result = generator.send(meth)
44
+ expect(result).to eq("<?xml version=\"1.0\" encoding=\"UTF-8\"?><#{meth}/>")
45
+ end
46
+ end
47
+
48
+ context :add do
49
+
50
+ it 'should yield a Message::Document object when #add is called with a block' do
51
+ documents = [{:id=>1, :name=>'sam', :cat=>['cat 1', 'cat 2']}]
52
+ add_attrs = {:boost=>200.00}
53
+ result = generator.add(documents, add_attrs) do |doc|
54
+ doc.field_by_name(:name).attrs[:boost] = 10
55
+ expect(doc.fields.size).to eq(4)
56
+ expect(doc.fields_by_name(:cat).size).to eq(2)
57
+ end
58
+ expect(result).to match(%r(name="cat">cat 1</field>))
59
+ expect(result).to match(%r(name="cat">cat 2</field>))
60
+ expect(result).to match(%r(<add boost="200.0">))
61
+ expect(result).to match(%r(boost="10"))
62
+ expect(result).to match(%r(<field name="id">1</field>))
63
+ end
64
+
65
+ it 'should work for values that yield enumerators' do
66
+ documents = [{id: 1, cat: ['cat 1', 'cat 2'].to_enum}]
67
+ result = generator.add(documents)
68
+
69
+ expect(result).to match(%r(name="cat">cat 1</field>))
70
+ expect(result).to match(%r(name="cat">cat 2</field>))
71
+ end
72
+
73
+ # add a single hash ("doc")
74
+ it 'should create an add from a hash' do
75
+ data = {
76
+ :id=>1,
77
+ :name=>'matt'
78
+ }
79
+ result = generator.add(data)
80
+ expect(result).to match(/<field name="name">matt<\/field>/)
81
+ expect(result).to match(/<field name="id">1<\/field>/)
82
+ end
83
+
84
+ # add an array of hashes
85
+ it 'should create many adds from an array of hashes' do
86
+ data = [
87
+ {
88
+ :id=>1,
89
+ :name=>'matt'
90
+ },
91
+ {
92
+ :id=>2,
93
+ :name=>'sam'
94
+ }
95
+ ]
96
+ message = generator.add(data)
97
+ expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><add><doc><field name=\"id\">1</field><field name=\"name\">matt</field></doc><doc><field name=\"id\">2</field><field name=\"name\">sam</field></doc></add>"
98
+ expect(message).to match(/<field name="name">matt<\/field>/)
99
+ expect(message).to match(/<field name="name">sam<\/field>/)
100
+ end
101
+
102
+ # multiValue field support test, thanks to Fouad Mardini!
103
+ it 'should create multiple fields from array values' do
104
+ data = {
105
+ :id => 1,
106
+ :name => ['matt1', 'matt2']
107
+ }
108
+ result = generator.add(data)
109
+ expect(result).to match(/<field name="name">matt1<\/field>/)
110
+ expect(result).to match(/<field name="name">matt2<\/field>/)
111
+ end
112
+
113
+ it 'should allow for objects which can be casted to an array' do
114
+ name = double("name", to_ary: ['matt1', 'matt2'])
115
+ data = {
116
+ :id => 1,
117
+ :name => name
118
+ }
119
+ result = generator.add(data)
120
+ expect(result).to match(/<field name="name">matt1<\/field>/)
121
+ expect(result).to match(/<field name="name">matt2<\/field>/)
122
+ end
123
+
124
+ it 'should create an add from a single Message::Document' do
125
+ document = RSolr::Xml::Document.new
126
+ document.add_field('id', 1)
127
+ document.add_field('name', 'matt', :boost => 2.0)
128
+ result = generator.add(document)
129
+ expect(result).to match(Regexp.escape('<?xml version="1.0" encoding="UTF-8"?>'))
130
+ expect(result).to match(/<field name="id">1<\/field>/)
131
+ expect(result).to match Regexp.escape('boost="2.0"')
132
+ expect(result).to match Regexp.escape('name="name"')
133
+ expect(result).to match Regexp.escape('matt</field>')
134
+ end
135
+
136
+ it 'should create adds from multiple Message::Documents' do
137
+ documents = (1..2).map do |i|
138
+ doc = RSolr::Xml::Document.new
139
+ doc.add_field('id', i)
140
+ doc.add_field('name', "matt#{i}")
141
+ doc
142
+ end
143
+ result = generator.add(documents)
144
+ expect(result).to match(/<field name="name">matt1<\/field>/)
145
+ expect(result).to match(/<field name="name">matt2<\/field>/)
146
+ end
147
+
148
+ end
15
149
 
16
- context :add do
150
+ context :delete_by_id do
151
+
152
+ it 'should create a doc id delete' do
153
+ expect(generator.delete_by_id(10)).to eq("<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><id>10</id></delete>")
154
+ end
155
+
156
+ it 'should create many doc id deletes' do
157
+ expect(generator.delete_by_id([1, 2, 3])).to eq("<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><id>1</id><id>2</id><id>3</id></delete>")
158
+ end
17
159
 
18
- it 'should yield a Message::Document object when #add is called with a block' do
19
- documents = [{:id=>1, :name=>'sam', :cat=>['cat 1', 'cat 2']}]
20
- add_attrs = {:boost=>200.00}
21
- result = generator.add(documents, add_attrs) do |doc|
22
- doc.field_by_name(:name).attrs[:boost] = 10
23
- doc.fields.size.should == 4
24
- doc.fields_by_name(:cat).size.should == 2
25
160
  end
26
- result.should match(%r(name="cat">cat 1</field>))
27
- result.should match(%r(name="cat">cat 2</field>))
28
- result.should match(%r(<add boost="200.0">))
29
- result.should match(%r(boost="10"))
30
- result.should match(%r(<field name="id">1</field>))
31
- end
161
+
162
+ context :delete_by_query do
163
+ it 'should create a query delete' do
164
+ expect(generator.delete_by_query('status:"LOST"')).to eq("<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query>status:\"LOST\"</query></delete>")
165
+ end
32
166
 
33
- # add a single hash ("doc")
34
- it 'should create an add from a hash' do
167
+ it 'should create many query deletes' do
168
+ expect(generator.delete_by_query(['status:"LOST"', 'quantity:0'])).to eq("<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query>status:\"LOST\"</query><query>quantity:0</query></delete>")
169
+ end
170
+ end
171
+
172
+ end
173
+ end
174
+
175
+ context :formatting do
176
+ it 'should format date objects into ISO 8601' do
35
177
  data = {
36
- :id=>1,
37
- :name=>'matt'
178
+ dt: Date.new(1992, 03, 15)
38
179
  }
39
180
  result = generator.add(data)
40
- result.should match(/<field name="name">matt<\/field>/)
41
- result.should match(/<field name="id">1<\/field>/)
181
+ expect(result).to match(/<field name="dt">1992-03-15T00:00:00Z<\/field>/)
42
182
  end
43
183
 
44
- # add an array of hashes
45
- it 'should create many adds from an array of hashes' do
46
- data = [
47
- {
48
- :id=>1,
49
- :name=>'matt'
50
- },
51
- {
52
- :id=>2,
53
- :name=>'sam'
54
- }
55
- ]
56
- message = generator.add(data)
57
- expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><add><doc><field name=\"id\">1</field><field name=\"name\">matt</field></doc><doc><field name=\"id\">2</field><field name=\"name\">sam</field></doc></add>"
58
- message.should match(/<field name="name">matt<\/field>/)
59
- message.should match(/<field name="name">sam<\/field>/)
184
+ it 'should format time objects into ISO 8601' do
185
+ data = {
186
+ dt: Time.new(1992, 03, 15, 16, 23, 55, 3600)
187
+ }
188
+ result = generator.add(data)
189
+ expect(result).to match(/<field name="dt">1992-03-15T15:23:55Z<\/field>/)
60
190
  end
61
191
 
62
- # multiValue field support test, thanks to Fouad Mardini!
63
- it 'should create multiple fields from array values' do
192
+ it 'should format datetime objects into ISO 8601' do
64
193
  data = {
65
- :id => 1,
66
- :name => ['matt1', 'matt2']
194
+ dt: DateTime.new(1992, 03, 15, 16, 23, 55, '+1')
67
195
  }
68
196
  result = generator.add(data)
69
- result.should match(/<field name="name">matt1<\/field>/)
70
- result.should match(/<field name="name">matt2<\/field>/)
197
+ expect(result).to match(/<field name="dt">1992-03-15T15:23:55Z<\/field>/)
71
198
  end
72
199
 
73
- it 'should create an add from a single Message::Document' do
74
- document = RSolr::Xml::Document.new
75
- document.add_field('id', 1)
76
- document.add_field('name', 'matt', :boost => 2.0)
77
- result = generator.add(document)
78
- result.should match(Regexp.escape('<?xml version="1.0" encoding="UTF-8"?>'))
79
- result.should match(/<field name="id">1<\/field>/)
80
- result.should match Regexp.escape('boost="2.0"')
81
- result.should match Regexp.escape('name="name"')
82
- result.should match Regexp.escape('matt</field>')
200
+ it 'passes through other values' do
201
+ data = {
202
+ whatever: 'some string'
203
+ }
204
+
205
+ result = generator.add(data)
206
+ expect(result).to match(/<field name="whatever">some string<\/field>/)
83
207
  end
84
-
85
- it 'should create adds from multiple Message::Documents' do
86
- documents = (1..2).map do |i|
87
- doc = RSolr::Xml::Document.new
88
- doc.add_field('id', i)
89
- doc.add_field('name', "matt#{i}")
90
- doc
208
+
209
+ # rails monkey-patches String to add a #to_time casting..
210
+ context 'with rails monkey patching' do
211
+ it 'passes through string values' do
212
+ data = {
213
+ whatever: double(to_s: 'some string', to_time: nil)
214
+ }
215
+
216
+ result = generator.add(data)
217
+ expect(result).to match(/<field name="whatever">some string<\/field>/)
91
218
  end
92
- result = generator.add(documents)
93
- result.should match(/<field name="name">matt1<\/field>/)
94
- result.should match(/<field name="name">matt2<\/field>/)
95
219
  end
96
-
97
- end
98
-
99
- context :delete_by_id do
100
-
101
- it 'should create a doc id delete' do
102
- generator.delete_by_id(10).should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><id>10</id></delete>"
103
- end
104
-
105
- it 'should create many doc id deletes' do
106
- generator.delete_by_id([1, 2, 3]).should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><id>1</id><id>2</id><id>3</id></delete>"
107
- end
108
-
109
220
  end
110
-
111
- context :delete_by_query do
112
- it 'should create a query delete' do
113
- generator.delete_by_query('status:"LOST"').should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query>status:\"LOST\"</query></delete>"
114
- end
115
-
116
- it 'should create many query deletes' do
117
- generator.delete_by_query(['status:"LOST"', 'quantity:0']).should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?><delete><query>status:\"LOST\"</query><query>quantity:0</query></delete>"
118
- end
119
- end
120
-
121
- end
221
+ end
@@ -0,0 +1 @@
1
+ {"initArgs":{},"managedList":[]}
@@ -0,0 +1,67 @@
1
+ <?xml version="1.0" ?>
2
+ <!--
3
+ Licensed to the Apache Software Foundation (ASF) under one or more
4
+ contributor license agreements. See the NOTICE file distributed with
5
+ this work for additional information regarding copyright ownership.
6
+ The ASF licenses this file to You under the Apache License, Version 2.0
7
+ (the "License"); you may not use this file except in compliance with
8
+ the License. You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
+ -->
18
+
19
+ <!-- Example exchange rates file for CurrencyField type named "currency" in example schema -->
20
+
21
+ <currencyConfig version="1.0">
22
+ <rates>
23
+ <!-- Updated from http://www.exchangerate.com/ at 2011-09-27 -->
24
+ <rate from="USD" to="ARS" rate="4.333871" comment="ARGENTINA Peso" />
25
+ <rate from="USD" to="AUD" rate="1.025768" comment="AUSTRALIA Dollar" />
26
+ <rate from="USD" to="EUR" rate="0.743676" comment="European Euro" />
27
+ <rate from="USD" to="BRL" rate="1.881093" comment="BRAZIL Real" />
28
+ <rate from="USD" to="CAD" rate="1.030815" comment="CANADA Dollar" />
29
+ <rate from="USD" to="CLP" rate="519.0996" comment="CHILE Peso" />
30
+ <rate from="USD" to="CNY" rate="6.387310" comment="CHINA Yuan" />
31
+ <rate from="USD" to="CZK" rate="18.47134" comment="CZECH REP. Koruna" />
32
+ <rate from="USD" to="DKK" rate="5.515436" comment="DENMARK Krone" />
33
+ <rate from="USD" to="HKD" rate="7.801922" comment="HONG KONG Dollar" />
34
+ <rate from="USD" to="HUF" rate="215.6169" comment="HUNGARY Forint" />
35
+ <rate from="USD" to="ISK" rate="118.1280" comment="ICELAND Krona" />
36
+ <rate from="USD" to="INR" rate="49.49088" comment="INDIA Rupee" />
37
+ <rate from="USD" to="XDR" rate="0.641358" comment="INTNL MON. FUND SDR" />
38
+ <rate from="USD" to="ILS" rate="3.709739" comment="ISRAEL Sheqel" />
39
+ <rate from="USD" to="JPY" rate="76.32419" comment="JAPAN Yen" />
40
+ <rate from="USD" to="KRW" rate="1169.173" comment="KOREA (SOUTH) Won" />
41
+ <rate from="USD" to="KWD" rate="0.275142" comment="KUWAIT Dinar" />
42
+ <rate from="USD" to="MXN" rate="13.85895" comment="MEXICO Peso" />
43
+ <rate from="USD" to="NZD" rate="1.285159" comment="NEW ZEALAND Dollar" />
44
+ <rate from="USD" to="NOK" rate="5.859035" comment="NORWAY Krone" />
45
+ <rate from="USD" to="PKR" rate="87.57007" comment="PAKISTAN Rupee" />
46
+ <rate from="USD" to="PEN" rate="2.730683" comment="PERU Sol" />
47
+ <rate from="USD" to="PHP" rate="43.62039" comment="PHILIPPINES Peso" />
48
+ <rate from="USD" to="PLN" rate="3.310139" comment="POLAND Zloty" />
49
+ <rate from="USD" to="RON" rate="3.100932" comment="ROMANIA Leu" />
50
+ <rate from="USD" to="RUB" rate="32.14663" comment="RUSSIA Ruble" />
51
+ <rate from="USD" to="SAR" rate="3.750465" comment="SAUDI ARABIA Riyal" />
52
+ <rate from="USD" to="SGD" rate="1.299352" comment="SINGAPORE Dollar" />
53
+ <rate from="USD" to="ZAR" rate="8.329761" comment="SOUTH AFRICA Rand" />
54
+ <rate from="USD" to="SEK" rate="6.883442" comment="SWEDEN Krona" />
55
+ <rate from="USD" to="CHF" rate="0.906035" comment="SWITZERLAND Franc" />
56
+ <rate from="USD" to="TWD" rate="30.40283" comment="TAIWAN Dollar" />
57
+ <rate from="USD" to="THB" rate="30.89487" comment="THAILAND Baht" />
58
+ <rate from="USD" to="AED" rate="3.672955" comment="U.A.E. Dirham" />
59
+ <rate from="USD" to="UAH" rate="7.988582" comment="UKRAINE Hryvnia" />
60
+ <rate from="USD" to="GBP" rate="0.647910" comment="UNITED KINGDOM Pound" />
61
+
62
+ <!-- Cross-rates for some common currencies -->
63
+ <rate from="EUR" to="GBP" rate="0.869914" />
64
+ <rate from="EUR" to="NOK" rate="7.800095" />
65
+ <rate from="GBP" to="NOK" rate="8.966508" />
66
+ </rates>
67
+ </currencyConfig>
@@ -0,0 +1,54 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright ownership.
4
+ # The ASF licenses this file to You under the Apache License, Version 2.0
5
+ # (the "License"); you may not use this file except in compliance with
6
+ # the License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ # a couple of test stopwords to test that the words are really being
17
+ # configured from this file:
18
+ stopworda
19
+ stopwordb
20
+
21
+ # Standard english stop words taken from Lucene's StopAnalyzer
22
+ a
23
+ an
24
+ and
25
+ are
26
+ as
27
+ at
28
+ be
29
+ but
30
+ by
31
+ for
32
+ if
33
+ in
34
+ into
35
+ is
36
+ it
37
+ no
38
+ not
39
+ of
40
+ on
41
+ or
42
+ such
43
+ that
44
+ the
45
+ their
46
+ then
47
+ there
48
+ these
49
+ they
50
+ this
51
+ to
52
+ was
53
+ will
54
+ with
@@ -0,0 +1,21 @@
1
+ # The ASF licenses this file to You under the Apache License, Version 2.0
2
+ # (the "License"); you may not use this file except in compliance with
3
+ # the License. You may obtain a copy of the License at
4
+ #
5
+ # http://www.apache.org/licenses/LICENSE-2.0
6
+ #
7
+ # Unless required by applicable law or agreed to in writing, software
8
+ # distributed under the License is distributed on an "AS IS" BASIS,
9
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # See the License for the specific language governing permissions and
11
+ # limitations under the License.
12
+
13
+ #-----------------------------------------------------------------------
14
+ # Use a protected word file to protect against the stemmer reducing two
15
+ # unrelated words to the same base word.
16
+
17
+ # Some non-words that normally won't be encountered,
18
+ # just to test that they won't be stemmed.
19
+ dontstems
20
+ zwhacky
21
+