diff options
Diffstat (limited to 'spec')
-rw-r--r-- | spec/controllers/public_body_controller_spec.rb | 31 | ||||
-rw-r--r-- | spec/controllers/request_controller_spec.rb | 85 | ||||
-rw-r--r-- | spec/fixtures/foi_attachments.yml | 1 | ||||
-rw-r--r-- | spec/integration/view_request_spec.rb | 32 | ||||
-rw-r--r-- | spec/models/incoming_message_spec.rb | 88 | ||||
-rw-r--r-- | spec/models/info_request_event_spec.rb | 25 | ||||
-rw-r--r-- | spec/models/info_request_spec.rb | 2 | ||||
-rw-r--r-- | spec/models/xapian_spec.rb | 64 | ||||
-rw-r--r-- | spec/spec_helper.rb | 17 |
9 files changed, 274 insertions, 71 deletions
diff --git a/spec/controllers/public_body_controller_spec.rb b/spec/controllers/public_body_controller_spec.rb index 53e6c169a..8182e1331 100644 --- a/spec/controllers/public_body_controller_spec.rb +++ b/spec/controllers/public_body_controller_spec.rb @@ -68,17 +68,19 @@ describe PublicBodyController, "when listing bodies" do response.should be_success end - it "should list all bodies even when there are no translations for selected locale" 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', + :short_name => 'EO', + :request_email => 'english@flourish.org', + :last_edit_editor => 'test', + :last_edit_comment => '') + english_only.save + end PublicBody.with_locale(:es) do - - spanish_only = PublicBody.new(:name => 'Spanish only', - :short_name => 'SO', - :request_email => 'spanish@flourish.org', - :last_edit_editor => 'test', - :last_edit_comment => '') - end - get :list - assigns[:public_bodies].length.should == 3 + get :list + assigns[:public_bodies].length.should == 3 + end end it "should list bodies in alphabetical order" do @@ -175,7 +177,7 @@ describe PublicBodyController, "when doing type ahead searches" do it "should return nothing for the empty query string" do get :search_typeahead, :q => "" response.should render_template('public_body/_search_ahead') - assigns[:xapian_requests].results.size.should == 0 + assigns[:xapian_requests].should be_nil end it "should return a body matching the given keyword, but not users with a matching description" do @@ -200,10 +202,9 @@ describe PublicBodyController, "when doing type ahead searches" do assigns[:xapian_requests].results[0][:model].name.should == public_bodies(:humpadink_public_body).name end - it "should return partial matches" do - get :search_typeahead, :q => "geral" # 'geral' for 'Geraldine' + it "should not return matches for short words" do + get :search_typeahead, :q => "b" response.should render_template('public_body/_search_ahead') - assigns[:xapian_requests].results.size.should == 1 - assigns[:xapian_requests].results[0][:model].name.should == public_bodies(:geraldine_public_body).name + assigns[:xapian_requests].should be_nil end end diff --git a/spec/controllers/request_controller_spec.rb b/spec/controllers/request_controller_spec.rb index 3420d212e..4994c2a8f 100644 --- a/spec/controllers/request_controller_spec.rb +++ b/spec/controllers/request_controller_spec.rb @@ -105,10 +105,12 @@ describe RequestController, "when showing one request" do integrate_views it "should receive incoming messages, send email to creator, and show them" do + ir = info_requests(:fancy_dog_request) + ir.incoming_messages.each { |x| x.parse_raw_email! } + get :show, :url_title => 'why_do_you_have_such_a_fancy_dog' size_before = assigns[:info_request_events].size - ir = info_requests(:fancy_dog_request) receive_incoming_mail('incoming-request-plain.email', ir.incoming_email) deliveries = ActionMailer::Base.deliveries deliveries.size.should == 1 @@ -120,6 +122,8 @@ describe RequestController, "when showing one request" do end it "should download attachments" do + ir = info_requests(:fancy_dog_request) + ir.incoming_messages.each { |x| x.parse_raw_email! } get :show, :url_title => 'why_do_you_have_such_a_fancy_dog' response.content_type.should == "text/html" size_before = assigns[:info_request_events].size @@ -129,7 +133,7 @@ describe RequestController, "when showing one request" do get :show, :url_title => 'why_do_you_have_such_a_fancy_dog' (assigns[:info_request_events].size - size_before).should == 1 - + ir.reload get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt'] response.content_type.should == "text/plain" response.should have_text(/Second hello/) @@ -148,16 +152,50 @@ describe RequestController, "when showing one request" do it "should generate valid HTML verson of plain text attachments " do ir = info_requests(:fancy_dog_request) receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email) + ir.reload get :get_attachment_as_html, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt.html'], :skip_cache => 1 response.content_type.should == "text/html" response.should have_text(/Second hello/) end + it "should not cause a reparsing of the raw email, even when the result would be a 404 " do + ir = info_requests(:fancy_dog_request) + receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email) + ir.reload + attachment = IncomingMessage.get_attachment_by_url_part_number(ir.incoming_messages[1].get_attachments_for_display, 2) + attachment.body.should have_text(/Second hello/) + + # change the raw_email associated with the message; this only be reparsed when explicitly asked for + ir.incoming_messages[1].raw_email.data = ir.incoming_messages[1].raw_email.data.sub("Second", "Third") + # asking for an attachment by the wrong filename results + # in a 404 for browsing users. This shouldn't cause a + # re-parse... + lambda { + get :get_attachment_as_html, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt.baz.html'], :skip_cache => 1 + }.should raise_error(ActiveRecord::RecordNotFound) + + attachment = IncomingMessage.get_attachment_by_url_part_number(ir.incoming_messages[1].get_attachments_for_display, 2) + attachment.body.should have_text(/Second hello/) + + # ...nor should asking for it by its correct filename... + get :get_attachment_as_html, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt.html'], :skip_cache => 1 + response.should_not have_text(/Third hello/) + + # ...but if we explicitly ask for attachments to be extracted, then they should be + force = true + ir.incoming_messages[1].parse_raw_email!(force) + attachment = IncomingMessage.get_attachment_by_url_part_number(ir.incoming_messages[1].get_attachments_for_display, 2) + attachment.body.should have_text(/Second hello/) + get :get_attachment_as_html, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt.html'], :skip_cache => 1 + response.should have_text(/Third hello/) + end + it "should treat attachments with unknown extensions as binary" do ir = info_requests(:fancy_dog_request) receive_incoming_mail('incoming-request-attachment-unknown-extension.email', ir.incoming_email) + ir.reload - get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.qwglhm'] + get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.qwglhm'], :skip_cache => 1 response.content_type.should == "application/octet-stream" response.should have_text(/an unusual sort of file/) end @@ -200,8 +238,9 @@ describe RequestController, "when showing one request" do ir.user.censor_rules << censor_rule receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email) + ir.reload - get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt'] + get :get_attachment, :incoming_message_id => ir.incoming_messages[1].id, :id => ir.id, :part => 2, :file_name => ['hello.txt'], :skip_cache => 1 response.content_type.should == "text/plain" response.should have_text(/xxxxxx hello/) end @@ -210,7 +249,22 @@ describe RequestController, "when showing one request" do ir = info_requests(:fancy_dog_request) receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email) + # XXX this is horrid, but don't know a better way. If we + # don't do this, the info_request_event to which the + # info_request is attached still uses the unmodified + # version from the fixture. + #event = info_request_events(:useless_incoming_message_event) + ir.reload + assert ir.info_request_events[3].incoming_message.get_attachments_for_display.count == 2 + ir.save! + ir.incoming_messages.last.save! get :show, :url_title => 'why_do_you_have_such_a_fancy_dog' + assert assigns[:info_request].info_request_events[3].incoming_message.get_attachments_for_display.count == 2 + # the issue is that the info_request_events have got cached on them the old info_requests. + # where i'm at: trying to replace those fields that got re-read from the raw email. however tests are failing in very strange ways. currently I don't appear to be getting any attachments parsed in at all when in the template (see "*****" in _correspondence.rhtml) but do when I'm in the code. + + # so at this point, assigns[:info_request].incoming_messages[1].get_attachments_for_display is returning stuff, but the equivalent thing in the template isn't. + # but something odd is that the above is return a whole load of attachments which aren't there in the controller response.body.should have_tag("p.attachment strong", /hello.txt/m) censor_rule = CensorRule.new() @@ -225,10 +279,17 @@ describe RequestController, "when showing one request" do end it "should make a zipfile available, which has a different URL when it changes" do + title = 'why_do_you_have_such_a_fancy_dog' ir = info_requests(:fancy_dog_request) session[:user_id] = ir.user.id # bob_smith_user + get :download_entire_request, :url_title => title + assigns[:url_path].should have_text(/#{title}.zip$/) + old_path = assigns[:url_path] + response.location.should have_text(/#{assigns[:url_path]}$/) + zipfile = Zip::ZipFile.open(File.join(File.dirname(__FILE__), "../../cache/zips", old_path)) { |zipfile| + zipfile.count.should == 2 + } receive_incoming_mail('incoming-request-two-same-name.email', ir.incoming_email) - title = 'why_do_you_have_such_a_fancy_dog' get :download_entire_request, :url_title => title assigns[:url_path].should have_text(/#{title}.zip$/) old_path = assigns[:url_path] @@ -1286,9 +1347,10 @@ end describe RequestController, "authority uploads a response from the web interface" do + integrate_views fixtures :public_bodies, :public_body_translations, :public_body_versions, :users, :info_requests, :incoming_messages, :outgoing_messages, :comments, :info_request_events, :track_things - before(:all) do + before(:each) do # domain after the @ is used for authentication of FOI officers, so to test it # we need a user which isn't at localhost. @normal_user = User.new(:name => "Mr. Normal", :email => "normal-user@flourish.org", @@ -1342,7 +1404,7 @@ describe RequestController, "authority uploads a response from the web interface # How do I test a file upload in rails? # http://stackoverflow.com/questions/1178587/how-do-i-test-a-file-upload-in-rails - it "should let the requester upload a file" do + it "should let the authority upload a file" do @ir = info_requests(:fancy_dog_request) incoming_before = @ir.incoming_messages.size session[:user_id] = @foi_officer_user.id @@ -1398,7 +1460,7 @@ describe RequestController, "when doing type ahead searches" do it "should return nothing for the empty query string" do get :search_typeahead, :q => "" response.should render_template('request/_search_ahead.rhtml') - assigns[:xapian_requests].results.size.should == 0 + assigns[:xapian_requests].should be_nil end it "should return a request matching the given keyword, but not users with a matching description" do @@ -1416,11 +1478,10 @@ describe RequestController, "when doing type ahead searches" do assigns[:xapian_requests].results[1][:model].title.should == info_requests(:naughty_chicken_request).title end - it "should return partial matches" do - get :search_typeahead, :q => "chick" # 'chick' for 'chicken' + it "should not return matches for short words" do + get :search_typeahead, :q => "a" response.should render_template('request/_search_ahead.rhtml') - assigns[:xapian_requests].results.size.should == 1 - assigns[:xapian_requests].results[0][:model].title.should == info_requests(:naughty_chicken_request).title + assigns[:xapian_requests].should be_nil end end diff --git a/spec/fixtures/foi_attachments.yml b/spec/fixtures/foi_attachments.yml new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/spec/fixtures/foi_attachments.yml @@ -0,0 +1 @@ + diff --git a/spec/integration/view_request_spec.rb b/spec/integration/view_request_spec.rb new file mode 100644 index 000000000..cf1e4ca6c --- /dev/null +++ b/spec/integration/view_request_spec.rb @@ -0,0 +1,32 @@ +require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') + +describe "When viewing requests" do + + fixtures [ + :users, + :public_bodies, + :public_body_translations, + :public_body_versions, + :info_requests, + :raw_emails, + :outgoing_messages, + :incoming_messages, + :comments, + :info_request_events, + :track_things, + ] + + before(:each) do + emails = raw_emails.clone + load_raw_emails_data(emails) + end + + it "should not make endlessly recursive JSON <link>s" do + @dog_request = info_requests(:fancy_dog_request) + get "request/#{@dog_request.url_title}?unfold=1" + response.body.should_not include("dog?unfold=1.json") + response.body.should include("dog.json?unfold=1") + end + +end + diff --git a/spec/models/incoming_message_spec.rb b/spec/models/incoming_message_spec.rb index 1a4baca0b..ed31b7c5c 100644 --- a/spec/models/incoming_message_spec.rb +++ b/spec/models/incoming_message_spec.rb @@ -64,7 +64,7 @@ end describe IncomingMessage, " display attachments" do it "should not show slashes in filenames" do - foi_attachment = FOIAttachment.new() + foi_attachment = FoiAttachment.new() # http://www.whatdotheyknow.com/request/post_commercial_manager_librarie#incoming-17233 foi_attachment.filename = "FOI/09/066 RESPONSE TO FOI REQUEST RECEIVED 21st JANUARY 2009.txt" expected_display_filename = foi_attachment.filename.gsub(/\//, " ") @@ -72,10 +72,11 @@ describe IncomingMessage, " display attachments" do end it "should not show slashes in subject generated filenames" do - foi_attachment = FOIAttachment.new() + foi_attachment = FoiAttachment.new() # http://www.whatdotheyknow.com/request/post_commercial_manager_librarie#incoming-17233 foi_attachment.within_rfc822_subject = "FOI/09/066 RESPONSE TO FOI REQUEST RECEIVED 21st JANUARY 2009" foi_attachment.content_type = 'text/plain' + foi_attachment.ensure_filename! expected_display_filename = foi_attachment.within_rfc822_subject.gsub(/\//, " ") + ".txt" foi_attachment.display_filename.should == expected_display_filename end @@ -118,8 +119,7 @@ describe IncomingMessage, " checking validity to reply to" do @incoming_message = IncomingMessage.new() @incoming_message.stub!(:mail).and_return(@mail) - - @incoming_message.valid_to_reply_to?.should == result + @incoming_message._calculate_valid_to_reply_to.should == result end it "says a valid email is fine" do @@ -284,10 +284,8 @@ describe IncomingMessage, " when censoring data" do end it "should apply censor rules to From: addresses" do - mock_mail = mock('Email object') - mock_mail.stub!(:from_name_if_present).and_return("Stilton Mouse") - @im.stub!(:mail).and_return(mock_mail) - + @im.stub!(:mail_from).and_return("Stilton Mouse") + @im.stub!(:last_parsed).and_return(Time.now) safe_mail_from = @im.safe_mail_from safe_mail_from.should == "Jarlsberg Mouse" end @@ -326,21 +324,23 @@ end describe IncomingMessage, " when uudecoding bad messages" do + fixtures :incoming_messages, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :users, :foi_attachments + + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should be able to do it at all" do mail_body = load_file_fixture('incoming-request-bad-uuencoding.email') mail = TMail::Mail.parse(mail_body) mail.base64_decode - - im = IncomingMessage.new + im = incoming_messages(:useless_incoming_message) im.stub!(:mail).and_return(mail) - ir = InfoRequest.new - im.info_request = ir - u = User.new - ir.user = u - - attachments = im.get_main_body_text_uudecode_attachments - attachments.size.should == 1 - attachments[0].filename.should == 'moo.txt' + im.extract_attachments! + attachments = im.foi_attachments + attachments.size.should == 2 + attachments[1].filename.should == 'moo.txt' + im.get_attachments_for_display.size.should == 1 end it "should apply censor rules" do @@ -348,12 +348,9 @@ describe IncomingMessage, " when uudecoding bad messages" do mail = TMail::Mail.parse(mail_body) mail.base64_decode - im = IncomingMessage.new + im = incoming_messages(:useless_incoming_message) im.stub!(:mail).and_return(mail) - ir = InfoRequest.new - im.info_request = ir - u = User.new - ir.user = u + ir = info_requests(:fancy_dog_request) @censor_rule = CensorRule.new() @censor_rule.text = "moo" @@ -361,26 +358,31 @@ describe IncomingMessage, " when uudecoding bad messages" do @censor_rule.last_edit_editor = "unknown" @censor_rule.last_edit_comment = "none" ir.censor_rules << @censor_rule + im.extract_attachments! - attachments = im.get_main_body_text_uudecode_attachments + attachments = im.get_attachments_for_display attachments.size.should == 1 - attachments[0].filename.should == 'bah.txt' + attachments[0].display_filename.should == 'bah.txt' end end describe IncomingMessage, "when messages are attached to messages" do + fixtures :incoming_messages, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :users, :foi_attachments + + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should flatten all the attachments out" do mail_body = load_file_fixture('incoming-request-attach-attachments.email') mail = TMail::Mail.parse(mail_body) mail.base64_decode - im = IncomingMessage.new + im = incoming_messages(:useless_incoming_message) im.stub!(:mail).and_return(mail) - ir = InfoRequest.new - im.info_request = ir - u = User.new - ir.user = u + + im.extract_attachments! attachments = im.get_attachments_for_display attachments.size.should == 3 @@ -391,17 +393,20 @@ describe IncomingMessage, "when messages are attached to messages" do end describe IncomingMessage, "when Outlook messages are attached to messages" do + fixtures :incoming_messages, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :users, :foi_attachments + + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should flatten all the attachments out" do mail_body = load_file_fixture('incoming-request-oft-attachments.email') mail = TMail::Mail.parse(mail_body) mail.base64_decode - im = IncomingMessage.new + im = incoming_messages(:useless_incoming_message) im.stub!(:mail).and_return(mail) - ir = InfoRequest.new - im.info_request = ir - u = User.new - ir.user = u + im.extract_attachments! attachments = im.get_attachments_for_display attachments.size.should == 2 @@ -411,17 +416,20 @@ describe IncomingMessage, "when Outlook messages are attached to messages" do end describe IncomingMessage, "when TNEF attachments are attached to messages" do + fixtures :incoming_messages, :raw_emails, :public_bodies, :public_body_translations, :info_requests, :users, :foi_attachments + + before(:each) do + load_raw_emails_data(raw_emails) + end + it "should flatten all the attachments out" do mail_body = load_file_fixture('incoming-request-tnef-attachments.email') mail = TMail::Mail.parse(mail_body) mail.base64_decode - im = IncomingMessage.new + im = incoming_messages(:useless_incoming_message) im.stub!(:mail).and_return(mail) - ir = InfoRequest.new - im.info_request = ir - u = User.new - ir.user = u + im.extract_attachments! attachments = im.get_attachments_for_display attachments.size.should == 2 diff --git a/spec/models/info_request_event_spec.rb b/spec/models/info_request_event_spec.rb index 666f5cb1a..3229284cc 100644 --- a/spec/models/info_request_event_spec.rb +++ b/spec/models/info_request_event_spec.rb @@ -50,5 +50,30 @@ describe InfoRequestEvent do end end + + describe "doing search/index stuff" do + fixtures :public_bodies, :public_body_translations, :public_body_versions, :users, :info_requests, :raw_emails, :incoming_messages, :outgoing_messages, :comments, :info_request_events, :track_things + + before(:each) do + load_raw_emails_data(raw_emails) + parse_all_incoming_messages + end + + it 'should get search text for outgoing messages' do + event = info_request_events(:useless_outgoing_message_event) + message = outgoing_messages(:useless_outgoing_message).body + event.search_text_main.should == message + "\n\n" + end + + it 'should get search text for incoming messages' do + event = info_request_events(:useless_incoming_message_event) + event.search_text_main.strip.should == "No way! I'm not going to tell you that in a month of Thursdays.\n\nThe Geraldine Quango" + end + + + end + + + end diff --git a/spec/models/info_request_spec.rb b/spec/models/info_request_spec.rb index 409d48ede..b1baa66a2 100644 --- a/spec/models/info_request_spec.rb +++ b/spec/models/info_request_spec.rb @@ -143,8 +143,8 @@ describe InfoRequest do end it "should cope with indexing after item is deleted" do + IncomingMessage.find(:all).each{|x| x.parse_raw_email!} rebuild_xapian_index - # delete event from underneath indexing; shouldn't cause error info_request_events(:useless_incoming_message_event).save! info_request_events(:useless_incoming_message_event).destroy diff --git a/spec/models/xapian_spec.rb b/spec/models/xapian_spec.rb index 932966dfb..ec11c944b 100644 --- a/spec/models/xapian_spec.rb +++ b/spec/models/xapian_spec.rb @@ -4,6 +4,7 @@ describe User, " when indexing users with Xapian" do fixtures :public_bodies, :public_body_translations, :public_body_versions, :users, :info_requests, :raw_emails, :incoming_messages, :outgoing_messages, :comments, :info_request_events, :track_things it "should search by name" do + parse_all_incoming_messages rebuild_xapian_index # def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page) xapian_object = InfoRequest.full_search([User], "Silly", 'created_at', true, nil, 100, 1) @@ -12,9 +13,10 @@ describe User, " when indexing users with Xapian" do end it "should search by 'about me' text" do + rebuild_xapian_index user = users(:bob_smith_user) - # def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page) + # def InfoRequest.full_search(models, query, order, ascending, collapse, per_page, page) xapian_object = InfoRequest.full_search([User], "stuff", 'created_at', true, nil, 100, 1) xapian_object.results.size.should == 1 xapian_object.results[0][:model].should == user @@ -332,6 +334,66 @@ describe PublicBody, " when indexing authorities by tag" do end end +describe PublicBody, " when only indexing selected things on a rebuild" do + fixtures :public_bodies, :public_body_translations, :public_body_versions, :users, :info_requests, :raw_emails, :incoming_messages, :outgoing_messages, :comments, :info_request_events, :track_things + before(:each) do + load_raw_emails_data(raw_emails) + end + + it "should only index what we ask it to" do + rebuild_xapian_index + body = public_bodies(:geraldine_public_body) + body.tag_string = 'mice:3' + body.name = 'frobzn' + body.save! + # only reindex 'variety' term + dropfirst = true + terms = "V" + values = false + texts = false + rebuild_xapian_index(terms, values, texts, dropfirst) + xapian_object = InfoRequest.full_search([PublicBody], "tag:mice", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 0 + 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 + # only reindex 'tag' and text + dropfirst = true + terms = "U" + values = false + texts = true + rebuild_xapian_index(terms, values, texts, dropfirst) + xapian_object = InfoRequest.full_search([PublicBody], "tag:mice", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 1 + 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 == 0 + # only reindex 'variety' term, but keeping the existing data in-place + dropfirst = false + terms = "V" + texts = false + rebuild_xapian_index(terms, values, texts, dropfirst) + xapian_object = InfoRequest.full_search([PublicBody], "tag:mice", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 1 + 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 + # only reindex 'variety' term, blowing away existing data + dropfirst = true + rebuild_xapian_index(terms, values, texts, dropfirst) + xapian_object = InfoRequest.full_search([PublicBody], "tag:mice", 'created_at', true, nil, 100, 1) + xapian_object.results.size.should == 0 + 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 + end +end + + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ecb67a3b4..5c5cd9a7f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -77,12 +77,21 @@ def load_file_fixture(file_name) return content end -def rebuild_xapian_index +def rebuild_xapian_index(terms = true, values = true, texts = true, dropfirst = true) + parse_all_incoming_messages + if dropfirst + begin + ActsAsXapian.readable_init + FileUtils.rm_r(ActsAsXapian.db_path) + rescue RuntimeError + end + ActsAsXapian.writable_init + end verbose = false # safe_rebuild=true, which involves forking to avoid memory leaks, doesn't work well with rspec. # unsafe is significantly faster, and we can afford possible memory leaks while testing. safe_rebuild = false - ActsAsXapian.rebuild_index(["PublicBody", "User", "InfoRequestEvent"].map{|m| m.constantize}, verbose, safe_rebuild) + ActsAsXapian.rebuild_index(["PublicBody", "User", "InfoRequestEvent"].map{|m| m.constantize}, verbose, terms, values, texts, safe_rebuild) end def update_xapian_index @@ -160,3 +169,7 @@ def load_raw_emails_data(raw_emails) end raw_email.data = load_file_fixture("useless_raw_email.email") end + +def parse_all_incoming_messages + IncomingMessage.find(:all).each{|x| x.parse_raw_email!} +end |