@user_yas = FactoryGirl.create :user_yas_with_owner
@user_rom = FactoryGirl.create :user_rom
@author_yas = @user_yas.author
+
+ @model = ScrollPanel
+ @scroll = FactoryGirl.create :scroll_hello, author: @author_yas
+ @panel = FactoryGirl.create :panel_hello, author: @author_yas
+ @item = FactoryGirl.create :scroll_panel_plain, scroll: @scroll, panel: @panel
end
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
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.parent_model).to eq Scroll
+ end
+ it 'すべてのリーフを取得できる' do
+ expect(@model.destination_model).to eq Panel
end
- describe 'leafs_itemsに於いて' do
- it 'すべてのリーフを取得できる' do
- define_leafs_items? @item, [ScrollPanel]
- end
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.binder_key).to eq 'scroll_id'
+ end
+
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.destination_key).to eq 'panel_id'
end
end
+ describe 'myに於いて' do
+ before do
+ end
+
+ it '名前の活用に於いて' do
+ expect(@item.binder_id).to eq @scroll.id
+ end
+
+ it '名前の活用に於いて' do
+ expect(@item.destination_id).to eq @panel.id
+ end
+ end
+
+ describe 'cutに於いて' do
+ it 'cut' do
+ expect(@item.binder_key).to eq @model.binder_key
+ end
+
+ it 'cut' do
+ expect(@item.destination_key).to eq @model.destination_key
+ end
+
+ end
+
+ describe 'playに於いて' do
+ it 'cut' do
+ expect(@model.play_list_order).to eq 'scroll_panels.t'
+ end
+
+ end
+
+ describe 'めんどいからチェックしない機能に於いて' do
+ it 'とりあえず呼び出すところまでは確認しておく' do
+ define_test @model
+ end
+ # max_t, find_t,
+ # own?, visible?
+ end
+
+ # new_t, collect_t, serial?, validate_t
+ # store, destroy_and_shorten,
+ # insert_shift, lesser_shift, higher_shift, update_shift, rotate
end
describe SheetPanel do
@user_yas = FactoryGirl.create :user_yas_with_owner
@user_rom = FactoryGirl.create :user_rom
@author_yas = @user_yas.author
+
+ @model = ScrollPanel
+ @scroll = FactoryGirl.create :scroll_hello, author: @author_yas
+ @panel = FactoryGirl.create :panel_hello, author: @author_yas
+ @item = FactoryGirl.create :scroll_panel_plain, scroll: @scroll, panel: @panel
+ end
+
+ describe '生成メソッドに於いて' do
+ before do
+ end
+
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.parent_model).to eq Scroll
+ end
+ it 'すべてのリーフを取得できる' do
+ expect(@model.destination_model).to eq Panel
+ end
+
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.binder_key).to eq 'scroll_id'
+ end
+
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.destination_key).to eq 'panel_id'
+ end
+
end
+ describe 'myに於いて' do
+ before do
+ end
+
+ it '名前の活用に於いて' do
+ expect(@item.binder_id).to eq @scroll.id
+ end
+
+ it '名前の活用に於いて' do
+ expect(@item.destination_id).to eq @panel.id
+ end
+ end
+
+ describe 'cutに於いて' do
+ it 'cut' do
+ expect(@item.binder_key).to eq @model.binder_key
+ end
+
+ it 'cut' do
+ expect(@item.destination_key).to eq @model.destination_key
+ end
+
+ end
+
+ describe 'playに於いて' do
+ it 'cut' do
+ expect(@model.play_list_order).to eq 'scroll_panels.t'
+ end
+
+ end
+
+ describe 'めんどいからチェックしない機能に於いて' do
+ it 'とりあえず呼び出すところまでは確認しておく' do
+ define_test @model
+ end
+ # max_t, find_t,
+ # own?, visible?
+ end
+
+ # new_t, collect_t, serial?, validate_t
+ # store, destroy_and_shorten,
+ # insert_shift, lesser_shift, higher_shift, update_shift, rotate
end
describe ComicStory do
include LeafMacros
before do
- @admin =FactoryGirl.create :admin
+ @admin = FactoryGirl.create :admin
@user_yas = FactoryGirl.create :user_yas_with_owner
@user_rom = FactoryGirl.create :user_rom
- @author_yas = @user_yas.author
+ @author_yas = @user_yas.author
+
+ @model = ComicStory
+ @comic = FactoryGirl.create :comic_doc, author: @author_yas
+ @story = FactoryGirl.create :story_install, author: @author_yas
+ @item = FactoryGirl.create :comic_story_plain, comic: @comic, story: @story
+ end
+
+ describe '生成メソッドに於いて' do
+ before do
+ end
+
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.parent_model).to eq Comic
+ end
+ it 'すべてのリーフを取得できる' do
+ expect(@model.destination_model).to eq Story
+ end
+
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.binder_key).to eq 'comic_id'
+ end
+
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.destination_key).to eq 'story_id'
+ end
+
+ end
+
+ describe 'myに於いて' do
+ before do
+ end
+
+ it '名前の活用に於いて' do
+ expect(@item.binder_id).to eq @comic.id
+ end
+
+ it '名前の活用に於いて' do
+ expect(@item.destination_id).to eq @story.id
+ end
+ end
+
+ describe 'cutに於いて' do
+ it 'cut' do
+ expect(@item.binder_key).to eq @model.binder_key
+ end
+
+ it 'cut' do
+ expect(@item.destination_key).to eq @model.destination_key
+ end
+
+ end
+
+ describe 'playに於いて' do
+ it 'cut' do
+ expect(@model.play_list_order).to eq 'comic_stories.t'
+ end
+
+ end
+
+ describe 'めんどいからチェックしない機能に於いて' do
+ it 'とりあえず呼び出すところまでは確認しておく' do
+ define_test @model
+ end
+ # max_t, find_t,
+ # own?, visible?
end
+ # new_t, collect_t, serial?, validate_t
+ # store, destroy_and_shorten,
+ # insert_shift, lesser_shift, higher_shift, update_shift, rotate
end
describe StorySheet 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 = StorySheet
+ @story = FactoryGirl.create :story_install, author: @author_yas
+ @sheet = FactoryGirl.create :sheet_install_a, author: @author_yas
+ @item = FactoryGirl.create :story_sheet_plain, story: @story, sheet: @sheet
+ end
+
+ describe '生成メソッドに於いて' do
+ before do
+ end
+
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.parent_model).to eq Story
+ end
+ it 'すべてのリーフを取得できる' do
+ expect(@model.destination_model).to eq Sheet
+ end
+
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.binder_key).to eq 'story_id'
+ end
+
+ it 'ぶら下がるそれぞれのリーフを取得できる' do
+ expect(@model.destination_key).to eq 'sheet_id'
+ end
+
+ end
+
+ describe 'myに於いて' do
+ before do
+ end
+
+ it '名前の活用に於いて' do
+ expect(@item.binder_id).to eq @story.id
+ end
+
+ it '名前の活用に於いて' do
+ expect(@item.destination_id).to eq @sheet.id
+ end
+ end
+
+ describe 'cutに於いて' do
+ it 'cut' do
+ expect(@item.binder_key).to eq @model.binder_key
+ end
+
+ it 'cut' do
+ expect(@item.destination_key).to eq @model.destination_key
+ end
+
+ end
+
+ describe 'playに於いて' do
+ it 'cut' do
+ expect(@model.play_list_order).to eq 'story_sheets.t'
+ end
+
+ end
+
+ describe 'めんどいからチェックしない機能に於いて' do
+ it 'とりあえず呼び出すところまでは確認しておく' do
+ define_test @model
+ end
+ # max_t, find_t,
+ # own?, visible?
+ end
+
+ # new_t, collect_t, serial?, validate_t
+ # store, destroy_and_shorten,
+ # insert_shift, lesser_shift, higher_shift, update_shift, rotate
+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
+
+ 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 'collect_tに於いて' do
+ before do
+ @dummy = FactoryGirl.create :scroll_great_with_scroll_panels, author: @author_yas
+ @scroll = FactoryGirl.create :scroll_hello, author: @author_yas
+ end
+
+ 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
+ end
+ context '依頼チェック' do
+ #テーブルが空で0に挿入
+ it 'Updateを依頼している' do
+ SheetPanel.stub(:update_all).with(any_args)
+ SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
+ @item = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+ @item.insert_shift
+ end
+ end
+ context 'テーブルに1件(t:0)で0に挿入したとき' do
+ before do
+ @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+ @item2 = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+ end
+ it '既存の行を1にシフトしている' do
+ @item2.insert_shift
+ l = SheetPanel.find :all
+ l.first.t.should eq 1
+ end
+ it 'これから挿入するt(0)が欠番になっている' do
+ @item2.insert_shift
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.include?(0).should_not be_true
+ end
+ end
+ context 'テーブルに2件(t:0,1)で1に挿入したとき' do
+ before do
+ @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.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+ end
+ it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
+ @item3.insert_shift
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [0, 2]
+ end
+ end
+ context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
+ before do
+ @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 '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
+ @item6.insert_shift
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [0, 1, 3, 4, 5]
+ end
+ end
+ context '先ほどのケース+他の用紙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
+ @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+ end
+ it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
+ @item6.insert_shift
+ l = SheetPanel.find(:all, :conditions => ['sheet_id = ?', @sheet.id]).map {|s| s.t }
+ l.sort.should eq [0, 1, 3, 4, 5]
+ end
+ it '他の用紙に影響がない' do
+ ot = @itemc2.t
+ @item6.insert_shift
+ @itemc2.reload
+ @itemc2.t.should eq ot
+ end
+ end
+ end
+ describe '少ない方に移動に於いて' do
+ before do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ @panel = FactoryGirl.create :panel, :author_id => @author.id
+ end
+ context '依頼チェック' do
+ it 'Updateを依頼している' do
+ @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
+ SheetPanel.stub(:update_all).with(any_args)
+ SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
+ ot = @item2.t
+ @item2.t = 0
+ @item2.lesser_shift ot
+ end
+ end
+ context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
+ before do
+ @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
+ @ot = @item2.t
+ @item2.t = 0
+ end
+ it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
+ #移動させたい行はそのまま残る
+ @item2.lesser_shift @ot
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [1, 1]
+ end
+ it '既存のt0を1にシフトしている' do
+ @item2.lesser_shift @ot
+ @item.reload
+ @item.t.should eq 1
+ end
+ end
+ context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
+ before do
+ @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
+ @ot = @item3.t
+ @item3.t = 1
+ end
+ it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
+ #移動させたい行はそのまま残る
+ @item3.lesser_shift @ot
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [0, 2, 2]
+ end
+ it '既存のt1を2にシフトしている' do
+ @item3.lesser_shift @ot
+ @item2.reload
+ @item2.t.should eq 2
+ end
+ end
+ context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
+ before do
+ @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(1)が欠番になっている' do
+ #移動させたい行はそのまま残る
+ @item4.lesser_shift @ot
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [0, 2, 3, 3, 4]
+ end
+ it '既存のt0には変化がない' do
+ @item4.lesser_shift @ot
+ @item.reload
+ @item.t.should eq 0
+ end
+ it '既存のt4には変化がない' do
+ @item4.lesser_shift @ot
+ @item5.reload
+ @item5.t.should eq 4
+ end
+ it '既存のt1を2にシフトしている' do
+ @item4.lesser_shift @ot
+ @item2.reload
+ @item2.t.should eq 2
+ end
+ it '既存のt2を3にシフトしている' do
+ @item4.lesser_shift @ot
+ @item3.reload
+ @item3.t.should eq 3
+ end
+ end
+ context '先ほどのケース+他の用紙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 'これから挿入するt(1)が欠番になっている' do
+ @item4.lesser_shift @ot
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [0, 0, 2, 3, 3, 4]
+ end
+ it '既存のt0には変化がない' do
+ @item4.lesser_shift @ot
+ @item.reload
+ @item.t.should eq 0
+ end
+ it '既存のt4には変化がない' do
+ @item4.lesser_shift @ot
+ @item5.reload
+ @item5.t.should eq 4
+ end
+ it '既存のt1を2にシフトしている' do
+ @item4.lesser_shift @ot
+ @item2.reload
+ @item2.t.should eq 2
+ end
+ it '既存のt2を3にシフトしている' do
+ @item4.lesser_shift @ot
+ @item3.reload
+ @item3.t.should eq 3
+ end
+ it '他の用紙に影響がない' do
+ @item4.lesser_shift @ot
+ @itemc2.reload
+ @itemc2.t.should eq 0
+ end
+ end
+ #例外ケース。
+ #負のときは0として正常扱い
+ context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
+ before do
+ @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
+ @ot = @item2.t
+ @item2.t = -1
+ end
+ it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
+ #移動させたい行はそのまま残る
+ @item2.lesser_shift @ot
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [1, 1]
+ end
+ it '既存のt0を1にシフトしている' do
+ @item2.lesser_shift @ot
+ @item.reload
+ @item.t.should eq 1
+ end
+ it '既存のt1は0に補正されている' do
+ @item2.lesser_shift @ot
+ @item2.t.should eq 0
+ end
+ end
+ end
+ describe '大きい方に移動に於いて' do
+ before do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ @panel = FactoryGirl.create :panel, :author_id => @author.id
+ end
+ context '依頼チェック' do
+ it 'Updateを依頼している' do
+ @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
+ SheetPanel.stub(:update_all).with(any_args)
+ SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
+ ot = @item.t
+ @item.t = 1
+ @item.higher_shift ot
+ end
+ end
+ context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
+ before do
+ @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
+ @ot = @item.t
+ @item.t = 1
+ end
+ it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
+ #移動させたい行はそのまま残る
+ @item.higher_shift @ot
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [0, 0]
+ end
+ it '既存のt1を0にシフトしている' do
+ @item.higher_shift @ot
+ @item2.reload
+ @item2.t.should eq 0
+ end
+ end
+ context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
+ before do
+ @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
+ @ot = @item.t
+ @item.t = 1
+ end
+ it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
+ #移動させたい行はそのまま残る
+ @item.higher_shift @ot
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [0, 0, 2]
+ end
+ it '既存のt1を0にシフトしている' do
+ @item.higher_shift @ot
+ @item2.reload
+ @item2.t.should eq 0
+ end
+ end
+ context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
+ before do
+ @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 'これから挿入するt(3)が欠番になっている' do
+ #移動させたい行はそのまま残る
+ @item2.higher_shift @ot
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [0, 1, 1, 2, 4]
+ end
+ it '既存のt0には変化がない' do
+ @item2.higher_shift @ot
+ @item.reload
+ @item.t.should eq 0
+ end
+ it '既存のt4には変化がない' do
+ @item2.higher_shift @ot
+ @item5.reload
+ @item5.t.should eq 4
+ end
+ it '既存のt2を1にシフトしている' do
+ @item2.higher_shift @ot
+ @item3.reload
+ @item3.t.should eq 1
+ end
+ it '既存のt3を2にシフトしている' do
+ @item2.higher_shift @ot
+ @item4.reload
+ @item4.t.should eq 2
+ end
+ end
+ context '先ほどのケース+他の用紙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 = @item2.t
+ @item2.t = 3
+ end
+ it 'これから挿入するt(3)が欠番になっている' do
+ #移動させたい行はそのまま残る
+ @item2.higher_shift @ot
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [0, 0, 1, 1, 2, 4]
+ end
+ it '既存のt0には変化がない' do
+ @item2.higher_shift @ot
+ @item.reload
+ @item.t.should eq 0
+ end
+ it '既存のt4には変化がない' do
+ @item2.higher_shift @ot
+ @item5.reload
+ @item5.t.should eq 4
+ end
+ it '既存のt2を1にシフトしている' do
+ @item2.higher_shift @ot
+ @item3.reload
+ @item3.t.should eq 1
+ end
+ it '既存のt3を2にシフトしている' do
+ @item2.higher_shift @ot
+ @item4.reload
+ @item4.t.should eq 2
+ end
+ it '他の用紙に影響がない' do
+ @item2.higher_shift @ot
+ @itemc2.reload
+ @itemc2.t.should eq 0
+ end
+ end
+ #例外ケース。
+ #max超えたときはmaxとして正常扱い
+ context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
+ before do
+ @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
+ @ot = @item.t
+ @item.t = 2
+ end
+ it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
+ #移動させたい行はそのまま残る
+ @item.higher_shift @ot
+ l = SheetPanel.find(:all).map {|s| s.t }
+ l.sort.should eq [0, 0]
+ end
+ it '既存のt1を0にシフトしている' do
+ @item.higher_shift @ot
+ @item2.reload
+ @item2.t.should eq 0
+ end
+ it '既存のt0は1に補正されている' do
+ @item.higher_shift @ot
+ @item.t.should eq 1
+ 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
+ @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+ end
+ context '新tが旧tより小さいとき' do
+ it '少ない方に移動を依頼している' do
+ SheetPanel.any_instance.stub(:lesser_shift).with(any_args)
+ SheetPanel.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
+ ot = @item2.t
+ @item2.t = 0
+ @item2.update_shift ot
+ end
+ end
+ context '新tが旧tより大きいとき' do
+ it '大きい方に移動を依頼している' do
+ SheetPanel.any_instance.stub(:higher_shift).with(any_args)
+ SheetPanel.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
+ ot = @item.t
+ @item.t = 1
+ @item.update_shift ot
+ end
+ end
+ end
+ describe '順序入れ替えに於いて' do
+ before do
+ @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+ @panel = FactoryGirl.create :panel, :author_id => @author.id
+ end
+ context 'オブジェクトが新規でtが空のとき' do
+ it '末尾追加としてtを補充依頼している' do
+ @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+ SheetPanel.stub(:new_t).with(any_args).and_return(0)
+ SheetPanel.should_receive(:new_t).with(any_args).exactly(1)
+ @item.t = nil
+ r = @item.rotate
+ end
+ end
+ context 'オブジェクトが新規でtが設定されているとき' do
+ it '挿入追加として挿入シフトを依頼している' do
+ @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
+ SheetPanel.any_instance.stub(:insert_shift).with(any_args)
+ SheetPanel.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
+ @item.t = 0
+ r = @item.rotate
+ end
+ end
+ context 'オブジェクトが新規でなくtが設定されているとき' do
+ it 'コマ移動として入れ替えを依頼している' do
+ @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
+ SheetPanel.any_instance.stub(:update_shift).with(any_args)
+ SheetPanel.any_instance.should_receive(:update_shift).with(1).exactly(1)
+ @item2.t = 0
+ r = @item.rotate 1
+ end
+ end
+ context 'オブジェクトが新規でなくtが空のとき' do
+ it '入れ替えもシフトもせず、tを空のままにしている' do
+ #結果、tに欠番が生じてシリアライズチェックでひっかかる
+ 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