OSDN Git Service

fix peta spec
[pettanr/pettanr.git] / spec / peta / root_spec.rb
index 34354e4..6a974b6 100644 (file)
@@ -13,26 +13,14 @@ describe Sheet do
   
   describe '生成メソッドに於いて' do
     before do
-      @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
-    end
-    
-    describe 'leaf_itemsに於いて' do
-      it 'ぶら下がるそれぞれのリーフを取得できる' do
-        define_leaf_items? @item, [ScrollPanel]
-      end
-    end
-    
-    describe 'leafs_itemsに於いて' do
-      it 'すべてのリーフを取得できる' do
-        define_leafs_items? @item, [ScrollPanel]
-      end
+      @item =  FactoryGirl.create :sheet_install_a_with_sheet_panels, author: @author_yas
     end
     
   end
   
-  describe 'boostに於いて' do
+  describe 'copy_attributesに於いて' do
     before do
-      @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
+      @item =  FactoryGirl.create :sheet_install_a_with_sheet_panels, author: @author_yas
     end
     
     it 'ブーストメッセージをリーフに伝搬する' do
@@ -52,29 +40,493 @@ describe Panel do
   
   describe '生成メソッドに於いて' do
     before do
+      @item =  FactoryGirl.create :panel_hello_with_scroll_panels, author: @author_yas
     end
     
-    describe 'leaf_itemsに於いて' do
-      it 'ぶら下がるそれぞれのリーフを取得できる' do
-        define_leaf_items? @item, [ComicStory]
+  end
+  
+end
+# 
+describe ScrollPanel do
+  include LeafMacros
+  before do
+    @admin =FactoryGirl.create :admin
+    @user_yas = FactoryGirl.create :user_yas_with_owner
+    @user_rom = FactoryGirl.create :user_rom
+    @author_yas =  @user_yas.author
+    
+    @model = ScrollPanel
+  end
+  
+  describe 'new_tに於いて' do
+    before do
+      @scroll =  FactoryGirl.create :scroll_hello, author: @author_yas
+    end
+    
+    context 'オーソドックスなデータのとき' do
+      it 'cut' do
+        expect(@model.new_t(@scroll.id)).to eq 0
       end
+      
     end
     
-    describe 'leafs_itemsに於いて' do
-      it 'すべてのリーフを取得できる' do
-        define_leafs_items? @item, [ComicStory]
+    context 'オーソドックスなデータのとき' do
+      it 'cut' do
+        @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
+        expect(@model.new_t(@scroll.id)).to eq 1
+      end
+      
+      it 'cut' do
+        @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
+        @item =  FactoryGirl.create :scroll_panel_plain, z: 2, t: 1, scroll: @scroll, panel: @panel
+        @item =  FactoryGirl.create :scroll_panel_plain, z: 3, t: 2, scroll: @scroll, panel: @panel
+        expect(@model.new_t(@scroll.id)).to eq 3
       end
+      
     end
     
   end
   
-  describe 'boostに於いて' do
+  describe 'collect_tに於いて' do
     before do
-      @item =  FactoryGirl.create :scroll_hello_with_scroll_panels, author: @author_yas
+      @dummy =  FactoryGirl.create :scroll_great_with_scroll_panels, author: @author_yas
+      @scroll =  FactoryGirl.create :scroll_hello, author: @author_yas
     end
     
-    it 'ブーストメッセージをリーフに伝搬する' do
+    context 'オーソドックスなデータのとき' do
+      it 'cut' do
+        expect(@model.collect_t(@scroll.id)).to be_empty
+      end
+      
     end
+    
+    context 'オーソドックスなデータのとき' do
+      it 'cut' do
+        @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
+        expect(@model.collect_t(@scroll.id)).to eq [0]
+      end
+      
+      it 'cut' do
+        @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
+        @item =  FactoryGirl.create :scroll_panel_plain, z: 2, t: 1, scroll: @scroll, panel: @panel
+        @item =  FactoryGirl.create :scroll_panel_plain, z: 3, t: 2, scroll: @scroll, panel: @panel
+        expect(@model.collect_t(@scroll.id)).to eq [0, 1, 2]
+      end
+      
+    end
+    
   end
   
