]> projects.mako.cc - selectricity-live/blob - vendor/plugins/attachment_fu/test/backends/file_system_test.rb
d3250c1a9f3eac38ae4e8409fbca2cbaee6ca53d
[selectricity-live] / vendor / plugins / attachment_fu / test / backends / file_system_test.rb
1 require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
3 class FileSystemTest < Test::Unit::TestCase
4   include BaseAttachmentTests
5   attachment_model FileAttachment
6
7   def test_filesystem_size_for_file_attachment(klass = FileAttachment)
8     attachment_model klass
9     assert_created 1 do
10       attachment = upload_file :filename => '/files/rails.png'
11       assert_equal attachment.size, File.open(attachment.full_filename).stat.size
12     end
13   end
14   
15   test_against_subclass :test_filesystem_size_for_file_attachment, FileAttachment
16
17   def test_should_not_overwrite_file_attachment(klass = FileAttachment)
18     attachment_model klass
19     assert_created 2 do
20       real = upload_file :filename => '/files/rails.png'
21       assert_valid real
22       assert !real.new_record?, real.errors.full_messages.join("\n")
23       assert !real.size.zero?
24       
25       fake = upload_file :filename => '/files/fake/rails.png'
26       assert_valid fake
27       assert !fake.size.zero?
28       
29       assert_not_equal File.open(real.full_filename).stat.size, File.open(fake.full_filename).stat.size
30     end
31   end
32   
33   test_against_subclass :test_should_not_overwrite_file_attachment, FileAttachment
34
35   def test_should_store_file_attachment_in_filesystem(klass = FileAttachment)
36     attachment_model klass
37     attachment = nil
38     assert_created do
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"    
42     end
43     attachment
44   end
45   
46   test_against_subclass :test_should_store_file_attachment_in_filesystem, FileAttachment
47   
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
52     assert_not_created do
53       use_temp_file 'files/rails.png' do |file|
54         attachment.filename        = 'rails2.png'
55         attachment.temp_path = File.join(fixture_path, file)
56         attachment.save!
57         assert  File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"    
58         assert !File.exists?(old_filename),             "#{old_filename} still exists"
59       end
60     end
61   end
62   
63   test_against_subclass :test_should_delete_old_file_when_updating, FileAttachment
64   
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
69     assert_not_created do
70       attachment.filename        = 'rails2.png'
71       attachment.save
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
76     end
77   end
78   
79   test_against_subclass :test_should_delete_old_file_when_renaming, FileAttachment
80 end

Benjamin Mako Hill || Want to submit a patch?