Skip to content

Instantly share code, notes, and snippets.

@jonmagic
Created December 22, 2009 18:13
Show Gist options
  • Save jonmagic/261915 to your computer and use it in GitHub Desktop.
Save jonmagic/261915 to your computer and use it in GitHub Desktop.
require "test_helper"
require File.expand_path(File.dirname(__FILE__) + '/../lib/grip')
class Foo
include MongoMapper::Document
include Grip
has_grid_attachment :image
has_grid_attachment :pdf
end
class TestContent< Test::Unit::TestCase
context "A MongoMapper Document" do
context "with an attachment" do
setup do
@image = File.open("#{File.dirname(__FILE__)}/cthulhu.png",'r')
@pdf = File.open("#{File.dirname(__FILE__)}/sample.pdf",'r')
@mp3 = File.open("#{File.dirname(__FILE__)}/song.mp3",'r')
@document = Foo.create(:image => @image, :pdf => @pdf, :mp3 => @mp3)
@from_collection = Foo.first
end
should "have correct mime type" do
assert_equal("image/png", @from_collection.image.content_type)
assert_equal("application/pdf", @from_collection.pdf.content_type)
end
should "respond to the dynamic keys" do
[:pdf_path,:image_path].each {|k| assert @document.respond_to? k }
end
should "have the correct paths" do
assert_equal("foo/#{@document.id}/image/cthulhu.png", @document.image_path)
assert_equal("foo/#{@document.id}/pdf/sample.pdf", @document.pdf_path)
end
should "cleanup attachments" do
img_path = "foo/#{@document._id}/image/cthulhu.png", @document.image_path
pdf_path = "foo/#{@document._id}/pdf/sample.pdf", @document.pdf_path
@document.destroy
assert !GridStore.exist?(MONGO_DB,img_path)
assert !GridStore.exist?(MONGO_DB,pdf_path)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment