aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/public_body.rb14
-rw-r--r--spec/models/public_body_spec.rb44
2 files changed, 56 insertions, 2 deletions
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index dcff761e2..fbe2956e3 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -655,13 +655,22 @@ class PublicBody < ActiveRecord::Base
end
end
+ def self.where_clause_for_stats(minimum_requests, total_column)
+ # When producing statistics for public bodies, we want to
+ # exclude any that are tagged with 'test' - we use a
+ # sub-select to find the IDs of those public bodies.
+ test_tagged_query = "SELECT model_id FROM has_tag_string_tags" \
+ " WHERE model = 'PublicBody' AND name = 'test'"
+ "#{total_column} >= #{minimum_requests} AND id NOT IN (#{test_tagged_query})"
+ end
+
# Return data for the 'n' public bodies with the highest (or
# lowest) number of requests, but only returning data for those
# with at least 'minimum_requests' requests.
def self.get_request_totals(n, highest, minimum_requests)
ordering = "info_requests_count"
ordering += " DESC" if highest
- where_clause = "info_requests_count >= #{minimum_requests}"
+ where_clause = where_clause_for_stats minimum_requests, 'info_requests_count'
public_bodies = PublicBody.order(ordering).where(where_clause).limit(n)
public_bodies.reverse! if highest
y_values = public_bodies.map { |pb| pb.info_requests_count }
@@ -682,7 +691,8 @@ class PublicBody < ActiveRecord::Base
ordering = "y_value"
ordering += " DESC" if highest
y_value_column = "(cast(#{column} as float) / #{total_column})"
- where_clause = "#{total_column} >= #{minimum_requests} AND #{column} IS NOT NULL"
+ where_clause = where_clause_for_stats minimum_requests, total_column
+ where_clause += " AND #{column} IS NOT NULL"
public_bodies = PublicBody.select("*, #{y_value_column} AS y_value").order(ordering).where(where_clause).limit(n)
public_bodies.reverse! if highest
y_values = public_bodies.map { |pb| pb.y_value.to_f }
diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb
index 3ef4c71e7..23842ccff 100644
--- a/spec/models/public_body_spec.rb
+++ b/spec/models/public_body_spec.rb
@@ -532,6 +532,7 @@ describe PublicBody, " when override all public body request emails set" do
end
describe PublicBody, "when calculating statistics" do
+
it "should not include unclassified or hidden requests in percentages" do
with_hidden_and_successful_requests do
totals_data = PublicBody.get_request_totals(n=3,
@@ -559,4 +560,47 @@ describe PublicBody, "when calculating statistics" do
percentages_data['y_values'][geraldine_index].should == 50
end
end
+
+ it "should only return totals for those with at least a minimum number of requests" do
+ minimum_requests = 1
+ with_enough_info_requests = PublicBody.where(["info_requests_count >= ?",
+ minimum_requests]).length
+ all_data = PublicBody.get_request_totals 4, true, minimum_requests
+ all_data['public_bodies'].length.should == with_enough_info_requests
+ end
+
+ it "should only return percentages for those with at least a minimum number of requests" do
+ with_hidden_and_successful_requests do
+ # With minimum requests at 3, this should return nil
+ # (corresponding to zero public bodies) since the only
+ # public body with just more than 3 info requests (The
+ # Geraldine Quango) has a hidden and an unclassified
+ # request within this block:
+ minimum_requests = 3
+ with_enough_info_requests = PublicBody.where(["info_requests_visible_classified_count >= ?",
+ minimum_requests]).length
+ all_data = PublicBody.get_request_percentages(column='info_requests_successful_count',
+ n=10,
+ true,
+ minimum_requests)
+ all_data.should be_nil
+ end
+ end
+
+ it "should only return those with at least a minimum number of requests, but not tagged 'test'" do
+ hpb = PublicBody.find_by_name 'Department for Humpadinking'
+
+ original_tag_string = hpb.tag_string
+ hpb.add_tag_if_not_already_present 'test'
+
+ begin
+ minimum_requests = 1
+ with_enough_info_requests = PublicBody.where(["info_requests_count >= ?", minimum_requests])
+ all_data = PublicBody.get_request_totals 4, true, minimum_requests
+ all_data['public_bodies'].length.should == 3
+ ensure
+ hpb.tag_string = original_tag_string
+ end
+ end
+
end