diff options
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/email_helpers.rb | 23 | ||||
-rw-r--r-- | spec/support/load_file_fixtures.rb | 14 | ||||
-rw-r--r-- | spec/support/xapian_index.rb | 42 |
3 files changed, 79 insertions, 0 deletions
diff --git a/spec/support/email_helpers.rb b/spec/support/email_helpers.rb new file mode 100644 index 000000000..7e98c39f6 --- /dev/null +++ b/spec/support/email_helpers.rb @@ -0,0 +1,23 @@ +def load_raw_emails_data + raw_emails_yml = File.join(RSpec.configuration.fixture_path, "raw_emails.yml") + for raw_email_id in YAML::load_file(raw_emails_yml).map{|k,v| v["id"]} do + raw_email = RawEmail.find(raw_email_id) + raw_email.data = load_file_fixture("raw_emails/%d.email" % [raw_email_id]) + end +end + +def receive_incoming_mail(email_name, email_to, email_from = 'geraldinequango@localhost') + email_name = file_fixture_name(email_name) + content = File.read(email_name) + content.gsub!('EMAIL_TO', email_to) + content.gsub!('EMAIL_FROM', email_from) + RequestMailer.receive(content) +end + +def get_fixture_mail(filename) + MailHandler.mail_from_raw_email(load_file_fixture(filename)) +end + +def parse_all_incoming_messages + IncomingMessage.find(:all).each{ |x| x.parse_raw_email! } +end diff --git a/spec/support/load_file_fixtures.rb b/spec/support/load_file_fixtures.rb new file mode 100644 index 000000000..08079f654 --- /dev/null +++ b/spec/support/load_file_fixtures.rb @@ -0,0 +1,14 @@ +def file_fixture_name(file_name) + return File.join(RSpec.configuration.fixture_path, "files", file_name) +end + +def load_file_fixture(file_name, as_binary=false) + file_name = file_fixture_name(file_name) + content = File.open(file_name, 'r') do |file| + if as_binary + file.set_encoding(Encoding::BINARY) if file.respond_to?(:set_encoding) + end + file.read + end + return content +end diff --git a/spec/support/xapian_index.rb b/spec/support/xapian_index.rb new file mode 100644 index 000000000..344c28ebb --- /dev/null +++ b/spec/support/xapian_index.rb @@ -0,0 +1,42 @@ +# Rebuild the current xapian index +def rebuild_xapian_index(terms = true, values = true, texts = true, dropfirst = true) + if dropfirst + begin + ActsAsXapian.readable_init + FileUtils.rm_r(ActsAsXapian.db_path) + rescue RuntimeError + end + ActsAsXapian.writable_init + ActsAsXapian.writable_db.close + end + parse_all_incoming_messages + # 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. + models = [PublicBody, User, InfoRequestEvent] + ActsAsXapian.rebuild_index(models, verbose=false, terms, values, texts, safe_rebuild=false) +end + +def update_xapian_index + ActsAsXapian.update_index(flush_to_disk=false, verbose=false) +end + +# Copy the xapian index created in create_fixtures_xapian_index to a temporary +# copy at the same level and point xapian at the copy +def get_fixtures_xapian_index() + # Create a base index for the fixtures if not already created + $existing_xapian_db ||= create_fixtures_xapian_index + # Store whatever the xapian db path is originally + $original_xapian_path ||= ActsAsXapian.db_path + path_array = $original_xapian_path.split(File::Separator) + path_array.pop + temp_path = File.join(path_array, 'test.temp') + FileUtils.remove_entry_secure(temp_path, force=true) + FileUtils.cp_r($original_xapian_path, temp_path) + ActsAsXapian.db_path = temp_path +end + +# Create a clean xapian index based on the fixture files and the raw_email data. +def create_fixtures_xapian_index + load_raw_emails_data + rebuild_xapian_index +end |