1 $:.unshift(File.dirname(__FILE__) + '/../lib')
3 ENV['RAILS_ENV'] = 'test'
6 require File.expand_path(File.join(File.dirname(__FILE__), '../../../../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 use_temp_file(fixture_filename)
91 temp_path = File.join('/tmp', File.basename(fixture_filename))
92 FileUtils.mkdir_p File.join(fixture_path, 'tmp')
93 FileUtils.cp File.join(fixture_path, fixture_filename), File.join(fixture_path, temp_path)
96 FileUtils.rm_rf File.join(fixture_path, 'tmp')
99 def assert_created(num = 1)
100 assert_difference attachment_model.base_class, :count, num do
101 if attachment_model.included_modules.include? DbFile
102 assert_difference DbFile, :count, num do
111 def assert_not_created
112 assert_created(0) { yield }
115 def should_reject_by_size_with(klass)
116 attachment_model klass
117 assert_not_created do
118 attachment = upload_file :filename => '/files/rails.png'
119 assert attachment.new_record?
120 assert attachment.errors.on(:size)
121 assert_nil attachment.db_file if attachment.respond_to?(:db_file)
125 def assert_difference(object, method = nil, difference = 1)
126 initial_value = object.send(method)
128 assert_equal initial_value + difference, object.send(method)
131 def assert_no_difference(object, method, &block)
132 assert_difference object, method, 0, &block
135 def attachment_model(klass = nil)
136 @attachment_model = klass if klass
141 require File.join(File.dirname(__FILE__), 'fixtures/attachment')
142 require File.join(File.dirname(__FILE__), 'base_attachment_tests')