aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/info_request_spec.rb
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/info_request_spec.rb')
-rw-r--r--spec/models/info_request_spec.rb146
1 files changed, 143 insertions, 3 deletions
diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb
index d80eabace..fac89109c 100644
--- a/spec/models/info_request_spec.rb
+++ b/spec/models/info_request_spec.rb
@@ -1,3 +1,4 @@
+# encoding: utf-8
# == Schema Information
#
# Table name: info_requests
@@ -26,6 +27,27 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe InfoRequest do
+ describe 'when validating', :focus => true do
+
+ it 'should accept a summary with ascii characters' do
+ info_request = InfoRequest.new(:title => 'abcde')
+ info_request.valid?
+ info_request.errors[:title].should be_empty
+ end
+
+ it 'should accept a summary with unicode characters' do
+ info_request = InfoRequest.new(:title => 'кажете')
+ info_request.valid?
+ info_request.errors[:title].should be_empty
+ end
+
+ it 'should not accept a summary with no ascii or unicode characters' do
+ info_request = InfoRequest.new(:title => '55555')
+ info_request.valid?
+ info_request.errors[:title].should_not be_empty
+ end
+ end
+
describe 'when generating a user name slug' do
before do
@@ -796,7 +818,7 @@ describe InfoRequest do
events[0].calculated_state.should == "waiting_response"
events[1].event_type.should == "response"
events[1].described_state.should be_nil
- events[1].calculated_state.should be_nil
+ events[1].calculated_state.should == 'waiting_response'
events[2].event_type.should == "status_update"
events[2].described_state.should == "waiting_response"
events[2].calculated_state.should == "waiting_response"
@@ -824,7 +846,7 @@ describe InfoRequest do
events[0].calculated_state.should == "waiting_response"
events[1].event_type.should == "response"
events[1].described_state.should be_nil
- events[1].calculated_state.should be_nil
+ events[1].calculated_state.should == 'waiting_response'
events[2].event_type.should == "status_update"
events[2].described_state.should == "waiting_response"
events[2].calculated_state.should == "waiting_response"
@@ -857,7 +879,7 @@ describe InfoRequest do
events[0].calculated_state.should == "waiting_response"
events[1].event_type.should == "response"
events[1].described_state.should be_nil
- events[1].calculated_state.should be_nil
+ events[1].calculated_state.should == 'waiting_response'
events[2].event_type.should == "status_update"
events[2].described_state.should == "waiting_response"
events[2].calculated_state.should == "waiting_response"
@@ -933,7 +955,125 @@ describe InfoRequest do
events[1].described_state.should == "successful"
events[1].calculated_state.should == "successful"
end
+
+ it "should have sensible event states" do
+ # An initial request is sent
+ request.log_event('sent', {})
+ request.set_described_state('waiting_response')
+
+ # A response is received
+ request.awaiting_description = true
+ request.log_event("response", {})
+
+ # The user marks the request as successful
+ request.log_event("status_update", {})
+ request.set_described_state("successful")
+
+ events = request.info_request_events
+ events.count.should == 3
+ events[0].event_type.should == "sent"
+ events[0].described_state.should == "waiting_response"
+ events[0].calculated_state.should == "waiting_response"
+ events[1].event_type.should == "response"
+ events[1].described_state.should be_nil
+ events[1].calculated_state.should == "successful"
+ events[2].event_type.should == "status_update"
+ events[2].described_state.should == "successful"
+ events[2].calculated_state.should == "successful"
+ end
+ end
+
+ context "another series of events on a request", :focus => true do
+ it "should have sensible event states" do
+ # An initial request is sent
+ request.log_event('sent', {})
+ request.set_described_state('waiting_response')
+ # An admin sets the status of the request to 'gone postal' using
+ # the admin interface
+ request.log_event("edit", {})
+ request.set_described_state("gone_postal")
+
+ events = request.info_request_events
+ events.count.should == 2
+ events[0].event_type.should == "sent"
+ events[0].described_state.should == "waiting_response"
+ events[0].calculated_state.should == "waiting_response"
+ events[1].event_type.should == "edit"
+ events[1].described_state.should == "gone_postal"
+ events[1].calculated_state.should == "gone_postal"
+ end
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 without saving a new version" do
+ pb = public_bodies(:geraldine_public_body)
+ old_version_count = pb.versions.count
+ 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.reload
+ 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.reload
+ pb.info_requests_successful_count.should == (old_successful_count + 1)
+ pb.info_requests_not_held_count.should == old_not_held_count
+ ir.destroy
+ pb.reload
+ pb.info_requests_successful_count.should == old_successful_count
+ pb.info_requests_successful_count.should == old_not_held_count
+ pb.versions.count.should == old_version_count
+ end
+
+ end
+
+
end