test/test_joint.rb in joint-0.2 vs test/test_joint.rb in joint-0.3
- old
+ new
@@ -4,203 +4,248 @@
include MongoMapper::Document
plugin Joint
key :title, String
attachment :image
- attachment :pdf
+ attachment :file
end
+module JointTestHelpers
+ def all_files
+ [@file, @image, @image2, @test1, @test2]
+ end
+
+ def rewind_files
+ all_files.each { |file| file.rewind }
+ end
+
+ def open_file(name)
+ File.open(File.join(File.dirname(__FILE__), 'fixtures', name), 'r')
+ end
+
+ def grid
+ @grid ||= Mongo::Grid.new(MongoMapper.database)
+ end
+end
+
class JointTest < Test::Unit::TestCase
- def setup
- MongoMapper.database.collections.each(&:remove)
+ include JointTestHelpers
- dir = File.dirname(__FILE__) + '/fixtures'
- @pdf = File.open("#{dir}/unixref.pdf", 'r')
- @image = File.open("#{dir}/mr_t.jpg", 'r')
- @pdf_contents = File.read("#{dir}/unixref.pdf")
- @image_contents = File.read("#{dir}/mr_t.jpg")
- @grid = Mongo::Grid.new(MongoMapper.database)
- @gridfs_collection = MongoMapper.database['fs.files']
+ def setup
+ super
+ @file = open_file('unixref.pdf')
+ @image = open_file('mr_t.jpg')
+ @image2 = open_file('harmony.png')
+ @test1 = open_file('test1.txt')
+ @test2 = open_file('test2.txt')
end
def teardown
- @pdf.close
- @image.close
+ all_files.each { |file| file.close }
end
context "Using Joint plugin" do
should "add each attachment to attachment_names" do
- Asset.attachment_names.should == [:image, :pdf]
+ Asset.attachment_names.should == Set.new([:image, :file])
end
should "add keys for each attachment" do
- [:image, :pdf].each do |attachment|
+ [:image, :file].each do |attachment|
[:id, :name, :type, :size].each do |key|
- Asset.keys.include?("#{attachment}_#{key}")
+ Asset.keys.should include("#{attachment}_#{key}")
end
end
end
end
- context "Assigning attachments to document" do
+ context "Assigning new attachments to document" do
setup do
- @doc = Asset.create(:image => @image, :pdf => @pdf)
- @doc.reload
+ @doc = Asset.create(:image => @image, :file => @file)
+ rewind_files
end
+ subject { @doc }
should "assign GridFS content_type" do
- @grid.get(@doc.image_id).content_type.should == 'image/jpeg'
- @grid.get(@doc.pdf_id).content_type.should == 'application/pdf'
+ grid.get(subject.image_id).content_type.should == 'image/jpeg'
+ grid.get(subject.file_id).content_type.should == 'application/pdf'
end
should "assign joint keys" do
- @doc.image_size.should == 13661
- @doc.pdf_size.should == 68926
+ subject.image_size.should == 13661
+ subject.file_size.should == 68926
- @doc.image_type.should == "image/jpeg"
- @doc.pdf_type.should == "application/pdf"
+ subject.image_type.should == "image/jpeg"
+ subject.file_type.should == "application/pdf"
- @doc.image_id.should_not be_nil
- @doc.pdf_id.should_not be_nil
+ subject.image_id.should_not be_nil
+ subject.file_id.should_not be_nil
- @doc.image_id.should be_instance_of(Mongo::ObjectID)
- @doc.pdf_id.should be_instance_of(Mongo::ObjectID)
+ subject.image_id.should be_instance_of(Mongo::ObjectID)
+ subject.file_id.should be_instance_of(Mongo::ObjectID)
end
should "allow accessing keys through attachment proxy" do
- @doc.image.size.should == 13661
- @doc.pdf.size.should == 68926
+ subject.image.size.should == 13661
+ subject.file.size.should == 68926
- @doc.image.type.should == "image/jpeg"
- @doc.pdf.type.should == "application/pdf"
+ subject.image.type.should == "image/jpeg"
+ subject.file.type.should == "application/pdf"
- @doc.image.id.should_not be_nil
- @doc.pdf.id.should_not be_nil
+ subject.image.id.should_not be_nil
+ subject.file.id.should_not be_nil
- @doc.image.id.should be_instance_of(Mongo::ObjectID)
- @doc.pdf.id.should be_instance_of(Mongo::ObjectID)
+ subject.image.id.should be_instance_of(Mongo::ObjectID)
+ subject.file.id.should be_instance_of(Mongo::ObjectID)
end
should "proxy unknown methods to GridIO object" do
- @doc.image.files_id.should == @doc.image_id
- @doc.image.content_type.should == 'image/jpeg'
- @doc.image.filename.should == 'mr_t.jpg'
- @doc.image.file_length.should == 13661
+ subject.image.files_id.should == subject.image_id
+ subject.image.content_type.should == 'image/jpeg'
+ subject.image.filename.should == 'mr_t.jpg'
+ subject.image.file_length.should == 13661
end
should "assign file name from path if original file name not available" do
- @doc.image_name.should == 'mr_t.jpg'
- @doc.pdf_name.should == 'unixref.pdf'
+ subject.image_name.should == 'mr_t.jpg'
+ subject.file_name.should == 'unixref.pdf'
end
should "save attachment contents correctly" do
- @doc.pdf.read.should == @pdf_contents
- @doc.image.read.should == @image_contents
+ subject.file.read.should == @file.read
+ subject.image.read.should == @image.read
end
should "know that attachment exists" do
- @doc.image?.should be(true)
- @doc.pdf?.should be(true)
+ subject.image?.should be(true)
+ subject.file?.should be(true)
end
should "clear assigned attachments so they don't get uploaded twice" do
Mongo::Grid.any_instance.expects(:put).never
- @doc.save
+ subject.save
end
end
+ context "Updating existing attachment" do
+ setup do
+ @doc = Asset.create(:file => @test1)
+ assert_no_grid_difference do
+ @doc.file = @test2
+ @doc.save!
+ end
+ rewind_files
+ end
+ subject { @doc }
+
+ should "not change attachment id" do
+ subject.file_id_changed?.should be(false)
+ end
+
+ should "update keys" do
+ subject.file_name.should == 'test2.txt'
+ subject.file_type.should == "text/plain"
+ subject.file_size.should == 5
+ end
+
+ should "update GridFS" do
+ grid.get(subject.file_id).filename.should == 'test2.txt'
+ grid.get(subject.file_id).content_type.should == 'text/plain'
+ grid.get(subject.file_id).file_length.should == 5
+ grid.get(subject.file_id).read.should == @test2.read
+ end
+ end
+
context "Updating document but not attachments" do
setup do
@doc = Asset.create(:image => @image)
@doc.update_attributes(:title => 'Updated')
@doc.reload
+ rewind_files
end
+ subject { @doc }
should "not affect attachment" do
- @doc.image.read.should == @image_contents
+ subject.image.read.should == @image.read
end
should "update document attributes" do
- @doc.title.should == 'Updated'
+ subject.title.should == 'Updated'
end
end
- context "Assigning file with where file pointer is not at beginning" do
+ context "Assigning file where file pointer is not at beginning" do
setup do
@image.read
@doc = Asset.create(:image => @image)
@doc.reload
+ rewind_files
end
+ subject { @doc }
should "rewind and correctly store contents" do
- @doc.image.read.should == @image_contents
+ subject.image.read.should == @image.read
end
end
context "Setting attachment to nil" do
setup do
@doc = Asset.create(:image => @image)
+ rewind_files
end
+ subject { @doc }
should "delete attachment after save" do
- assert_no_difference '@gridfs_collection.find().count' do
- @doc.image = nil
- end
-
- assert_difference '@gridfs_collection.find().count', -1 do
- @doc.save
- end
+ assert_no_grid_difference { subject.image = nil }
+ assert_grid_difference(-1) { subject.save }
end
-
+
should "clear nil attachments after save and not attempt to delete again" do
- @doc.image = nil
- @doc.save
+ Mongo::Grid.any_instance.expects(:delete).once
+ subject.image = nil
+ subject.save
Mongo::Grid.any_instance.expects(:delete).never
- @doc.save
+ subject.save
end
end
context "Retrieving attachment that does not exist" do
setup do
@doc = Asset.create
+ rewind_files
end
+ subject { @doc }
should "know that the attachment is not present" do
- @doc.image?.should be(false)
+ subject.image?.should be(false)
end
should "raise Mongo::GridError" do
- assert_raises(Mongo::GridError) { @doc.image.read }
+ assert_raises(Mongo::GridError) { subject.image.read }
end
end
context "Destroying a document" do
setup do
@doc = Asset.create(:image => @image)
+ rewind_files
end
+ subject { @doc }
should "remove files from grid fs as well" do
- assert_difference "@gridfs_collection.find().count", -1 do
- @doc.destroy
- end
+ assert_grid_difference(-1) { subject.destroy }
end
end
context "Assigning file name" do
should "default to path" do
Asset.create(:image => @image).image.name.should == 'mr_t.jpg'
end
should "use original_filename if available" do
- begin
- file = Tempfile.new('testing.txt')
- def file.original_filename
- 'testing.txt'
- end
- doc = Asset.create(:image => file)
- assert_equal 'testing.txt', doc.image_name
- ensure
- file.close
+ def @image.original_filename
+ 'testing.txt'
end
+ doc = Asset.create(:image => @image)
+ assert_equal 'testing.txt', doc.image_name
end
end
end
\ No newline at end of file