PageRenderTime 125ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/imagealchemy/vendor/plugins/paperclip/test/attachment_test.rb

https://github.com/retohuber/edu
Ruby | 768 lines | 658 code | 109 blank | 1 comment | 10 complexity | 4cf7bfcd8f7611bd721c03d5211ff011 MD5 | raw file
  1. require 'test/helper'
  2. class Dummy
  3. # This is a dummy class
  4. end
  5. class AttachmentTest < Test::Unit::TestCase
  6. should "return the path based on the url by default" do
  7. @attachment = attachment :url => "/:class/:id/:basename"
  8. @model = @attachment.instance
  9. @model.id = 1234
  10. @model.avatar_file_name = "fake.jpg"
  11. assert_equal "#{RAILS_ROOT}/public/fake_models/1234/fake", @attachment.path
  12. end
  13. context "Attachment default_options" do
  14. setup do
  15. rebuild_model
  16. @old_default_options = Paperclip::Attachment.default_options.dup
  17. @new_default_options = @old_default_options.merge({
  18. :path => "argle/bargle",
  19. :url => "fooferon",
  20. :default_url => "not here.png"
  21. })
  22. end
  23. teardown do
  24. Paperclip::Attachment.default_options.merge! @old_default_options
  25. end
  26. should "be overrideable" do
  27. Paperclip::Attachment.default_options.merge!(@new_default_options)
  28. @new_default_options.keys.each do |key|
  29. assert_equal @new_default_options[key],
  30. Paperclip::Attachment.default_options[key]
  31. end
  32. end
  33. context "without an Attachment" do
  34. setup do
  35. @dummy = Dummy.new
  36. end
  37. should "return false when asked exists?" do
  38. assert !@dummy.avatar.exists?
  39. end
  40. end
  41. context "on an Attachment" do
  42. setup do
  43. @dummy = Dummy.new
  44. @attachment = @dummy.avatar
  45. end
  46. Paperclip::Attachment.default_options.keys.each do |key|
  47. should "be the default_options for #{key}" do
  48. assert_equal @old_default_options[key],
  49. @attachment.instance_variable_get("@#{key}"),
  50. key
  51. end
  52. end
  53. context "when redefined" do
  54. setup do
  55. Paperclip::Attachment.default_options.merge!(@new_default_options)
  56. @dummy = Dummy.new
  57. @attachment = @dummy.avatar
  58. end
  59. Paperclip::Attachment.default_options.keys.each do |key|
  60. should "be the new default_options for #{key}" do
  61. assert_equal @new_default_options[key],
  62. @attachment.instance_variable_get("@#{key}"),
  63. key
  64. end
  65. end
  66. end
  67. end
  68. end
  69. context "An attachment with similarly named interpolations" do
  70. setup do
  71. rebuild_model :path => ":id.omg/:id-bbq/:idwhat/:id_partition.wtf"
  72. @dummy = Dummy.new
  73. @dummy.stubs(:id).returns(1024)
  74. @file = File.new(File.join(File.dirname(__FILE__),
  75. "fixtures",
  76. "5k.png"), 'rb')
  77. @dummy.avatar = @file
  78. end
  79. teardown { @file.close }
  80. should "make sure that they are interpolated correctly" do
  81. assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path
  82. end
  83. end
  84. context "An attachment with a :rails_env interpolation" do
  85. setup do
  86. @rails_env = "blah"
  87. @id = 1024
  88. rebuild_model :path => ":rails_env/:id.png"
  89. @dummy = Dummy.new
  90. @dummy.stubs(:id).returns(@id)
  91. @file = StringIO.new(".")
  92. @dummy.avatar = @file
  93. end
  94. should "return the proper path" do
  95. temporary_rails_env(@rails_env) {
  96. assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path
  97. }
  98. end
  99. end
  100. context "An attachment with a default style and an extension interpolation" do
  101. setup do
  102. @attachment = attachment :path => ":basename.:extension",
  103. :styles => { :default => ["100x100", :png] },
  104. :default_style => :default
  105. @file = StringIO.new("...")
  106. @file.expects(:original_filename).returns("file.jpg")
  107. end
  108. should "return the right extension for the path" do
  109. @attachment.assign(@file)
  110. assert_equal "file.png", @attachment.path
  111. end
  112. end
  113. context "An attachment with :convert_options" do
  114. setup do
  115. rebuild_model :styles => {
  116. :thumb => "100x100",
  117. :large => "400x400"
  118. },
  119. :convert_options => {
  120. :all => "-do_stuff",
  121. :thumb => "-thumbnailize"
  122. }
  123. @dummy = Dummy.new
  124. @dummy.avatar
  125. end
  126. should "report the correct options when sent #extra_options_for(:thumb)" do
  127. assert_equal "-thumbnailize -do_stuff", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect
  128. end
  129. should "report the correct options when sent #extra_options_for(:large)" do
  130. assert_equal "-do_stuff", @dummy.avatar.send(:extra_options_for, :large)
  131. end
  132. before_should "call extra_options_for(:thumb/:large)" do
  133. Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:thumb)
  134. Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:large)
  135. end
  136. end
  137. context "An attachment with :convert_options that is a proc" do
  138. setup do
  139. rebuild_model :styles => {
  140. :thumb => "100x100",
  141. :large => "400x400"
  142. },
  143. :convert_options => {
  144. :all => lambda{|i| i.all },
  145. :thumb => lambda{|i| i.thumb }
  146. }
  147. Dummy.class_eval do
  148. def all; "-all"; end
  149. def thumb; "-thumb"; end
  150. end
  151. @dummy = Dummy.new
  152. @dummy.avatar
  153. end
  154. should "report the correct options when sent #extra_options_for(:thumb)" do
  155. assert_equal "-thumb -all", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect
  156. end
  157. should "report the correct options when sent #extra_options_for(:large)" do
  158. assert_equal "-all", @dummy.avatar.send(:extra_options_for, :large)
  159. end
  160. before_should "call extra_options_for(:thumb/:large)" do
  161. Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:thumb)
  162. Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:large)
  163. end
  164. end
  165. context "An attachment with :path that is a proc" do
  166. setup do
  167. rebuild_model :path => lambda{ |attachment| "path/#{attachment.instance.other}.:extension" }
  168. @file = File.new(File.join(File.dirname(__FILE__),
  169. "fixtures",
  170. "5k.png"), 'rb')
  171. @dummyA = Dummy.new(:other => 'a')
  172. @dummyA.avatar = @file
  173. @dummyB = Dummy.new(:other => 'b')
  174. @dummyB.avatar = @file
  175. end
  176. teardown { @file.close }
  177. should "return correct path" do
  178. assert_equal "path/a.png", @dummyA.avatar.path
  179. assert_equal "path/b.png", @dummyB.avatar.path
  180. end
  181. end
  182. context "An attachment with :styles that is a proc" do
  183. setup do
  184. rebuild_model :styles => lambda{ |attachment| {:thumb => "50x50#", :large => "400x400"} }
  185. @attachment = Dummy.new.avatar
  186. end
  187. should "have the correct geometry" do
  188. assert_equal "50x50#", @attachment.styles[:thumb][:geometry]
  189. end
  190. end
  191. context "An attachment with :url that is a proc" do
  192. setup do
  193. rebuild_model :url => lambda{ |attachment| "path/#{attachment.instance.other}.:extension" }
  194. @file = File.new(File.join(File.dirname(__FILE__),
  195. "fixtures",
  196. "5k.png"), 'rb')
  197. @dummyA = Dummy.new(:other => 'a')
  198. @dummyA.avatar = @file
  199. @dummyB = Dummy.new(:other => 'b')
  200. @dummyB.avatar = @file
  201. end
  202. teardown { @file.close }
  203. should "return correct url" do
  204. assert_equal "path/a.png", @dummyA.avatar.url(:original, false)
  205. assert_equal "path/b.png", @dummyB.avatar.url(:original, false)
  206. end
  207. end
  208. geometry_specs = [
  209. [ lambda{|z| "50x50#" }, :png ],
  210. lambda{|z| "50x50#" },
  211. { :geometry => lambda{|z| "50x50#" } }
  212. ]
  213. geometry_specs.each do |geometry_spec|
  214. context "An attachment geometry like #{geometry_spec}" do
  215. setup do
  216. rebuild_model :styles => { :normal => geometry_spec }
  217. @attachment = Dummy.new.avatar
  218. end
  219. should "not run the procs immediately" do
  220. assert_kind_of Proc, @attachment.styles[:normal][:geometry]
  221. end
  222. context "when assigned" do
  223. setup do
  224. @file = StringIO.new(".")
  225. @attachment.assign(@file)
  226. end
  227. should "have the correct geometry" do
  228. assert_equal "50x50#", @attachment.styles[:normal][:geometry]
  229. end
  230. end
  231. end
  232. end
  233. context "An attachment with both 'normal' and hash-style styles" do
  234. setup do
  235. rebuild_model :styles => {
  236. :normal => ["50x50#", :png],
  237. :hash => { :geometry => "50x50#", :format => :png }
  238. }
  239. @dummy = Dummy.new
  240. @attachment = @dummy.avatar
  241. end
  242. [:processors, :whiny, :convert_options, :geometry, :format].each do |field|
  243. should "have the same #{field} field" do
  244. assert_equal @attachment.styles[:normal][field], @attachment.styles[:hash][field]
  245. end
  246. end
  247. end
  248. context "An attachment with :processors that is a proc" do
  249. setup do
  250. rebuild_model :styles => { :normal => '' }, :processors => lambda { |a| [ :test ] }
  251. @attachment = Dummy.new.avatar
  252. end
  253. should "not run the proc immediately" do
  254. assert_kind_of Proc, @attachment.styles[:normal][:processors]
  255. end
  256. context "when assigned" do
  257. setup do
  258. @attachment.assign(StringIO.new("."))
  259. end
  260. should "have the correct processors" do
  261. assert_equal [ :test ], @attachment.styles[:normal][:processors]
  262. end
  263. end
  264. end
  265. context "An attachment with erroring processor" do
  266. setup do
  267. rebuild_model :processor => [:thumbnail], :styles => { :small => '' }, :whiny_thumbnails => true
  268. @dummy = Dummy.new
  269. Paperclip::Thumbnail.expects(:make).raises(Paperclip::PaperclipError, "cannot be processed.")
  270. @file = StringIO.new("...")
  271. @file.stubs(:to_tempfile).returns(@file)
  272. @dummy.avatar = @file
  273. end
  274. should "correctly forward processing error message to the instance" do
  275. @dummy.valid?
  276. assert_contains @dummy.errors.full_messages, "Avatar cannot be processed."
  277. end
  278. end
  279. context "An attachment with multiple processors" do
  280. setup do
  281. class Paperclip::Test < Paperclip::Processor; end
  282. @style_params = { :once => {:one => 1, :two => 2} }
  283. rebuild_model :processors => [:thumbnail, :test], :styles => @style_params
  284. @dummy = Dummy.new
  285. @file = StringIO.new("...")
  286. @file.stubs(:to_tempfile).returns(@file)
  287. Paperclip::Test.stubs(:make).returns(@file)
  288. Paperclip::Thumbnail.stubs(:make).returns(@file)
  289. end
  290. context "when assigned" do
  291. setup { @dummy.avatar = @file }
  292. before_should "call #make on all specified processors" do
  293. expected_params = @style_params[:once].merge({:processors => [:thumbnail, :test], :whiny => true, :convert_options => ""})
  294. Paperclip::Thumbnail.expects(:make).with(@file, expected_params, @dummy.avatar).returns(@file)
  295. Paperclip::Test.expects(:make).with(@file, expected_params, @dummy.avatar).returns(@file)
  296. end
  297. before_should "call #make with attachment passed as third argument" do
  298. expected_params = @style_params[:once].merge({:processors => [:thumbnail, :test], :whiny => true, :convert_options => ""})
  299. Paperclip::Test.expects(:make).with(@file, expected_params, @dummy.avatar).returns(@file)
  300. end
  301. end
  302. end
  303. context "An attachment with no processors defined" do
  304. setup do
  305. rebuild_model :processors => [], :styles => {:something => 1}
  306. @dummy = Dummy.new
  307. @file = StringIO.new("...")
  308. end
  309. should "raise when assigned to" do
  310. assert_raises(RuntimeError){ @dummy.avatar = @file }
  311. end
  312. end
  313. context "Assigning an attachment with post_process hooks" do
  314. setup do
  315. rebuild_model :styles => { :something => "100x100#" }
  316. Dummy.class_eval do
  317. before_avatar_post_process :do_before_avatar
  318. after_avatar_post_process :do_after_avatar
  319. before_post_process :do_before_all
  320. after_post_process :do_after_all
  321. def do_before_avatar; end
  322. def do_after_avatar; end
  323. def do_before_all; end
  324. def do_after_all; end
  325. end
  326. @file = StringIO.new(".")
  327. @file.stubs(:to_tempfile).returns(@file)
  328. @dummy = Dummy.new
  329. Paperclip::Thumbnail.stubs(:make).returns(@file)
  330. @attachment = @dummy.avatar
  331. end
  332. should "call the defined callbacks when assigned" do
  333. @dummy.expects(:do_before_avatar).with()
  334. @dummy.expects(:do_after_avatar).with()
  335. @dummy.expects(:do_before_all).with()
  336. @dummy.expects(:do_after_all).with()
  337. Paperclip::Thumbnail.expects(:make).returns(@file)
  338. @dummy.avatar = @file
  339. end
  340. should "not cancel the processing if a before_post_process returns nil" do
  341. @dummy.expects(:do_before_avatar).with().returns(nil)
  342. @dummy.expects(:do_after_avatar).with()
  343. @dummy.expects(:do_before_all).with().returns(nil)
  344. @dummy.expects(:do_after_all).with()
  345. Paperclip::Thumbnail.expects(:make).returns(@file)
  346. @dummy.avatar = @file
  347. end
  348. should "cancel the processing if a before_post_process returns false" do
  349. @dummy.expects(:do_before_avatar).never
  350. @dummy.expects(:do_after_avatar).never
  351. @dummy.expects(:do_before_all).with().returns(false)
  352. @dummy.expects(:do_after_all).never
  353. Paperclip::Thumbnail.expects(:make).never
  354. @dummy.avatar = @file
  355. end
  356. should "cancel the processing if a before_avatar_post_process returns false" do
  357. @dummy.expects(:do_before_avatar).with().returns(false)
  358. @dummy.expects(:do_after_avatar).never
  359. @dummy.expects(:do_before_all).with().returns(true)
  360. @dummy.expects(:do_after_all).never
  361. Paperclip::Thumbnail.expects(:make).never
  362. @dummy.avatar = @file
  363. end
  364. end
  365. context "Assigning an attachment" do
  366. setup do
  367. rebuild_model :styles => { :something => "100x100#" }
  368. @file = StringIO.new(".")
  369. @file.expects(:original_filename).returns("5k.png\n\n")
  370. @file.expects(:content_type).returns("image/png\n\n")
  371. @file.stubs(:to_tempfile).returns(@file)
  372. @dummy = Dummy.new
  373. Paperclip::Thumbnail.expects(:make).returns(@file)
  374. @dummy.expects(:run_callbacks).with(:before_avatar_post_process, {:original => @file})
  375. @dummy.expects(:run_callbacks).with(:before_post_process, {:original => @file})
  376. @dummy.expects(:run_callbacks).with(:after_avatar_post_process, {:original => @file, :something => @file})
  377. @dummy.expects(:run_callbacks).with(:after_post_process, {:original => @file, :something => @file})
  378. @attachment = @dummy.avatar
  379. @dummy.avatar = @file
  380. end
  381. should "strip whitespace from original_filename field" do
  382. assert_equal "5k.png", @dummy.avatar.original_filename
  383. end
  384. should "strip whitespace from content_type field" do
  385. assert_equal "image/png", @dummy.avatar.instance.avatar_content_type
  386. end
  387. end
  388. context "Attachment with strange letters" do
  389. setup do
  390. rebuild_model
  391. @not_file = mock
  392. @tempfile = mock
  393. @not_file.stubs(:nil?).returns(false)
  394. @not_file.expects(:size).returns(10)
  395. @tempfile.expects(:size).returns(10)
  396. @not_file.expects(:to_tempfile).returns(@tempfile)
  397. @not_file.expects(:original_filename).returns("sheep_say_bæ.png\r\n")
  398. @not_file.expects(:content_type).returns("image/png\r\n")
  399. @dummy = Dummy.new
  400. @attachment = @dummy.avatar
  401. @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
  402. @attachment.expects(:queue_existing_for_delete)
  403. @attachment.expects(:post_process)
  404. @attachment.expects(:valid?).returns(true)
  405. @attachment.expects(:validate)
  406. @dummy.avatar = @not_file
  407. end
  408. should "remove strange letters and replace with underscore (_)" do
  409. assert_equal "sheep_say_b_.png", @dummy.avatar.original_filename
  410. end
  411. end
  412. context "An attachment" do
  413. setup do
  414. @old_defaults = Paperclip::Attachment.default_options.dup
  415. Paperclip::Attachment.default_options.merge!({
  416. :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
  417. })
  418. FileUtils.rm_rf("tmp")
  419. rebuild_model
  420. @instance = Dummy.new
  421. @attachment = Paperclip::Attachment.new(:avatar, @instance)
  422. @file = File.new(File.join(File.dirname(__FILE__),
  423. "fixtures",
  424. "5k.png"), 'rb')
  425. end
  426. teardown do
  427. @file.close
  428. Paperclip::Attachment.default_options.merge!(@old_defaults)
  429. end
  430. should "raise if there are not the correct columns when you try to assign" do
  431. @other_attachment = Paperclip::Attachment.new(:not_here, @instance)
  432. assert_raises(Paperclip::PaperclipError) do
  433. @other_attachment.assign(@file)
  434. end
  435. end
  436. should "return its default_url when no file assigned" do
  437. assert @attachment.to_file.nil?
  438. assert_equal "/avatars/original/missing.png", @attachment.url
  439. assert_equal "/avatars/blah/missing.png", @attachment.url(:blah)
  440. end
  441. should "return nil as path when no file assigned" do
  442. assert @attachment.to_file.nil?
  443. assert_equal nil, @attachment.path
  444. assert_equal nil, @attachment.path(:blah)
  445. end
  446. context "with a file assigned in the database" do
  447. setup do
  448. @attachment.stubs(:instance_read).with(:file_name).returns("5k.png")
  449. @attachment.stubs(:instance_read).with(:content_type).returns("image/png")
  450. @attachment.stubs(:instance_read).with(:file_size).returns(12345)
  451. now = Time.now
  452. Time.stubs(:now).returns(now)
  453. @attachment.stubs(:instance_read).with(:updated_at).returns(Time.now)
  454. end
  455. should "return a correct url even if the file does not exist" do
  456. assert_nil @attachment.to_file
  457. assert_match %r{^/system/avatars/#{@instance.id}/blah/5k\.png}, @attachment.url(:blah)
  458. end
  459. should "make sure the updated_at mtime is in the url if it is defined" do
  460. assert_match %r{#{Time.now.to_i}$}, @attachment.url(:blah)
  461. end
  462. should "make sure the updated_at mtime is NOT in the url if false is passed to the url method" do
  463. assert_no_match %r{#{Time.now.to_i}$}, @attachment.url(:blah, false)
  464. end
  465. context "with the updated_at field removed" do
  466. setup do
  467. @attachment.stubs(:instance_read).with(:updated_at).returns(nil)
  468. end
  469. should "only return the url without the updated_at when sent #url" do
  470. assert_match "/avatars/#{@instance.id}/blah/5k.png", @attachment.url(:blah)
  471. end
  472. end
  473. should "return the proper path when filename has a single .'s" do
  474. assert_equal "./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.png", @attachment.path
  475. end
  476. should "return the proper path when filename has multiple .'s" do
  477. @attachment.stubs(:instance_read).with(:file_name).returns("5k.old.png")
  478. assert_equal "./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.old.png", @attachment.path
  479. end
  480. context "when expecting three styles" do
  481. setup do
  482. styles = {:styles => { :large => ["400x400", :png],
  483. :medium => ["100x100", :gif],
  484. :small => ["32x32#", :jpg]}}
  485. @attachment = Paperclip::Attachment.new(:avatar,
  486. @instance,
  487. styles)
  488. end
  489. context "and assigned a file" do
  490. setup do
  491. now = Time.now
  492. Time.stubs(:now).returns(now)
  493. @attachment.assign(@file)
  494. end
  495. should "be dirty" do
  496. assert @attachment.dirty?
  497. end
  498. context "and saved" do
  499. setup do
  500. @attachment.save
  501. end
  502. should "return the real url" do
  503. file = @attachment.to_file
  504. assert file
  505. assert_match %r{^/system/avatars/#{@instance.id}/original/5k\.png}, @attachment.url
  506. assert_match %r{^/system/avatars/#{@instance.id}/small/5k\.jpg}, @attachment.url(:small)
  507. file.close
  508. end
  509. should "commit the files to disk" do
  510. [:large, :medium, :small].each do |style|
  511. io = @attachment.to_io(style)
  512. assert File.exists?(io)
  513. assert ! io.is_a?(::Tempfile)
  514. io.close
  515. end
  516. end
  517. should "save the files as the right formats and sizes" do
  518. [[:large, 400, 61, "PNG"],
  519. [:medium, 100, 15, "GIF"],
  520. [:small, 32, 32, "JPEG"]].each do |style|
  521. cmd = %Q[identify -format "%w %h %b %m" "#{@attachment.path(style.first)}"]
  522. out = `#{cmd}`
  523. width, height, size, format = out.split(" ")
  524. assert_equal style[1].to_s, width.to_s
  525. assert_equal style[2].to_s, height.to_s
  526. assert_equal style[3].to_s, format.to_s
  527. end
  528. end
  529. should "still have its #file attribute not be nil" do
  530. assert ! (file = @attachment.to_file).nil?
  531. file.close
  532. end
  533. context "and trying to delete" do
  534. setup do
  535. @existing_names = @attachment.styles.keys.collect do |style|
  536. @attachment.path(style)
  537. end
  538. end
  539. should "delete the files after assigning nil" do
  540. @attachment.expects(:instance_write).with(:file_name, nil)
  541. @attachment.expects(:instance_write).with(:content_type, nil)
  542. @attachment.expects(:instance_write).with(:file_size, nil)
  543. @attachment.expects(:instance_write).with(:updated_at, nil)
  544. @attachment.assign nil
  545. @attachment.save
  546. @existing_names.each{|f| assert ! File.exists?(f) }
  547. end
  548. should "delete the files when you call #clear and #save" do
  549. @attachment.expects(:instance_write).with(:file_name, nil)
  550. @attachment.expects(:instance_write).with(:content_type, nil)
  551. @attachment.expects(:instance_write).with(:file_size, nil)
  552. @attachment.expects(:instance_write).with(:updated_at, nil)
  553. @attachment.clear
  554. @attachment.save
  555. @existing_names.each{|f| assert ! File.exists?(f) }
  556. end
  557. should "delete the files when you call #delete" do
  558. @attachment.expects(:instance_write).with(:file_name, nil)
  559. @attachment.expects(:instance_write).with(:content_type, nil)
  560. @attachment.expects(:instance_write).with(:file_size, nil)
  561. @attachment.expects(:instance_write).with(:updated_at, nil)
  562. @attachment.destroy
  563. @existing_names.each{|f| assert ! File.exists?(f) }
  564. end
  565. end
  566. end
  567. end
  568. end
  569. end
  570. context "when trying a nonexistant storage type" do
  571. setup do
  572. rebuild_model :storage => :not_here
  573. end
  574. should "not be able to find the module" do
  575. assert_raise(NameError){ Dummy.new.avatar }
  576. end
  577. end
  578. end
  579. context "An attachment with only a avatar_file_name column" do
  580. setup do
  581. ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
  582. table.column :avatar_file_name, :string
  583. end
  584. rebuild_class
  585. @dummy = Dummy.new
  586. @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb')
  587. end
  588. teardown { @file.close }
  589. should "not error when assigned an attachment" do
  590. assert_nothing_raised { @dummy.avatar = @file }
  591. end
  592. should "return the time when sent #avatar_updated_at" do
  593. now = Time.now
  594. Time.stubs(:now).returns(now)
  595. @dummy.avatar = @file
  596. assert now, @dummy.avatar.updated_at
  597. end
  598. should "return nil when reloaded and sent #avatar_updated_at" do
  599. @dummy.save
  600. @dummy.reload
  601. assert_nil @dummy.avatar.updated_at
  602. end
  603. should "return the right value when sent #avatar_file_size" do
  604. @dummy.avatar = @file
  605. assert_equal @file.size, @dummy.avatar.size
  606. end
  607. context "and avatar_updated_at column" do
  608. setup do
  609. ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp
  610. rebuild_class
  611. @dummy = Dummy.new
  612. end
  613. should "not error when assigned an attachment" do
  614. assert_nothing_raised { @dummy.avatar = @file }
  615. end
  616. should "return the right value when sent #avatar_updated_at" do
  617. now = Time.now
  618. Time.stubs(:now).returns(now)
  619. @dummy.avatar = @file
  620. assert_equal now.to_i, @dummy.avatar.updated_at
  621. end
  622. end
  623. context "and avatar_content_type column" do
  624. setup do
  625. ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string
  626. rebuild_class
  627. @dummy = Dummy.new
  628. end
  629. should "not error when assigned an attachment" do
  630. assert_nothing_raised { @dummy.avatar = @file }
  631. end
  632. should "return the right value when sent #avatar_content_type" do
  633. @dummy.avatar = @file
  634. assert_equal "image/png", @dummy.avatar.content_type
  635. end
  636. end
  637. context "and avatar_file_size column" do
  638. setup do
  639. ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :integer
  640. rebuild_class
  641. @dummy = Dummy.new
  642. end
  643. should "not error when assigned an attachment" do
  644. assert_nothing_raised { @dummy.avatar = @file }
  645. end
  646. should "return the right value when sent #avatar_file_size" do
  647. @dummy.avatar = @file
  648. assert_equal @file.size, @dummy.avatar.size
  649. end
  650. should "return the right value when saved, reloaded, and sent #avatar_file_size" do
  651. @dummy.avatar = @file
  652. @dummy.save
  653. @dummy = Dummy.find(@dummy.id)
  654. assert_equal @file.size, @dummy.avatar.size
  655. end
  656. end
  657. end
  658. end