test/test_joint.rb in joint-0.1.1 vs test/test_joint.rb in joint-0.2
- old
+ new
@@ -1,108 +1,206 @@
require 'helper'
-class Foo
+class Asset
include MongoMapper::Document
plugin Joint
+ key :title, String
attachment :image
attachment :pdf
end
class JointTest < Test::Unit::TestCase
def setup
MongoMapper.database.collections.each(&:remove)
- @grid = Mongo::Grid.new(MongoMapper.database)
- 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")
-
- @doc = Foo.create(:image => @image, :pdf => @pdf)
- @doc.reload
+ 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']
end
def teardown
@pdf.close
@image.close
end
- test "assigns grid fs content type correctly" do
- assert_equal "image/jpeg", @grid.get(@doc.image_id).content_type
- assert_equal "application/pdf", @grid.get(@doc.pdf_id).content_type
+ context "Using Joint plugin" do
+ should "add each attachment to attachment_names" do
+ Asset.attachment_names.should == [:image, :pdf]
+ end
+
+ should "add keys for each attachment" do
+ [:image, :pdf].each do |attachment|
+ [:id, :name, :type, :size].each do |key|
+ Asset.keys.include?("#{attachment}_#{key}")
+ end
+ end
+ end
end
- test "assigns keys correctly" do
- assert_equal 13661, @doc.image_size
- assert_equal 68926, @doc.pdf_size
+ context "Assigning attachments to document" do
+ setup do
+ @doc = Asset.create(:image => @image, :pdf => @pdf)
+ @doc.reload
+ end
- assert_equal "image/jpeg", @doc.image_type
- assert_equal "application/pdf", @doc.pdf_type
+ 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'
+ end
- assert_not_nil @doc.image_id
- assert_not_nil @doc.pdf_id
+ should "assign joint keys" do
+ @doc.image_size.should == 13661
+ @doc.pdf_size.should == 68926
- assert_kind_of Mongo::ObjectID, @doc.image_id
- assert_kind_of Mongo::ObjectID, @doc.pdf_id
- end
+ @doc.image_type.should == "image/jpeg"
+ @doc.pdf_type.should == "application/pdf"
- test "accessing keys through attachment proxy" do
- assert_equal 13661, @doc.image.size
- assert_equal 68926, @doc.pdf.size
+ @doc.image_id.should_not be_nil
+ @doc.pdf_id.should_not be_nil
- assert_equal "image/jpeg", @doc.image.type
- assert_equal "application/pdf", @doc.pdf.type
+ @doc.image_id.should be_instance_of(Mongo::ObjectID)
+ @doc.pdf_id.should be_instance_of(Mongo::ObjectID)
+ end
- assert_not_nil @doc.image.id
- assert_not_nil @doc.pdf.id
+ should "allow accessing keys through attachment proxy" do
+ @doc.image.size.should == 13661
+ @doc.pdf.size.should == 68926
- assert_kind_of Mongo::ObjectID, @doc.image.id
- assert_kind_of Mongo::ObjectID, @doc.pdf.id
+ @doc.image.type.should == "image/jpeg"
+ @doc.pdf.type.should == "application/pdf"
+
+ @doc.image.id.should_not be_nil
+ @doc.pdf.id.should_not be_nil
+
+ @doc.image.id.should be_instance_of(Mongo::ObjectID)
+ @doc.pdf.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
+ 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'
+ end
+
+ should "save attachment contents correctly" do
+ @doc.pdf.read.should == @pdf_contents
+ @doc.image.read.should == @image_contents
+ end
+
+ should "know that attachment exists" do
+ @doc.image?.should be(true)
+ @doc.pdf?.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
+ end
end
- test "sends unknown proxy methods to grid io object" do
- assert_equal 13661, @doc.image.file_length
- assert_equal 'image/jpeg', @doc.image.content_type
- assert_equal 'mr_t.jpg', @doc.image.filename
- assert_equal @doc.image_id, @doc.image.files_id
+ context "Updating document but not attachments" do
+ setup do
+ @doc = Asset.create(:image => @image)
+ @doc.update_attributes(:title => 'Updated')
+ @doc.reload
+ end
+
+ should "not affect attachment" do
+ @doc.image.read.should == @image_contents
+ end
+
+ should "update document attributes" do
+ @doc.title.should == 'Updated'
+ end
end
- test "assigns file name from path if original file name not available" do
- assert_equal 'mr_t.jpg', @doc.image_name
- assert_equal 'unixref.pdf', @doc.pdf_name
+ context "Assigning file with where file pointer is not at beginning" do
+ setup do
+ @image.read
+ @doc = Asset.create(:image => @image)
+ @doc.reload
+ end
+
+ should "rewind and correctly store contents" do
+ @doc.image.read.should == @image_contents
+ end
end
- test "assigns file name from original filename if available" do
- begin
- file = Tempfile.new('testing.txt')
- def file.original_filename
- 'testing.txt'
+ context "Setting attachment to nil" do
+ setup do
+ @doc = Asset.create(:image => @image)
+ end
+
+ should "delete attachment after save" do
+ assert_no_difference '@gridfs_collection.find().count' do
+ @doc.image = nil
end
- doc = Foo.create(:image => file)
- assert_equal 'testing.txt', doc.image_name
- ensure
- file.close
+
+ assert_difference '@gridfs_collection.find().count', -1 do
+ @doc.save
+ end
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).never
+ @doc.save
+ end
end
- test "responds to keys" do
- [ :pdf_size, :pdf_id, :pdf_name, :pdf_type,
- :image_size, :image_id, :image_name, :image_type
- ].each do |method|
- assert @doc.respond_to?(method)
+ context "Retrieving attachment that does not exist" do
+ setup do
+ @doc = Asset.create
end
+
+ should "know that the attachment is not present" do
+ @doc.image?.should be(false)
+ end
+
+ should "raise Mongo::GridError" do
+ assert_raises(Mongo::GridError) { @doc.image.read }
+ end
end
- test "saves attachments correctly" do
- assert_equal @pdf_contents, @doc.pdf.read
- assert_equal @image_contents, @doc.image.read
+ context "Destroying a document" do
+ setup do
+ @doc = Asset.create(:image => @image)
+ end
+
+ should "remove files from grid fs as well" do
+ assert_difference "@gridfs_collection.find().count", -1 do
+ @doc.destroy
+ end
+ end
end
- test "cleans up attachments on destroy" do
- @doc.destroy
- assert_raises(Mongo::GridError) { @grid.get(@doc.image_id) }
- assert_raises(Mongo::GridError) { @grid.get(@doc.pdf_id) }
+ 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
+ end
+ end
end
end
\ No newline at end of file