From 32625f08b9c7e7bb65bd24099e64d7d17be5e45e Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Fri, 16 Aug 2013 18:07:37 +0100 Subject: Add a test for the PublicBodyController statistics action --- spec/controllers/public_body_controller_spec.rb | 34 +++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 4e1841164..000d1cf80 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -208,6 +208,40 @@ describe PublicBodyController, "when showing JSON version for API" do end +describe PublicBodyController, "when showing public body statistics" do + + it "should render the right template with the right data" do + config = MySociety::Config.load_default() + config['MINIMUM_REQUESTS_FOR_STATISTICS'] = 1 + get :statistics + response.should render_template('public_body/statistics') + # There are 5 different graphs we're creating at the moment. + assigns[:graph_list].length.should == 5 + # The first is the only one with raw values, the rest are + # percentages with error bars: + assigns[:graph_list].each_with_index do |graph, index| + if index == 0 + graph['errorbars'].should be_false + graph['x_values'].length.should == 4 + graph['x_values'].should == [0, 1, 2, 3] + graph['y_values'].should == [1, 2, 2, 4] + else + graph['errorbars'].should be_true + # Just check the first one: + if index == 1 + graph['x_values'].should == [0, 1, 2, 3] + graph['y_values'].should == [0, 50, 100, 100] + end + # Check that at least every confidence interval value is + # numeric: + graph['cis_below'].each { |v| v.should be_instance_of(Float) } + graph['cis_above'].each { |v| v.should be_instance_of(Float) } + end + end + end + +end + describe PublicBodyController, "when doing type ahead searches" do render_views -- cgit v1.2.3 From e5855f7fd2657574c5af99890c63d530ca3bb5d0 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Mon, 19 Aug 2013 10:34:05 +0100 Subject: Improve calculation of PublicBody statistics columns On PublicBody, we don't need to update info_requests_count because that's already done with :counter_cache. On the other hand, info_requests_successful_count and info_requests_not_held_count can't be updated easily with counter_cache (since they need conditions to be attached). Instead we update them in post_save and post_destroy, as suggested here: http://blog.douglasfshearer.com/post/17495285851/custom-counter-cache-with-conditions This also adds tests to ensure that the after_(save|destroy) callbacks are called and that they modify the counts correctly. --- spec/controllers/public_body_controller_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 000d1cf80..87544b96a 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -233,7 +233,7 @@ describe PublicBodyController, "when showing public body statistics" do graph['y_values'].should == [0, 50, 100, 100] end # Check that at least every confidence interval value is - # numeric: + # a Float (rather than NilClass, say): graph['cis_below'].each { |v| v.should be_instance_of(Float) } graph['cis_above'].each { |v| v.should be_instance_of(Float) } end -- cgit v1.2.3 From b75d79b08227192b75e4f3b24ce4e762f4286ef9 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Tue, 20 Aug 2013 10:04:54 +0100 Subject: Add a config option to enable the public body statistics page --- spec/controllers/public_body_controller_spec.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 87544b96a..92130f3d0 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -213,6 +213,7 @@ describe PublicBodyController, "when showing public body statistics" do it "should render the right template with the right data" do config = MySociety::Config.load_default() config['MINIMUM_REQUESTS_FOR_STATISTICS'] = 1 + config['PUBLIC_BODY_STATISTICS_PAGE'] = true get :statistics response.should render_template('public_body/statistics') # There are 5 different graphs we're creating at the moment. -- cgit v1.2.3 From 3fb80a12652792df9da06c22b4bb944ab80bf376 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Thu, 29 Aug 2013 10:24:02 +0100 Subject: Make falling back to default locale in public body listings optional As the code stood, the list method in PublicBodyController would only return results that had translations of the public body in the default locale. This has a variety of problems if you're viewing pages in the non-default locale - for example, the "first letter" links wouldn't bring up the public bodies that began with that letter in the current locale, only those that began with it in the default locale. Ideally, every public body would be translated into every available locale for the site, but there are cases where deployers wish to have public body listings also include those from the default locale, in case there are untralsated public bodies: https://groups.google.com/d/msg/alaveteli-dev/zUY_USaAMAM/M7KTQ9RC5YUJ This commit makes the default behaviour to look for public body listings only in the current locale, but if the new configuration option PUBLIC_BODY_LIST_FALLBACK_TO_DEFAULT_LOCALE is set, then public body listings will be looked for in both the current locale and the default locale. Fixes #1000 --- spec/controllers/public_body_controller_spec.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 4e1841164..2b921b488 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -78,7 +78,8 @@ describe PublicBodyController, "when listing bodies" do response.should be_success end - it "should list all bodies from default locale, even when there are no translations for selected locale" do + it "if fallback is requested, should list all bodies from default locale, even when there are no translations for selected locale" do + AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) I18n.with_locale(:en) do @english_only = PublicBody.new(:name => 'English only', :short_name => 'EO', @@ -96,6 +97,12 @@ describe PublicBodyController, "when listing bodies" do response.should contain('El Department for Humpadinking') end + it 'should not show the internal admin authority' do + PublicBody.internal_admin_body + get :list, {:locale => 'en'} + response.should_not contain('Internal admin authority') + end + it 'should show public body names in the selected locale language if present for a locale with underscores' do AlaveteliLocalization.set_locales('he_IL en', 'en') get :list, {:locale => 'he_IL'} -- cgit v1.2.3 From 6395868beaaefb5525c19ce8b5deeaeb61925883 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Tue, 10 Sep 2013 13:54:52 +0100 Subject: Add tests to assert the new public body listing behaviour These tests will fail unless we have the new behaviour for public body listing, which depend on the setting of PUBLIC_BODY_LIST_FALLBACK_TO_DEFAULT_LOCALE. --- spec/controllers/public_body_controller_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 2b921b488..1700053fb 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -92,6 +92,20 @@ describe PublicBodyController, "when listing bodies" do assigns[:public_bodies].include?(@english_only).should == true end + it 'if fallback is requested, should still list public bodies only with translations in the current locale' do + AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) + I18n.with_locale(:es) do + @spanish_only = PublicBody.new(:name => 'Español Solamente', + :short_name => 'ES', + :request_email => 'spanish@flourish.org', + :last_edit_editor => 'test', + :last_edit_comment => '') + @spanish_only.save + end + get :list, {:locale => 'es'} + assigns[:public_bodies].include?(@spanish_only).should == true + end + it 'should show public body names in the selected locale language if present' do get :list, {:locale => 'es'} response.should contain('El Department for Humpadinking') -- cgit v1.2.3 From 8146051161e0359b621019bb25091f2aa0634594 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Tue, 10 Sep 2013 14:12:27 +0100 Subject: Factor out the code to generate example single language public bodies It's useful to be able to generate example public bodies that only have translations in particular languages. --- spec/controllers/public_body_controller_spec.rb | 39 +++++++++++++++---------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 1700053fb..d4f359a80 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -78,30 +78,37 @@ describe PublicBodyController, "when listing bodies" do response.should be_success end + def make_single_language_example(locale) + result = nil + I18n.with_locale(locale) do + case locale + when :en + result = PublicBody.new(:name => 'English only', + :short_name => 'EO') + when :es + result = PublicBody.new(:name => 'Español Solamente', + :short_name => 'ES') + else + raise StandardError.new "Unknown locale #{locale}" + end + result.request_email = "#{locale}@example.org" + result.last_edit_editor = 'test' + result.last_edit_comment = '' + result.save + end + result + end + it "if fallback is requested, should list all bodies from default locale, even when there are no translations for selected locale" do AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) - I18n.with_locale(:en) do - @english_only = PublicBody.new(:name => 'English only', - :short_name => 'EO', - :request_email => 'english@flourish.org', - :last_edit_editor => 'test', - :last_edit_comment => '') - @english_only.save - end + @english_only = make_single_language_example :en get :list, {:locale => 'es'} assigns[:public_bodies].include?(@english_only).should == true end it 'if fallback is requested, should still list public bodies only with translations in the current locale' do AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) - I18n.with_locale(:es) do - @spanish_only = PublicBody.new(:name => 'Español Solamente', - :short_name => 'ES', - :request_email => 'spanish@flourish.org', - :last_edit_editor => 'test', - :last_edit_comment => '') - @spanish_only.save - end + @spanish_only = make_single_language_example :es get :list, {:locale => 'es'} assigns[:public_bodies].include?(@spanish_only).should == true end -- cgit v1.2.3 From dff7f73bc0ce0054cd4c837357b1ad3cf7bdfe1c Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Tue, 10 Sep 2013 14:31:12 +0100 Subject: Don't display duplicate public bodies with the fallback This introduces some raw SQL statement for the fallback case, but we can't see an easy way to avoid that in this case. This commit also adds some tests that assert the sorting and non-duplication properties of the listing. Thanks to Louise Crow for working out the SQL expression for falling back to the default locale. --- spec/controllers/public_body_controller_spec.rb | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'spec/controllers/public_body_controller_spec.rb') diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index d4f359a80..70ed482b2 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -1,6 +1,8 @@ # coding: utf-8 require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +require 'nokogiri' + describe PublicBodyController, "when showing a body" do render_views @@ -99,6 +101,14 @@ describe PublicBodyController, "when listing bodies" do result end + it "with no fallback, should only return bodies from the current locale" do + @english_only = make_single_language_example :en + @spanish_only = make_single_language_example :es + get :list, {:locale => 'es'} + assigns[:public_bodies].include?(@english_only).should == false + assigns[:public_bodies].include?(@spanish_only).should == true + end + it "if fallback is requested, should list all bodies from default locale, even when there are no translations for selected locale" do AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) @english_only = make_single_language_example :en @@ -113,6 +123,14 @@ describe PublicBodyController, "when listing bodies" do assigns[:public_bodies].include?(@spanish_only).should == true end + it "if fallback is requested, make sure that there are no duplicates listed" do + AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) + get :list, {:locale => 'es'} + pb_ids = assigns[:public_bodies].map { |pb| pb.id } + unique_pb_ids = pb_ids.uniq + pb_ids.sort.should === unique_pb_ids.sort + end + it 'should show public body names in the selected locale language if present' do get :list, {:locale => 'es'} response.should contain('El Department for Humpadinking') @@ -124,6 +142,18 @@ describe PublicBodyController, "when listing bodies" do response.should_not contain('Internal admin authority') end + it 'should order on the translated name, even with the fallback' do + # The names of each public body is in: + # Public Body Name + # ... eo extract all of those, and check that they are ordered: + AlaveteliConfiguration.stub!(:public_body_list_fallback_to_default_locale).and_return(true) + get :list, {:locale => 'es'} + parsed = Nokogiri::HTML(response.body) + public_body_names = parsed.xpath '//span[@class="head"]/a/text()' + public_body_names = public_body_names.map { |pb| pb.to_s } + public_body_names.should == public_body_names.sort + end + it 'should show public body names in the selected locale language if present for a locale with underscores' do AlaveteliLocalization.set_locales('he_IL en', 'en') get :list, {:locale => 'he_IL'} -- cgit v1.2.3