+  describe 'tチェックに於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+      @panel = FactoryGirl.create :panel, :author_id => @author.id
+      @item = FactoryGirl.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+    end
+    context 'つつがなく終わるとき' do
+      it 't収集を依頼している' do
+        SheetPanel.should_receive(:collect_t).with(any_args).exactly(1)
+        SheetPanel.stub(:collect_t).with(any_args).and_return([])
+        SheetPanel.stub(:serial?).with(any_args).and_return(true)
+        r = SheetPanel.validate_t @item
+      end
+      it '収集したtをシリアライズチェック依頼している' do
+        SheetPanel.stub(:collect_t).with(any_args).and_return([])
+        SheetPanel.should_receive(:serial?).with(any_args).exactly(1)
+        SheetPanel.stub(:serial?).with(any_args).and_return(true)
+        r = SheetPanel.validate_t @item
+      end
+    end
+    #実データでチェック
+    #依頼チェックだけでは不安なので最低限のチェックを
+    context '新規のとき' do
+      it '一件だけで正常通過している' do
+        @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
+        r = SheetPanel.validate_t @item
+        r.should be_true 
+      end
+    end
+    context '既存のとき' do
+      it '2件目を作っても正常通過している' do
+        @item = FactoryGirl.create :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
+        @item2 = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
+        r = SheetPanel.validate_t @item2
+        r.should be_true 
+      end
+    end
+  end
+  describe '保存に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+      @panel = FactoryGirl.create :panel, :author_id => @author.id
+      @item = FactoryGirl.build :sheet_panel, :t => nil, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+    end
+    context 'つつがなく終わるとき' do
+      it '編集許可チェックを依頼している' do
+        SheetPanel.any_instance.stub(:allow?).with(any_args).and_return(true)
+        SheetPanel.any_instance.should_receive(:allow?).with(any_args).exactly(1)
+        r = @item.store
+      end
+      it '順序入れ替えを依頼している' do
+        SheetPanel.any_instance.stub(:rotate).with(any_args).and_return(0)
+        SheetPanel.any_instance.should_receive(:rotate).with(any_args).exactly(1)
+        SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
+        SheetPanel.stub(:validate_t).with(any_args).and_return(true)
+        r = @item.store 
+      end
+      it '保存を依頼している' do
+        SheetPanel.stub(:new_t).with(any_args).and_return(0)
+        SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
+        SheetPanel.any_instance.should_receive(:save).with(any_args).exactly(1)
+        SheetPanel.stub(:validate_t).with(any_args).and_return(true)
+        r = @item.store
+      end
+      it 'tのシリアライズチェックを依頼している' do
+        SheetPanel.stub(:new_t).with(any_args).and_return(0)
+        SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
+        SheetPanel.stub(:validate_t).with(any_args).and_return(true)
+        SheetPanel.should_receive(:validate_t).with(any_args).exactly(1)
+        r = @item.store
+      end
+    end
+    #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
+    context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したとき' do
+      before do
+        @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
+        @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
+        @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+      end
+      it '既存のt0には変化がない' do
+        @item6.store
+        @item.reload
+        @item.t.should eq 0
+      end
+      it '既存のt1には変化がない' do
+        @item6.store
+        @item2.reload
+        @item2.t.should eq 1
+      end
+      it '既存のt2を3にシフトしている' do
+        @item6.store
+        @item3.reload
+        @item3.t.should eq 3
+      end
+      it '既存のt3を4にシフトしている' do
+        @item6.store
+        @item4.reload
+        @item4.t.should eq 4
+      end
+      it '既存のt5を5にシフトしている' do
+        @item6.store
+        @item5.reload
+        @item5.t.should eq 5
+      end
+      it '新規のt2が作成されている' do
+        @item6.store
+        @item6.reload
+        @item6.t.should eq 2
+      end
+      it '他の用紙に影響がない' do
+        @ot = @itemc2.t
+        @item6.store
+        @itemc2.reload
+        @itemc2.t.should eq @ot
+      end
+    end
+    context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したとき' do
+      before do
+        @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
+        @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
+        @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @ot = @item4.t
+        @item4.t = 1
+      end
+      it '既存のt0には変化がない' do
+        @item4.store @ot
+        @item.reload
+        @item.t.should eq 0
+      end
+      it '既存のt4には変化がない' do
+        @item4.store @ot
+        @item5.reload
+        @item5.t.should eq 4
+      end
+      it '既存のt1を2にシフトしている' do
+        @item4.store @ot
+        @item2.reload
+        @item2.t.should eq 2
+      end
+      it '既存のt2を3にシフトしている' do
+        @item4.store @ot
+        @item3.reload
+        @item3.t.should eq 3
+      end
+      it '既存のt3を1にシフトしている' do
+        @item4.store @ot
+        @item4.reload
+        @item4.t.should eq 1
+      end
+      it '他の用紙に影響がない' do
+        @item4.store @ot
+        @itemc2.reload
+        @itemc2.t.should eq 0
+      end
+    end
+    context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で1を3に移動したとき' do
+      before do
+        @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
+        @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
+        @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @ot = @item2.t
+        @item2.t = 3
+      end
+      it '既存のt0には変化がない' do
+        @item2.store @ot
+        @item.reload
+        @item.t.should eq 0
+      end
+      it '既存のt4には変化がない' do
+        @item2.store @ot
+        @item5.reload
+        @item5.t.should eq 4
+      end
+      it '既存のt1を3にシフトしている' do
+        @item2.store @ot
+        @item2.reload
+        @item2.t.should eq 3
+      end
+      it '既存のt2を1にシフトしている' do
+        @item2.store @ot
+        @item3.reload
+        @item3.t.should eq 1
+      end
+      it '既存のt3を2にシフトしている' do
+        @item2.store @ot
+        @item4.reload
+        @item4.t.should eq 2
+      end
+      it '他の用紙に影響がない' do
+        @item2.store @ot
+        @itemc2.reload
+        @itemc2.t.should eq 0
+      end
+    end
+    #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
+    context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したが保存に失敗したとき' do
+      before do
+        SheetPanel.any_instance.stub(:save).with(any_args).and_return(false)
+        @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
+        @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
+        @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+      end
+      it '既存のtに変化がない' do
+        @item6.store
+        @item.reload
+        @item.t.should eq 0
+        @item2.reload
+        @item2.t.should eq 1
+        @item3.reload
+        @item3.t.should eq 2
+        @item4.reload
+        @item4.t.should eq 3
+        @item5.reload
+        @item5.t.should eq 4
+        @itemc2.reload
+        @itemc2.t.should eq 0
+      end
+      it 'falseを返す' do
+        r = @item6.store
+        r.should be_false
+      end
+    end
+    context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したがシリアルチェックに失敗したとき' do
+      before do
+        SheetPanel.stub(:validate_t).with(any_args).and_return(false)
+        @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
+        @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
+        @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @ot = @item4.t
+        @item4.t = 1
+      end
+      it '既存のtに変化がない' do
+        @item4.store @ot
+        @item.reload
+        @item.t.should eq 0
+        @item2.reload
+        @item2.t.should eq 1
+        @item3.reload
+        @item3.t.should eq 2
+        @item4.reload
+        @item4.t.should eq 3
+        @item5.reload
+        @item5.t.should eq 4
+        @itemc2.reload
+        @itemc2.t.should eq 0
+      end
+      it 'falseを返す' do
+        r = @item4.store @ot
+        r.should be_false
+      end
+      it 'tにエラーメッセージが入っている' do
+        @item4.store @ot
+        @item4.errors[:t].should_not be_empty
+        @item4.valid?.should be_true
+      end
+    end
+    context '編集不可だったとき' do
+      before do
+        @item = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        SheetPanel.any_instance.stub(:allow?).and_return(false)
+      end
+      it '403Forbidden例外を返す' do
+        lambda{
+          @item.store
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+  end
+  describe '切り詰め処理つき削除に於いて' do
+    before do
+      @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+      @panel = FactoryGirl.create :panel, :author_id => @author.id
+      @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+    end
+    context 'つつがなく終わるとき' do
+      it '削除される' do
+        lambda{
+          @item.destroy_and_shorten
+        }.should change(SheetPanel, :count ).by(-1)
+      end
+      it 'Trueを返す' do
+        r = @item.destroy_and_shorten
+        r.should be_true 
+      end
+    end
+    context '削除に失敗したとき' do
+      before do
+        SheetPanel.any_instance.stub(:destroy).and_return(false)
+      end
+      it 'ロールバックされる' do
+        lambda{
+          @item.destroy_and_shorten
+        }.should_not change(SheetPanel, :count )
+      end
+      it 'Falseを返す' do
+        r = @item.destroy_and_shorten
+        r.should be_false
+      end
+    end
+    #連携テスト。切り詰めが直接DBをいじる
+    context '2件で先頭を削除したとき' do
+      before do
+        @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+      end
+      it '行が削除される' do
+        lambda{
+          @item.destroy_and_shorten
+        }.should change(SheetPanel, :count ).by(-1)
+      end
+      it '先頭は削除される' do
+        @item.destroy_and_shorten
+        lambda{
+          SheetPanel.find @item.id
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+      it '2件目は前に詰められる' do
+        @item.destroy_and_shorten
+        @item2.reload
+        @item2.t.should eq 0
+      end
+    end
+    context '3件で先頭を削除したとき' do
+      before do
+        @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+      end
+      it '行が削除される' do
+        lambda{
+          @item.destroy_and_shorten
+        }.should change(SheetPanel, :count ).by(-1)
+      end
+      it '先頭は削除される' do
+        @item.destroy_and_shorten
+        lambda{
+          SheetPanel.find @item.id
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+      it '2件目は前に詰められる' do
+        @item.destroy_and_shorten
+        @item2.reload
+        @item2.t.should eq 0
+      end
+      it '3件目は前に詰められる' do
+        @item.destroy_and_shorten
+        @item3.reload
+        @item3.t.should eq 1
+      end
+    end
+    context '5件で3件目を削除したとき' do
+      before do
+        @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+        @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+      end
+      it '行が削除される' do
+        lambda{
+          @item3.destroy_and_shorten
+        }.should change(SheetPanel, :count ).by(-1)
+      end
+      it '1件目は変化がない' do
+        @item3.destroy_and_shorten
+        @item.reload
+        @item.t.should eq 0
+      end
+      it '2件目は変化がない' do
+        @item3.destroy_and_shorten
+        @item2.reload
+        @item2.t.should eq 1
+      end
+      it '3件目は削除される' do
+        @item3.destroy_and_shorten
+        lambda{
+          SheetPanel.find @item3.id
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+      it '4件目は前に詰められる' do
+        @item3.destroy_and_shorten
+        @item4.reload
+        @item4.t.should eq 2
+      end
+      it '5件目は前に詰められる' do
+        @item3.destroy_and_shorten
+        @item5.reload
+        @item5.t.should eq 3
+      end
+    end
+    #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
+  end
 end
+