]> projects.mako.cc - selectricity-live/blob - vendor/plugins/attachment_fu/test/test_helper.rb
made the license clear
[selectricity-live] / vendor / plugins / attachment_fu / test / test_helper.rb
1 $:.unshift(File.dirname(__FILE__) + '/../lib')
2
3 ENV['RAILS_ENV'] = 'test'
4 ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
5
6 require 'test/unit'
7 require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
8 require 'active_record/fixtures'
9 require 'action_controller/test_process'
10
11 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
12 ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
13
14 db_adapter = ENV['DB']
15
16 # no db passed, try one of these fine config-free DBs before bombing.
17 db_adapter ||= 
18   begin
19     require 'rubygems'
20     require 'sqlite'
21     'sqlite'
22   rescue MissingSourceFile
23     begin
24       require 'sqlite3'
25       'sqlite3'
26     rescue MissingSourceFile
27     end
28   end
29
30 if db_adapter.nil?
31   raise "No DB Adapter selected.  Pass the DB= option to pick one, or install Sqlite or Sqlite3."
32 end
33
34 ActiveRecord::Base.establish_connection(config[db_adapter])
35
36 load(File.dirname(__FILE__) + "/schema.rb")
37
38 Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures"
39 $LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
40
41 class Test::Unit::TestCase #:nodoc:
42   include ActionController::TestProcess
43   def create_fixtures(*table_names)
44     if block_given?
45       Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
46     else
47       Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
48     end
49   end
50
51   def setup
52     Attachment.saves = 0
53     DbFile.transaction { [Attachment, FileAttachment, OrphanAttachment, MinimalAttachment, DbFile].each { |klass| klass.delete_all } }
54     attachment_model self.class.attachment_model
55   end
56   
57   def teardown
58     FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
59   end
60
61   self.use_transactional_fixtures = true
62   self.use_instantiated_fixtures  = false
63
64   def self.attachment_model(klass = nil)
65     @attachment_model = klass if klass 
66     @attachment_model
67   end
68
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
74     end
75   end
76
77   def self.test_against_subclass(test_method, klass)
78     test_against_class test_method, klass, true
79   end
80
81   protected
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?
86         return att
87       end
88     end
89
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?
94         return att
95       end
96     end
97     
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)
102       yield temp_path
103     ensure
104       FileUtils.rm_rf File.join(fixture_path, 'tmp')
105     end
106
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
111             yield
112           end
113         else
114           yield
115         end
116       end
117     end
118     
119     def assert_not_created
120       assert_created(0) { yield }
121     end
122     
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)
130       end
131     end
132     
133     def assert_difference(object, method = nil, difference = 1)
134       initial_value = object.send(method)
135       yield
136       assert_equal initial_value + difference, object.send(method)
137     end
138     
139     def assert_no_difference(object, method, &block)
140       assert_difference object, method, 0, &block
141     end
142     
143     def attachment_model(klass = nil)
144       @attachment_model = klass if klass 
145       @attachment_model
146     end
147 end
148
149 require File.join(File.dirname(__FILE__), 'fixtures/attachment')
150 require File.join(File.dirname(__FILE__), 'base_attachment_tests')

Benjamin Mako Hill || Want to submit a patch?