diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 35 | ||||
-rw-r--r-- | spec/fixtures/public_bodies.yml | 18 | ||||
-rw-r--r-- | spec/lib/confidence_intervals.rb | 30 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 66 |
4 files changed, 149 insertions, 0 deletions
diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 4e1841164..92130f3d0 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -208,6 +208,41 @@ 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 + 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. + 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 + # 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 + end + end + +end + describe PublicBodyController, "when doing type ahead searches" do render_views diff --git a/spec/fixtures/public_bodies.yml b/spec/fixtures/public_bodies.yml index 65cba4b28..6eae53db8 100644 --- a/spec/fixtures/public_bodies.yml +++ b/spec/fixtures/public_bodies.yml @@ -12,6 +12,9 @@ geraldine_public_body: created_at: 2007-10-24 10:51:01.161639 api_key: 1 info_requests_count: 4 + info_requests_successful_count: 0 + info_requests_not_held_count: 0 + info_requests_overdue_count: 3 humpadink_public_body: name: "Department for Humpadinking" first_letter: D @@ -27,6 +30,9 @@ humpadink_public_body: notes: An albatross told me!!! api_key: 2 info_requests_count: 2 + info_requests_successful_count: 1 + info_requests_not_held_count: 0 + info_requests_overdue_count: 1 forlorn_public_body: name: "Department of Loneliness" first_letter: D @@ -42,6 +48,9 @@ forlorn_public_body: notes: A very lonely public body that no one has corresponded with api_key: 3 info_requests_count: 0 + info_requests_successful_count: 0 + info_requests_not_held_count: 0 + info_requests_overdue_count: 0 silly_walks_public_body: id: 5 version: 1 @@ -57,6 +66,9 @@ silly_walks_public_body: notes: You know the one. api_key: 4 info_requests_count: 2 + info_requests_successful_count: 2 + info_requests_not_held_count: 0 + info_requests_overdue_count: 0 sensible_walks_public_body: id: 6 version: 1 @@ -72,6 +84,9 @@ sensible_walks_public_body: created_at: 2008-10-25 10:51:01.161639 api_key: 5 info_requests_count: 1 + info_requests_successful_count: 1 + info_requests_not_held_count: 0 + info_requests_overdue_count: 0 other_public_body: id: 7 version: 1 @@ -87,3 +102,6 @@ other_public_body: created_at: 2008-10-25 10:51:01.161639 api_key: 6 info_requests_count: 0 + info_requests_successful_count: 0 + info_requests_not_held_count: 0 + info_requests_overdue_count: 0 diff --git a/spec/lib/confidence_intervals.rb b/spec/lib/confidence_intervals.rb new file mode 100644 index 000000000..cb8717f3d --- /dev/null +++ b/spec/lib/confidence_intervals.rb @@ -0,0 +1,30 @@ +require 'confidence_intervals' + +describe "ci_bounds" do + + describe "when passed all successes" do + it "should never return a high CI above 1" do + ci = ci_bounds 16, 16, 0.01 + ci[1].should be <= 1 + end + end + + describe "when passed all failures" do + it "should never return a low CI below 0" do + ci = ci_bounds 0, 10, 0.05 + ci[0].should be >= 0 + end + end + + describe "when passed 4 out of 10 successes (with 0.05 power)" do + it "should return the correct Wilson's interval" do + # The expected results here were taken from an online + # calculator: + # http://www.vassarstats.net/prop1.html + ci = ci_bounds 7, 10, 0.05 + ci[0].should be_within(0.001).of(0.3968) + ci[1].should be_within(0.001).of(0.8922) + end + end + +end diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 3451e018f..3f2e02189 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -857,4 +857,70 @@ describe InfoRequest do end end end + + describe 'when saving an info_request' do + + before do + @info_request = InfoRequest.new(:external_url => 'http://www.example.com', + :external_user_name => 'Example User', + :title => 'Some request or other', + :public_body => public_bodies(:geraldine_public_body)) + end + + it "should call purge_in_cache and update_counter_cache" do + @info_request.should_receive(:purge_in_cache) + # Twice - once for save, once for destroy: + @info_request.should_receive(:update_counter_cache).twice + @info_request.save! + @info_request.destroy + end + + end + + describe 'when destroying an info_request' do + + before do + @info_request = InfoRequest.new(:external_url => 'http://www.example.com', + :external_user_name => 'Example User', + :title => 'Some request or other', + :public_body => public_bodies(:geraldine_public_body)) + end + + it "should call update_counter_cache" do + @info_request.save! + @info_request.should_receive(:update_counter_cache) + @info_request.destroy + end + + end + + describe 'when changing a described_state' do + + it "should change the counts on its PublicBody" do + pb = public_bodies(:geraldine_public_body) + old_successful_count = pb.info_requests_successful_count + old_not_held_count = pb.info_requests_not_held_count + ir = InfoRequest.new(:external_url => 'http://www.example.com', + :external_user_name => 'Example User', + :title => 'Some request or other', + :described_state => 'partially_successful', + :public_body => pb) + ir.save! + pb.info_requests_successful_count.should == (old_successful_count + 1) + ir.described_state = 'not_held' + ir.save! + pb.info_requests_successful_count.should == old_successful_count + pb.info_requests_not_held_count.should == (old_not_held_count + 1) + ir.described_state = 'successful' + ir.save! + pb.info_requests_successful_count.should == (old_successful_count + 1) + pb.info_requests_not_held_count.should == old_not_held_count + ir.destroy + pb.info_requests_successful_count.should == old_successful_count + pb.info_requests_successful_count.should == old_not_held_count + end + + end + + end |