endeca 1.3.7
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +37 -0
- data/README.rdoc +60 -0
- data/Rakefile +52 -0
- data/endeca.gemspec +20 -0
- data/example/benchmark.rb +13 -0
- data/example/listing.rb +33 -0
- data/lib/class_to_proc.rb +5 -0
- data/lib/core_ext.rb +106 -0
- data/lib/endeca.rb +76 -0
- data/lib/endeca/benchmarking.rb +38 -0
- data/lib/endeca/breadcrumb.rb +42 -0
- data/lib/endeca/breadcrumbs.rb +13 -0
- data/lib/endeca/dimension.rb +38 -0
- data/lib/endeca/document.rb +146 -0
- data/lib/endeca/document_collection.rb +112 -0
- data/lib/endeca/logging.rb +9 -0
- data/lib/endeca/map.rb +191 -0
- data/lib/endeca/readers.rb +93 -0
- data/lib/endeca/refinement.rb +42 -0
- data/lib/endeca/refinement_dimension.rb +32 -0
- data/lib/endeca/request.rb +88 -0
- data/lib/endeca/transformer.rb +43 -0
- data/spec/core_ext_spec.rb +134 -0
- data/spec/endeca/benchmarking_spec.rb +33 -0
- data/spec/endeca/breadcrumb_spec.rb +90 -0
- data/spec/endeca/dimension_spec.rb +91 -0
- data/spec/endeca/document_collection_spec.rb +158 -0
- data/spec/endeca/document_spec.rb +378 -0
- data/spec/endeca/map_spec.rb +122 -0
- data/spec/endeca/readers_spec.rb +118 -0
- data/spec/endeca/refinement_dimension_spec.rb +74 -0
- data/spec/endeca/refinement_spec.rb +72 -0
- data/spec/endeca/request_spec.rb +107 -0
- data/spec/endeca/transformer_spec.rb +50 -0
- data/spec/endeca_spec.rb +37 -0
- data/spec/rcov.opts +5 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +1 -0
- metadata +97 -0
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), %w[.. spec_helper])
|
2
|
+
|
3
|
+
module Endeca
|
4
|
+
module Breadcrumbs
|
5
|
+
class Navigation < Endeca::Breadcrumb
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe Endeca::Breadcrumb do
|
11
|
+
before do
|
12
|
+
@dimension_value = {
|
13
|
+
"DimValueID" => "4343565665",
|
14
|
+
"RemovalLink" => "N=",
|
15
|
+
"DimValueName" => "Apartment"
|
16
|
+
}
|
17
|
+
|
18
|
+
@navigation_hash = {
|
19
|
+
"Type" => "Navigation",
|
20
|
+
"DimensionName" => "property type",
|
21
|
+
"DimensionRemovalLink" => "N=",
|
22
|
+
"DimensionValues" => [@dimension_value]
|
23
|
+
}
|
24
|
+
|
25
|
+
@dimensions = { "Dimensions" => [@navigation_hash] }
|
26
|
+
@breadcrumb = Endeca::Breadcrumb.new( @dimensions )
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".create" do
|
30
|
+
before do
|
31
|
+
@navigation = Endeca::Breadcrumb.create(@navigation_hash)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should create a breadcrumb of the appropriate type" do
|
35
|
+
Endeca::Breadcrumb.create(@navigation_hash).
|
36
|
+
should be_a_kind_of(Endeca::Breadcrumbs::Navigation)
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "with an invalid type" do
|
40
|
+
it do
|
41
|
+
creating_invalid_breadcrumb = lambda{Endeca::Breadcrumb.create({'Type' => 'Invalid'})}
|
42
|
+
creating_invalid_breadcrumb.should raise_error(Endeca::Breadcrumbs::TypeError)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe ".to_proc" do
|
48
|
+
it "should call create" do
|
49
|
+
Endeca::Breadcrumb.should_receive(:create).with(:obj)
|
50
|
+
[:obj].map(&Endeca::Breadcrumb)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#==' do
|
55
|
+
it "should compare Breadcrumbs by name" do
|
56
|
+
doc_1, doc_2 = Endeca::Breadcrumb.new, Endeca::Breadcrumb.new
|
57
|
+
doc_1.stub!(:name).and_return('property type')
|
58
|
+
doc_2.stub!(:name).and_return('property type')
|
59
|
+
(doc_1 == doc_2).should be_true
|
60
|
+
|
61
|
+
doc_2.stub!(:name).and_return('something else')
|
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
|
+
@breadcrumb.inspect.should include(Endeca::Breadcrumb.name)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "includes the hex string of the object id" do
|
72
|
+
@breadcrumb.inspect.should include("0x#{@breadcrumb.object_id.to_s(16)}")
|
73
|
+
end
|
74
|
+
|
75
|
+
it "includes the inspected name" do
|
76
|
+
@breadcrumb.stub!(:name).and_return('A Name')
|
77
|
+
@breadcrumb.inspect.should include('name="A Name"')
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "#type" do
|
82
|
+
it "returns the Type value" do
|
83
|
+
Endeca::Breadcrumb.new('Type' => 'AType').type.should == 'AType'
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe "#name" do
|
88
|
+
it {Endeca::Breadcrumb.new.name.should == ''}
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,91 @@
|
|
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 "#to_endeca_params" do
|
13
|
+
before do
|
14
|
+
@dimension = Endeca::Dimension.new
|
15
|
+
@selection = mock('selection link')
|
16
|
+
@removal = mock('removal link')
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "with a selection link" do
|
20
|
+
it "should return the selection link" do
|
21
|
+
@dimension.stub!(:selection_link => @selection_link)
|
22
|
+
@dimension.stub!(:removal_link => nil)
|
23
|
+
|
24
|
+
@dimension.to_endeca_params.should == @selection_link
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "with a removal link" do
|
29
|
+
it "should return the selection link" do
|
30
|
+
@dimension.stub!(:selection_link => nil)
|
31
|
+
@dimension.stub!(:removal_link => @removal_link)
|
32
|
+
|
33
|
+
@dimension.to_endeca_params.should == @removal_link
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#inspect" do
|
39
|
+
before do
|
40
|
+
@dimension = Endeca::Dimension.new
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should include the class" do
|
44
|
+
@dimension.inspect.should include(Endeca::Dimension.name)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should include the hex formatted object_id" do
|
48
|
+
id = 123
|
49
|
+
@dimension.stub!(:object_id).and_return(id)
|
50
|
+
@dimension.inspect.should include("0x#{id.to_s(16)}")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should include the id" do
|
54
|
+
id = 123
|
55
|
+
@dimension.stub!(:id).and_return(id)
|
56
|
+
@dimension.inspect.should include("id=#{id}")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should include the inspected name" do
|
60
|
+
name = 'name'
|
61
|
+
@dimension.stub!(:name).and_return(name)
|
62
|
+
@dimension.inspect.should include("name=#{name.inspect}")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "#==" do
|
67
|
+
it "should compare ids" do
|
68
|
+
dim_1 = Endeca::Dimension.new
|
69
|
+
dim_2 = Endeca::Dimension.new
|
70
|
+
dim_2.stub!(:id).and_return(dim_1.id)
|
71
|
+
|
72
|
+
(dim_1 == dim_2).should be_true
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "#<=>" do
|
77
|
+
it "should compare names" do
|
78
|
+
name = mock('name')
|
79
|
+
|
80
|
+
dim_1 = Endeca::Dimension.new
|
81
|
+
dim_2 = Endeca::Dimension.new
|
82
|
+
|
83
|
+
dim_1.stub!(:name).and_return(name)
|
84
|
+
dim_2.stub!(:name).and_return(name)
|
85
|
+
|
86
|
+
name.should_receive(:<=>).with(name)
|
87
|
+
|
88
|
+
dim_1 <=> dim_2
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,158 @@
|
|
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 = { "Dimensions" => [ {
|
8
|
+
"ExpansionLink" => "N=&Ne=7",
|
9
|
+
"DimensionName" => "city",
|
10
|
+
"DimensionID" => "7" } ] }
|
11
|
+
@breadcrumb = { "Dimensions" => [ {
|
12
|
+
"DimensionName" => "property type",
|
13
|
+
"DimensionRemovalLink" => "N=",
|
14
|
+
"Type" => "Navigation" } ] }
|
15
|
+
|
16
|
+
@raw = {
|
17
|
+
'Records' => [@document],
|
18
|
+
'MetaInfo' => @metainfo,
|
19
|
+
'Refinements' => [@refinement],
|
20
|
+
'Breadcrumbs' => [@breadcrumb]
|
21
|
+
}
|
22
|
+
|
23
|
+
@aggregate_raw = {
|
24
|
+
'AggrRecords' => [{'Records' => [@document]}]
|
25
|
+
}
|
26
|
+
|
27
|
+
@document_collection = Endeca::DocumentCollection.new(@raw)
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#new' do
|
31
|
+
it "should store the raw data" do
|
32
|
+
@document_collection.raw.should == @raw
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#attributes' do
|
37
|
+
it "should return the metainfo hash of the collection" do
|
38
|
+
@document_collection.attributes.should == @metainfo
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#documents' do
|
43
|
+
it "should return the correct document class for subclassees" do
|
44
|
+
class ConcreteDocument < Endeca::Document; end
|
45
|
+
collection = Endeca::DocumentCollection.new(@raw, ConcreteDocument)
|
46
|
+
collection.documents.first.class.should == ConcreteDocument
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "with a normal record set" do
|
50
|
+
it "should return a collection of documents" do
|
51
|
+
@document_collection.documents.should == [Endeca::Document.new(@document)]
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "with an aggregate record set" do
|
56
|
+
before do
|
57
|
+
@document_collection = Endeca::DocumentCollection.new(@aggregate_raw)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should find the documents" do
|
61
|
+
@document_collection.documents.size.should == 1
|
62
|
+
@document_collection.documents.first.should == Endeca::Document.new(@document)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe "with no record set" do
|
67
|
+
before { @document_collection = Endeca::DocumentCollection.new({}) }
|
68
|
+
it { @document_collection.documents.should be_empty }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "#aggregate?" do
|
73
|
+
describe "with a normal document collection" do
|
74
|
+
it { @document_collection.aggregate?.should be_false }
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "with an aggregate document collection" do
|
78
|
+
before { @document_collection = Endeca::DocumentCollection.new(@aggregate_raw) }
|
79
|
+
it { @document_collection.aggregate?.should be_true }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "#previous_page" do
|
84
|
+
describe "when on the first page" do
|
85
|
+
before { @document_collection.stub!(:current_page).and_return(0) }
|
86
|
+
|
87
|
+
it { @document_collection.previous_page.should be_nil }
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "when on any other page" do
|
91
|
+
before do
|
92
|
+
@document_collection.stub!(:current_page).and_return(4)
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should return the previous page" do
|
96
|
+
@document_collection.previous_page.should == 3
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
describe "#next_page" do
|
102
|
+
before do
|
103
|
+
@document_collection.stub!(:total_pages).and_return(20)
|
104
|
+
end
|
105
|
+
|
106
|
+
describe "when on the last page" do
|
107
|
+
before { @document_collection.stub!(:current_page).and_return(20) }
|
108
|
+
it { @document_collection.next_page.should be_nil }
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "when on any other page" do
|
112
|
+
before { @document_collection.stub!(:current_page).and_return(4) }
|
113
|
+
|
114
|
+
it "should return the next page" do
|
115
|
+
@document_collection.next_page.should == 5
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe '#refinements' do
|
121
|
+
it 'should return a collection of Refinement objects' do
|
122
|
+
@document_collection.refinements.should == [Endeca::Refinement.new(@refinement)]
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should return refinement by name" do
|
126
|
+
@document_collection.refinement_by_name('city').
|
127
|
+
should == Endeca::Refinement.new(@refinement)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe '#breadcrumbs' do
|
132
|
+
it 'should return a collection of Breadcrumb objects' do
|
133
|
+
@document_collection.breadcrumbs.should == [Endeca::Breadcrumb.new(@breadcrumb)]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "#each" do
|
138
|
+
it "should yield to the documents" do
|
139
|
+
block = lambda{1}
|
140
|
+
@document_collection.documents.should_receive(:each).with(&block)
|
141
|
+
@document_collection.each(&block)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "#is_a?" do
|
146
|
+
describe "Array" do
|
147
|
+
it { @document_collection.is_a?(Array).should be_true }
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "DocumentCollection" do
|
151
|
+
it { @document_collection.is_a?(Endeca::DocumentCollection).should be_true }
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "String" do
|
155
|
+
it { @document_collection.is_a?(String).should be_false }
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
@@ -0,0 +1,378 @@
|
|
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
|
+
|
44
|
+
describe "with splitting values" do
|
45
|
+
it "should duplicate the key for each value passed" do
|
46
|
+
Endeca::Document.map(:features => :feature_cat).split_into(:ntk => :ntt)
|
47
|
+
[
|
48
|
+
{:ntk=>"feature_cat|feature_cat", :ntt=>"Driveway|Alarm"}
|
49
|
+
].should include(Endeca::Document.transform_query_options(:features => 'Driveway,Alarm'))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '.find' do
|
55
|
+
before do
|
56
|
+
@query_options = {:foo => :bar}
|
57
|
+
end
|
58
|
+
|
59
|
+
it do
|
60
|
+
Endeca::Document.path nil
|
61
|
+
lambda{ Endeca::Document.find('') }.should raise_error(Endeca::RequestError)
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#==' do
|
65
|
+
it "should compare documents by id" do
|
66
|
+
id = 1
|
67
|
+
doc_1 = Endeca::Document.new
|
68
|
+
doc_1.stub!(:id).and_return(id)
|
69
|
+
doc_2 = Endeca::Document.new
|
70
|
+
doc_2.stub!(:id).and_return(id)
|
71
|
+
|
72
|
+
(doc_1 == doc_2).should be_true
|
73
|
+
|
74
|
+
doc_2.stub!(:id).and_return(2)
|
75
|
+
|
76
|
+
(doc_1 == doc_2).should be_false
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe '.inspect' do
|
81
|
+
it "includes the class name" do
|
82
|
+
@document.inspect.should include(Endeca::Document.name)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "includes the hex string of the object id" do
|
86
|
+
@document.inspect.should include("0x#{@document.object_id.to_s(16)}")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "#dimensions" do
|
91
|
+
describe "with a nil Dimensions hash" do
|
92
|
+
it "should be empty" do
|
93
|
+
@document.stub!(:raw).and_return({})
|
94
|
+
@document.dimensions.should be_empty
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
describe 'with a raw Dimensions hash with a key "key" with one element' do
|
99
|
+
it 'should return a hash with that dimension at the key "key"' do
|
100
|
+
dimension_hash = mock('Dimension Hash')
|
101
|
+
dimension = mock(Endeca::Dimension)
|
102
|
+
|
103
|
+
@document.
|
104
|
+
stub!(:raw).
|
105
|
+
and_return({'Dimensions' => {"key" => [dimension_hash]}})
|
106
|
+
|
107
|
+
Endeca::Dimension.
|
108
|
+
should_receive(:new).
|
109
|
+
with(dimension_hash).
|
110
|
+
and_return(dimension)
|
111
|
+
|
112
|
+
@document.dimensions["key"].should == [dimension]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe 'with :first' do
|
118
|
+
it 'should call .first and pass the query options' do
|
119
|
+
Endeca::Document.should_receive(:first).with(@query_options)
|
120
|
+
Endeca::Document.find(:first, @query_options)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe 'with :all' do
|
125
|
+
it "should call .all and pass the query options and self" do
|
126
|
+
Endeca::Document.should_receive(:all).with(@query_options)
|
127
|
+
Endeca::Document.find(:all, @query_options)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe 'with an Integer' do
|
132
|
+
it "should call .by_id with the argument and the query options" do
|
133
|
+
id = 5
|
134
|
+
Endeca::Document.should_receive(:by_id).with(id, @query_options)
|
135
|
+
Endeca::Document.find(id, @query_options)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
describe 'with an id string' do
|
140
|
+
it "should call .by_id with the argument and the query options" do
|
141
|
+
id = '5'
|
142
|
+
Endeca::Document.should_receive(:by_id).with(id, @query_options)
|
143
|
+
Endeca::Document.find(id, @query_options)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe 'with an query string' do
|
148
|
+
it "should call find with the argument and the query options" do
|
149
|
+
query_string = 'N=0&Ntk=propertycity&Ntt=Atlanta'
|
150
|
+
Endeca::Document.should_receive(:all).with(query_string)
|
151
|
+
Endeca::Document.find(query_string)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe 'with only query options' do
|
156
|
+
it 'should call .all with the query options' do
|
157
|
+
Endeca::Document.should_receive(:all).with(@query_options)
|
158
|
+
Endeca::Document.find(@query_options)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
describe ".first" do
|
164
|
+
it "should make a request with the correct query" do
|
165
|
+
Endeca::Request.
|
166
|
+
should_receive(:perform).
|
167
|
+
with('http://endeca.example.com', {:id => '1234'}).
|
168
|
+
and_return({'Records' => []})
|
169
|
+
|
170
|
+
Endeca::Document.first(:id => '1234')
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should instantiate a new Endeca::Document from the first record in the response hash" do
|
174
|
+
hash = {'Records' => [:document]}
|
175
|
+
Endeca::Request.stub!(:perform).and_return(hash)
|
176
|
+
Endeca::Document.
|
177
|
+
should_receive(:new).
|
178
|
+
with(:document)
|
179
|
+
Endeca::Document.first(:id => '1234')
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should instantiate a new Endeca::Document from the first record in the response hash with aggregate results" do
|
183
|
+
hash = {'AggrRecords' => [{'Records' => [:document]}]}
|
184
|
+
Endeca::Request.stub!(:perform).and_return(hash)
|
185
|
+
Endeca::Document.
|
186
|
+
should_receive(:new).
|
187
|
+
with(:document)
|
188
|
+
Endeca::Document.first(:id => '1234')
|
189
|
+
end
|
190
|
+
|
191
|
+
describe "when no matching record is returned" do
|
192
|
+
it "should be nil" do
|
193
|
+
Endeca::Request.stub!(:perform).and_return({'Records' => nil})
|
194
|
+
Endeca::Document.first(:id => '1234').should be_nil
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe '.by_id' do
|
200
|
+
it 'should merge the id into the query options and call .first' do
|
201
|
+
Endeca::Document.should_receive(:first).with(:id => '1234', :skip_default_endeca_parameters => true)
|
202
|
+
Endeca::Document.by_id('1234')
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe '.all' do
|
207
|
+
before do
|
208
|
+
@query_options = {:foo => :bar}
|
209
|
+
@records = mock('RecordArray')
|
210
|
+
@response = {'Records' => @records}
|
211
|
+
Endeca::Document.
|
212
|
+
stub!(:request).
|
213
|
+
and_return(@response)
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'should perform a request with the query options' do
|
217
|
+
Endeca::Document.
|
218
|
+
should_receive(:request).
|
219
|
+
with(@query_options).
|
220
|
+
and_return(@response)
|
221
|
+
|
222
|
+
Endeca::Document.all(@query_options)
|
223
|
+
end
|
224
|
+
|
225
|
+
it 'should create a new DocumentCollection from the full response and the document class' do
|
226
|
+
Endeca::DocumentCollection.should_receive(:new).with(@response, Endeca::Document)
|
227
|
+
Endeca::Document.all(@query_options)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
describe ".all" do
|
232
|
+
describe "with a query string" do
|
233
|
+
it 'should make a request with the query string' do
|
234
|
+
Endeca::Request.should_receive(:perform).with(anything, "query string")
|
235
|
+
Endeca::Document.all("query string")
|
236
|
+
end
|
237
|
+
|
238
|
+
describe "and default parameters" do
|
239
|
+
it "should make a request with the query string and ignore the defaults" do
|
240
|
+
Endeca::Document.default_params :foo => :bar
|
241
|
+
Endeca::Request.should_receive(:perform).with(anything, "query string")
|
242
|
+
Endeca::Document.all("query string")
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
describe "with a collection_class on the document" do
|
248
|
+
before(:all) do
|
249
|
+
class SubDocumentCollection < Endeca::DocumentCollection; end
|
250
|
+
Endeca::Document.collection_class SubDocumentCollection
|
251
|
+
|
252
|
+
Endeca::Request.stub!(:perform).and_return({})
|
253
|
+
end
|
254
|
+
|
255
|
+
it "should initialize an collection of the correct class" do
|
256
|
+
Endeca::Document.all(:id => 1).should be_a_kind_of(SubDocumentCollection)
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
end
|
261
|
+
|
262
|
+
describe ".transform_query_options" do
|
263
|
+
before do
|
264
|
+
@query_options = {
|
265
|
+
:id => 5
|
266
|
+
}
|
267
|
+
end
|
268
|
+
|
269
|
+
after do
|
270
|
+
Endeca::Document.mappings = {}
|
271
|
+
end
|
272
|
+
|
273
|
+
describe "when the key to be mapped is a string" do
|
274
|
+
it "maps correctly" do
|
275
|
+
Endeca::Document.map(:apartments => :showapartments)
|
276
|
+
Endeca::Document.transform_query_options("apartments" => true).
|
277
|
+
should == {:showapartments => 1}
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
describe "with multiple keys that are joined" do
|
282
|
+
it "should include all the keys" do
|
283
|
+
require 'pp'
|
284
|
+
Endeca::Document.map(:apartments => :showapartments).into('Ntk' => 'Ntt')
|
285
|
+
Endeca::Document.map(:colleges => :showcolleges).into('Ntk' => 'Ntt')
|
286
|
+
ntk = Endeca::Document.transform_query_options('apartments' => '1', 'colleges' => '2')[:Ntk]
|
287
|
+
ntk.should include('showapartments')
|
288
|
+
ntk.should include('showcolleges')
|
289
|
+
ntt = Endeca::Document.transform_query_options('apartments' => '1', 'colleges' => '2')[:Ntt]
|
290
|
+
ntt.should include('1')
|
291
|
+
ntt.should include('2')
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
describe "when there is no mapping for the given key" do
|
296
|
+
it "returns the query options without modification" do
|
297
|
+
Endeca::Document.transform_query_options(@query_options).
|
298
|
+
should == @query_options
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
describe "when there is a mapping with" do
|
303
|
+
describe "a new key" do
|
304
|
+
before do
|
305
|
+
Endeca::Document.map(:id => :new_id)
|
306
|
+
end
|
307
|
+
|
308
|
+
it "should replace the key with the new key" do
|
309
|
+
new_query_options = Endeca::Document.
|
310
|
+
transform_query_options(@query_options)
|
311
|
+
|
312
|
+
new_query_options.should == {:new_id => 5}
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
describe "a new key and a value transformation" do
|
317
|
+
before do
|
318
|
+
Endeca::Document.map(:id => :new_id).transform {|id| id.to_s}
|
319
|
+
end
|
320
|
+
|
321
|
+
it "should replace the value with the transformed value" do
|
322
|
+
Endeca::Document.transform_query_options(@query_options).
|
323
|
+
should == {:new_id => "5"}
|
324
|
+
end
|
325
|
+
|
326
|
+
describe "that includes the current value" do
|
327
|
+
before do
|
328
|
+
Endeca::Document.map(:name => :new_name) do |name, current|
|
329
|
+
[current,name].compact.join('|')
|
330
|
+
end
|
331
|
+
|
332
|
+
@current_query_options = {:name => 'bar', :new_name => 'foo'}
|
333
|
+
end
|
334
|
+
end
|
335
|
+
end
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
describe "with default query parameters" do
|
340
|
+
before do
|
341
|
+
Endeca::Document.default_params :foo => :bar
|
342
|
+
end
|
343
|
+
|
344
|
+
after do
|
345
|
+
Endeca::Document.default_params({})
|
346
|
+
end
|
347
|
+
|
348
|
+
it "should get the default params" do
|
349
|
+
Endeca::Document.get_default_params.should == {:foo => :bar}
|
350
|
+
end
|
351
|
+
|
352
|
+
it "should include the default parameters" do
|
353
|
+
Endeca::Request.should_receive(:perform).with(anything, {:foo => :bar})
|
354
|
+
Endeca::Document.request({})
|
355
|
+
end
|
356
|
+
|
357
|
+
describe "that are mapped" do
|
358
|
+
it "should map the default parameters" do
|
359
|
+
Endeca::Document.map(:foo => :new_foo)
|
360
|
+
Endeca::Request.should_receive(:perform).with(anything, {:new_foo => :bar})
|
361
|
+
Endeca::Document.request({})
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
describe "#method_missing" do
|
367
|
+
it "should include a suggestion to add a reader" do
|
368
|
+
lambda{ @document.missing_method }.
|
369
|
+
should raise_error(NoMethodError, "undefined method 'missing_method' for #{@document.inspect}. Do you need to add a reader for it?")
|
370
|
+
end
|
371
|
+
end
|
372
|
+
|
373
|
+
describe "#inspect" do
|
374
|
+
it "should return details of the Document class" do
|
375
|
+
Endeca::Document.inspect.should == "#<Endeca::Document>\nPath: \"http://endeca.example.com\"\nCollection Class: SubDocumentCollection\"\nMappings:\n\tfoo: {:new_foo=>nil}\n\t \nDefaultParams:\n\t \n"
|
376
|
+
end
|
377
|
+
end
|
378
|
+
end
|