bcms_google_mini_search 1.2.1 → 1.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,3 @@
1
+ require 'rexml/document'
1
2
  require 'bcms_google_mini_search/routes'
2
3
  require 'bcms_google_mini_search/gsa'
@@ -112,9 +112,9 @@ module GSA
112
112
  def initialize(xml_element = nil)
113
113
  return if xml_element == nil
114
114
  self.number = xml_element.attributes["N"]
115
- self.title = xml_element.elements["T"].text
116
- self.url = xml_element.elements["U"].text
117
- self.description = xml_element.elements["S"].text
115
+ self.title = xml_element.elements["T"].try(:text)
116
+ self.url = xml_element.elements["U"].try(:text)
117
+ self.description = xml_element.elements["S"].try(:text)
118
118
 
119
119
  cache_element = xml_element.elements["HAS/C"]
120
120
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bcms_google_mini_search
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
4
+ hash: 27
5
+ prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 2
9
- - 1
10
- version: 1.2.1
9
+ - 2
10
+ version: 1.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - BrowserMedia
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-05 00:00:00 -04:00
18
+ date: 2011-11-01 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -43,13 +43,6 @@ files:
43
43
  - lib/bcms_google_mini_search/routes.rb
44
44
  - rails/init.rb
45
45
  - README.markdown
46
- - test/performance/browsing_test.rb
47
- - test/test_helper.rb
48
- - test/unit/gsa_test.rb
49
- - test/unit/helpers/search_engine_helper_test.rb
50
- - test/unit/portlets/google_mini_search_engine_portlet_test.rb
51
- - test/unit/portlets/search_box_portlet_test.rb
52
- - test/unit/search_result_test.rb
53
46
  has_rdoc: true
54
47
  homepage: http://github.com/browsermedia/bcms_google_mini_search
55
48
  licenses: []
@@ -80,15 +73,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
73
  requirements: []
81
74
 
82
75
  rubyforge_project: bcms_google_mini_search
83
- rubygems_version: 1.3.7
76
+ rubygems_version: 1.6.2
84
77
  signing_key:
85
78
  specification_version: 3
86
79
  summary: A Google Mini Search Module for BrowserCMS
