OSDN Git Service

passed. original picture model test
[pettanr/pettanr.git] / spec / models / original_picture_spec.rb
index 3e8505b..5bdc074 100644 (file)
@@ -10,6 +10,7 @@ describe OriginalPicture do
     @other_user = Factory( :user_yas)
     @other_author = @other_user.author
     @other_artist = Factory :artist_yas, :author_id => @other_author.id
+    @license = Factory :license
   end
   
   describe '検証に於いて' do
@@ -30,6 +31,10 @@ describe OriginalPicture do
         @op = Factory.build :original_picture, :artist_id => @artist.id, :ext => 'a'*5
         @op.should_not be_valid
       end
+      it 'png,gif,jpeg以外なら失敗する' do
+        @op = Factory.build :original_picture, :artist_id => @artist.id, :ext => 'bmp'
+        @op.should_not be_valid
+      end
     end
     context 'widthを検証するとき' do
       it 'nullなら失敗する' do
@@ -89,7 +94,7 @@ describe OriginalPicture do
         @op.should_not be_valid
       end
       it '2MB以上なら失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => 2000000
+        @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => 2000000+1
         @op.should_not be_valid
       end
     end
@@ -127,21 +132,6 @@ describe OriginalPicture do
     before do
     end
     
-    context '初期値を補充するとき' do
-      it '空なら0が補充される' do
-        @comic.supply_default
-        @comic.editable.should == 0
-        @comic.visible.should == 0
-      end
-      it 'editableが空でないなら変化なし' do
-        @comic.editable = 1
-        lambda{@comic.supply_default}.should_not change(@comic, :editable)
-      end
-      it 'visibleが空でないなら変化なし' do
-        @comic.visible = 1
-        lambda{@comic.supply_default}.should_not change(@comic, :visible)
-      end
-    end
   end
   
   describe '作者判定に於いて' do
@@ -152,17 +142,17 @@ describe OriginalPicture do
       op.own?(@author).should == true
     end
     it '他人のならno' do
-      op = Factory :original_picture, :artist_id => @other_artist. id
-      op.own?(@author).should_not == true
+      op = Factory :original_picture, :artist_id => @other_artist.id
+      op.own?(@author).should == false
     end
     it '作家が不明ならno' do
       op = Factory :original_picture, :artist_id => @artist.id
-      op.own?(nil).should_not == true
+      op.own?(nil).should == false
     end
     it '作家が絵師でないならno' do
       other_user = Factory( :user_yas)
       op = Factory :original_picture, :artist_id => @artist.id
-      op.own?(other_user).should_not == true
+      op.own?(@other_author).should == false
     end
   end
   describe '単体取得に於いて' do
@@ -170,7 +160,7 @@ describe OriginalPicture do
       @op = Factory :original_picture, :artist_id => @artist.id
     end
     it '指定のコミックを返す' do
-      pic = OriginalPicture.show @op.id
+      pic = OriginalPicture.show @op.id, @author
       pic.should eq @op
     end
     context '関連テーブルオプションがないとき' do
@@ -216,17 +206,17 @@ describe OriginalPicture do
       end
     end
     it 'リストを返す' do
-      pic = OriginalPicture.list
+      pic = OriginalPicture.list @artist.id
       pic.should eq [@op]
     end
     it '他人の原画は含んでいない' do
       Factory :original_picture, :artist_id => @other_artist.id
-      pic = OriginalPicture.list
+      pic = OriginalPicture.list @artist.id
       pic.should eq [@op]
     end
     it '時系列で並んでいる' do
       newpic = Factory :original_picture, :artist_id => @artist.id
-      pic = OriginalPicture.list
+      pic = OriginalPicture.list @artist.id
       pic.should eq [newpic, @op]
     end
     context 'DBに5件あって1ページの件数を2件に変えたとして' do
@@ -238,39 +228,190 @@ describe OriginalPicture do
         OriginalPicture.stub(:default_page_size).and_return(2)
       end
       it '通常は2件を返す' do
-        pic = OriginalPicture.list
+        pic = OriginalPicture.list @artist.id
         pic.should have(2).items 
       end
       it 'page=1なら末尾2件を返す' do
         #時系列で並んでいる
-        pic = OriginalPicture.list({}, 1)
+        pic = OriginalPicture.list( @artist.id, {}, 1)
         pic.should eq [@op5, @op4]
       end
       it 'page=2なら中間2件を返す' do
-        pic = OriginalPicture.list({}, 2)
+        pic = OriginalPicture.list( @artist.id, {}, 2)
         pic.should eq [@op3, @op2]
       end
       it 'page=3なら先頭1件を返す' do
-        pic = OriginalPicture.list({}, 3)
+        pic = OriginalPicture.list( @artist.id, {}, 3)
         pic.should eq [@op]
       end
     end
   end
-  describe '画像データ変換に於いて' do
+  describe 'RMagick変換に於いて' do
     before do
-      @op = Factory :original_picture, :artist_id => @artist.id
+      @op = Factory.build :original_picture, :artist_id => @artist.id
     end
     context 'つつがなく終わるとき' do
       it '画像データをオブジェクト化している' do
