pothoven-attachment_fu 3.2.6 → 3.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/test_helper.rb DELETED
@@ -1,180 +0,0 @@
1
-
2
- $:.unshift(File.dirname(__FILE__) + '/../lib')
3
-
4
- ENV['RAILS_ENV'] = 'test'
5
- ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
6
-
7
- require 'test/unit'
8
- require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
9
-
10
- config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
11
- ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
12
-
13
- db_adapter = ENV['DB']
14
-
15
- # no db passed, try one of these fine config-free DBs before bombing.
16
- db_adapter ||=
17
- begin
18
- require 'rubygems'
19
- require 'sqlite'
20
- 'sqlite'
21
- rescue MissingSourceFile
22
- begin
23
- require 'sqlite3'
24
- 'sqlite3'
25
- rescue MissingSourceFile
26
- end
27
- end
28
-
29
- if db_adapter.nil?
30
- raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
31
- end
32
-
33
- ActiveRecord::Base.establish_connection(config[db_adapter])
34
-
35
- load(File.dirname(__FILE__) + "/schema.rb")
36
-
37
- FIXTURE_PATH = File.dirname(__FILE__) + "/fixtures"
38
- $LOAD_PATH.unshift(FIXTURE_PATH)
39
-
40
- class Test::Unit::TestCase #:nodoc:
41
- include ActionDispatch::TestProcess
42
- def create_fixtures(*table_names)
43
- if block_given?
44
- Fixtures.create_fixtures(FIXTURE_PATH, table_names) { yield }
45
- else
46
- Fixtures.create_fixtures(FIXTURE_PATH, table_names)
47
- end
48
- end
49
-
50
- def setup
51
- Attachment.saves = 0
52
- DbFile.transaction { [Attachment, FileAttachment, OrphanAttachment, MinimalAttachment, DbFile].each { |klass| klass.delete_all } }
53
- attachment_model self.class.attachment_model
54
- end
55
-
56
- def teardown
57
- FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
58
- # Files generated by random_tempfile_filename
59
- FileUtils.rm_rf Dir['[0-9]*.{png,jpg}']
60
- end
61
-
62
- #self.use_transactional_fixtures = true
63
- #self.use_instantiated_fixtures = false
64
-
65
- def self.attachment_model(klass = nil)
66
- @attachment_model = klass if klass
67
- @attachment_model
68
- end
69
-
70
- def self.test_against_class(test_method, klass, subclass = false)
71
- define_method("#{test_method}_on_#{:sub if subclass}class") do
72
- klass = Class.new(klass) if subclass
73
- attachment_model klass
74
- send test_method, klass
75
- end
76
- end
77
-
78
- def self.test_against_subclass(test_method, klass)
79
- test_against_class test_method, klass, true
80
- end
81
-
82
- protected
83
- def upload_file(options = {})
84
- use_temp_file options[:filename] do |file|
85
- opts = { :uploaded_data => fixture_file_upload(file, options[:content_type] || 'image/png') }
86
- opts.update(options.reject { |k, v| ![:imageable_type, :imageable_id].include?(k) })
87
- att = attachment_model.create opts
88
- att.reload unless att.new_record?
89
- return att
90
- end
91
- end
92
-
93
- def upload_merb_file(options = {})
94
- use_temp_file options[:filename] do |file|
95
- 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')}
96
- att.reload unless att.new_record?
97
- return att
98
- end
99
- end
100
-
101
- def use_temp_file(fixture_filename)
102
- temp_path = File.join('/tmp', File.basename(fixture_filename))
103
- temp_dir = File.join(FIXTURE_PATH, 'tmp')
104
- use_file = File.join(FIXTURE_PATH, temp_path)
105
- FileUtils.mkdir_p temp_dir
106
- FileUtils.cp File.join(FIXTURE_PATH, fixture_filename), use_file
107
- yield use_file
108
- ensure
109
- FileUtils.rm_rf temp_dir
110
- end
111
-
112
- def assert_created(num = 1)
113
- assert_difference attachment_model.base_class, :count, num do
114
- if attachment_model.included_modules.include? DbFile
115
- assert_difference DbFile, :count, num do
116
- yield
117
- end
118
- else
119
- yield
120
- end
121
- end
122
- end
123
-
124
- def assert_valid(record)
125
- assert record.valid?, record.errors.full_messages.join("\n")
126
- end
127
-
128
- def assert_file_jpeg_quality(model, thumbnail, expected)
129
- filename = if model.respond_to?(:full_filename)
130
- model.full_filename(thumbnail)
131
- else
132
- thumb = thumbnail ? model.thumbnails.find(:first, :conditions => { :thumbnail => thumbnail.to_s }, :include => :db_file) : model
133
- unless thumb && thumb.db_file && thumb.db_file.data && thumb.db_file.data.size > 0
134
- STDERR.puts "Cannot find DB file data for thumbnail #{thumbnail.inspect} -> Aborting JPEG quality check."
135
- return
136
- end
137
- result = Tempfile.new('dbfile_dump').path
138
- File.open(result, 'wb') { |f| f.write(thumb.db_file.data) }
139
- result
140
- end
141
- quality = %x(identify -format '%Q' "#{filename}" 2> /dev/null)
142
- if $?.success?
143
- assert_equal expected, quality.to_i, "Produced JPEG quality (thumbnail: #{thumbnail.inspect}) is incorrect."
144
- else
145
- STDERR.puts "ImageMagick's identify not found / not in PATH: can't quickly check produced image quality."
146
- end
147
- end
148
-
149
- def assert_not_created
150
- assert_created(0) { yield }
151
- end
152
-
153
- def should_reject_by_size_with(klass)
154
- attachment_model klass
155
- assert_not_created do
156
- attachment = upload_file :filename => '/files/rails.png'
157
- assert attachment.new_record?
158
- assert attachment.errors.on(:size)
159
- assert_nil attachment.db_file if attachment.respond_to?(:db_file)
160
- end
161
- end
162
-
163
- def assert_difference(object, method = nil, difference = 1)
164
- initial_value = object.send(method)
165
- yield
166
- assert_equal initial_value + difference, object.send(method)
167
- end
168
-
169
- def assert_no_difference(object, method, &block)
170
- assert_difference object, method, 0, &block
171
- end
172
-
173
- def attachment_model(klass = nil)
174
- @attachment_model = klass if klass
175
- @attachment_model
176
- end
177
- end
178
-
179
- require File.join(File.dirname(__FILE__), 'fixtures/attachment')
180
- require File.join(File.dirname(__FILE__), 'base_attachment_tests')
@@ -1,55 +0,0 @@
1
- require File.expand_path(File.join(File.dirname(__FILE__), 'test_helper'))
2
-
3
- class ValidationTest < Test::Unit::TestCase
4
- def test_should_invalidate_big_files
5
- @attachment = SmallAttachment.new
6
- assert !@attachment.valid?
7
- assert @attachment.errors[:size]
8
-
9
- @attachment.size = 2000
10
- assert !@attachment.valid?
11
- assert @attachment.errors[:size], @attachment.errors.full_messages.to_sentence
12
-
13
- @attachment.size = 1000
14
- assert !@attachment.valid?
15
- assert @attachment.errors[:size].empty?
16
- end
17
-
18
- def test_should_invalidate_small_files
19
- @attachment = BigAttachment.new
20
- assert !@attachment.valid?
21
- assert @attachment.errors[:size]
22
-
23
- @attachment.size = 2000
24
- assert !@attachment.valid?
25
- assert @attachment.errors[:size], @attachment.errors.full_messages.to_sentence
26
-
27
- @attachment.size = 1.megabyte
28
- assert !@attachment.valid?
29
- assert @attachment.errors[:size].empty?
30
- end
31
-
32
- def test_should_validate_content_type
33
- @attachment = PdfAttachment.new
34
- assert !@attachment.valid?
35
- assert @attachment.errors[:content_type]
36
-
37
- @attachment.content_type = 'foo'
38
- assert !@attachment.valid?
39
- assert @attachment.errors[:content_type]
40
-
41
- @attachment.content_type = 'pdf'
42
- assert !@attachment.valid?
43
- assert @attachment.errors[:content_type].empty?
44
- end
45
-
46
- def test_should_require_filename
47
- @attachment = Attachment.new
48
- assert !@attachment.valid?
49
- assert @attachment.errors[:filename]
50
-
51
- @attachment.filename = 'foo'
52
- assert !@attachment.valid?
53
- assert @attachment.errors[:filename].empty?
54
- end
55
- end