1 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
4 class FileSystemTest < Test::Unit::TestCase
5 include BaseAttachmentTests
6 attachment_model FileAttachment
8 def test_filesystem_size_for_file_attachment(klass = FileAttachment)
11 attachment = upload_file :filename => '/files/rails.png'
12 assert_equal attachment.size, File.open(attachment.full_filename).stat.size
16 test_against_subclass :test_filesystem_size_for_file_attachment, FileAttachment
18 def test_should_not_overwrite_file_attachment(klass = FileAttachment)
19 attachment_model klass
21 real = upload_file :filename => '/files/rails.png'
23 assert !real.new_record?, real.errors.full_messages.join("\n")
24 assert !real.size.zero?
26 fake = upload_file :filename => '/files/fake/rails.png'
28 assert !fake.size.zero?
30 assert_not_equal File.open(real.full_filename).stat.size, File.open(fake.full_filename).stat.size
34 test_against_subclass :test_should_not_overwrite_file_attachment, FileAttachment
36 def test_should_store_file_attachment_in_filesystem(klass = FileAttachment)
37 attachment_model klass
40 attachment = upload_file :filename => '/files/rails.png'
41 assert_valid attachment
42 assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
47 test_against_subclass :test_should_store_file_attachment_in_filesystem, FileAttachment
49 def test_should_delete_old_file_when_updating(klass = FileAttachment)
50 attachment_model klass
51 attachment = upload_file :filename => '/files/rails.png'
52 old_filename = attachment.full_filename
54 use_temp_file 'files/rails.png' do |file|
55 attachment.filename = 'rails2.png'
56 attachment.temp_paths.unshift File.join(fixture_path, file)
58 assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
59 assert !File.exists?(old_filename), "#{old_filename} still exists"
64 test_against_subclass :test_should_delete_old_file_when_updating, FileAttachment
66 def test_should_delete_old_file_when_renaming(klass = FileAttachment)
67 attachment_model klass
68 attachment = upload_file :filename => '/files/rails.png'
69 old_filename = attachment.full_filename
71 attachment.filename = 'rails2.png'
73 assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
74 assert !File.exists?(old_filename), "#{old_filename} still exists"
75 assert !attachment.reload.size.zero?
76 assert_equal 'rails2.png', attachment.filename
80 test_against_subclass :test_should_delete_old_file_when_renaming, FileAttachment
82 def test_path_partitioning_works_on_integer_id(klass = FileAttachment)
83 attachment_model klass
85 # Create a random attachment object, doesn't matter what.
86 attachment = upload_file :filename => '/files/rails.png'
87 old_id = attachment.id
91 assert_equal ["0000", "0001", "bar.txt"], attachment.send(:partitioned_path, "bar.txt")
93 attachment.id = old_id
97 test_against_subclass :test_path_partitioning_works_on_integer_id, FileAttachment
99 def test_path_partitioning_with_string_id_works_by_generating_hash(klass = FileAttachmentWithStringId)
100 attachment_model klass
102 # Create a random attachment object, doesn't matter what.
103 attachment = upload_file :filename => '/files/rails.png'
104 old_id = attachment.id
105 attachment.id = "hello world some long string"
106 hash = Digest::SHA512.hexdigest("hello world some long string")
115 ], attachment.send(:partitioned_path, "bar.txt")
117 attachment.id = old_id
121 test_against_subclass :test_path_partitioning_with_string_id_works_by_generating_hash, FileAttachmentWithStringId
123 def test_path_partition_string_id_hashing_is_turned_off_if_id_is_uuid(klass = FileAttachmentWithUuid)
124 attachment_model klass
126 # Create a random attachment object, doesn't matter what.
127 attachment = upload_file :filename => '/files/rails.png'
128 old_id = attachment.id
129 attachment.id = "0c0743b698483569dc65909a8cdb3bf9"
136 ], attachment.send(:partitioned_path, "bar.txt")
138 attachment.id = old_id
142 test_against_subclass :test_path_partition_string_id_hashing_is_turned_off_if_id_is_uuid, FileAttachmentWithUuid