From e497fbbab47b1c0182e644a94011db3ba7621343 Mon Sep 17 00:00:00 2001 From: "Jonathan S. Katz" Date: Wed, 22 Oct 2008 01:41:21 -0400 Subject: [PATCH] Added basic specs for attachment CRUD --- lib/couchrest/core/model.rb | 2 +- spec/couchrest/core/model_spec.rb | 58 ++++++++++++++++++++++++++++++- spec/fixtures/attachments/README | 3 ++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 spec/fixtures/attachments/README diff --git a/lib/couchrest/core/model.rb b/lib/couchrest/core/model.rb index 6aaca0c..7221d6d 100644 --- a/lib/couchrest/core/model.rb +++ b/lib/couchrest/core/model.rb @@ -588,7 +588,7 @@ module CouchRest def get_mime_type(file) MIME::Types.type_for(file.path).empty? ? - 'text\/plain' : MIME::Types.type_for(file.path).content_type.gsub(/\//,'\/') + 'text\/plain' : MIME::Types.type_for(file.path).first.content_type.gsub(/\//,'\/') end def set_attachment_attr(file, attachment_name) diff --git a/spec/couchrest/core/model_spec.rb b/spec/couchrest/core/model_spec.rb index e15de5a..2ac80c5 100644 --- a/spec/couchrest/core/model_spec.rb +++ b/spec/couchrest/core/model_spec.rb @@ -727,4 +727,60 @@ describe CouchRest::Model do lambda{Basic.get(@obj.id)}.should raise_error end end -end + + describe "creating an attachment" do + before(:each) do + @obj = Basic.new + @obj.save.should == true + @file_ext = File.open(FIXTURE_PATH + '/attachments/test.html') + @file_no_text = File.open(FIXTURE_PATH + '/attachments/README') + @attachment_name = 'my_attachment' + end + + it "should create an attachment from file with an extension" do + @obj.create_attachment(@file_ext, @attachment_name) + @obj.save.should == true + reloaded_obj = Basic.get(@obj.id) + reloaded_obj['_attachments'][@attachment_name].should_not be_nil + end + + it "should create an attachment from file without an extension" do + @obj.create_attachment(@file_no_text, @attachment_name) + @obj.save.should == true + reloaded_obj = Basic.get(@obj.id) + reloaded_obj['_attachments'][@attachment_name].should_not be_nil + end + end + + describe 'reading, updating, and deleting an attachment' do + before(:each) do + @obj = Basic.new + @file = File.open(FIXTURE_PATH + '/attachments/test.html') + @attachment_name = 'my_attachment' + @obj.create_attachment(@file, @attachment_name) + @obj.save.should == true + @file.rewind + end + + it 'should read an attachment that exists' do + @obj.read_attachment(@attachment_name).should == @file.read + end + + it 'should update an attachment that exists' do + file = File.open(FIXTURE_PATH + '/attachments/README') + @file.should_not == file + @obj.update_attachment(file, @attachment_name) + @obj.save + reloaded_obj = Basic.get(@obj.id) + file.rewind + reloaded_obj.read_attachment(@attachment_name).should_not == @file.read + reloaded_obj.read_attachment(@attachment_name).should == file.read + end + + it 'should delete an attachment that exists' do + @obj.delete_attachment(@attachment_name) + @obj.save + lambda{Basic.get(@obj.id).read_attachment(@attachment_name)}.should raise_error + end + end +end \ No newline at end of file diff --git a/spec/fixtures/attachments/README b/spec/fixtures/attachments/README new file mode 100644 index 0000000..931b370 --- /dev/null +++ b/spec/fixtures/attachments/README @@ -0,0 +1,3 @@ +This is an example README file. + +More of the README, whee. \ No newline at end of file