OSDN Git Service

t30350#:fix destroy
[pettanr/pettanr.git] / spec / models / resource_picture_spec.rb
index f614348..2dd12d7 100644 (file)
@@ -968,6 +968,91 @@ describe ResourcePicture do
     \r
   end\r
   \r
+  describe '公開停止に於いて' do\r
+    before do\r
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id\r
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id\r
+    end\r
+    context '事前チェックしておく' do\r
+      before do\r
+        ResourcePicture.any_instance.stub(:destroy).and_return(true)\r
+        PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_return(true)\r
+        PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_return(true)\r
+      end\r
+      it '素材モデルに削除を依頼している' do\r
+        ResourcePicture.any_instance.should_receive(:destroy).exactly(1)\r
+        r = @rp.unpublish\r
+      end\r
+      it '保管庫にサムネイルの画像データ削除を依頼している' do\r
+        PictureIO.resource_picture_io.should_receive(:delete).with(@rp.filename).exactly(1)\r
+        r = @rp.unpublish\r
+      end\r
+      it '保管庫にフルサイズの画像データ削除を依頼している' do\r
+        PictureIO.resource_picture_io.should_receive(:delete).with(@rp.filename, 'full').exactly(1)\r
+        r = @rp.unpublish\r
+      end\r
+    end\r
+    context 'つつがなく終わるとき' do\r
+      it '自身を削除する' do\r
+        lambda {\r
+          r = @rp.unpublish\r
+        }.should change(ResourcePicture, :count).by(-1)\r
+        lambda {\r
+          r = ResourcePicture.find @rp.id\r
+        }.should raise_error\r
+      end\r
+      it 'Trueを返す' do\r
+        r = @rp.unpublish\r
+        r.should be_true\r
+      end\r
+    end\r
+    context '自身の削除に失敗したとき' do\r
+      before do\r
+        ResourcePicture.any_instance.stub(:destroy).with(any_args).and_return(false)\r
+      end\r
+      it 'Falseを返す' do\r
+        r = @rp.unpublish\r
+        r.should be_false\r
+      end\r
+      it 'ロールバックしている' do\r
+        lambda {\r
+          r = @rp.unpublish\r
+        }.should_not change(ResourcePicture, :count)\r
+      end\r
+    end\r
+    context 'サムネイル画像の削除に失敗したとき' do\r
+      before do\r
+        PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_raise(PictureIO::Error)\r
+        PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_return(true)\r
+      end\r
+      it 'Falseを返す' do\r
+        r = @rp.unpublish\r
+        r.should be_false\r
+      end\r
+      it 'ロールバックしている' do\r
+        lambda {\r
+          r = @rp.unpublish\r
+        }.should_not change(ResourcePicture, :count)\r
+      end\r
+    end\r
+    context 'フルサイズ画像の削除に失敗したとき' do\r
+      before do\r
+        PictureIO.resource_picture_io.stub(:delete).with(@rp.filename).and_return(true)\r
+        PictureIO.resource_picture_io.stub(:delete).with(@rp.filename, 'full').and_raise(PictureIO::Error)\r
+      end\r
+      it 'Falseを返す' do\r
+        r = @rp.unpublish\r
+        r.should be_false\r
+      end\r
+      it 'ロールバックしている' do\r
+        lambda {\r
+          r = @rp.unpublish\r
+        }.should_not change(ResourcePicture, :count)\r
+      end\r
+    end\r
+  end\r
+  \r
   describe 'クレジットデータに於いて' do\r
     before do\r
       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id\r