diff options
author | Robin Houston <robin.houston@gmail.com> | 2012-01-26 23:21:12 +0000 |
---|---|---|
committer | Robin Houston <robin.houston@gmail.com> | 2012-01-26 23:21:12 +0000 |
commit | 24bbaa5afac5ce27c351e3b460be1b0182446ba1 (patch) | |
tree | 3527992bb6e08e13fa1e7d686663bf10f491cd9e | |
parent | 00aa3e5675d2137b55e8e0bf494f4a2078a5151f (diff) |
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.
-rw-r--r-- | spec/controllers/admin_public_body_controller_spec.rb | 73 | ||||
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 30 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 59 | ||||
-rw-r--r-- | spec/controllers/user_controller_spec.rb | 17 | ||||
-rw-r--r-- | spec/fixtures/public_bodies.yml | 13 | ||||
-rw-r--r-- | spec/fixtures/public_body_translations.yml | 19 | ||||
-rw-r--r-- | spec/models/public_body_spec.rb | 44 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 28 |
8 files changed, 176 insertions, 107 deletions
diff --git a/spec/controllers/admin_public_body_controller_spec.rb b/spec/controllers/admin_public_body_controller_spec.rb index 2670f2add..6954e22a1 100644 --- a/spec/controllers/admin_public_body_controller_spec.rb +++ b/spec/controllers/admin_public_body_controller_spec.rb @@ -25,9 +25,9 @@ describe AdminPublicBodyController, "when administering public bodies" do end it "creates a new public body" do - PublicBody.count.should == 2 + n = PublicBody.count post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' } } - PublicBody.count.should == 3 + PublicBody.count.should == n + 1 end it "edits a public body" do @@ -42,13 +42,19 @@ describe AdminPublicBodyController, "when administering public bodies" do pb.name.should == "Renamed" end + it "does not destroy a public body that has associated requests" do + id = public_bodies(:humpadink_public_body).id + n = PublicBody.count + post :destroy, { :id => id } + response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id => id) + PublicBody.count.should == n + end + it "destroys a public body" do - PublicBody.count.should == 2 - info_request_events(:badger_outgoing_message_event).destroy - outgoing_messages(:badger_outgoing_message).destroy - info_requests(:badger_request).destroy - post :destroy, { :id => 3 } - PublicBody.count.should == 1 + n = PublicBody.count + post :destroy, { :id => public_bodies(:forlorn_public_body).id } + response.should redirect_to(:controller=>'admin_public_body', :action=>'list') + PublicBody.count.should == n - 1 end it "sets a using_admin flag" do @@ -64,10 +70,10 @@ describe AdminPublicBodyController, "when administering public bodies and paying it "disallows non-authenticated users to do anything" do @request.env["HTTP_AUTHORIZATION"] = "" - PublicBody.count.should == 2 + n = PublicBody.count.should post :destroy, { :id => 3 } response.code.should == "401" - PublicBody.count.should == 2 + PublicBody.count.should == n session[:using_admin].should == nil end @@ -76,25 +82,22 @@ describe AdminPublicBodyController, "when administering public bodies and paying config['ADMIN_USERNAME'] = '' config['ADMIN_PASSWORD'] = '' @request.env["HTTP_AUTHORIZATION"] = "" - PublicBody.count.should == 2 - info_request_events(:badger_outgoing_message_event).destroy - outgoing_messages(:badger_outgoing_message).destroy - info_requests(:badger_request).destroy - post :destroy, { :id => 3 } - PublicBody.count.should == 1 + + n = PublicBody.count + post :destroy, { :id => public_bodies(:forlorn_public_body).id } + PublicBody.count.should == n - 1 session[:using_admin].should == 1 end + it "skips admin authorisation when no username set" do config = MySociety::Config.load_default() config['ADMIN_USERNAME'] = '' config['ADMIN_PASSWORD'] = 'fuz' @request.env["HTTP_AUTHORIZATION"] = "" - PublicBody.count.should == 2 - info_request_events(:badger_outgoing_message_event).destroy - outgoing_messages(:badger_outgoing_message).destroy - info_requests(:badger_request).destroy - post :destroy, { :id => 3 } - PublicBody.count.should == 1 + + n = PublicBody.count + post :destroy, { :id => public_bodies(:forlorn_public_body).id } + PublicBody.count.should == n - 1 session[:using_admin].should == 1 end it "forces authorisation when password and username set" do @@ -102,14 +105,11 @@ describe AdminPublicBodyController, "when administering public bodies and paying config['ADMIN_USERNAME'] = 'biz' config['ADMIN_PASSWORD'] = 'fuz' @request.env["HTTP_AUTHORIZATION"] = "" - PublicBody.count.should == 2 + n = PublicBody.count.should basic_auth_login(@request, "baduser", "badpassword") - info_request_events(:badger_outgoing_message_event).destroy - outgoing_messages(:badger_outgoing_message).destroy - info_requests(:badger_request).destroy - post :destroy, { :id => 3 } + post :destroy, { :id => public_bodies(:forlorn_public_body).id } response.code.should == "401" - PublicBody.count.should == 2 + PublicBody.count.should == n session[:using_admin].should == nil end @@ -179,12 +179,9 @@ describe AdminPublicBodyController, "when administering public bodies with i18n" end it "destroy a public body" do - PublicBody.count.should == 2 - info_request_events(:badger_outgoing_message_event).destroy - outgoing_messages(:badger_outgoing_message).destroy - info_requests(:badger_request).destroy - post :destroy, { :id => 3 } - PublicBody.count.should == 1 + n = PublicBody.count + post :destroy, { :id => public_bodies(:forlorn_public_body).id } + PublicBody.count.should == n - 1 end end @@ -207,23 +204,23 @@ describe AdminPublicBodyController, "when creating public bodies with i18n" do it "creates a new public body in one locale" do - PublicBody.count.should == 2 + n = PublicBody.count post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code' } } - PublicBody.count.should == 3 + PublicBody.count.should == n + 1 body = PublicBody.find_by_name("New Quango") response.should redirect_to(:controller=>'admin_public_body', :action=>'show', :id=>body.id) end it "creates a new public body with multiple locales" do - PublicBody.count.should == 2 + n = PublicBody.count post :create, { :public_body => { :name => "New Quango", :short_name => "", :tag_string => "blah", :request_email => 'newquango@localhost', :last_edit_comment => 'From test code', :translated_versions => [{ :locale => "es", :name => "Mi Nuevo Quango", :short_name => "", :request_email => 'newquango@localhost' }] } } - PublicBody.count.should == 3 + PublicBody.count.should == n + 1 body = PublicBody.find_by_name("New Quango") body.translations.map {|t| t.locale.to_s}.sort.should == ["en", "es"] diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 131412a90..4b657849a 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -26,13 +26,23 @@ describe PublicBodyController, "when showing a body" do assigns[:public_body].should == public_bodies(:humpadink_public_body) end - it "should assign the requests" do + it "should assign the requests (1)" do get :show, :url_name => "tgq", :view => 'all' - assigns[:xapian_requests].results.count.should == 2 + assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all( + :conditions => ["public_body_id = ?", public_bodies(:geraldine_public_body).id]) + end + + it "should assign the requests (2)" do get :show, :url_name => "tgq", :view => 'successful' - assigns[:xapian_requests].results.count.should == 0 + assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all( + :conditions => ["described_state = ? and public_body_id = ?", + "successful", public_bodies(:geraldine_public_body).id]) + end + + it "should assign the requests (3)" do get :show, :url_name => "dfh", :view => 'all' - assigns[:xapian_requests].results.count.should == 1 + assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all( + :conditions => ["public_body_id = ?", public_bodies(:humpadink_public_body).id]) end it "should assign the body using different locale from that used for url_name" do @@ -81,16 +91,16 @@ describe PublicBodyController, "when listing bodies" do it "should list all bodies from default locale, even when there are no translations for selected locale" do PublicBody.with_locale(:en) do - english_only = PublicBody.new(:name => 'English only', + @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 + @english_only.save end PublicBody.with_locale(:es) do get :list - assigns[:public_bodies].length.should == 3 + assigns[:public_bodies].include?(@english_only).should == true end end @@ -99,7 +109,7 @@ describe PublicBodyController, "when listing bodies" do response.should render_template('list') - assigns[:public_bodies].should == [ public_bodies(:humpadink_public_body), public_bodies(:geraldine_public_body) ] + assigns[:public_bodies].should == PublicBody.all(:order => "name", :conditions => "id <> #{PublicBody.internal_admin_body.id}") assigns[:tag].should == "all" assigns[:description].should == "" end @@ -135,11 +145,11 @@ describe PublicBodyController, "when listing bodies" do get :list, :tag => "other" response.should render_template('list') - assigns[:public_bodies].should == [ public_bodies(:geraldine_public_body) ] + assigns[:public_bodies].should =~ PublicBody.all(:conditions => "id not in (#{public_bodies(:humpadink_public_body).id}, #{PublicBody.internal_admin_body.id})") get :list response.should render_template('list') - assigns[:public_bodies].count.should == 2 + assigns[:public_bodies].should =~ PublicBody.all(:conditions => "id <> #{PublicBody.internal_admin_body.id}") end 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 diff --git a/spec/controllers/user_controller_spec.rb b/spec/controllers/user_controller_spec.rb index a90fa18df..81d3ff2e5 100644 --- a/spec/controllers/user_controller_spec.rb +++ b/spec/controllers/user_controller_spec.rb @@ -46,16 +46,19 @@ describe UserController, "when showing a user" do it "should search the user's contributions" do get :show, :url_name => "bob_smith" - assigns[:xapian_requests].results.count.should == 3 + assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should =~ InfoRequest.all(:conditions => "user_id = #{users(:bob_smith_user).id}") + get :show, :url_name => "bob_smith", :user_query => "money" - assigns[:xapian_requests].results.count.should == 1 + assigns[:xapian_requests].results.map{|x|x[:model].info_request}.should == [info_requests(:naughty_chicken_request)] end -# Error handling not quite good enough for this yet -# it "should not show unconfirmed users" do -# get :show, :url_name => "silly_emnameem" -# assigns[:display_users].should == [ users(:silly_name_user) ] -# end + it "should not show unconfirmed users" do + begin + get :show, :url_name => "unconfirmed_user" + rescue => e + end + e.should be_an_instance_of(ActiveRecord::RecordNotFound) + end end diff --git a/spec/fixtures/public_bodies.yml b/spec/fixtures/public_bodies.yml index 191dd68bb..93fd0fb2e 100644 --- a/spec/fixtures/public_bodies.yml +++ b/spec/fixtures/public_bodies.yml @@ -23,3 +23,16 @@ humpadink_public_body: url_name: dfh created_at: 2007-10-25 10:51:01.161639 notes: An albatross told me!!! +forlorn_public_body: + name: "Department of Loneliness" + first_letter: D + updated_at: 2011-01-26 14:11:02.12345 + last_edit_comment: 'Aw, bless.' + request_email: forlorn-requests@localhost + id: 4 + version: 1 + last_edit_editor: "robin" + short_name: DoL + url_name: lonely + created_at: 2011-01-26 14:11:02.12345 + notes: A very lonely public body that no one has corresponded with diff --git a/spec/fixtures/public_body_translations.yml b/spec/fixtures/public_body_translations.yml index b5e947044..08727e45c 100644 --- a/spec/fixtures/public_body_translations.yml +++ b/spec/fixtures/public_body_translations.yml @@ -24,8 +24,8 @@ humpadink_es_public_body_translation: name: "El Department for Humpadinking" first_letter: E request_email: humpadink-requests@localhost - id: "3" - public_body_id: "3" + id: 3 + public_body_id: 3 short_name: eDfH url_name: edfh locale: es @@ -35,9 +35,20 @@ humpadink_en_public_body_translation: name: "Department for Humpadinking" first_letter: D request_email: humpadink-requests@localhost - id: "4" - public_body_id: "3" + id: 4 + public_body_id: 3 short_name: DfH url_name: dfh locale: en notes: An albatross told me!!! + +forlorn_en_public_body_translation: + name: "Department of Loneliness" + first_letter: D + request_email: forlorn-requests@localhost + id: 5 + public_body_id: 4 + short_name: DoL + url_name: lonely + locale: en + notes: A very lonely public body that no one has corresponded with diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 07e8f291d..97fa04f93 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -239,6 +239,7 @@ describe PublicBody, " when dealing public body locales" do end describe PublicBody, " when loading CSV files" do + fixtures :public_bodies, :public_body_versions, :public_body_translations before(:each) do # InternalBody is created the first time it's accessed, which happens sometimes during imports, # depending on the tag used. By accessing it here before every test, it doesn't disturb our checks later on @@ -248,11 +249,10 @@ describe PublicBody, " when loading CSV files" do it "should import even if no email is provided" do errors, notes = PublicBody.import_csv("1,aBody", '', 'replace', true, 'someadmin') # true means dry run errors.should == [] + puts "notes = #{notes.inspect}" notes.size.should == 2 - notes.should == [ - "line 1: creating new authority 'aBody' (locale: en):\n\t{\"name\":\"aBody\"}", - "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n" - ] + notes[0].should == "line 1: creating new authority 'aBody' (locale: en):\n\t{\"name\":\"aBody\"}" + notes[1].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ end it "should do a dry run successfully" do @@ -262,12 +262,12 @@ describe PublicBody, " when loading CSV files" do errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin') # true means dry run errors.should == [] notes.size.should == 4 - notes.should == [ + notes[0..2].should == [ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}", "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}", "line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}", - "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n" - ] + ] + notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ PublicBody.count.should == original_count end @@ -279,12 +279,12 @@ describe PublicBody, " when loading CSV files" do errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin') # false means real run errors.should == [] notes.size.should == 4 - notes.should == [ + notes[0..2].should == [ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}", "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}", "line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}", - "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n" - ] + ] + notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ PublicBody.count.should == original_count + 3 end @@ -296,12 +296,12 @@ describe PublicBody, " when loading CSV files" do errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin') # false means real run errors.should == [] notes.size.should == 4 - notes.should == [ + notes[0..2].should == [ "line 1: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\"\}", "line 2: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\"\}", "line 3: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\"\}", - "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n" - ] + ] + notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ PublicBody.count.should == original_count + 3 end @@ -312,12 +312,12 @@ describe PublicBody, " when loading CSV files" do errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin') # true means dry run errors.should == [] notes.size.should == 4 - notes.should == [ + notes[0..2].should == [ "line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t\{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"\}", "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t\{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\",\"tag_string\":\"scottish\"\}", "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t\{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\",\"tag_string\":\"fake aTag\"\}", - "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n" - ] + ] + notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ PublicBody.count.should == original_count end @@ -366,15 +366,15 @@ describe PublicBody, " when loading CSV files" do errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', false, 'someadmin', [:en, :es]) errors.should == [] notes.size.should == 7 - notes.should == [ + notes[0..5].should == [ "line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"}", "line 2: creating new authority 'North West Fake Authority' (locale: es):\n\t{\"name\":\"Autoridad del Nordeste\"}", "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\",\"tag_string\":\"scottish\"}", "line 3: creating new authority 'Scottish Fake Authority' (locale: es):\n\t{\"name\":\"Autoridad Escocesa\"}", "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\",\"tag_string\":\"fake aTag\"}", "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: es):\n\t{\"name\":\"Autoridad Irlandesa\"}", - "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n" - ] + ] + notes[6].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ PublicBody.count.should == original_count + 3 @@ -395,12 +395,12 @@ describe PublicBody, " when loading CSV files" do errors, notes = PublicBody.import_csv(csv_contents, '', 'replace', true, 'someadmin', ['en', :xx]) # true means dry run errors.should == [] notes.size.should == 4 - notes.should == [ + notes[0..2].should == [ "line 2: creating new authority 'North West Fake Authority' (locale: en):\n\t{\"name\":\"North West Fake Authority\",\"request_email\":\"north_west_foi@localhost\",\"home_page\":\"http://northwest.org\"}", "line 3: creating new authority 'Scottish Fake Authority' (locale: en):\n\t{\"name\":\"Scottish Fake Authority\",\"request_email\":\"scottish_foi@localhost\",\"home_page\":\"http://scottish.org\",\"tag_string\":\"scottish\"}", "line 4: creating new authority 'Fake Authority of Northern Ireland' (locale: en):\n\t{\"name\":\"Fake Authority of Northern Ireland\",\"request_email\":\"ni_foi@localhost\",\"tag_string\":\"fake aTag\"}", - "Notes: Some bodies are in database, but not in CSV file:\n Department for Humpadinking\n Geraldine Quango\nYou may want to delete them manually.\n" - ] + ] + notes[3].should =~ /Notes: Some bodies are in database, but not in CSV file:\n( [A-Za-z ]+\n)*You may want to delete them manually.\n/ PublicBody.count.should == original_count end diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index 412146b53..6f4f49373 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -60,14 +60,11 @@ describe PublicBody, " when indexing public bodies with Xapian" do xapian_object.results.size.should == 1 xapian_object.results[0][:model].should == public_bodies(:humpadink_public_body) - info_request_events(:badger_outgoing_message_event).destroy - outgoing_messages(:badger_outgoing_message).destroy - info_requests(:badger_request).destroy - public_bodies(:humpadink_public_body).destroy + public_bodies(:forlorn_public_body).destroy update_xapian_index - xapian_object = InfoRequest.full_search([PublicBody], "albatross", 'created_at', true, nil, 100, 1) - xapian_object.results.size.should == 0 + xapian_object = InfoRequest.full_search([PublicBody], "lonely", 'created_at', true, nil, 100, 1) + xapian_object.results.should == [] end end @@ -130,7 +127,7 @@ describe PublicBody, " when indexing requests by body they are to" do end describe User, " when indexing requests by user they are from" do - fixtures :users, :info_requests, :raw_emails, :incoming_messages, :outgoing_messages, :comments, :info_request_events, :track_things + fixtures :users, :info_requests, :raw_emails, :incoming_messages, :outgoing_messages, :comments, :info_request_events, :track_things, :public_bodies, :public_body_versions, :public_body_translations before(:each) do load_raw_emails_data(raw_emails) rebuild_xapian_index @@ -138,13 +135,13 @@ describe User, " when indexing requests by user they are from" do it "should find requests from the user" do xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, nil, 100, 1) - xapian_object.results.size.should == 5 + xapian_object.results.map{|x|x[:model]}.should =~ InfoRequestEvent.all(:conditions => "info_request_id in (select id from info_requests where user_id = #{users(:bob_smith_user).id})") end it "should find just the sent message events from a particular user" do # def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page) xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith variety:sent", 'created_at', true, nil, 100, 1) - xapian_object.results.size.should == 3 + xapian_object.results.map{|x|x[:model]}.should =~ InfoRequestEvent.all(:conditions => "info_request_id in (select id from info_requests where user_id = #{users(:bob_smith_user).id}) and event_type = 'sent'") xapian_object.results[2][:model].should == info_request_events(:useless_outgoing_message_event) xapian_object.results[1][:model].should == info_request_events(:silly_outgoing_message_event) end @@ -159,8 +156,7 @@ describe User, " when indexing requests by user they are from" do # def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page) xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, 'request_collapse', 100, 1) - xapian_object.results.size.should == 2 - xapian_object.results[1][:model].should == info_request_events(:silly_comment_event) + xapian_object.results.map{|x|x[:model].info_request}.should =~ InfoRequest.all(:conditions => "user_id = #{users(:bob_smith_user).id}") end it "should not get confused searching for requests when one user has a name which has same stem as another" do @@ -190,7 +186,7 @@ describe User, " when indexing requests by user they are from" do it "should update index correctly when URL name of user changes" do # initial search xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, nil, 100, 1) - xapian_object.results.size.should == 5 + xapian_object.results.map{|x|x[:model]}.should =~ InfoRequestEvent.all(:conditions => "info_request_id in (select id from info_requests where user_id = #{users(:bob_smith_user).id})") models_found_before = xapian_object.results.map { |x| x[:model] } # change the URL name of the body @@ -204,9 +200,7 @@ describe User, " when indexing requests by user they are from" do xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:bob_smith", 'created_at', true, nil, 100, 1) xapian_object.results.size.should == 0 xapian_object = InfoRequest.full_search([InfoRequestEvent], "requested_by:robert_smith", 'created_at', true, nil, 100, 1) - xapian_object.results.size.should == 5 models_found_after = xapian_object.results.map { |x| x[:model] } - models_found_before.should == models_found_after end end @@ -347,7 +341,7 @@ describe PublicBody, " when only indexing selected things on a rebuild" do xapian_object = InfoRequest.full_search([PublicBody], "frobzn", 'created_at', true, nil, 100, 1) xapian_object.results.size.should == 0 xapian_object = InfoRequest.full_search([PublicBody], "variety:authority", 'created_at', true, nil, 100, 1) - xapian_object.results.size.should == 2 + xapian_object.results.map{|x|x[:model]}.should =~ PublicBody.all # only reindex 'tag' and text dropfirst = true terms = "U" @@ -370,7 +364,7 @@ describe PublicBody, " when only indexing selected things on a rebuild" do xapian_object = InfoRequest.full_search([PublicBody], "frobzn", 'created_at', true, nil, 100, 1) xapian_object.results.size.should == 1 xapian_object = InfoRequest.full_search([PublicBody], "variety:authority", 'created_at', true, nil, 100, 1) - xapian_object.results.size.should == 2 + xapian_object.results.map{|x|x[:model]}.should =~ PublicBody.all # only reindex 'variety' term, blowing away existing data dropfirst = true rebuild_xapian_index(terms, values, texts, dropfirst) @@ -379,7 +373,7 @@ describe PublicBody, " when only indexing selected things on a rebuild" do xapian_object = InfoRequest.full_search([PublicBody], "frobzn", 'created_at', true, nil, 100, 1) xapian_object.results.size.should == 0 xapian_object = InfoRequest.full_search([PublicBody], "variety:authority", 'created_at', true, nil, 100, 1) - xapian_object.results.size.should == 2 + xapian_object.results.map{|x|x[:model]}.should =~ PublicBody.all end end |