87
- test_files:
88
- - test/performance/browsing_test.rb
89
- - test/test_helper.rb
90
- - test/unit/gsa_test.rb
91
- - test/unit/helpers/search_engine_helper_test.rb
92
- - test/unit/portlets/google_mini_search_engine_portlet_test.rb
93
- - test/unit/portlets/search_box_portlet_test.rb
94
- - test/unit/search_result_test.rb
80
+ test_files: []
81
+
@@ -1,9 +0,0 @@
1
- require 'test_helper'
2
- require 'performance_test_help'
3
-
4
- # Profiling results for each test method are written to tmp/performance.
5
- class BrowsingTest < ActionController::PerformanceTest
6
- def test_homepage
7
- get '/'
8
- end
9
- end
@@ -1,39 +0,0 @@
1
- ENV["RAILS_ENV"] = "test"
2
- require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
3
- require 'test_help'
4
- require 'mocha'
5
-
6
- class ActiveSupport::TestCase
7
- # Transactional fixtures accelerate your tests by wrapping each test method
8
- # in a transaction that's rolled back on completion. This ensures that the
9
- # test database remains unchanged so your fixtures don't have to be reloaded
10
- # between every test method. Fewer database queries means faster tests.
11
- #
12
- # Read Mike Clark's excellent walkthrough at
13
- # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
14
- #
15
- # Every Active Record database supports transactions except MyISAM tables
16
- # in MySQL. Turn off transactional fixtures in this case; however, if you
17
- # don't care one way or the other, switching from MyISAM to InnoDB tables
18
- # is recommended.
19
- #
20
- # The only drawback to using transactional fixtures is when you actually
21
- # need to test transactions. Since your test is bracketed by a transaction,
22
- # any transactions started in your code will be automatically rolled back.
23
- self.use_transactional_fixtures = true
24
-
25
- # Instantiated fixtures are slow, but give you @david where otherwise you
26
- # would need people(:david). If you don't want to migrate your existing
27
- # test cases which use the @david style and don't mind the speed hit (each
28
- # instantiated fixtures translates to a database query per test method),
29
- # then set this back to true.
30
- self.use_instantiated_fixtures = false
31
-
32
- # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
33
- #
34
- # Note: You'll currently still have to declare fixtures explicitly in integration tests
35
- # -- they do not yet inherit this setting
36
- fixtures :all
37
-
38
- # Add more helper methods to be used by all tests here...
39
- end
@@ -1,182 +0,0 @@
1
- require "test_helper"
2
-
3
- class GSA::ApplianceTest < ActiveSupport::TestCase
4
-
5
- test "Create Engine" do
6
- app = GSA::Engine.new
7
- app.host = "http://example.com"
8
- assert_equal "http://example.com", app.host
9
- assert_equal 8080, app.port
10
- assert_equal "/search", app.path
11
- end
12
-
13
- test "Create from options" do
14
- app = GSA::Engine.new({:host=>"http://example.com", :front_end=>"F", :collection=>"C"})
15
- assert_equal "http://example.com", app.host
16
- assert_equal "F", app.default_front_end
17
- assert_equal "C", app.default_collection
18
- end
19
-
20
- test "options_for_query" do
21
- options = {:host=>"http://example.com", :front_end=>"F", :collection=>"C"}
22
- app = GSA::Engine.new(options)
23
- assert_equal options, app.options_for_query
24
- end
25
- end
26
-
27
- class SuggestedQueries < ActiveSupport::TestCase
28
-
29
- def setup
30
- @cluster_xml = <<XML
31
- <toplevel>
32
- <Response>
33
- <algorithm data="Concepts"/>
34
- <t_cluster int="95"/>
35
- <cluster>
36
- <gcluster>
37
- <label data="label 0"/>
38
- </gcluster>
39
- <gcluster>
40
- <label data="label 1"/>
41
- </gcluster>
42
- <gcluster>
43
- <label data="label 2"/>
44
- </gcluster>
45
- <gcluster>
46
- <label data="label 3"/>
47
- </gcluster>
48
- <gcluster>
49
- <label data="label 4"/>
50
- </gcluster>
51
- <gcluster>
52
- <label data="label 5"/>
53
- </gcluster>
54
- <gcluster>
55
- <label data="label 6"/>
56
- </gcluster>
57
- <gcluster>
58
- <label data="label 7"/>
59
- </gcluster>
60
- <gcluster>
61
- <label data="label 8"/>
62
- </gcluster>
63
- <gcluster>
64
- <label data="label 9"/>
65
- </gcluster>
66
- </cluster>
67
- </Response>
68
- </toplevel>
69
- XML
70
-
71
- @app = GSA::Appliance.new(:host=>"http://example.com", :collection=>"My_Collection", :front_end=>"My_Front")
72
-
73
- end
74
-
75
- test "parse results" do
76
- suggested_queries = GSA::SuggestedQueries.new(@cluster_xml)
77
- assert_equal 10, suggested_queries.size
78
- assert_equal "label 0", suggested_queries[0].query
79
- assert_equal "label 9", suggested_queries[9].query
80
- end
81
-
82
- test "Google Search Appliances should generate the URL for Dynamic Results Clustering" do
83
- expected = "http://example.com/cluster?coutput=xml&q=TEST&site=My_Collection&client=My_Front&output=xml_no_dtd&oe=UTF-8&ie=UTF-8"
84
- assert_equal expected, @app.send(:narrow_search_results_url, "TEST")
85
- end
86
-
87
- test "URLs will escape queries" do
88
- expected = "http://example.com/cluster?coutput=xml&q=TWO+WORDS&site=My_Collection&client=My_Front&output=xml_no_dtd&oe=UTF-8&ie=UTF-8"
89
- assert_equal expected, @app.send(:narrow_search_results_url, "TWO WORDS")
90
- end
91
-
92
- test "find narrowed search results" do
93
- @app.expects(:narrow_search_results_url).with("TEST").returns("EXPECTED URL")
94
- SearchResult.expects(:fetch_document).with("EXPECTED URL").returns("XML Content")
95
- expected_suggestions = mock()
96
- GSA::SuggestedQueries.expects(:new).with("XML Content").returns(expected_suggestions)
97
-
98
- assert_equal expected_suggestions, @app.find_narrow_search_results("TEST")
99
-
100
- end
101
-
102
- test "each_with_index" do
103
- suggestions = GSA::SuggestedQueries.new(@cluster_xml)
104
- count = 0
105
- suggestions.each_with_index do |s, i|
106
- assert_not_nil s
107
- assert_not_nil i
108
- count += 1
109
- end
110
- assert_equal suggestions.size, count
111
- end
112
-
113
- test "each" do
114
- suggestions = GSA::SuggestedQueries.new(@cluster_xml)
115
- count = 0
116
- suggestions.each do |s|
117
- assert_not_nil s
118
- count += 1
119
- end
120
- assert_equal suggestions.size, count
121
- end
122
-
123
- test "A nil query should return an empty set of Suggested Queries" do
124
- r = @app.find_narrow_search_results(nil)
125
- assert_equal 0, r.size
126
- end
127
- end
128
-
129
- class ResultsTest < ActiveSupport::TestCase
130
-
131
- def setup
132
- @results = GSA::Results.new
133
- @results.query = "QUERY"
134
- @result = GSA::Result.new
135
- @result.results = @results
136
-
137
- @engine = GSA::Engine.new(:host=>"http://mini.someurl.com")
138
- @query = GSA::Query.new(:engine=>@engine, :collection=>"COLLECT", :front_end=>"FRONT_END")
139
- end
140
-
141
- test "Create result from xml" do
142
- xml = <<XML
143
- <R N="1">
144
- <U>http://someurl.com</U>
145
- <T>TITLE</T>
146
- <S>BLURB</S>
147
- <HAS>
148
- <C SZ="1k" CID="Ax1j5"/>
149
- </HAS>
150
- </R>
151
- XML
152
- xml_doc = REXML::Document.new(xml)
153
- result = GSA::Result.new(xml_doc.elements.first)
154
- assert_equal "http://someurl.com", result.url
155
- assert_equal "TITLE", result.title
156
- assert_equal "BLURB", result.description
157
- assert_equal "1k", result.size
158
- assert_equal "1", result.number
159
- assert_equal "Ax1j5", result.cache_id
160
- end
161
-
162
- test "cached_document_param" do
163
- @result.cache_id = "A2B"
164
- @result.url = "http://example.com"
165
-
166
- assert_equal "cache:A2B:http://example.com+QUERY", @result.cached_document_param
167
- end
168
-
169
- test "cached_document_param with no result attached" do
170
- @result.results = nil
171
- @result.cache_id = "A2B"
172
- @result.url = "http://example.com"
173
-
174
- assert_equal "cache:A2B:http://example.com", @result.cached_document_param
175
- end
176
-
177
- test "cached_document_url" do
178
- @result.expects(:cached_document_param).returns("cache:something")
179
- expected_url = "http://mini.someurl.com/search?q=cache%3Asomething&output=xml_no_dtd&client=FRONT_END&site=COLLECT&filter=0&proxystylesheet=FRONT_END&oe=UTF-8&ie=UTF-8"
180
- assert_equal expected_url, @result.cached_document_url(@query)
181
- end
182
- end
@@ -1,4 +0,0 @@
1
- require 'test_helper'
2
-
3
- class SearchEngineHelperTest < ActionView::TestCase
4
- end
@@ -1,53 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '/../../test_helper')
2
-
3
- class GoogleMiniSearchEngineTest < ActiveSupport::TestCase
4
-
5
- def setup
6
- @portlet = GoogleMiniSearchEnginePortlet.new(:name=>"Engine", :path => "/engine")
7
- end
8
-
9
- test "Should be able to create new instance of a portlet" do
10
- assert GoogleMiniSearchEnginePortlet.create!(:name => "New Portlet")
11
- end
12
-
13
- test "Path attribute can be set in constructor" do
14
- portlet = GoogleMiniSearchEnginePortlet.create!(:name=>"Engine", :path => "/engine")
15
- assert_equal "/engine", portlet.path
16
- end
17
-
18
- test "Determine if Narrow Your Search is enabled?" do
19
- @portlet.enable_narrow_your_search = "1"
20
- assert_equal true, @portlet.narrow_your_search?
21
-
22
- @portlet.enable_narrow_your_search = "0"
23
- assert_equal false, @portlet.narrow_your_search?
24
-
25
- @portlet.enable_narrow_your_search = ""
26
- assert_equal false, @portlet.narrow_your_search?
27
-
28
- @portlet.enable_narrow_your_search = nil
29
- assert_equal false, @portlet.narrow_your_search?
30
- end
31
-
32
-
33
- end
34
-
35
- class RenderTest < ActiveSupport::TestCase
36
- def setup
37
- @portlet = GoogleMiniSearchEnginePortlet.new(:name=>"Engine", :path => "/engine")
38
- @params = {:start => 10, :query => "X", :site=>'default_collection', :sort=>"date:D:S:d1"}
39
- @portlet.expects('params').returns(@params).at_least_once
40
- end
41
-
42
- test "Sort params" do
43
- SearchResult.expects(:find).with("X", {:start => 10, :portlet => @portlet, :site=>'default_collection', :sort=>"date:D:S:d1"})
44
- @portlet.render
45
- end
46
-
47
- test "Find narrow queries only if enabled" do
48
- @portlet.enable_narrow_your_search = "1"
49
- GSA::Appliance.any_instance.expects(:find_narrow_search_results).with("X")
50
- SearchResult.expects(:find).with("X", {:start => 10, :portlet => @portlet, :site=>'default_collection', :sort=>"date:D:S:d1"})
51
- @portlet.render
52
- end
53
- end
@@ -1,9 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '/../../test_helper')
2
-
3
- class SearchBoxTest < ActiveSupport::TestCase
4
-
5
- test "Should be able to create new instance of a portlet" do
6
- assert SearchBoxPortlet.create!(:name => "New Portlet")
7
- end
8
-
9
- end
@@ -1,587 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '/../test_helper')
2
-
3
-
4
- class SearchResultTest < ActiveSupport::TestCase
5
-
6
- def setup
7
- @xml_string = <<EOF
8
- <GSP>
9
- <RES>
10
- <M>2</M>
11
- <R N="1">
12
- <U>http://someurl.com</U>
13
- <T>TITLE</T>
14
- <S>BLURB</S>
15
- <HAS>
16
- <C SZ="1k" />
17
- </HAS>
18
- </R>
19
- <R N="2">
20
- <U>http://someurl2.com</U>
21
- <T>TITLE 2</T>
22
- <S>BLURB 2</S>
23
- <HAS>
24
- <C SZ="2k"/>
25
- </HAS>
26
- </R>
27
- </RES>
28
- </GSP>
29
- EOF
30
- @xml_doc = REXML::Document.new @xml_string
31
-
32
- @large_results_set = <<EOF
33
- <GSP>
34
- <RES>
35
- <M>35</M>
36
- <R N="1">
37
- <U>http://someurl.com</U>
38
- <T>TITLE</T>
39
- <S>BLURB</S>
40
- <HAS>
41
- <C SZ="1k" />
42
- </HAS>
43
- </R>
44
- <R N="2">
45
- <U>http://someurl2.com</U>
46
- <T>TITLE 2</T>
47
- <S>BLURB 2</S>
48
- <HAS>
49
- <C SZ="2k"/>
50
- </HAS>
51
- </R>
52
- </RES>
53
- </GSP>
54
- EOF
55
- @large_xml_doc = REXML::Document.new(@large_results_set)
56
-
57
- end
58
-
59
- test "Parse result count from google mini results xml." do
60
-
61
- xml = <<EOF
62
- <GSP>
63
- <RES>
64
- <M>35</M>
65
- </RES>
66
- </GSP>
67
- EOF
68
- empty_doc = REXML::Document.new xml
69
- assert_equal 35, SearchResult.parse_results_count(empty_doc)
70
- assert_equal 35, SearchResult.parse_xml(empty_doc).results_count
71
- assert_equal 4, SearchResult.parse_xml(empty_doc).num_pages
72
-
73
- end
74
-
75
- test "Default result count is zero." do
76
-
77
- xml = <<EOF
78
- <GSP>
79
- <RES>
80
- </RES>
81
- </GSP>
82
- EOF
83
- empty_doc = REXML::Document.new xml
84
- assert_equal 0, SearchResult.parse_results_count(empty_doc)
85
-
86
- end
87
-
88
- test "Empty xml gives empty results" do
89
- xml = <<EOF
90
- <GSP>
91
- <RES>
92
- </RES>
93
- </GSP>
94
- EOF
95
- empty_doc = REXML::Document.new xml
96
- SearchResult.expects(:fetch_xml_doc).with("therapy", {}).returns(empty_doc)
97
-
98
- assert_equal [], SearchResult.find("therapy", {})
99
- end
100
-
101
- test "Parse result set" do
102
-
103
- results = SearchResult.parse_hits(@xml_doc)
104
- assert_equal 2, results.size
105
-
106
- assert_equal "1", results[0].number
107
- assert_equal "http://someurl.com", results[0].url
108
- assert_equal "TITLE", results[0].title
109
- assert_equal "BLURB", results[0].description
110
- assert_equal "1k", results[0].size
111
-
112
- assert_equal "2", results[1].number
113
- assert_equal "http://someurl2.com", results[1].url
114
- assert_equal "TITLE 2", results[1].title
115
- assert_equal "BLURB 2", results[1].description
116
- assert_equal "2k", results[1].size
117
- end
118
-
119
-
120
- test "Calculates the results pages" do
121
- assert_equal 1, SearchResult.calculate_results_pages(9)
122
- assert_equal 1, SearchResult.calculate_results_pages(10)
123
- assert_equal 2, SearchResult.calculate_results_pages(19)
124
- assert_equal 2, SearchResult.calculate_results_pages(20)
125
- assert_equal 3, SearchResult.calculate_results_pages(21)
126
- assert_equal 4, SearchResult.calculate_results_pages(40)
127
- assert_equal 0, SearchResult.calculate_results_pages(0)
128
-
129
- end
130
-
131
- test "Calculates current page based on total results and start" do
132
- results = SearchResult::QueryResult.new
133
- results.start = 0
134
- assert_equal 1, results.current_page
135
-
136
- results.start = 10
137
- assert_equal 2, results.current_page
138
-
139
- results.start = 20
140
- assert_equal 3, results.current_page
141
-
142
- results.start = 30
143
- assert_equal 4, results.current_page
144
- end
145
-
146
- test "Next start" do
147
- r = SearchResult::QueryResult.new
148
- r.start = 0
149
- assert_equal 10, r.next_start
150
- r.start = 10
151
- assert_equal 20, r.next_start
152
- r.start = 20
153
- assert_equal 30, r.next_start
154
- end
155
-
156
- test "Find results should return a paging list of documents with no start" do
157
- SearchResult.expects(:fetch_xml_doc).with("therapy", {}).returns(@large_xml_doc)
158
-
159
- results = SearchResult.find("therapy")
160
- assert_equal "therapy", results.query
161
- assert_equal 35, results.results_count
162
- assert_equal 4, results.num_pages
163
- assert_equal 0, results.start
164
- assert_equal 2, results.size
165
- assert_equal 1, results.current_page
166
- assert_equal 10, results.next_start
167
- assert results.next_page?
168
- assert_equal -10, results.previous_start
169
- assert_equal false, results.previous_page?
170
- assert_equal (1..4), results.pages
171
-
172
- end
173
-
174
-
175
- test "Find results starts on page 2, if a start is specified" do
176
- SearchResult.expects(:fetch_xml_doc).with("therapy", :start=>10).returns(@large_xml_doc)
177
-
178
- results = SearchResult.find("therapy", :start => 10)
179
- assert_equal 35, results.results_count
180
- assert_equal 4, results.num_pages
181
- assert_equal 10, results.start
182
- assert_equal 2, results.size
183
- assert_equal 2, results.current_page
184
- assert_equal 20, results.next_start
185
- assert results.next_page?
186
- assert_equal 0, results.previous_start
187
- assert_equal true, results.previous_page?
188
- end
189
-
190
- test "No next start when on the last page" do
191
- SearchResult.expects(:fetch_xml_doc).with("therapy", {:start => 30}).returns(@large_xml_doc)
192
-
193
- results = SearchResult.find("therapy", :start => 30)
194
- assert_equal 40, results.next_start
195
- assert_equal false, results.next_page?
196
- assert_equal 20, results.previous_start
197
- assert_equal true, results.previous_page?
198
- end
199
-
200
- test "Should be no previous or next for a single page of results" do
201
- SearchResult.expects(:fetch_xml_doc).with("therapy", {}).returns(@xml_doc)
202
-
203
- results = SearchResult.find("therapy")
204
- assert_equal false, results.next_page?
205
- assert_equal false, results.previous_page?
206
- assert_equal [], results.pages
207
- end
208
-
209
-
210
-
211
- test "current_page should check to see if the current page matches" do
212
- results = SearchResult::QueryResult.new
213
- results.start = 0
214
-
215
- assert_equal true, results.current_page?(1)
216
- assert_equal false, results.current_page?(2)
217
- assert_equal false, results.current_page?(3)
218
- assert_equal false, results.current_page?(4)
219
-
220
- end
221
-
222
-
223
- test "Portlet attributes are used to look up path" do
224
- portlet = GoogleMiniSearchEnginePortlet.new(:name=>"Engine", :path => "/engine")
225
- SearchResult.expects(:fetch_xml_doc).with("therapy", {:portlet=> portlet}).returns(@xml_doc)
226
-
227
- results = SearchResult.find("therapy", {:portlet=> portlet})
228
-
229
- assert_equal "/engine", results.path
230
- end
231
-
232
- test "Default path is used if no portlet specified" do
233
- SearchResult.expects(:fetch_xml_doc).with("therapy", {}).returns(@xml_doc)
234
- results = SearchResult.find("therapy", {})
235
- assert_equal "/search/search-results", results.path
236
- end
237
-
238
- test "Uses service URL from portlet" do
239
- portlet = GoogleMiniSearchEnginePortlet.new(
240
- :name=>"Engine", :path => "/engine", :service_url => "http://mini.someurl.com",
241
- :collection_name => "COLLECT", :front_end_name => "FRONT_END")
242
-
243
- url = SearchResult.create_url_for_query({:portlet => portlet}, "STUFF")
244
- assert_equal "http://mini.someurl.com/search?q=STUFF&output=xml_no_dtd&client=FRONT_END&site=COLLECT&filter=0&oe=UTF-8&ie=UTF-8", url
245
-
246
- url = SearchResult.create_url_for_query({:portlet => portlet, :start=>100}, "STUFF")
247
- assert_equal "http://mini.someurl.com/search?q=STUFF&output=xml_no_dtd&client=FRONT_END&site=COLLECT&filter=0&start=100&oe=UTF-8&ie=UTF-8", url
248
-
249
- end
250
-
251
- test "Create Engine and Query from portlet attributes" do
252
- portlet = GoogleMiniSearchEnginePortlet.new(
253
- :name=>"Engine", :path => "/engine", :service_url => "http://mini.someurl.com",
254
- :collection_name => "COLLECT", :front_end_name => "FRONT_END")
255
-
256
- query = SearchResult.create_query("therapy", {:portlet=>portlet})
257
- assert_equal "http://mini.someurl.com", query.engine.host
258
- assert_equal portlet.front_end_name, query.front_end
259
- assert_equal portlet.collection_name, query.collection
260
- assert_equal "therapy", query.query
261
- end
262
-
263
- test "should look up options from portlet and add to hash" do
264
- portlet = GoogleMiniSearchEnginePortlet.new(
265
- :name=>"Engine", :path => "/engine", :service_url => "http://mini.someurl.com",
266
- :collection_name => "COLLECT", :front_end_name => "FRONT_END")
267
- options = {:portlet=>portlet}
268
- SearchResult.normalize_query_options(options)
269
-
270
- assert_equal "FRONT_END", options[:front_end]
271
- assert_equal "COLLECT", options[:collection]
272
- assert_equal "http://mini.someurl.com", options[:host]
273
- assert_equal nil, options[:portlet]
274
- end
275
-
276
- test "Create an appliance from attributes in the portlet." do
277
- portlet = GoogleMiniSearchEnginePortlet.new(
278
- :name=>"Engine", :path => "/engine", :service_url => "http://mini.someurl.com",
279
- :collection_name => "COLLECT", :front_end_name => "FRONT_END")
280
-
281
- gsa = SearchResult.new_gsa(portlet)
282
- assert_equal "http://mini.someurl.com", gsa.host
283
- assert_equal "FRONT_END", gsa.default_front_end
284
- assert_equal "COLLECT", gsa.default_collection
285
- end
286
-
287
- test "Explicitly passing a collection in will query with that rather than a default collection" do
288
- portlet = GoogleMiniSearchEnginePortlet.new(
289
- :name=>"Engine", :path => "/engine", :service_url => "http://mini.someurl.com",
290
- :collection_name => "COLLECT", :front_end_name => "FRONT_END")
291
-
292
- url = SearchResult.create_url_for_query({:portlet => portlet, :site=>"ANOTHER_COLLECTION"}, "STUFF")
293
- assert_equal "http://mini.someurl.com/search?q=STUFF&output=xml_no_dtd&client=FRONT_END&site=ANOTHER_COLLECTION&filter=0&oe=UTF-8&ie=UTF-8", url
294
- end
295
-
296
- test "Handles multiword queries" do
297
- url = SearchResult.create_url_for_query({}, "One Two")
298
- assert_equal "/search?q=One+Two&output=xml_no_dtd&client=&site=&filter=0&oe=UTF-8&ie=UTF-8", url
299
- end
300
-
301
- test "sort is added to google mini query" do
302
- url = SearchResult.create_url_for_query({:sort=>"XYZ"}, "STUFF")
303
- assert_equal "/search?q=STUFF&output=xml_no_dtd&client=&site=&filter=0&sort=XYZ&oe=UTF-8&ie=UTF-8", url
304
- end
305
-
306
- test "sort params are escaped" do
307
- url = SearchResult.create_url_for_query({:sort=>"date:D:S:d1"}, "STUFF")
308
- assert_equal "/search?q=STUFF&output=xml_no_dtd&client=&site=&filter=0&sort=date%3AD%3AS%3Ad1&oe=UTF-8&ie=UTF-8", url
309
- end
310
-
311
- test "Handles keymatches in results" do
312
- @xml_with_keymatches = <<XML
313
- <GSP>
314
- <GM>
315
- <GL>http://url1.org</GL>
316
- <GD>URL 1</GD>
317
- </GM>
318
- <GM>
319
- <GL>http://url2.org</GL>
320
- <GD>URL 2</GD>
321
- </GM>
322
- <RES>
323
- <M>35</M>
324
- <R N="1">
325
- <U>http://someurl.com</U>
326
- <T>TITLE</T>
327
- <S>BLURB</S>
328
- <HAS>
329
- <C SZ="1k" />
330
- </HAS>
331
- </R>
332
- <R N="2">
333
- <U>http://someurl2.com</U>
334
- <T>TITLE 2</T>
335
- <S>BLURB 2</S>
336
- <HAS>
337
- <C SZ="2k"/>
338
- </HAS>
339
- </R>
340
- </RES>
341
- </GSP>
342
- XML
343
- @results_with_keymatches = REXML::Document.new @xml_with_keymatches
344
-
345
- result = SearchResult.parse_xml @results_with_keymatches
346
-
347
- assert_equal true, result.key_matches?
348
- assert_equal 2, result.key_matches.size
349
- assert_equal "http://url1.org", result.key_matches[0].url
350
- assert_equal "URL 1", result.key_matches[0].title
351
- assert_equal "http://url2.org", result.key_matches[1].url
352
- assert_equal "URL 2", result.key_matches[1].title
353
- end
354
-
355
- test "Handles results with no keymatches" do
356
- result = SearchResult.parse_xml @xml_doc
357
- assert_equal false, result.key_matches?
358
- end
359
-
360
- test "Handle Synonyms / Related Queries" do
361
- xml_with_synonyms = <<XML
362
- <GSP>
363
- <Synonyms>
364
- <OneSynonym q="Query 1">Label 1</OneSynonym>
365
- <OneSynonym q="Query 2">Label 2</OneSynonym>
366
- </Synonyms>
367
- <RES>
368
- <M>35</M>
369
- <R N="1">
370
- <U>http://someurl.com</U>
371
- <T>TITLE</T>
372
- <S>BLURB</S>
373
- <HAS>
374
- <C SZ="1k" />
375
- </HAS>
376
- </R>
377
- <R N="2">
378
- <U>http://someurl2.com</U>
379
- <T>TITLE 2</T>
380
- <S>BLURB 2</S>
381
- <HAS>
382
- <C SZ="2k"/>
383
- </HAS>
384
- </R>
385
- </RES>
386
- </GSP>
387
- XML
388
- xml_doc_with_synonyms = REXML::Document.new xml_with_synonyms
389
-
390
- result = SearchResult.parse_xml xml_doc_with_synonyms
391
-
392
- result.expects(:path).returns("/search").twice
393
- assert_equal true, result.synonyms?
394
- assert_equal 2, result.synonyms.size
395
- assert_equal "Label 1", result.synonyms[0].label
396
- assert_equal "Query 1", result.synonyms[0].query
397
- assert_equal "/search?query=Query 1", result.synonyms[0].url
398
- assert_equal "Label 2", result.synonyms[1].label
399
- assert_equal "Query 2", result.synonyms[1].query
400
- assert_equal "/search?query=Query 2", result.synonyms[1].url
401
-
402
- end
403
-
404
- test "Handles results with no Synonyms" do
405
- result = SearchResult.parse_xml @xml_doc
406
- assert_equal false, result.synonyms?
407
- end
408
-
409
- test "Calculate URL for Related Queries/Synonyms" do
410
- syn = SearchResult::Synonym.new
411
- syn.query = "Testing"
412
- mock_result = mock()
413
- mock_result.expects(:path).returns("/random")
414
- syn.query_result = mock_result
415
- assert_equal "/random?query=Testing", syn.url
416
- end
417
-
418
- test "No query = empty results" do
419
- result = SearchResult.find(nil)
420
- assert_equal 0, result.size
421
- assert_equal 0, result.start
422
- assert_equal false, result.next_page?
423
- assert_equal false, result.previous_page?
424
- assert_equal false, result.key_matches?
425
- assert_equal false, result.synonyms?
426
- assert_equal [], result.pages
427
- end
428
-
429
- test "Search results for PDF's that have no size" do
430
- pdf_results = <<EOF
431
- <GSP>
432
- <RES>
433
- <M>35</M>
434
- <R N="1">
435
- <U>http://someurl.com</U>
436
- <T>TITLE</T>
437
- <S>BLURB</S>
438
- <HAS>
439
- <C SZ="1k" />
440
- </HAS>
441
- </R>
442
- <R N="2">
443
- <U>http://someurl2.com</U>
444
- <T>TITLE 2</T>
445
- <S>BLURB 2</S>
446
- <HAS>
447
- <L/>
448
- </HAS>
449
- </R>
450
- </RES>
451
- </GSP>
452
- EOF
453
- xml_doc = REXML::Document.new(pdf_results)
454
- results = SearchResult.parse_xml xml_doc
455
- assert_equal 2, results.size
456
-
457
- assert_equal "1", results[0].number
458
- assert_equal "http://someurl.com", results[0].url
459
- assert_equal "TITLE", results[0].title
460
- assert_equal "BLURB", results[0].description
461
- assert_equal "1k", results[0].size
462
-
463
- assert_equal "2", results[1].number
464
- assert_equal "http://someurl2.com", results[1].url
465
- assert_equal "TITLE 2", results[1].title
466
- assert_equal "BLURB 2", results[1].description
467
- assert_equal "", results[1].size
468
- end
469
-
470
- end
471
-
472
- class SearchPathsTest < ActiveSupport::TestCase
473
-
474
- def setup
475
- @results = SearchResult::QueryResult.new
476
- @results.start = 0
477
- @results.path = "/search/search-results"
478
- @results.query = "X"
479
- end
480
-
481
- test "path_for" do
482
- assert_equal "/search/search-results?query=Y", @results.path_for("Y")
483
- end
484
-
485
- test "sort by date" do
486
- assert_equal "#{@results.path}?query=#{@results.query}&sort=#{SearchResult::QueryResult::SORT_BY_DATE_PARAM}", @results.sort_by_date_path
487
- end
488
-
489
- test "sort by relevance" do
490
- assert @results.sort_by_relevance_path != @results.sort_by_date_path, "Paths should not be the same."
491
- assert_equal "#{@results.path}?query=#{@results.query}&sort=#{SearchResult::QueryResult::SORT_BY_RELEVANCE_PARAM}", @results.sort_by_relevance_path
492
- end
493
-
494
- test "Path to next page" do
495
- assert_equal "/search/search-results?query=X&start=10", @results.next_page_path
496
- end
497
-
498
- test "Path to previous page" do
499
- @results.start = 20
500
- assert_equal "/search/search-results?query=X&start=10", @results.previous_page_path
501
- end
502
-
503
- test "Sets path to default search-results" do
504
- assert_equal "/search/search-results", @results.path
505
- end
506
-
507
- test "Setting path overrides the defaults" do
508
- @results.path = "/other"
509
- assert_equal "/other", @results.path
510
- end
511
-
512
- test "page_path" do
513
- assert_equal "/search/search-results?query=X&start=0", @results.page_path(1)
514
- assert_equal "/search/search-results?query=X&start=10", @results.page_path(2)
515
- assert_equal "/search/search-results?query=X&start=20", @results.page_path(3)
516
- assert_equal "/search/search-results?query=X&start=30", @results.page_path(4)
517
- end
518
-
519
- test "sorting_by_date?" do
520
- assert_equal true, @results.sorting_by_date?({:sort=>SearchResult::QueryResult::SORT_BY_DATE_PARAM})
521
- assert_equal false, @results.sorting_by_date?({:sort=>SearchResult::QueryResult::SORT_BY_RELEVANCE_PARAM})
522
- assert_equal false, @results.sorting_by_date?({})
523
- end
524
- end
525
-
526
-
527
- class PagingTest < ActiveSupport::TestCase
528
-
529
- def setup
530
- @results = SearchResult::QueryResult.new
531
- and_the_max_number_pages_is 100
532
- end
533
-
534
- test "Behavior of Ruby Ranges" do
535
- c = 0
536
- (1..4).each_with_index do |i, count|
537
- assert_equal count + 1, i
538
- c = count
539
- end
540
- assert_equal 3, c
541
- end
542
-
543
-
544
- test "When on page 1, show links for pages 1 - 10" do
545
- when_current_page_is(1)
546
- assert_equal (1..10), @results.pages
547
- end
548
-
549
- test "When on page 11, show links for pages 1-20" do
550
- when_current_page_is(11)
551
- assert_equal (1..20), @results.pages
552
- end
553
-
554
- test "When on page 12, show links for pages 2-22" do
555
- when_current_page_is 12
556
- assert_equal (2..21), @results.pages
557
- end
558
-
559
- test "When less than 10 pages only show up to last page" do
560
- when_current_page_is 1
561
- and_the_max_number_pages_is 4
562
-
563
- assert_equal (1..4), @results.pages
564
- end
565
-
566
- test "When no results, should be empty set of pages." do
567
- when_current_page_is 1
568
- and_the_max_number_pages_is 0
569
- assert_equal [], @results.pages
570
- end
571
-
572
- test "With one page, return a single page." do
573
- when_current_page_is 1
574
- and_the_max_number_pages_is 1
575
- assert_equal [], @results.pages, "A single page of results needs no pager control"
576
- end
577
-
578
- private
579
-
580
- def and_the_max_number_pages_is(number)
581
- @results.expects(:num_pages).returns(number).times(0..5)
582
- end
583
-
584
- def when_current_page_is(current_page)
585
- @results.expects(:current_page).returns(current_page).times(0..5)
586
- end
587
- end