aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSeb Bacon <seb.bacon@gmail.com>2011-08-29 13:13:59 +0100
committerSeb Bacon <seb.bacon@gmail.com>2011-08-29 13:13:59 +0100
commitb41edc7ae069e6071f7ff7223c1e60cca5e75e8c (patch)
treef2d7a1d218465247843036ccf8153fd8994afd30
parenteef876ff7ce15f1a15e6760a1f251c98d383cf4e (diff)
Add tests & fixes for new search/filtering functionality
-rw-r--r--app/controllers/application_controller.rb4
-rw-r--r--app/controllers/general_controller.rb1
-rw-r--r--app/controllers/public_body_controller.rb12
-rw-r--r--app/controllers/request_controller.rb1
-rw-r--r--app/views/general/search.rhtml2
-rw-r--r--spec/controllers/general_controller_spec.rb28
-rw-r--r--spec/controllers/public_body_controller_spec.rb10
-rw-r--r--spec/controllers/request_controller_spec.rb16
-rw-r--r--spec/integration/search_request_spec.rb48
9 files changed, 112 insertions, 10 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index cb7f3b23d..cb64cb922 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -413,10 +413,10 @@ class ApplicationController < ActionController::Base
def get_date_range_from_params
query = ""
if param_exists(:request_date_after) && !param_exists(:request_date_before)
- params[:request_date_before] = Date.now.strftime("%d/%m/%Y")
+ params[:request_date_before] = Time.now.strftime("%d/%m/%Y")
query += " #{params[:request_date_after]}..#{params[:request_date_before]}"
elsif !param_exists(:request_date_after) && param_exists(:request_date_before)
- params[:request_date_after] = "01/01/2008"
+ params[:request_date_after] = "01/01/2001"
end
if param_exists(:request_date_after)
query = " #{params[:request_date_after]}..#{params[:request_date_before]}"
diff --git a/app/controllers/general_controller.rb b/app/controllers/general_controller.rb
index 2d0527d3a..40c79cdb8 100644
--- a/app/controllers/general_controller.rb
+++ b/app/controllers/general_controller.rb
@@ -139,7 +139,6 @@ class GeneralController < ApplicationController
@sort_postfix = combined.pop
@sortby = @sort_postfix
end
-
if combined.size > 0 && (['bodies', 'requests', 'users', 'all'].include?(combined[-1]))
@variety_postfix = combined.pop
case @variety_postfix
diff --git a/app/controllers/public_body_controller.rb b/app/controllers/public_body_controller.rb
index d7d8c2c56..830f37855 100644
--- a/app/controllers/public_body_controller.rb
+++ b/app/controllers/public_body_controller.rb
@@ -90,27 +90,27 @@ class PublicBodyController < ApplicationController
@query = "%#{params[:public_body_query].nil? ? "" : params[:public_body_query]}%"
@tag = params[:tag]
@locale = self.locale_from_params()
- locale_condition = "upper(public_body_translations.name) LIKE upper(?) AND public_body_translations.locale = ?"
+ locale_condition = "(upper(public_body_translations.name) LIKE upper(?) OR upper(public_body_translations.notes) LIKE upper (?)) AND public_body_translations.locale = ?"
if @tag.nil? or @tag == "all"
@tag = "all"
- conditions = [locale_condition, @query, @locale]
+ conditions = [locale_condition, @query, @query, @locale]
elsif @tag == 'other'
category_list = PublicBodyCategories::CATEGORIES.map{|c| "'"+c+"'"}.join(",")
conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id
and has_tag_string_tags.model = \'PublicBody\'
- and has_tag_string_tags.name in (' + category_list + ')) = 0', @query, @locale]
+ and has_tag_string_tags.name in (' + category_list + ')) = 0', @query, @query, @locale]
elsif @tag.size == 1
@tag.upcase!
- conditions = [locale_condition + ' AND public_body_translations.first_letter = ?', @query, @locale, @tag]
+ conditions = [locale_condition + ' AND public_body_translations.first_letter = ?', @query, @query, @locale, @tag]
elsif @tag.include?(":")
name, value = HasTagString::HasTagStringTag.split_tag_into_name_value(@tag)
conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id
and has_tag_string_tags.model = \'PublicBody\'
- and has_tag_string_tags.name = ? and has_tag_string_tags.value = ?) > 0', @query, @locale, name, value]
+ and has_tag_string_tags.name = ? and has_tag_string_tags.value = ?) > 0', @query, @query, @locale, name, value]
else
conditions = [locale_condition + ' AND (select count(*) from has_tag_string_tags where has_tag_string_tags.model_id = public_bodies.id
and has_tag_string_tags.model = \'PublicBody\'
- and has_tag_string_tags.name = ?) > 0', @query, @locale, @tag]
+ and has_tag_string_tags.name = ?) > 0', @query, @query, @locale, @tag]
end
if @tag.size == 1
@description = _("beginning with") + " '" + @tag + "'"
diff --git a/app/controllers/request_controller.rb b/app/controllers/request_controller.rb
index 3c283baae..79b8e4816 100644
--- a/app/controllers/request_controller.rb
+++ b/app/controllers/request_controller.rb
@@ -134,6 +134,7 @@ class RequestController < ApplicationController
@title = "View and search requests"
sortby = "newest"
@page = get_search_page_from_params if !@page # used in cache case, as perform_search sets @page as side effect
+
behavior_cache :tag => [@view, @page] do
xapian_object = perform_search([InfoRequestEvent], query, sortby, 'request_collapse')
@list_results = xapian_object.results.map { |r| r[:model] }
diff --git a/app/views/general/search.rhtml b/app/views/general/search.rhtml
index 95ce217ba..15e00b85e 100644
--- a/app/views/general/search.rhtml
+++ b/app/views/general/search.rhtml
@@ -164,7 +164,7 @@
<% if @xapian_requests_hits == 1 && @page == 1 %>
<h1><%= _("One FOI request matching your search", :user_search_query => h(@query)) %></h1>
<% else %>
- <h1><%= _("FOI requests {{start_count}} to {{end_count}} of {{total_count}}", :start_count => ((@page-1)*@requests_per_page+1).to_s, :end_count => [@page*@requests_per_page, @xapian_requests.matches_estimated].min.to_s, :total_count => @xapian_requests.matches_estimated.to_s, :user_search_query => h(@query)) %></h1>
+ <h1><%= _("FOI requests {{start_count}} to {{end_count}} of {{total_count}}", :start_count => ((@page-1)*@requests_per_page+1).to_s, :end_count => [@page*@requests_per_page, @xapian_requests.matches_estimated].min.to_s, :total_count => @xapian_requests.matches_estimated.to_s, :user_search_query => h(@query)) %></h1>
<% end %>
<% if @track_thing %>
diff --git a/spec/controllers/general_controller_spec.rb b/spec/controllers/general_controller_spec.rb
index a76cdd3b6..4d7f1831d 100644
--- a/spec/controllers/general_controller_spec.rb
+++ b/spec/controllers/general_controller_spec.rb
@@ -122,6 +122,34 @@ describe GeneralController, "when searching" do
end
+ it "should filter results based on end of URL being 'all'" do
+ get :search, :combined => ['"bob"', "all"]
+ assigns[:xapian_requests].results.size.should == 2
+ assigns[:xapian_users].results.size.should == 1
+ assigns[:xapian_bodies].results.size.should == 0
+ end
+
+ it "should filter results based on end of URL being 'users'" do
+ get :search, :combined => ['"bob"', "users"]
+ assigns[:xapian_requests].should == nil
+ assigns[:xapian_users].results.size.should == 1
+ assigns[:xapian_bodies].should == nil
+ end
+
+ it "should filter results based on end of URL being 'requests'" do
+ get :search, :combined => ['"bob"', "requests"]
+ assigns[:xapian_requests].results.size.should == 2
+ assigns[:xapian_users].should == nil
+ assigns[:xapian_bodies].should == nil
+ end
+
+ it "should filter results based on end of URL being 'bodies'" do
+ get :search, :combined => ['"quango"', "bodies"]
+ assigns[:xapian_requests].should == nil
+ assigns[:xapian_users].should == nil
+ assigns[:xapian_bodies].results.size.should == 1
+ end
+
it "should show help when searching for nothing" do
get :search_redirect, :query => nil
response.should render_template('search')
diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb
index 0ac2726ea..df3fc10dc 100644
--- a/spec/controllers/public_body_controller_spec.rb
+++ b/spec/controllers/public_body_controller_spec.rb
@@ -78,6 +78,16 @@ describe PublicBodyController, "when listing bodies" do
assigns[:description].should == "all"
end
+ it "should support simple searching of bodies by title" do
+ get :list, :public_body_query => 'quango'
+ assigns[:public_bodies].should == [ public_bodies(:geraldine_public_body) ]
+ end
+
+ it "should support simple searching of bodies by notes" do
+ get :list, :public_body_query => 'Albatross'
+ assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body) ]
+ end
+
it "should list bodies in alphabetical order with different locale" do
I18n.default_locale = :es
get :list
diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb
index 7d0cd2c1a..2b153ac7a 100644
--- a/spec/controllers/request_controller_spec.rb
+++ b/spec/controllers/request_controller_spec.rb
@@ -20,6 +20,22 @@ describe RequestController, "when listing recent requests" do
response.should render_template('list')
end
+ it "should filter requests" do
+ get :list, :view => 'all'
+ assigns[:list_results].size.should == 2
+ get :list, :view => 'successful'
+ assigns[:list_results].size.should == 0
+ end
+
+ it "should filter requests by date" do
+ get :list, :view => 'all', :request_date_before => '13/10/2007'
+ assigns[:list_results].size.should == 1
+ get :list, :view => 'all', :request_date_after => '13/10/2007'
+ assigns[:list_results].size.should == 1
+ get :list, :view => 'all', :request_date_after => '10/10/2007', :request_date_before => '01/01/2010'
+ assigns[:list_results].size.should == 2
+ end
+
it "should assign the first page of results" do
xap_results = mock_model(ActsAsXapian::Search,
:results => (1..25).to_a.map { |m| { :model => m } },
diff --git a/spec/integration/search_request_spec.rb b/spec/integration/search_request_spec.rb
index 9398519b7..84239f7a3 100644
--- a/spec/integration/search_request_spec.rb
+++ b/spec/integration/search_request_spec.rb
@@ -1,9 +1,57 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe "When searching" do
+
+ fixtures [ :info_requests,
+ :info_request_events,
+ :public_bodies,
+ :public_body_translations,
+ :users,
+ :raw_emails,
+ :outgoing_messages,
+ :incoming_messages,
+ :comments ]
+
+ before(:each) do
+ load_raw_emails_data(raw_emails)
+ end
+
it "should not strip quotes from quoted query" do
request_via_redirect("post", "/search", :query => '"mouse stilton"')
response.body.should include("&quot;mouse stilton&quot;")
end
+
+ it "should correctly execute simple search" do
+ request_via_redirect("post", "/search",
+ :query => 'bob'
+ )
+ response.body.should include("One person matching")
+ end
+
+ it "should correctly filter searches for requests" do
+ request_via_redirect("post", "/search/bob/requests")
+ response.body.should_not include("One person matching")
+ response.body.should include("FOI requests 1 to 2 of 2")
+ end
+
+ it "should correctly filter searches for successful requests" do
+ request_via_redirect("post", "/search",
+ :query => "bob",
+ :latest_status => ['successful'])
+ response.body.should include("no requests matching your query")
+ end
+
+ it "should correctly filter searches for comments" do
+ request_via_redirect("post", "/search",
+ :query => "daftest",
+ :request_variety => ['comments'])
+ response.body.should include("One FOI request matching your search")
+
+ request_via_redirect("post", "/search",
+ :query => "daftest",
+ :request_variety => ['response','sent'])
+ response.body.should include("no requests matching your query")
+ end
+
end