OSDN Git Service

merge v04
[pettanr/pettanr.git] / spec / models / panel_spec.rb
index 7ae0330..12e8205 100644 (file)
@@ -21,26 +21,16 @@ describe Panel do
   \r
   describe '検証に於いて' do\r
     before do\r
-      @comic = Factory :comic, :author_id => @author.id\r
     end\r
     \r
     it 'オーソドックスなデータなら通る' do\r
-      @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
+      @panel = Factory.build :panel, :author_id => @author.id\r
       @panel.should be_valid\r
     end\r
     \r
-    context 'comic_idを検証するとき' do\r
-      before do\r
-        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => nil\r
-      end\r
-      it 'テストデータの確認' do\r
-        @panel.comic_id = @comic.id\r
-        @panel.should be_valid\r
-      end\r
-    end\r
     context 'widthを検証するとき' do\r
       before do\r
-        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
       end\r
       it 'テストデータの確認' do\r
         @panel.width = 1\r
@@ -65,7 +55,7 @@ describe Panel do
     end\r
     context 'heightを検証するとき' do\r
       before do\r
-        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
       end\r
       it 'テストデータの確認' do\r
         @panel.height = '1'\r
@@ -90,7 +80,7 @@ describe Panel do
     end\r
     context 'borderを検証するとき' do\r
       before do\r
-        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
       end\r
       it 'テストデータの確認' do\r
         @panel.border = '1'\r
@@ -115,7 +105,7 @@ describe Panel do
     end\r
     context 'xを検証するとき' do\r
       before do\r
-        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
       end\r
       it 'テストデータの確認' do\r
         @panel.x = '1'\r
@@ -136,7 +126,7 @@ describe Panel do
     end\r
     context 'yを検証するとき' do\r
       before do\r
-        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
       end\r
       it 'テストデータの確認' do\r
         @panel.y = '1'\r
@@ -157,7 +147,7 @@ describe Panel do
     end\r
     context 'zを検証するとき' do\r
       before do\r
-        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
       end\r
       it 'テストデータの確認' do\r
         @panel.z = '1'\r
@@ -176,30 +166,9 @@ describe Panel do
         @panel.should_not be_valid\r
       end\r
     end\r
-    context 'tを検証するとき' do\r
-      before do\r
-        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
-      end\r
-      it 'テストデータの確認' do\r
-        @panel.t = '1'\r
-        @panel.should be_valid\r
-      end\r
-      it '数値でなければ失敗する' do\r
-        @panel.t = 'a'\r
-        @panel.should_not be_valid\r
-      end\r
-      it '0なら通る' do\r
-        @panel.t = '0'\r
-        @panel.should be_valid\r
-      end\r
-      it '負でも失敗する' do\r
-        @panel.t = -1\r
-        @panel.should_not be_valid\r
-      end\r
-    end\r
     context 'author_idを検証するとき' do\r
       before do\r
-        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
       end\r
       it 'テストデータの確認' do\r
         @panel.author_id = @author.id\r
@@ -220,97 +189,30 @@ describe Panel do
     end\r
     context '全体を検証するとき' do\r
       before do\r
-        @panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id\r
+        @panel = Factory :panel, :author_id => @author.id\r
       end\r
     end\r
   end\r
   \r
-  describe 'ã\83\87ã\83¼ã\82¿補充に於いて' do\r
+  describe 'ã\83\87ã\83\95ã\82©ã\83«ã\83\88å\80¤補充に於いて' do\r
     before do\r
-      @comic = Factory :comic, :author_id => @author.id\r
-      @panel = Factory.build :panel, :comic_id => @comic.id\r
-    end\r
-    context 'widthを補充' do\r
-      it '空の時はコミックから補充する' do\r
-        @panel.width = nil\r
-        @panel.supply_default @author\r
-        @panel.width.should eq @comic.width\r
-      end\r
-      it '空の時でもコミックが不在なら補充しない' do\r
-        @panel.width = nil\r
-        @panel.comic_id = nil\r
-        @panel.supply_default @author\r
-        @panel.width.should be_nil\r
-      end\r
-      it '空でない時は変化しない' do\r
-        @panel.width = 45\r
-        lambda {\r
-          @panel.supply_default @author\r
-        }.should_not change @panel, :width\r
-      end\r
-    end\r
-    context 'heightを補充' do\r
-      it '空の時はコミックから補充する' do\r
-        @panel.height = nil\r
-        @panel.supply_default @author\r
-        @panel.height.should eq @comic.height\r
-      end\r
-      it '空の時でもコミックが不在なら補充しない' do\r
-        @panel.height = nil\r
-        @panel.comic_id = nil\r
-        @panel.supply_default @author\r
-        @panel.height.should be_nil\r
-      end\r
-      it '空でない時は変化しない' do\r
-        @panel.height = 87\r
-        lambda {\r
-          @panel.supply_default @author\r
-        }.should_not change @panel, :height\r
-      end\r
+      @panel = Factory.build :panel, :author_id => @author.id\r
     end\r
-    context 'borderを補充' do\r
-      it '空の時は0を補充する' do\r
-        @panel.border = nil\r
-        @panel.supply_default @author\r
-        @panel.border.should eq 0\r
-      end\r
-      it '空でない時は変化しない' do\r
-        @panel.border = 1\r
-        lambda {\r
-          @panel.supply_default @author\r
-        }.should_not change @panel, :border\r
-      end\r
+    it 'borderは2を補充する' do\r
+      @panel.border = nil\r
+      @panel.supply_default\r
+      @panel.border.should eq 2\r
     end\r
-    context 'tを補充' do\r
-      it '空の時はコミック内のtの最大値+1を補充する' do\r
-        pl = Factory :panel, :author_id => @author.id, :comic_id => @comic.id, :t => 0\r
-        @panel.t = nil\r
-        @panel.supply_default @author\r
-        @panel.t.should eq 1\r
-      end\r
-      it '空でコミック初のコマなら0を補充する' do\r
-        @panel.t = nil\r
-        @panel.supply_default @author\r
-        @panel.t.should eq 0\r
-      end\r
-      it '空の時でも更新ケースなら補充しない' do\r
-        pl = Factory :panel, :author_id => @author.id, :comic_id => @comic.id, :t => 1\r
-        pl.t = nil\r
-        lambda {\r
-          pl.supply_default @author\r
-        }.should_not change pl, :t\r
-      end\r
-      it '空でない時は変化しない' do\r
-        @panel.t = 1\r
-        lambda {\r
-          @panel.supply_default @author\r
-        }.should_not change @panel, :t\r
-      end\r
+  end\r
+  \r
+  describe '上書き補充に於いて' do\r
+    before do\r
+      @panel = Factory.build :panel, :author_id => @author.id\r
     end\r
     context 'author_idを補充' do\r
       it 'ログイン中の作家idを補充する' do\r
         @panel.author_id = nil\r
-        @panel.supply_default @author\r
+        @panel.overwrite @author\r
         @panel.author_id.should eq @author.id\r
       end\r
     end\r
@@ -319,53 +221,54 @@ describe Panel do
   \r
   describe '作者判定に於いて' do\r
     before do\r
-      @comic = Factory :comic, :author_id => @author.id\r
     end\r
     it '自作のコマならyes' do\r
-      panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id\r
+      panel = Factory :panel, :author_id => @author.id\r
       panel.own?(@author).should == true\r
     end\r
     it '他人のコマならno' do\r
-      panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id\r
-      panel.own?(@author).should == false\r
+      panel = Factory :panel, :author_id => @author.id\r
+      panel.own?(@other_author).should == false\r
     end\r
     it '作家が不明ならno' do\r
-      panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id\r
+      panel = Factory :panel, :author_id => @author.id\r
       panel.own?(nil).should == false\r
     end\r
   end\r
   describe '閲覧許可に於いて' do\r
     before do\r
-      @comic = Factory :comic, :author_id => @author.id\r
     end\r
-    it '自作の公開コミックのコマを見るときは許可する' do\r
-      Comic.any_instance.stub(:visible?).and_return(true)\r
-      panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id\r
+    it '自作のコマを見るときは許可する' do\r
+      Panel.any_instance.stub(:own?).and_return(true)\r
+      Panel.any_instance.stub(:publish?).and_return(true)\r
+      panel = Factory :panel, :author_id => @author.id\r
       panel.visible?(@author).should == true\r
     end\r
