Browse files

tests

git-svn-id: http://svn.techno-weenie.net/projects/plugins/attachment_fu@2549 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information...
1 parent 1e91f65 commit 941233203ad0ddd85929e91625cfa98bdaaa334b technoweenie committed Dec 14, 2006
View
4 Rakefile
@@ -5,14 +5,14 @@ require 'rake/rdoctask'
desc 'Default: run unit tests.'
task :default => :test
-desc 'Test the acts_as_attachment plugin.'
+desc 'Test the attachment_fu plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
-desc 'Generate documentation for the acts_as_attachment plugin.'
+desc 'Generate documentation for the attachment_fu plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ActsAsAttachment'
View
18 test/database.yml
@@ -0,0 +1,18 @@
+sqlite:
+ :adapter: sqlite
+ :dbfile: attachment_fu_plugin.sqlite.db
+sqlite3:
+ :adapter: sqlite3
+ :dbfile: attachment_fu_plugin.sqlite3.db
+postgresql:
+ :adapter: postgresql
+ :username: postgres
+ :password: postgres
+ :database: attachment_fu_plugin_test
+ :min_messages: ERROR
+mysql:
+ :adapter: mysql
+ :host: localhost
+ :username: rails
+ :password:
+ :database: attachment_fu_plugin_test
View
84 test/fixtures/attachment.rb
@@ -0,0 +1,84 @@
+class Attachment < ActiveRecord::Base
+ @@saves = 0
+ cattr_accessor :saves
+ has_attachment
+ validates_as_attachment
+ after_attachment_saved do |record|
+ self.saves += 1
+ end
+end
+
+class SmallAttachment < Attachment
+ has_attachment :max_size => 1.kilobyte
+end
+
+class BigAttachment < Attachment
+ has_attachment :size => 1.megabyte..2.megabytes
+end
+
+class PdfAttachment < Attachment
+ has_attachment :content_type => 'pdf'
+end
+
+class DocAttachment < Attachment
+ has_attachment :content_type => %w(pdf doc txt)
+end
+
+class ImageAttachment < Attachment
+ has_attachment :content_type => :image, :resize_to => [50,50]
+end
+
+class ImageOrPdfAttachment < Attachment
+ has_attachment :content_type => ['pdf', :image], :resize_to => 'x50'
+end
+
+class ImageWithThumbsAttachment < Attachment
+ has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
+ after_resize do |record, img|
+ record.aspect_ratio = img.columns.to_f / img.rows.to_f
+ end
+end
+
+class FileAttachment < ActiveRecord::Base
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files'
+ validates_as_attachment
+end
+
+class ImageFileAttachment < FileAttachment
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
+ :content_type => :image, :resize_to => [50,50]
+end
+
+class ImageWithThumbsFileAttachment < FileAttachment
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
+ :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
+ after_resize do |record, img|
+ record.aspect_ratio = img.columns.to_f / img.rows.to_f
+ end
+end
+
+class ImageWithThumbsClassFileAttachment < FileAttachment
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files',
+ :thumbnails => { :thumb => [50, 50] }, :resize_to => [55,55],
+ :thumbnail_class => 'ImageThumbnail'
+end
+
+class ImageThumbnail < FileAttachment
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files/thumbnails'
+end
+
+# no parent
+class OrphanAttachment < ActiveRecord::Base
+ has_attachment
+ validates_as_attachment
+end
+
+# no filename, no size, no content_type
+class MinimalAttachment < ActiveRecord::Base
+ has_attachment :file_system_path => 'vendor/plugins/attachment_fu/test/files'
+ validates_as_attachment
+
+ def filename
+ "#{id}.file"
+ end
+end
View
BIN test/fixtures/files/fake/rails.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
1 test/fixtures/files/foo.txt
@@ -0,0 +1 @@
+foo
View
BIN test/fixtures/files/rails.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
41 test/schema.rb
@@ -0,0 +1,41 @@
+ActiveRecord::Schema.define(:version => 0) do
+ create_table :attachments, :force => true do |t|
+ t.column :db_file_id, :integer
+ t.column :parent_id, :integer
+ t.column :thumbnail, :string
+ t.column :filename, :string, :limit => 255
+ t.column :content_type, :string, :limit => 255
+ t.column :size, :integer
+ t.column :width, :integer
+ t.column :height, :integer
+ t.column :aspect_ratio, :float
+ end
+
+ create_table :file_attachments, :force => true do |t|
+ t.column :parent_id, :integer
+ t.column :thumbnail, :string
+ t.column :filename, :string, :limit => 255
+ t.column :content_type, :string, :limit => 255
+ t.column :size, :integer
+ t.column :width, :integer
+ t.column :height, :integer
+ t.column :type, :string
+ t.column :aspect_ratio, :float
+ end
+
+ create_table :orphan_attachments, :force => true do |t|
+ t.column :db_file_id, :integer
+ t.column :filename, :string, :limit => 255
+ t.column :content_type, :string, :limit => 255
+ t.column :size, :integer
+ end
+
+ create_table :minimal_attachments, :force => true do |t|
+ t.column :size, :integer
+ t.column :content_type, :string, :limit => 255
+ end
+
+ create_table :db_files, :force => true do |t|
+ t.column :data, :binary
+ end
+end
View
103 test/test_helper.rb
@@ -0,0 +1,103 @@
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+
+require 'test/unit'
+require File.expand_path(File.join(File.dirname(__FILE__), '../../../../config/environment.rb'))
+require 'breakpoint'
+require 'active_record/fixtures'
+require 'action_controller/test_process'
+
+config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
+ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
+ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
+
+load(File.dirname(__FILE__) + "/schema.rb")
+
+Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures/"
+$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
+
+class Test::Unit::TestCase #:nodoc:
+ include ActionController::TestProcess
+ def create_fixtures(*table_names)
+ if block_given?
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
+ else
+ Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
+ end
+ end
+
+ def setup
+ FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
+ attachment_model self.class.attachment_model
+ end
+
+ self.use_transactional_fixtures = true
+ self.use_instantiated_fixtures = false
+
+ def self.attachment_model(klass = nil)
+ @attachment_model = klass if klass
+ @attachment_model
+ end
+
+ def self.test_against_class(test_method, klass, subclass = false)
+ define_method("#{test_method}_on_#{:sub if subclass}class") do
+ klass = Class.new(klass) if subclass
+ attachment_model klass
+ send test_method, klass
+ end
+ end
+
+ def self.test_against_subclass(test_method, klass)
+ test_against_class test_method, klass, true
+ end
+
+ protected
+ def upload_file(options = {})
+ att = attachment_model.create :uploaded_data => fixture_file_upload(options[:filename], options[:content_type] || 'image/png')
+ att.reload unless att.new_record?
+ att
+ end
+
+ def assert_created(num = 1)
+ assert_difference attachment_model.base_class, :count, num do
+ if attachment_model.included_modules.include? DbFile
+ assert_difference DbFile, :count, num do
+ yield
+ end
+ else
+ yield
+ end
+ end
+ end
+
+ def assert_not_created
+ assert_created(0) { yield }
+ end
+
+ def should_reject_by_size_with(klass)
+ attachment_model klass
+ assert_not_created do
+ attachment = upload_file :filename => '/files/rails.png'
+ assert attachment.new_record?
+ assert attachment.errors.on(:size)
+ assert_nil attachment.db_file if attachment.respond_to?(:db_file)
+ end
+ end
+
+ def assert_difference(object, method = nil, difference = 1)
+ initial_value = object.send(method)
+ yield
+ assert_equal initial_value + difference, object.send(method)
+ end
+
+ def assert_no_difference(object, method, &block)
+ assert_difference object, method, 0, &block
+ end
+
+ def attachment_model(klass = nil)
+ @attachment_model = klass if klass
+ @attachment_model
+ end
+end
+
+require File.join(File.dirname(__FILE__), 'fixtures/attachment')
+require File.join(File.dirname(__FILE__), 'fixtures/base_attachment_tests')

0 comments on commit 9412332

Please sign in to comment.