primedia-endeca 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ describe Endeca::Dimension do
4
+ describe ".new" do
5
+ it "should set the raw attribute" do
6
+ dimension = Endeca::Dimension.new(:raw)
7
+
8
+ dimension.raw.should == :raw
9
+ end
10
+ end
11
+
12
+ describe "#inspect" do
13
+ before do
14
+ @dimension = Endeca::Dimension.new
15
+ end
16
+
17
+ it "should include the class" do
18
+ @dimension.inspect.should include(Endeca::Dimension.name)
19
+ end
20
+
21
+ it "should include the hex formatted object_id" do
22
+ id = 123
23
+ @dimension.stub!(:object_id).and_return(id)
24
+ @dimension.inspect.should include("0x#{id.to_s(16)}")
25
+ end
26
+
27
+ it "should include the id" do
28
+ id = 123
29
+ @dimension.stub!(:id).and_return(id)
30
+ @dimension.inspect.should include("id=#{id}")
31
+ end
32
+
33
+ it "should include the inspected name" do
34
+ name = 'name'
35
+ @dimension.stub!(:name).and_return(name)
36
+ @dimension.inspect.should include("name=#{name.inspect}")
37
+ end
38
+ end
39
+
40
+ describe "#==" do
41
+ it "should compare ids" do
42
+ dim_1 = Endeca::Dimension.new
43
+ dim_2 = Endeca::Dimension.new
44
+ dim_2.stub!(:id).and_return(dim_1.id)
45
+
46
+ (dim_1 == dim_2).should be_true
47
+ end
48
+ end
49
+
50
+ describe "#<=>" do
51
+ it "should compare names" do
52
+ name = mock('name')
53
+
54
+ dim_1 = Endeca::Dimension.new
55
+ dim_2 = Endeca::Dimension.new
56
+
57
+ dim_1.stub!(:name).and_return(name)
58
+ dim_2.stub!(:name).and_return(name)
59
+
60
+ name.should_receive(:<=>).with(name)
61
+
62
+ dim_1 <=> dim_2
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,116 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ describe Endeca::DocumentCollection do
4
+ before do
5
+ @metainfo = mock('MetaInfo', :[] => nil)
6
+ @document = mock('Document', :[] => nil)
7
+ @refinement = mock('Refinement', :[] => [])
8
+ @raw = {
9
+ 'Records' => [@document],
10
+ 'MetaInfo' => @metainfo,
11
+ 'Refinements' => [@refinement]
12
+ }
13
+ @document_collection = Endeca::DocumentCollection.new(@raw)
14
+ end
15
+
16
+ describe '#new' do
17
+ it "should store the raw data" do
18
+ @document_collection.raw.should == @raw
19
+ end
20
+ end
21
+
22
+ describe '#attributes' do
23
+ it "should return the metainfo hash of the collection" do
24
+ @document_collection.attributes.should == @metainfo
25
+ end
26
+ end
27
+
28
+ describe '#documents' do
29
+ it "should return a collection of documents" do
30
+ @document_collection.documents.size.should == 1
31
+ @document_collection.documents.first.should == Endeca::Document.new(@document)
32
+ end
33
+
34
+ it "should return the correct document class for subclassees" do
35
+ class ConcreteDocument < Endeca::Document; end
36
+ collection = Endeca::DocumentCollection.new(@raw, ConcreteDocument)
37
+ collection.documents.first.class.should == ConcreteDocument
38
+ end
39
+ end
40
+
41
+ describe "#previous_page" do
42
+ describe "when on the first page" do
43
+ before do
44
+ @document_collection.stub!(:current_page).and_return(0)
45
+ end
46
+
47
+ it "should be nil" do
48
+ @document_collection.previous_page.should be_nil
49
+ end
50
+ end
51
+
52
+ describe "when on any other page" do
53
+ before do
54
+ @document_collection.stub!(:current_page).and_return(4)
55
+ end
56
+
57
+ it "should return the previous page" do
58
+ @document_collection.previous_page.should == 3
59
+ end
60
+ end
61
+ end
62
+
63
+ describe "#next_page" do
64
+ before do
65
+ @document_collection.stub!(:total_pages).and_return(20)
66
+ end
67
+
68
+ describe "when on the last page" do
69
+ before do
70
+ @document_collection.stub!(:current_page).and_return(20)
71
+ end
72
+
73
+ it "should be nil" do
74
+ @document_collection.next_page.should be_nil
75
+ end
76
+ end
77
+
78
+ describe "when on any other page" do
79
+ before do
80
+ @document_collection.stub!(:current_page).and_return(4)
81
+ end
82
+
83
+ it "should return the next page" do
84
+ @document_collection.next_page.should == 5
85
+ end
86
+ end
87
+ end
88
+
89
+ describe '#refinements' do
90
+ it 'should return a collection of Refinement objects' do
91
+ @document_collection.refinements.should == [Endeca::Refinement.new(@refinement)]
92
+ end
93
+ end
94
+
95
+ describe "#each" do
96
+ it "should yield to the documents" do
97
+ block = lambda{1}
98
+ @document_collection.documents.should_receive(:each).with(&block)
99
+ @document_collection.each(&block)
100
+ end
101
+ end
102
+
103
+ describe "#is_a?" do
104
+ it "should be true if the compared class is Array" do
105
+ @document_collection.is_a?(Array).should be_true
106
+ end
107
+
108
+ it "should be true if the compared class is Endeca::DocumentCollection" do
109
+ @document_collection.is_a?(Endeca::DocumentCollection).should be_true
110
+ end
111
+
112
+ it "should be false if the compared class is String" do
113
+ @document_collection.is_a?(String).should be_false
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,290 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ describe Endeca::Document do
4
+ before do
5
+ Endeca::Document.path 'http://endeca.example.com'
6
+ @document = Endeca::Document.new
7
+ end
8
+
9
+ describe '.map' do
10
+ after do
11
+ Endeca::Document.mappings = {}
12
+ end
13
+
14
+ describe "with a mapping value" do
15
+ it "should assign the mapping with a nil transformation" do
16
+ Endeca::Document.map(:id => :endecaID)
17
+ map = Endeca::Map.new :id, :endecaID
18
+ Endeca::Document.mappings.should include({:id => map})
19
+ end
20
+ end
21
+
22
+ describe "with two maps that join on the same key in a parent hash" do
23
+ it "should join the key and value on the delimiter" do
24
+ Endeca::Document.map(:state => :propertystate).into(:ntk => :ntt)
25
+ Endeca::Document.map(:city => :propertycity).into(:ntk => :ntt)
26
+ [
27
+ {:ntk=>"propertycity|propertystate", :ntt=>"Atlanta|Georgia"},
28
+ {:ntk=>"propertystate|propertycity", :ntt=>"Georgia|Atlanta"}
29
+ ].should include(Endeca::Document.transform_query_options(:city => 'Atlanta', :state => 'Georgia'))
30
+ end
31
+ end
32
+
33
+ describe "with two maps that join on the same key without mapping to a hash" do
34
+ it "should join on the delimiter" do
35
+ Endeca::Document.map(:limit => :recs_per_page).into(:M)
36
+ Endeca::Document.map(:expand_refinements => :expand_all_dims).into(:M)
37
+
38
+ # Hashes are not ordered and order is not important
39
+ [{:M => "recs_per_page:10|expand_all_dims:1"}, {:M => "expand_all_dims:1|recs_per_page:10"}].
40
+ should include(Endeca::Document.transform_query_options(:limit => 10, :expand_refinements => 1))
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '.find' do
46
+ before do
47
+ @query_options = {:foo => :bar}
48
+ end
49
+
50
+ describe '#==' do
51
+ it "should compare documents by id" do
52
+ id = 1
53
+ doc_1 = Endeca::Document.new
54
+ doc_1.stub!(:id).and_return(id)
55
+ doc_2 = Endeca::Document.new
56
+ doc_2.stub!(:id).and_return(id)
57
+
58
+ (doc_1 == doc_2).should be_true
59
+
60
+ doc_2.stub!(:id).and_return(2)
61
+
62
+ (doc_1 == doc_2).should be_false
63
+ end
64
+ end
65
+
66
+ describe '#inspect' do
67
+ it "includes the class name" do
68
+ @document.inspect.should include(Endeca::Document.name)
69
+ end
70
+
71
+ it "includes the hex string of the object id" do
72
+ @document.inspect.should include("0x#{@document.object_id.to_s(16)}")
73
+ end
74
+ end
75
+
76
+ describe "#dimensions" do
77
+ describe "with a nil Dimensions hash" do
78
+ it "should be empty" do
79
+ @document.stub!(:raw).and_return({})
80
+ @document.dimensions.should be_empty
81
+ end
82
+ end
83
+
84
+ describe 'with a raw Dimensions hash with a key "key" with one element' do
85
+ it 'should return a hash with that dimension at the key "key"' do
86
+ dimension_hash = mock('Dimension Hash')
87
+ dimension = mock(Endeca::Dimension)
88
+
89
+ @document.
90
+ stub!(:raw).
91
+ and_return({'Dimensions' => {"key" => dimension_hash}})
92
+
93
+ Endeca::Dimension.
94
+ should_receive(:new).
95
+ with(dimension_hash).
96
+ and_return(dimension)
97
+
98
+ @document.dimensions["key"].should == dimension
99
+ end
100
+ end
101
+ end
102
+
103
+ describe 'with :first' do
104
+ it 'should call .first and pass the query options' do
105
+ Endeca::Document.should_receive(:first).with(@query_options)
106
+ Endeca::Document.find(:first, @query_options)
107
+ end
108
+ end
109
+
110
+ describe 'with :all' do
111
+ it "should call .all and pass the query options and self" do
112
+ Endeca::Document.should_receive(:all).with(@query_options)
113
+ Endeca::Document.find(:all, @query_options)
114
+ end
115
+ end
116
+
117
+ describe 'with an Integer' do
118
+ it "should call .by_id with the argument and the query options" do
119
+ id = 5
120
+ Endeca::Document.should_receive(:by_id).with(id, @query_options)
121
+ Endeca::Document.find(id, @query_options)
122
+ end
123
+ end
124
+
125
+ describe 'with an id string' do
126
+ it "should call .by_id with the argument and the query options" do
127
+ id = '5'
128
+ Endeca::Document.should_receive(:by_id).with(id, @query_options)
129
+ Endeca::Document.find(id, @query_options)
130
+ end
131
+ end
132
+
133
+ describe 'with only query options' do
134
+ it 'should call .all with the query options' do
135
+ Endeca::Document.should_receive(:all).with(@query_options)
136
+ Endeca::Document.find(@query_options)
137
+ end
138
+ end
139
+ end
140
+
141
+ describe ".first" do
142
+ it "should make a request with the correct query" do
143
+ Endeca::Request.
144
+ should_receive(:perform).
145
+ with('http://endeca.example.com', {:id => '1234'}).
146
+ and_return({'Records' => []})
147
+
148
+ Endeca::Document.first(:id => '1234')
149
+ end
150
+
151
+ it "should instantiate a new Endeca::Document from the first record in the response hash" do
152
+ hash = {'Records' => [:document]}
153
+ Endeca::Request.stub!(:perform).and_return(hash)
154
+ Endeca::Document.
155
+ should_receive(:new).
156
+ with(:document)
157
+ Endeca::Document.first(:id => '1234')
158
+ end
159
+
160
+ describe "when no matching record is returned" do
161
+ it "should be nil" do
162
+ Endeca::Request.stub!(:perform).and_return({'Records' => nil})
163
+ Endeca::Document.first(:id => '1234').should be_nil
164
+ end
165
+ end
166
+ end
167
+
168
+ describe '.by_id' do
169
+ it 'should merge the id into the query options and call .first' do
170
+ Endeca::Document.should_receive(:first).with(:id => '1234')
171
+ Endeca::Document.by_id('1234')
172
+ end
173
+ end
174
+
175
+ describe '.all' do
176
+ before do
177
+ @query_options = {:foo => :bar}
178
+ @records = mock('RecordArray')
179
+ @response = {'Records' => @records}
180
+ Endeca::Document.
181
+ stub!(:request).
182
+ and_return(@response)
183
+ end
184
+
185
+ it 'should perform a request with the query options' do
186
+ Endeca::Document.
187
+ should_receive(:request).
188
+ with(@query_options).
189
+ and_return(@response)
190
+
191
+ Endeca::Document.all(@query_options)
192
+ end
193
+
194
+ it 'should create a new DocumentCollection from the full response and the document class' do
195
+ Endeca::DocumentCollection.should_receive(:new).with(@response, Endeca::Document)
196
+ Endeca::Document.all(@query_options)
197
+ end
198
+ end
199
+
200
+ describe ".transform_query_options" do
201
+ before do
202
+ @query_options = {
203
+ :id => 5
204
+ }
205
+ end
206
+
207
+ after do
208
+ Endeca::Document.mappings = {}
209
+ end
210
+
211
+ describe "when there is no mapping for the given key" do
212
+ it "returns the query options without modification" do
213
+ Endeca::Document.transform_query_options(@query_options).
214
+ should == @query_options
215
+ end
216
+ end
217
+
218
+ describe "when there is a mapping with" do
219
+ describe "a new key" do
220
+ before do
221
+ Endeca::Document.map(:id => :new_id)
222
+ end
223
+
224
+ it "should replace the key with the new key" do
225
+ new_query_options = Endeca::Document.
226
+ transform_query_options(@query_options)
227
+
228
+ new_query_options.should == {:new_id => 5}
229
+ end
230
+ end
231
+
232
+ describe "a new key and a value transformation" do
233
+ before do
234
+ Endeca::Document.map(:id => :new_id).transform {|id| id.to_s}
235
+ end
236
+
237
+ it "should replace the value with the transformed value" do
238
+ Endeca::Document.transform_query_options(@query_options).
239
+ should == {:new_id => "5"}
240
+ end
241
+
242
+ describe "that includes the current value" do
243
+ before do
244
+ Endeca::Document.map(:name => :new_name) do |name, current|
245
+ [current,name].compact.join('|')
246
+ end
247
+
248
+ @current_query_options = {:name => 'bar', :new_name => 'foo'}
249
+ end
250
+
251
+ it "should transform the new value with the current value" do
252
+ pending("Not sure of the use case.") do
253
+ expected_name = "foo|bar"
254
+ Endeca::Document.transform_query_options(@current_query_options).
255
+ should == {:new_name => expected_name}
256
+ end
257
+ end
258
+ end
259
+ end
260
+ end
261
+ end
262
+
263
+ describe "with default query parameters" do
264
+ before do
265
+ Endeca::Document.default_params :foo => :bar
266
+ end
267
+
268
+ after do
269
+ Endeca::Document.default_params({})
270
+ end
271
+
272
+ it "should get the default params" do
273
+ Endeca::Document.get_default_params.should == {:foo => :bar}
274
+ end
275
+
276
+ it "should include the default parameters" do
277
+ Endeca::Request.should_receive(:perform).with(anything, {:foo => :bar})
278
+ Endeca::Document.request({})
279
+ end
280
+
281
+ describe "that are mapped" do
282
+ it "should map the default parameters" do
283
+ Endeca::Document.map(:foo => :new_foo)
284
+ Endeca::Request.should_receive(:perform).with(anything, {:new_foo => :bar})
285
+ Endeca::Document.request({})
286
+ end
287
+ end
288
+ end
289
+
290
+ end