-    it '自作のコマは非公開コミックでも許可する' do\r
-      Comic.any_instance.stub(:visible?).and_return(false)\r
-      panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id\r
+    it '自作のコマは非公開でも許可する' do\r
+      Panel.any_instance.stub(:own?).and_return(true)\r
+      Panel.any_instance.stub(:publish?).and_return(false)\r
+      panel = Factory :panel, :author_id => @author.id\r
       panel.visible?(@author).should == true\r
     end\r
-    it '他人のコマでも公開コミックなら許可する' do\r
-      Comic.any_instance.stub(:visible?).and_return(true)\r
-      panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id\r
+    it '他人のコマでも公開なら許可する' do\r
+      Panel.any_instance.stub(:own?).and_return(false)\r
+      Panel.any_instance.stub(:publish?).and_return(true)\r
+      panel = Factory :panel, :author_id => @author.id\r
       panel.visible?(@author).should == true\r
     end\r
-    it '他人のコマで非公開コミックなら許可しない' do\r
-      Comic.any_instance.stub(:visible?).and_return(false)\r
-      panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id\r
+    it '他人のコマで非公開なら許可しない' do\r
+      Panel.any_instance.stub(:own?).and_return(false)\r
+      Panel.any_instance.stub(:publish?).and_return(false)\r
+      panel = Factory :panel, :author_id => @author.id\r
       panel.visible?(@author).should == false\r
     end\r
   end\r
   describe '単体取得に於いて' do\r
     before do\r
-      @comic = Factory :comic, :author_id => @author.id\r
-      @panel = Factory :panel, :comic_id => @comic.id, :author_id => @author.id\r
+      @panel = Factory :panel, :author_id => @author.id\r
     end\r
     it '指定のコマを返す' do\r
-      Comic.any_instance.stub(:visible?).and_return(true)\r
+      Panel.any_instance.stub(:visible?).and_return(true)\r
       pl = Panel.show @panel.id, @author\r
       pl.should eq @panel\r
     end\r
@@ -385,15 +288,36 @@ describe Panel do
       end\r
     end\r
   end\r
+  describe '単体取得に於いて' do\r
+    before do\r
+      @panel = Factory :panel, :author_id => @author.id\r
+    end\r
+    it '指定のコマを返す' do\r
+      Panel.any_instance.stub(:own?).and_return(true)\r
+      pl = Panel.edit @panel.id, @author\r
+      pl.should eq @panel\r
+    end\r
+    context '他人のコマを開こうとしたとき' do\r
+      it '403Forbidden例外を返す' do\r
+        Panel.any_instance.stub(:own?).and_return(false)\r
+        lambda{\r
+          Panel.edit @panel.id, @author\r
+        }.should raise_error(ActiveRecord::Forbidden)\r
+      end\r
+    end\r
+    context '存在しないコマを開こうとしたとき' do\r
+      it '404RecordNotFound例外を返す' do\r
+        lambda{\r
+          Panel.edit 110, @author\r
+        }.should raise_error(ActiveRecord::RecordNotFound)\r
+      end\r
+    end\r
+  end\r
   describe '関連テーブルプションに於いて' do\r
     context 'オプションがないとき' do\r
-      it '4つの項目を含んでいる' do\r
-        r = Panel.show_include_opt\r
-        r.should have(4).items\r
-      end\r
-      it 'コミックを含んでいる' do\r
+      it '3つの項目を含んでいる' do\r
         r = Panel.show_include_opt\r
-        r.has_key?(:comic).should be_true\r
+        r.should have(3).items\r
       end\r
       it 'コマ絵を含んでいる' do\r
         r = Panel.show_include_opt\r
@@ -429,9 +353,9 @@ describe Panel do
       end\r
     end\r
     context 'オプションで原画を含ませたとき' do\r
-      it '5つの項目を含んでいる' do\r
+      it '4つの項目を含んでいる' do\r
         r = Panel.show_include_opt(:include => {:test => {}})\r
-        r.should have(5).items\r
+        r.should have(4).items\r
       end\r
       it 'testを含んでいる' do\r
         r = Panel.show_include_opt(:include => {:test => {}})\r
@@ -444,13 +368,9 @@ describe Panel do
       r = Panel.show_json_include_opt\r
       r.has_key?(:include).should be_true\r
     end\r
