From 24bbaa5afac5ce27c351e3b460be1b0182446ba1 Mon Sep 17 00:00:00 2001 From: Robin Houston Date: Thu, 26 Jan 2012 23:21:12 +0000 Subject: Refactor test code so new test data can be added Previously many of the tests made assumptions about the global structure of the test data set: the total number of requests, for example, or the names of all public bodies. This makes it difficult to add to the test data. This change is intended to make the test data easier to extend by eliminating such global assumptions. --- spec/controllers/request_controller_spec.rb | 59 ++++++++++++++++++++++++----- 1 file changed, 50 insertions(+), 9 deletions(-) (limited to 'spec/controllers/request_controller_spec.rb') diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 055c9b3d4..a77d1ac27 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -23,20 +23,42 @@ describe RequestController, "when listing recent requests" do it "should filter requests" do get :list, :view => 'all' - assigns[:list_results].size.should == 3 + assigns[:list_results].map(&:info_request).should =~ InfoRequest.all + # default sort order is the request with the most recently created event first - assigns[:list_results][0].info_request.id.should == 104 + assigns[:list_results].map(&:info_request).should == InfoRequest.all( + :order => "(select max(info_request_events.created_at) from info_request_events where info_request_events.info_request_id = info_requests.id) DESC") + get :list, :view => 'successful' - assigns[:list_results].size.should == 0 + assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( + :conditions => "id in ( + select info_request_id + from info_request_events + where not exists ( + select * + from info_request_events later_events + where later_events.created_at > info_request_events.created_at + and later_events.info_request_id = info_request_events.info_request_id + ) + and info_request_events.described_state in ('successful', 'partially_successful') + )") end it "should filter requests by date" do + # The semantics of the search are that it finds any InfoRequest + # that has any InfoRequestEvent created in the specified range + get :list, :view => 'all', :request_date_before => '13/10/2007' - assigns[:list_results].size.should == 1 + assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( + :conditions => "id in (select info_request_id from info_request_events where created_at < '2007-10-13'::date)") + get :list, :view => 'all', :request_date_after => '13/10/2007' - assigns[:list_results].size.should == 3 + assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( + :conditions => "id in (select info_request_id from info_request_events where created_at > '2007-10-13'::date)") + get :list, :view => 'all', :request_date_after => '13/10/2007', :request_date_before => '01/11/2007' - assigns[:list_results].size.should == 1 + assigns[:list_results].map(&:info_request).should =~ InfoRequest.all( + :conditions => "id in (select info_request_id from info_request_events where created_at between '2007-10-13'::date and '2007-11-01'::date)") end it "should make a sane-sized cache tag" do @@ -46,13 +68,32 @@ describe RequestController, "when listing recent requests" do it "should list internal_review requests as unresolved ones" do get :list, :view => 'awaiting' - assigns[:list_results].size.should == 0 + + # This doesn’t precisely duplicate the logic of the actual + # query, but it is close enough to give the same result with + # the current set of test data. + assigns[:list_results].should =~ InfoRequestEvent.all( + :conditions => "described_state in ( + 'waiting_response', 'waiting_clarification', + 'internal_review', 'gone_postal', 'error_message', 'requires_admin' + ) and not exists ( + select * + from info_request_events later_events + where later_events.created_at > info_request_events.created_at + and later_events.info_request_id = info_request_events.info_request_id + )") + + + get :list, :view => 'awaiting' + assigns[:list_results].map(&:info_request).include?(info_requests(:fancy_dog_request)).should == false + event = info_request_events(:useless_incoming_message_event) - event.calculated_state = "internal_review" + event.described_state = event.calculated_state = "internal_review" event.save! rebuild_xapian_index + get :list, :view => 'awaiting' - assigns[:list_results].size.should == 1 + assigns[:list_results].map(&:info_request).include?(info_requests(:fancy_dog_request)).should == true end it "should assign the first page of results" do -- cgit v1.2.3