1 $:.unshift(File.dirname(__FILE__) + '/../lib')
3 ENV['RAILS_ENV'] = 'test'
4 ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
7 require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
8 require 'active_record/fixtures'
9 require 'action_controller/test_process'
11 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
12 ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
14 db_adapter = ENV['DB']
16 # no db passed, try one of these fine config-free DBs before bombing.
22 rescue MissingSourceFile
26 rescue MissingSourceFile
31 raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
34 ActiveRecord::Base.establish_connection(config[db_adapter])
36 load(File.dirname(__FILE__) + "/schema.rb")
38 Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures"
39 $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
41 class Test::Unit::TestCase #:nodoc:
42 include ActionController::TestProcess
43 def create_fixtures(*table_names)
45 Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
47 Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
53 DbFile.transaction { [Attachment, FileAttachment, OrphanAttachment, MinimalAttachment, DbFile].each { |klass| klass.delete_all } }
54 attachment_model self.class.attachment_model
58 FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
61 self.use_transactional_fixtures = true
62 self.use_instantiated_fixtures = false
64 def self.attachment_model(klass = nil)
65 @attachment_model = klass if klass
69 def self.test_against_class(test_method, klass, subclass = false)
70 define_method("#{test_method}_on_#{:sub if subclass}class") do
71 klass = Class.new(klass) if subclass
72 attachment_model klass
73 send test_method, klass
77 def self.test_against_subclass(test_method, klass)
78 test_against_class test_method, klass, true
82 def upload_file(options = {})
83 use_temp_file options[:filename] do |file|
84 att = attachment_model.create :uploaded_data => fixture_file_upload(file, options[:content_type] || 'image/png')
85 att.reload unless att.new_record?
90 def upload_merb_file(options = {})
91 use_temp_file options[:filename] do |file|
92 att = attachment_model.create :uploaded_data => {"size" => file.size, "content_type" => options[:content_type] || 'image/png', "filename" => file, 'tempfile' => fixture_file_upload(file, options[:content_type] || 'image/png')}
93 att.reload unless att.new_record?
98 def use_temp_file(fixture_filename)
99 temp_path = File.join('/tmp', File.basename(fixture_filename))
100 FileUtils.mkdir_p File.join(fixture_path, 'tmp')
101 FileUtils.cp File.join(fixture_path, fixture_filename), File.join(fixture_path, temp_path)
104 FileUtils.rm_rf File.join(fixture_path, 'tmp')
107 def assert_created(num = 1)
108 assert_difference attachment_model.base_class, :count, num do
109 if attachment_model.included_modules.include? DbFile
110 assert_difference DbFile, :count, num do
119 def assert_not_created
120 assert_created(0) { yield }
123 def should_reject_by_size_with(klass)
124 attachment_model klass
125 assert_not_created do
126 attachment = upload_file :filename => '/files/rails.png'
127 assert attachment.new_record?
128 assert attachment.errors.on(:size)
129 assert_nil attachment.db_file if attachment.respond_to?(:db_file)
133 def assert_difference(object, method = nil, difference = 1)
134 initial_value = object.send(method)
136 assert_equal initial_value + difference, object.send(method)
139 def assert_no_difference(object, method, &block)
140 assert_difference object, method, 0, &block
143 def attachment_model(klass = nil)
144 @attachment_model = klass if klass
149 require File.join(File.dirname(__FILE__), 'fixtures/attachment')
150 require File.join(File.dirname(__FILE__), 'base_attachment_tests')