-    it '4つの項目を含んでいる' do\r
-      r = Panel.show_json_include_opt[:include]\r
-      r.should have(4).items\r
-    end\r
-    it 'コミックを含んでいる' do\r
+    it '3つの項目を含んでいる' do\r
       r = Panel.show_json_include_opt[:include]\r
-      r.has_key?(:comic).should be_true\r
+      r.should have(3).items\r
     end\r
     it 'コマ絵を含んでいる' do\r
       r = Panel.show_json_include_opt[:include]\r
@@ -487,8 +407,7 @@ describe Panel do
   end\r
   describe '一覧取得に於いて' do\r
     before do\r
-      @comic = Factory :comic, :author_id => @author.id\r
-      @panel = Factory :panel, :comic_id => @comic.id, :author_id => @author.id\r
+      @panel = Factory :panel, :author_id => @author.id\r
     end\r
     context 'page補正について' do\r
       it '文字列から数値に変換される' do\r
@@ -520,16 +439,21 @@ describe Panel do
       pl.should eq [@panel]\r
     end\r
     it '時系列で並んでいる' do\r
-      npl = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 1, :updated_at => Time.now + 100\r
+      npl = Factory :panel, :author_id => @author.id, :updated_at => Time.now + 100\r
       pl = Panel.list\r
       pl.should eq [npl, @panel]\r
     end\r
+    it '非公開のコマは含まない' do\r
+      npl = Factory :panel, :author_id => @author.id, :publish => 0\r
+      pl = Panel.list\r
+      pl.should eq [@panel]\r
+    end\r
     context 'DBに5件あって1ページの件数を2件に変えたとして' do\r
       before do\r
-        @npl2 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 1, :updated_at => Time.now + 100\r
-        @npl3 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 2, :updated_at => Time.now + 200\r
-        @npl4 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 3, :updated_at => Time.now + 300\r
-        @npl5 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 4, :updated_at => Time.now + 400\r
+        @npl2 = Factory :panel, :author_id => @author.id, :updated_at => Time.now + 100\r
+        @npl3 = Factory :panel, :author_id => @author.id, :updated_at => Time.now + 200\r
+        @npl4 = Factory :panel, :author_id => @author.id, :updated_at => Time.now + 300\r
+        @npl5 = Factory :panel, :author_id => @author.id, :updated_at => Time.now + 400\r
         Panel.stub(:default_page_size).and_return(2)\r
       end\r
       it '通常は2件を返す' do\r
@@ -556,13 +480,9 @@ describe Panel do
       r = Panel.list_opt\r
       r.has_key?(:include).should be_true\r
     end\r
-    it '4つの項目を含んでいる' do\r
-      r = Panel.list_opt[:include]\r
-      r.should have(4).items\r
-    end\r
-    it 'コミックを含んでいる' do\r
+    it '3つの項目を含んでいる' do\r
       r = Panel.list_opt[:include]\r
-      r.has_key?(:comic).should be_true\r
+      r.should have(3).items\r
     end\r
     it 'コマ絵を含んでいる' do\r
       r = Panel.list_opt[:include]\r
@@ -602,13 +522,9 @@ describe Panel do
       r = Panel.list_json_opt\r
       r.has_key?(:include).should be_true\r
     end\r
-    it '4つの項目を含んでいる' do\r
+    it '3つの項目を含んでいる' do\r
       r = Panel.list_json_opt[:include]\r
-      r.should have(4).items\r
-    end\r
-    it 'コミックを含んでいる' do\r
-      r = Panel.list_json_opt[:include]\r
-      r.has_key?(:comic).should be_true\r
+      r.should have(3).items\r
     end\r
     it 'コマ絵を含んでいる' do\r
       r = Panel.list_json_opt[:include]\r
@@ -968,4 +884,117 @@ describe Panel do
       end\r
     end\r
   end\r
