endeca 1.3.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,122 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ describe Endeca::Map do
4
+ before do
5
+ @query = {:foo => "bazz", :bizz => "somevalue"}
6
+ @map = Endeca::Map.new :foo, :bizz
7
+
8
+ end
9
+
10
+ describe ".perform" do
11
+ it "should return correctly mapped hash" do
12
+ @map.perform(@query).should == {:bizz => "bazz"}
13
+ end
14
+
15
+ it "should be indifferent to string or symbol keys" do
16
+ @map.perform("foo" => "bazz").should == {:bizz => "bazz"}
17
+ end
18
+
19
+ it "should transform the value based on the block" do
20
+ map = @map.transform{|val| val.to_s.upcase}
21
+ map.perform(@query).should == {:bizz => "BAZZ"}
22
+ end
23
+
24
+ it "should nest in parent_hash" do
25
+ @map.into({:bip => :bop}).perform(@query).
26
+ should == {:bip => "bizz", :bop => "bazz"}
27
+ end
28
+
29
+ it "should join the new value with the existing value" do
30
+ map = @map.join('|')
31
+ map.perform(@query).should == {:bizz => "somevalue|bazz"}
32
+ end
33
+ end
34
+
35
+ describe "#into" do
36
+ it "should assign the hash to map the given query hash into" do
37
+ @map.into(:foo => :bar).instance_variable_get(:@into).should == {:foo => :bar}
38
+ end
39
+
40
+ it "should assign the default character to join keys to values with" do
41
+ @map.into(:foo => :bar).instance_variable_get(:@with).should == ':'
42
+ end
43
+
44
+ it "should assign the default character used to join key/value pairs" do
45
+ @map.into(:foo => :bar).instance_variable_get(:@join).should == '|'
46
+ end
47
+ end
48
+
49
+ describe "#perform_into" do
50
+ describe "with an enclosing string" do
51
+ it "wraps the parameter" do
52
+ map = Endeca::Map.new(:foo, :bar)
53
+ map.into(:bizz).enclose(:AND)
54
+ map.perform(:foo => :quux).should == {:bizz => 'AND(bar:quux)'}
55
+ end
56
+ end
57
+
58
+ describe "with replace" do
59
+ it "replaces the existing parameter" do
60
+ map = Endeca::Map.new(:foo, :bar)
61
+ map.into(:bizz).replace!
62
+ map.perform(:foo => :quux, :bizz => :foobar).should == {:bizz => 'bar:quux'}
63
+ end
64
+ end
65
+
66
+ describe "with enclose and replace" do
67
+ it "wraps the parameter and replaces the existing parameter" do
68
+ map = Endeca::Map.new(:foo, :bar)
69
+ map.into(:bizz).enclose(:AND).replace!
70
+ map.perform(:foo => :quux, :bizz => :foobar).should == {:bizz => 'AND(bar:quux)'}
71
+ end
72
+ end
73
+ end
74
+
75
+ describe "#with" do
76
+ it "should assign the character to join keys to values with" do
77
+ @map.with('*').instance_variable_get(:@with).should == '*'
78
+ end
79
+ end
80
+
81
+ describe "#join" do
82
+ it "should assign the character used to join key/value pairs" do
83
+ @map.join('*').instance_variable_get(:@join).should == '*'
84
+ end
85
+ end
86
+
87
+ describe "#boolean" do
88
+ it "should convert a true value to its integral equivalent" do
89
+ @map.boolean.perform(:foo => true).should == {:bizz => 1}
90
+ end
91
+
92
+ it "should convert a false value to its integral equivalent" do
93
+ @map.boolean.perform(:foo => false).should == {:bizz => 0}
94
+ end
95
+ end
96
+
97
+ describe "#==" do
98
+ it "is true if the keys, join and transformations are equivalent" do
99
+ Endeca::Map.new(:foo, :bar).into(:M).should == Endeca::Map.new(:foo, :bar).into(:M)
100
+ end
101
+ end
102
+
103
+ describe "#transform" do
104
+ it "should execute the transformation block on the query" do
105
+ map = Endeca::Map.new(:field_list, :F).transform do |fields_array|
106
+ fields_array.collect{|field| "#{field.to_s}:1"}.join('|')
107
+ end
108
+ map.perform(:field_list => [:first_name, :last_name, :email]).
109
+ should == {:F => "first_name:1|last_name:1|email:1"}
110
+ end
111
+ end
112
+
113
+ describe ".inspect" do
114
+ it "should return details of the map" do
115
+ query = {:foo => "bazz", :bizz => "somevalue"}
116
+ map = Endeca::Map.new :foo
117
+ map.into(:Ntk => :Ntt)
118
+ map.inspect.should include(":Ntk=>\"foo\"")
119
+ map.inspect.should include(":Ntt=>\"\"")
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,118 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ describe Endeca::Readers do
4
+ class Helper
5
+ extend Endeca::Readers
6
+ end
7
+
8
+ before do
9
+ @helper = Class.new(Endeca::Document)
10
+ @a_helper = @helper.new('Properties' => {'helper_id' => "1", 'nil_id' => nil, 'empty_field' => ''})
11
+ end
12
+
13
+ describe ".add_reader" do
14
+ it "should add a method named for the argument" do
15
+ @helper.add_reader(:float_reader) {|x| x.to_f}
16
+ @helper.should respond_to(:float_reader)
17
+ end
18
+ end
19
+
20
+ describe ".field_names" do
21
+ it "should include added readers" do
22
+ @helper.reader(:helper_id)
23
+ @helper.field_names.should include(:helper_id)
24
+ end
25
+ end
26
+
27
+ describe ".reader" do
28
+
29
+ describe "with a symbol" do
30
+ it "adds a reader that returns the attribute by that key" do
31
+ @helper.reader(:helper_id)
32
+ @a_helper.should respond_to(:helper_id)
33
+ @a_helper.helper_id.should == "1"
34
+ end
35
+ end
36
+
37
+ describe "with a hash" do
38
+ it "adds a reader that returns the corresponding element" do
39
+ @helper.reader(:helper_id => :new_helper_id)
40
+ @a_helper.should respond_to(:new_helper_id)
41
+ @a_helper.new_helper_id.should == "1"
42
+ end
43
+ end
44
+
45
+ describe "with a hash and a block" do
46
+ it "adds a reader that returns the corresponding element, cast by calling the block" do
47
+ @helper.reader(:helper_id => :succ_helper_id){|id| id.succ}
48
+ @a_helper.should respond_to(:succ_helper_id)
49
+ @a_helper.succ_helper_id.should == "1".succ
50
+ end
51
+ end
52
+ end
53
+
54
+ describe ".integer_reader" do
55
+ it "adds a reader that casts the value to an integer" do
56
+ @helper.integer_reader(:helper_id)
57
+ @a_helper.helper_id.should == 1
58
+ end
59
+
60
+ describe "when the value is nil" do
61
+ it "should return 0" do
62
+ @helper.integer_reader(:nil_id)
63
+ @a_helper.nil_id.should == 0
64
+ end
65
+ end
66
+
67
+ describe "when the field does not exist" do
68
+ it "should return 0" do
69
+ @helper.integer_reader(:non_existant_field)
70
+ @a_helper.non_existant_field.should == 0
71
+ end
72
+ end
73
+
74
+ describe "when the value is an empty string" do
75
+ it "should return 0" do
76
+ @helper.integer_reader(:empty_field)
77
+ @a_helper.empty_field.should == 0
78
+ end
79
+ end
80
+ end
81
+
82
+ describe ".decimal_reader" do
83
+ it "adds a reader that casts the value to an decimal" do
84
+ @helper.decimal_reader(:helper_id)
85
+ @a_helper.helper_id.should == BigDecimal.new("1")
86
+ end
87
+ end
88
+
89
+ describe ".float_reader" do
90
+ it "adds a reader that casts the value to an float" do
91
+ @helper.float_reader(:helper_id)
92
+ a_helper = @helper.new('Properties' => {'helper_id' => "1.9234"})
93
+ a_helper.helper_id.should == Float("1.9234")
94
+ end
95
+
96
+ it "adds a reader that casts the value to an float" do
97
+ @helper.float_reader(:helper_id)
98
+ a_helper = @helper.new('Properties' => {'unhelpful_id' => "1.9234"})
99
+ a_helper.helper_id.should == nil
100
+ end
101
+ end
102
+
103
+ describe ".boolean_reader" do
104
+ it "adds a reader that casts the value to an boolean" do
105
+ @helper.boolean_reader(:helper_id)
106
+ @a_helper.helper_id.should == true
107
+ end
108
+ end
109
+
110
+ describe "exception handling" do
111
+ it "should raise an Endeca::Reader error if the call fails" do
112
+ helper = Class.new(Endeca::Document)
113
+ helper.integer_reader(:helper_id)
114
+ helper = helper.new('Properties' => {'helper_id' => 'W'})
115
+ lambda{helper.helper_id}.should raise_error(Endeca::ReaderError, %{invalid value for Integer: "W"})
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Endeca::RefinementDimension do
4
+ describe ".new" do
5
+ it "should set the raw attribute" do
6
+ dimension = Endeca::RefinementDimension.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::RefinementDimension.new("ExpansionLink" => "expansion link")
15
+ end
16
+
17
+ it() {@dimension.to_endeca_params.should == "expansion link"}
18
+
19
+ end
20
+
21
+ describe "#inspect" do
22
+ before do
23
+ @dimension = Endeca::RefinementDimension.new
24
+ end
25
+
26
+ it "should include the class" do
27
+ @dimension.inspect.should include(Endeca::RefinementDimension.name)
28
+ end
29
+
30
+ it "should include the hex formatted object_id" do
31
+ id = 123
32
+ @dimension.stub!(:object_id).and_return(id)
33
+ @dimension.inspect.should include("0x#{id.to_s(16)}")
34
+ end
35
+
36
+ it "should include the id" do
37
+ id = 123
38
+ @dimension.stub!(:id).and_return(id)
39
+ @dimension.inspect.should include("id=#{id}")
40
+ end
41
+
42
+ it "should include the inspected name" do
43
+ name = 'name'
44
+ @dimension.stub!(:name).and_return(name)
45
+ @dimension.inspect.should include("name=#{name.inspect}")
46
+ end
47
+ end
48
+
49
+ describe "#==" do
50
+ it "should compare ids" do
51
+ dim_1 = Endeca::RefinementDimension.new
52
+ dim_2 = Endeca::RefinementDimension.new
53
+ dim_2.stub!(:id).and_return(dim_1.id)
54
+
55
+ (dim_1 == dim_2).should be_true
56
+ end
57
+ end
58
+
59
+ describe "#<=>" do
60
+ it "should compare names" do
61
+ name = mock('name')
62
+
63
+ dim_1 = Endeca::RefinementDimension.new
64
+ dim_2 = Endeca::RefinementDimension.new
65
+
66
+ dim_1.stub!(:name).and_return(name)
67
+ dim_2.stub!(:name).and_return(name)
68
+
69
+ name.should_receive(:<=>).with(name)
70
+
71
+ dim_1 <=> dim_2
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,72 @@
1
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
2
+
3
+ describe Endeca::Refinement do
4
+ before do
5
+ @dimension_value = {
6
+ "DimValueID" => "4294965335",
7
+ "SelectionLink" => "N=4294965335&Ne=3",
8
+ "DimValueName" => "Winter Park",
9
+ "NumberofRecords" => "44"
10
+ }
11
+
12
+ dimensions = { "Dimensions" => [
13
+ {
14
+ "DimensionID" => "3",
15
+ "DimensionName" => "state",
16
+ "ContractionLink" => "N=",
17
+ "DimensionValues" => [@dimension_value]
18
+ }
19
+ ]
20
+ }
21
+ @refinement = Endeca::Refinement.new( dimensions )
22
+ end
23
+
24
+ describe '#==' do
25
+ it "should compare refinements by id" do
26
+ doc_1, doc_2 = Endeca::Refinement.new, Endeca::Refinement.new
27
+ doc_1.stub!(:id).and_return(1)
28
+ doc_2.stub!(:id).and_return(1)
29
+ (doc_1 == doc_2).should be_true
30
+
31
+ doc_2.stub!(:id).and_return(2)
32
+ (doc_1 == doc_2).should be_false
33
+ end
34
+ end
35
+
36
+ describe '#inspect' do
37
+ it "includes the class name" do
38
+ @refinement.inspect.should include(Endeca::Refinement.name)
39
+ end
40
+
41
+ it "includes the hex string of the object id" do
42
+ @refinement.inspect.should include("0x#{@refinement.object_id.to_s(16)}")
43
+ end
44
+
45
+ it "includes the id" do
46
+ @refinement.stub!(:id).and_return(1)
47
+ @refinement.inspect.should include('id=1')
48
+ end
49
+
50
+ it "includes the inspected name" do
51
+ @refinement.stub!(:name).and_return('A Name')
52
+ @refinement.inspect.should include('name="A Name"')
53
+ end
54
+ end
55
+
56
+ it "should return to_endeca_params on the contraction link " do
57
+ @refinement.to_endeca_params.should == "N="
58
+ end
59
+
60
+ it "should return an array of dimensions for dimension_values" do
61
+ my_dimension = Endeca::Dimension.new(@dimension_value)
62
+ @refinement.dimension_values.should == [my_dimension]
63
+ end
64
+
65
+ it "should return an array of dimensions" do
66
+ refinement_dim_raw = mock("raw dimension refinement")
67
+ refinement_dim = mock(Endeca::RefinementDimension)
68
+ @refinement.attributes['Dimensions'] = [refinement_dim_raw]
69
+ Endeca::RefinementDimension.should_receive(:new).with(refinement_dim_raw).and_return(refinement_dim)
70
+ @refinement.dimensions.should == [refinement_dim]
71
+ end
72
+ end
@@ -0,0 +1,107 @@
1
+ require 'rubygems'
2
+ require 'fake_web'
3
+ require 'json'
4
+ require File.join(File.dirname(__FILE__), %w[.. spec_helper])
5
+
6
+ describe Endeca::Request do
7
+ before do
8
+ @path = 'http://example.com/foobar'
9
+ end
10
+
11
+ describe '.perform' do
12
+ it "initalializes a new request object and performs the request" do
13
+ path = 'path'
14
+ query = 'query'
15
+ request = mock('Endeca::Request')
16
+ request.should_receive(:perform)
17
+ Endeca::Request.should_receive(:new).with(path, query).and_return(request)
18
+
19
+ Endeca::Request.perform(path, query)
20
+ end
21
+ end
22
+
23
+ describe '#perform' do
24
+ before do
25
+ @request = Endeca::Request.new(@path)
26
+ end
27
+
28
+ it "should not make more than one request" do
29
+ @request.should_receive(:handle_response).exactly(1).times.and_return({})
30
+ @request.perform
31
+ end
32
+
33
+ describe "when successful" do
34
+ before do
35
+ @response_hash = {"foo" => "bar"}
36
+ FakeWeb.register_uri(@path, :string => @response_hash.to_json)
37
+ end
38
+
39
+ it "should return the parsed JSON of the response body" do
40
+ @request.perform.should == @response_hash
41
+ end
42
+
43
+ it "should not raise an error" do
44
+ lambda { @request.perform }.should_not raise_error
45
+ end
46
+ end
47
+
48
+ describe "when unsuccessful" do
49
+ before do
50
+ FakeWeb.register_uri(@path, :status => ['404', 'Not Found'])
51
+ end
52
+
53
+ it "should raise an Endeca::RequestError" do
54
+ lambda {@request.perform}.should raise_error(Endeca::RequestError, '404 "Not Found"')
55
+ end
56
+
57
+ end
58
+
59
+ describe "when the response contains an error hash" do
60
+ before do
61
+ @error_message = "com.endeca.soleng.urlformatter.QueryBuildException: com.endeca.navigation.UrlENEQueryParseException: java.lang.NumberFormatException: For input string: \"asdjkhfgasdfjkhg\""
62
+ @error_response = {
63
+ "methodResponse"=>
64
+ {"fault"=>
65
+ {"value"=>
66
+ {"faultCode"=>"-1",
67
+ "faultString"=> @error_message}}}}
68
+ @error_request = Endeca::Request.new(@path)
69
+ @error_request.stub!(:handle_response).and_return(@error_response)
70
+ end
71
+
72
+ it "should raise an Endeca::RequestError" do
73
+ lambda { @error_request.perform }.should raise_error(Endeca::RequestError, @error_message)
74
+ end
75
+ end
76
+
77
+ end
78
+
79
+ describe '#uri' do
80
+
81
+ describe "with a hash of query options" do
82
+
83
+ it "should append the query options onto the url" do
84
+ query = {:foo => :bar}
85
+ Endeca::Request.new(@path, query).uri.query.should == query.to_endeca_params
86
+ end
87
+
88
+ end
89
+
90
+ describe "with a query with '/_/' in it" do
91
+
92
+ it "should not insert the ? before the query" do
93
+ query = "/Beds-2/Baths-3/Dishwasher/_/N=324432/Ne=listing?test=true"
94
+ Endeca::Request.new(@path, query).uri.to_s.should == "#{@path}#{query}"
95
+ end
96
+
97
+ end
98
+
99
+ end
100
+
101
+ describe "with a string of query options" do
102
+ it "should append the query options string onto the url" do
103
+ query = 'N=56'
104
+ Endeca::Request.new(@path, query).uri.query.should == query.to_endeca_params
105
+ end
106
+ end
107
+ end