+        Magick::Image.stub(:from_blob).and_return(['mgkobj'])
+        @op.data_to_mgk('mgkbin').should eq 'mgkobj'
+      end
+    end
+    context '失敗するとき' do
+      it 'Falseを返す' do
+        Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
+        @op.data_to_mgk('mgkbin').should be_false
+      end
+      it '全体エラーがセットされている' do
+        Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
+        lambda {
+          @op.data_to_mgk('mgkbin')
+        }.should change(@op.errors[:base], :count)
       end
     end
   end
-  describe '新規作成に於いて' do
+  describe '作成・更新に於いて' do
     before do
-      @op = Factory :original_picture, :artist_id => @artist.id
+      @op = Factory.build :original_picture, :artist_id => @artist.id
+      class Mgk ; class Image ; end ; end
+      @filesize = 76543
+      Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
+      Mgk.any_instance.stub(:format).with(any_args).and_return('png')
+      Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
+      Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
+      Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
+      Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
+      PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
+      
+      ResourcePicture.stub(:update_picture).with(any_args).and_return(
+        Factory.build :resource_picture, :artist_id => @artist.id
+      )
+    end
+    context '事前チェック' do
+      before do
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
+        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
+        ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
+      end
+      it 'RMagick変換を依頼している' do
+        OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
+        @op.store 'bindata', @artist, @license.id\r
+      end
+      it '自身に属性をセットしている' do
+        OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
+        @op.store 'bindata', @artist, @license.id\r
+      end
+      it '自身が保存されている' do
+        OriginalPicture.any_instance.should_receive(:save).exactly(1)\r
+        @op.store 'bindata', @artist, @license.id\r
+      end
+      it 'PictureIoに画像データの保存を依頼している' do
+        PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
+        @op.store 'bindata', @artist, @license.id\r
+      end
+      it '素材モデルに対象素材を問い合わせている' do
+        ResourcePicture.should_receive(:update_picture).with(any_args).exactly(1)
+        @op.store 'bindata', @artist, @license.id\r
+      end
+      it '対象素材オブジェクトに保存を依頼している' do
+        ResourcePicture.any_instance.should_receive(:store).with(any_args).exactly(1)
+        @op.store 'bindata', @artist, @license.id\r
+      end
     end
     context 'つつがなく終わるとき' do
-      it '画像データをオブジェクト化している' do
+      before do
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+#        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
+        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
+        ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
+      end
+      it '自身に属性をセットしている' do
+        lambda {
+          @op.store 'bindata', @artist, @license.id\r
+        }.should change @op, :filesize
+      end
+      it '原画モデルが作成されている' do
+        lambda {
+          @op.store 'bindata', @artist, @license.id
+        }.should change OriginalPicture, :count
+      end
+      it '原画が保存されている' do
+        @op.store 'bindata', @artist, @license.id
+        OriginalPicture.find(@op).should_not be_nil\r
+      end
+      it 'Trueを返す' do
+        @op.store('bindata', @artist, @license.id).should eq true\r
+      end
+    end
+    
+    context 'RMagick変換が失敗したとき' do
+      before do
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
+      end
+      it 'falseを返す' do
+        @op.store('bindata', @artist, @license.id).should be_false\r
+      end
+      it '自身の保存は呼ばれていない' do
+        OriginalPicture.any_instance.should_not_receive(:save)\r
+      end
+    end
+    context '自身の保存に失敗したとき' do
+      before do
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)\r
+      end
+      it 'falseを返す' do
+        @op.store('bindata', @artist, @license.id).should be_false\r
+      end
+      it '更新されていない' do
+        @op.store('bindata', @artist, @license.id)
+        @op.should be_a_new OriginalPicture\r
+      end
+      it '原画の保存は呼ばれていない' do
+        PictureIO::LocalPicture.any_instance.should_not_receive(:put)\r
+      end
+    end
+    context '原画の保存に失敗したとき' do
+      before do
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
+        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(false)
+      end
+      it 'falseを返す' do
+        @op.store('bindata', @artist, @license.id).should be_false\r
+      end
+      it '更新されていない' do
+        @op.store('bindata', @artist, @license.id)\r
+        @op.should be_a_new OriginalPicture\r
+      end
+      it '対象素材オブジェクトの保存は呼ばれていない' do
+        ResourcePicture.any_instance.should_not_receive(:save)\r
+        @op.store('bindata', @artist, @license.id)\r
+      end
+      it '全体エラーメッセージがセットされている' do
+        lambda {
+          @op.store('bindata', @artist, @license.id)\r
+        }.should change(@op.errors[:base], :count)
+      end
+    end
+    context '対象素材オブジェクトの保存に失敗したとき' do
+      before do
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
+        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
+        ResourcePicture.any_instance.stub(:store).with(any_args).and_return(false)
+      end
+      it 'falseを返す' do
+        @op.store('bindata', @artist, @license.id).should be_false\r
+      end
+      it '更新されていない' do
+        @op.store('bindata', @artist, @license.id)\r
+        @op.should be_a_new OriginalPicture\r
+      end
+      it '全体エラーメッセージがセットされている' do
+        lambda {
+          @op.store('bindata', @artist, @license.id)\r
+        }.should change(@op.errors[:base], :count)
       end
     end
   end