+  describe '複合チェックに於いて' do\r
+    context 'つつがなく終わるとき' do\r
+      it 'している' do\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
+        @panel.panel_pictures.build(\r
+          Factory.attributes_for(:panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id, :t => 0)\r
+        )\r
+        @panel.panel_pictures.build(\r
+          Factory.attributes_for(:panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id, :t => 1)\r
+        )\r
+        sb1 = @panel.speech_balloons.build(\r
+          Factory.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 2)\r
+        )\r
+        sb1.balloons.build(\r
+          Factory.attributes_for(:balloon, :speech_balloon_id => sb1.id)\r
+        )\r
+        sb1.speeches.build(\r
+          Factory.attributes_for(:speech, :speech_balloon_id => sb1.id)\r
+        )\r
+        sb2 = @panel.speech_balloons.build(\r
+          Factory.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 3)\r
+        )\r
+        sb2.balloons.build(\r
+          Factory.attributes_for(:balloon, :speech_balloon_id => sb2.id)\r
+        )\r
+        sb2.speeches.build(\r
+          Factory.attributes_for(:speech, :speech_balloon_id => sb2.id)\r
+        )\r
+        r = @panel.validate_child\r
+        r.should be_true\r
+      end\r
+    end\r
+  end\r
+  describe '保存に於いて' do\r
+    context 'つつがなく終わるとき' do\r
+      it 'idチェックを依頼している' do\r
+        Panel.should_receive(:validate_elements_id).with(any_args).exactly(1)\r
+        Panel.stub(:validate_elements_id).with(any_args).and_return(true)\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
+        r = @panel.store\r
+      end\r
+      it '保存を依頼している' do\r
+        Panel.stub(:validate_elements_id).with(any_args).and_return(true)\r
+        Panel.any_instance.should_receive(:save).with(any_args).exactly(1)\r
+        Panel.any_instance.stub(:save).with(any_args).and_return(true)\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
+        r = @panel.store\r
+      end\r
+      it 'Trueを返す' do\r
+        Panel.stub(:validate_elements_id).with(any_args).and_return(true)\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
+        r = @panel.store\r
+        r.should be_true\r
+      end\r
+      it 'エラーメッセージがセットされていない' do\r
+        Panel.stub(:validate_elements_id).with(any_args).and_return(true)\r
+        @panel = Factory :panel, :author_id => @author.id\r
+        r = @panel.store\r
+        @panel.errors.empty?.should be_true\r
+      end\r
+      context '新規のとき' do\r
+        it 'Trueを返す' do\r
+          Panel.stub(:validate_elements_id).with(any_args).and_return(true)\r
+          @panel = Factory.build :panel, :author_id => @author.id\r
+          r = @panel.store\r
+          r.should be_true\r
+        end\r
+        it '行が追加されている' do\r
+          Panel.stub(:validate_elements_id).with(any_args).and_return(true)\r
+          @panel = Factory.build :panel, :author_id => @author.id\r
+          lambda {\r
+            r = @panel.store\r
+          }.should change(Panel, :count)\r
+        end\r
+      end\r
+      context '更新のとき' do\r
+        it 'Trueを返す' do\r
+          Panel.stub(:validate_elements_id).with(any_args).and_return(true)\r
+          @panel = Factory :panel, :author_id => @author.id\r
+          r = @panel.store\r
+          r.should be_true\r
+        end\r
+        it '行が追加されていない' do\r
+          Panel.stub(:validate_elements_id).with(any_args).and_return(true)\r
+          @panel = Factory :panel, :author_id => @author.id\r
+          lambda {\r
+            r = @panel.store\r
+          }.should_not change(Panel, :count)\r
+        end\r
+      end\r
+    end\r
+    context 'idチェックが失敗するとき' do\r
+      it 'Falseを返す' do\r
+        Panel.stub(:validate_elements_id).with(any_args).and_return(false)\r
+        @panel = Factory :panel, :author_id => @author.id\r
+        r = @panel.store\r
+        r.should be_false\r
+      end\r
+      it 'エラーメッセージがセットされている' do\r
+        Panel.stub(:validate_elements_id).with(any_args).and_return(false)\r
+        @panel = Factory :panel, :author_id => @author.id\r
+        r = @panel.store\r
+        @panel.errors.empty?.should be_false\r
+      end\r
+      it '行が追加されていない' do\r
+        Panel.stub(:validate_elements_id).with(any_args).and_return(false)\r
+        @panel = Factory.build :panel, :author_id => @author.id\r
+        lambda {\r
+          r = @panel.store\r
+        }.should_not change(Panel, :count)\r
+      end\r
+    end\r
+  end\r
 end\r