PageRenderTime 34ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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

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