1 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
3 class FileSystemTest < Test::Unit::TestCase
4 include BaseAttachmentTests
5 attachment_model FileAttachment
7 def test_filesystem_size_for_file_attachment(klass = FileAttachment)
10 attachment = upload_file :filename => '/files/rails.png'
11 assert_equal attachment.size, File.open(attachment.full_filename).stat.size
15 test_against_subclass :test_filesystem_size_for_file_attachment, FileAttachment
17 def test_should_not_overwrite_file_attachment(klass = FileAttachment)
18 attachment_model klass
20 real = upload_file :filename => '/files/rails.png'
22 assert !real.new_record?, real.errors.full_messages.join("\n")
23 assert !real.size.zero?
25 fake = upload_file :filename => '/files/fake/rails.png'
27 assert !fake.size.zero?
29 assert_not_equal File.open(real.full_filename).stat.size, File.open(fake.full_filename).stat.size
33 test_against_subclass :test_should_not_overwrite_file_attachment, FileAttachment
35 def test_should_store_file_attachment_in_filesystem(klass = FileAttachment)
36 attachment_model klass
39 attachment = upload_file :filename => '/files/rails.png'
40 assert_valid attachment
41 assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
46 test_against_subclass :test_should_store_file_attachment_in_filesystem, FileAttachment
48 def test_should_delete_old_file_when_updating(klass = FileAttachment)
49 attachment_model klass
50 attachment = upload_file :filename => '/files/rails.png'
51 old_filename = attachment.full_filename
53 use_temp_file 'files/rails.png' do |file|
54 attachment.filename = 'rails2.png'
55 attachment.temp_path = File.join(fixture_path, file)
57 assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
58 assert !File.exists?(old_filename), "#{old_filename} still exists"
63 test_against_subclass :test_should_delete_old_file_when_updating, FileAttachment
65 def test_should_delete_old_file_when_renaming(klass = FileAttachment)
66 attachment_model klass
67 attachment = upload_file :filename => '/files/rails.png'
68 old_filename = attachment.full_filename
70 attachment.filename = 'rails2.png'
72 assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
73 assert !File.exists?(old_filename), "#{old_filename} still exists"
74 assert !attachment.reload.size.zero?
75 assert_equal 'rails2.png', attachment.filename
79 test_against_subclass :test_should_delete_old_file_when_renaming, FileAttachment