]> projects.mako.cc - selectricity-live/blob - vendor/plugins/attachment_fu/test/test_helper.rb
Major update of Selectricity to work with Rails 2.2.2 from 1.2!
[selectricity-live] / vendor / plugins / attachment_fu / test / test_helper.rb
1 $:.unshift(File.dirname(__FILE__) + '/../lib')
2
3 ENV['RAILS_ENV'] = 'test'
4
5 require 'test/unit'
6 require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
7 require 'breakpoint'
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 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)
94       yield temp_path
95     ensure
96       FileUtils.rm_rf File.join(fixture_path, 'tmp')
97     end
98
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
103             yield
104           end
105         else
106           yield
107         end
108       end
109     end
110     
111     def assert_not_created
112       assert_created(0) { yield }
113     end
114     
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)
122       end
123     end
124     
125     def assert_difference(object, method = nil, difference = 1)
126       initial_value = object.send(method)
127       yield
128       assert_equal initial_value + difference, object.send(method)
129     end
130     
131     def assert_no_difference(object, method, &block)
132       assert_difference object, method, 0, &block
133     end
134     
135     def attachment_model(klass = nil)
136       @attachment_model = klass if klass 
137       @attachment_model
138     end
139 end
140
141 require File.join(File.dirname(__FILE__), 'fixtures/attachment')
142 require File.join(File.dirname(__FILE__), 'base_attachment_tests')

Benjamin Mako Hill || Want to submit a patch?