test/test_joint.rb in joint-0.3.2 vs test/test_joint.rb in joint-0.4

- old
+ new

@@ -110,12 +110,12 @@ subject.file_type.should == "application/pdf" subject.image_id.should_not be_nil subject.file_id.should_not be_nil - subject.image_id.should be_instance_of(BSON::ObjectID) - subject.file_id.should be_instance_of(BSON::ObjectID) + subject.image_id.should be_instance_of(BSON::ObjectId) + subject.file_id.should be_instance_of(BSON::ObjectId) end should "allow accessing keys through attachment proxy" do subject.image.size.should == 13661 subject.file.size.should == 68926 @@ -124,12 +124,12 @@ subject.file.type.should == "application/pdf" subject.image.id.should_not be_nil subject.file.id.should_not be_nil - subject.image.id.should be_instance_of(BSON::ObjectID) - subject.file.id.should be_instance_of(BSON::ObjectID) + subject.image.id.should be_instance_of(BSON::ObjectId) + subject.file.id.should be_instance_of(BSON::ObjectId) end should "proxy unknown methods to GridIO object" do subject.image.files_id.should == subject.image_id subject.image.content_type.should == 'image/jpeg' @@ -150,10 +150,15 @@ should "know that attachment exists" do subject.image?.should be(true) subject.file?.should be(true) end + should "respond with false when asked if the attachment is nil?" do + subject.image.nil?.should be(false) + subject.file.nil?.should be(false) + end + should "clear assigned attachments so they don't get uploaded twice" do Mongo::Grid.any_instance.expects(:put).never subject.save end end @@ -229,17 +234,42 @@ should "delete attachment after save" do assert_no_grid_difference { subject.image = nil } assert_grid_difference(-1) { subject.save } end + should "know that the attachment has been nullified" do + subject.image = nil + subject.image?.should be(false) + end + + should "respond with true when asked if the attachment is nil?" do + subject.image = nil + subject.image.nil?.should be(true) + end + should "clear nil attachments after save and not attempt to delete again" do Mongo::Grid.any_instance.expects(:delete).once subject.image = nil subject.save Mongo::Grid.any_instance.expects(:delete).never subject.save end + + should "clear id, name, type, size" do + subject.image = nil + subject.save + assert_nil subject.image_id + assert_nil subject.image_name + assert_nil subject.image_type + assert_nil subject.image_size + subject.reload + assert_nil subject.image_id + assert_nil subject.image_name + assert_nil subject.image_type + assert_nil subject.image_size + end + end context "Retrieving attachment that does not exist" do setup do @doc = Asset.create @@ -249,10 +279,14 @@ should "know that the attachment is not present" do subject.image?.should be(false) end + should "respond with true when asked if the attachment is nil?" do + subject.image.nil?.should be(true) + end + should "raise Mongo::GridFileNotFound" do assert_raises(Mongo::GridFileNotFound) { subject.image.read } end end @@ -277,8 +311,32 @@ def @image.original_filename 'testing.txt' end doc = Asset.create(:image => @image) assert_equal 'testing.txt', doc.image_name + end + end + + context "Validating attachment presence" do + setup do + @model_class = Class.new do + include MongoMapper::Document + plugin Joint + attachment :file, :required => true + end + end + + should "work" do + model = @model_class.new + model.should_not be_valid + + model.file = @file + model.should be_valid + + model.file = nil + model.should_not be_valid + + model.file = @image + model.should be_valid end end end \ No newline at end of file