OSDN Git Service

fix peta spec
[pettanr/pettanr.git] / spec / peta / leaf_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #スクロール
4
5 describe ScrollPanel do
6   include LeafMacros
7   before do
8     @admin =FactoryGirl.create :admin
9     @user_yas = FactoryGirl.create :user_yas_with_owner
10     @user_rom = FactoryGirl.create :user_rom
11     @author_yas =  @user_yas.author
12     
13     @model = ScrollPanel
14     @scroll =  FactoryGirl.create :scroll_hello, author: @author_yas
15     @panel =  FactoryGirl.create :panel_hello, author: @author_yas
16     @item =  FactoryGirl.create :scroll_panel_plain, scroll: @scroll, panel: @panel
17   end
18   
19   describe '生成メソッドに於いて' do
20     before do
21     end
22     
23     it 'ぶら下がるそれぞれのリーフを取得できる' do
24       expect(@model.parent_model).to eq Scroll
25     end
26     it 'すべてのリーフを取得できる' do
27       expect(@model.destination_model).to eq Panel
28     end
29     
30     it 'ぶら下がるそれぞれのリーフを取得できる' do
31       expect(@model.binder_key).to eq 'scroll_id'
32     end
33     
34     it 'ぶら下がるそれぞれのリーフを取得できる' do
35       expect(@model.destination_key).to eq 'panel_id'
36     end
37     
38   end
39   
40   describe 'myに於いて' do
41     before do
42     end
43     
44     it '名前の活用に於いて' do
45       expect(@item.binder_id).to eq @scroll.id
46     end
47     
48     it '名前の活用に於いて' do
49       expect(@item.destination_id).to eq @panel.id
50     end
51   end
52   
53   describe 'cutに於いて' do
54     it 'cut' do
55       expect(@item.binder_key).to eq @model.binder_key
56     end
57     
58     it 'cut' do
59       expect(@item.destination_key).to eq @model.destination_key
60     end
61     
62   end
63   
64   describe 'playに於いて' do
65     it 'cut' do
66       expect(@model.play_list_order).to eq 'scroll_panels.t'
67     end
68     
69   end
70   
71   describe 'めんどいからチェックしない機能に於いて' do
72     it 'とりあえず呼び出すところまでは確認しておく' do
73       define_test @model
74     end
75     # max_t, find_t, 
76     # own?, visible?
77   end
78   
79     # new_t, collect_t, serial?, validate_t
80     # store, destroy_and_shorten, 
81     # insert_shift, lesser_shift, higher_shift, update_shift, rotate
82 end
83
84 describe SheetPanel do
85   include LeafMacros
86   before do
87     @admin =FactoryGirl.create :admin
88     @user_yas = FactoryGirl.create :user_yas_with_owner
89     @user_rom = FactoryGirl.create :user_rom
90     @author_yas =  @user_yas.author
91     
92     @model = ScrollPanel
93     @scroll =  FactoryGirl.create :scroll_hello, author: @author_yas
94     @panel =  FactoryGirl.create :panel_hello, author: @author_yas
95     @item =  FactoryGirl.create :scroll_panel_plain, scroll: @scroll, panel: @panel
96   end
97   
98   describe '生成メソッドに於いて' do
99     before do
100     end
101     
102     it 'ぶら下がるそれぞれのリーフを取得できる' do
103       expect(@model.parent_model).to eq Scroll
104     end
105     it 'すべてのリーフを取得できる' do
106       expect(@model.destination_model).to eq Panel
107     end
108     
109     it 'ぶら下がるそれぞれのリーフを取得できる' do
110       expect(@model.binder_key).to eq 'scroll_id'
111     end
112     
113     it 'ぶら下がるそれぞれのリーフを取得できる' do
114       expect(@model.destination_key).to eq 'panel_id'
115     end
116     
117   end
118   
119   describe 'myに於いて' do
120     before do
121     end
122     
123     it '名前の活用に於いて' do
124       expect(@item.binder_id).to eq @scroll.id
125     end
126     
127     it '名前の活用に於いて' do
128       expect(@item.destination_id).to eq @panel.id
129     end
130   end
131   
132   describe 'cutに於いて' do
133     it 'cut' do
134       expect(@item.binder_key).to eq @model.binder_key
135     end
136     
137     it 'cut' do
138       expect(@item.destination_key).to eq @model.destination_key
139     end
140     
141   end
142   
143   describe 'playに於いて' do
144     it 'cut' do
145       expect(@model.play_list_order).to eq 'scroll_panels.t'
146     end
147     
148   end
149   
150   describe 'めんどいからチェックしない機能に於いて' do
151     it 'とりあえず呼び出すところまでは確認しておく' do
152       define_test @model
153     end
154     # max_t, find_t, 
155     # own?, visible?
156   end
157   
158     # new_t, collect_t, serial?, validate_t
159     # store, destroy_and_shorten, 
160     # insert_shift, lesser_shift, higher_shift, update_shift, rotate
161 end
162
163 describe ComicStory do
164   include LeafMacros
165   before do
166     @admin = FactoryGirl.create :admin
167     @user_yas = FactoryGirl.create :user_yas_with_owner
168     @user_rom = FactoryGirl.create :user_rom
169     @author_yas = @user_yas.author
170     
171     @model = ComicStory
172     @comic = FactoryGirl.create :comic_doc, author: @author_yas
173     @story = FactoryGirl.create :story_install, author: @author_yas
174     @item = FactoryGirl.create :comic_story_plain, comic: @comic, story: @story
175   end
176   
177   describe '生成メソッドに於いて' do
178     before do
179     end
180     
181     it 'ぶら下がるそれぞれのリーフを取得できる' do
182       expect(@model.parent_model).to eq Comic
183     end
184     it 'すべてのリーフを取得できる' do
185       expect(@model.destination_model).to eq Story
186     end
187     
188     it 'ぶら下がるそれぞれのリーフを取得できる' do
189       expect(@model.binder_key).to eq 'comic_id'
190     end
191     
192     it 'ぶら下がるそれぞれのリーフを取得できる' do
193       expect(@model.destination_key).to eq 'story_id'
194     end
195     
196   end
197   
198   describe 'myに於いて' do
199     before do
200     end
201     
202     it '名前の活用に於いて' do
203       expect(@item.binder_id).to eq @comic.id
204     end
205     
206     it '名前の活用に於いて' do
207       expect(@item.destination_id).to eq @story.id
208     end
209   end
210   
211   describe 'cutに於いて' do
212     it 'cut' do
213       expect(@item.binder_key).to eq @model.binder_key
214     end
215     
216     it 'cut' do
217       expect(@item.destination_key).to eq @model.destination_key
218     end
219     
220   end
221   
222   describe 'playに於いて' do
223     it 'cut' do
224       expect(@model.play_list_order).to eq 'comic_stories.t'
225     end
226     
227   end
228   
229   describe 'めんどいからチェックしない機能に於いて' do
230     it 'とりあえず呼び出すところまでは確認しておく' do
231       define_test @model
232     end
233     # max_t, find_t, 
234     # own?, visible?
235   end
236   
237     # new_t, collect_t, serial?, validate_t
238     # store, destroy_and_shorten, 
239     # insert_shift, lesser_shift, higher_shift, update_shift, rotate
240 end
241
242 describe StorySheet do
243   include LeafMacros
244   before do
245     @admin = FactoryGirl.create :admin
246     @user_yas = FactoryGirl.create :user_yas_with_owner
247     @user_rom = FactoryGirl.create :user_rom
248     @author_yas =  @user_yas.author
249     
250     @model = StorySheet
251     @story = FactoryGirl.create :story_install, author: @author_yas
252     @sheet = FactoryGirl.create :sheet_install_a, author: @author_yas
253     @item = FactoryGirl.create :story_sheet_plain, story: @story, sheet: @sheet
254   end
255   
256   describe '生成メソッドに於いて' do
257     before do
258     end
259     
260     it 'ぶら下がるそれぞれのリーフを取得できる' do
261       expect(@model.parent_model).to eq Story
262     end
263     it 'すべてのリーフを取得できる' do
264       expect(@model.destination_model).to eq Sheet
265     end
266     
267     it 'ぶら下がるそれぞれのリーフを取得できる' do
268       expect(@model.binder_key).to eq 'story_id'
269     end
270     
271     it 'ぶら下がるそれぞれのリーフを取得できる' do
272       expect(@model.destination_key).to eq 'sheet_id'
273     end
274     
275   end
276   
277   describe 'myに於いて' do
278     before do
279     end
280     
281     it '名前の活用に於いて' do
282       expect(@item.binder_id).to eq @story.id
283     end
284     
285     it '名前の活用に於いて' do
286       expect(@item.destination_id).to eq @sheet.id
287     end
288   end
289   
290   describe 'cutに於いて' do
291     it 'cut' do
292       expect(@item.binder_key).to eq @model.binder_key
293     end
294     
295     it 'cut' do
296       expect(@item.destination_key).to eq @model.destination_key
297     end
298     
299   end
300   
301   describe 'playに於いて' do
302     it 'cut' do
303       expect(@model.play_list_order).to eq 'story_sheets.t'
304     end
305     
306   end
307   
308   describe 'めんどいからチェックしない機能に於いて' do
309     it 'とりあえず呼び出すところまでは確認しておく' do
310       define_test @model
311     end
312     # max_t, find_t, 
313     # own?, visible?
314   end
315   
316     # new_t, collect_t, serial?, validate_t
317     # store, destroy_and_shorten, 
318     # insert_shift, lesser_shift, higher_shift, update_shift, rotate
319 end
320
321
322 describe ScrollPanel do
323   include LeafMacros
324   before do
325     @admin =FactoryGirl.create :admin
326     @user_yas = FactoryGirl.create :user_yas_with_owner
327     @user_rom = FactoryGirl.create :user_rom
328     @author_yas =  @user_yas.author
329     
330     @model = ScrollPanel
331   end
332   
333   describe 'new_tに於いて' do
334     before do
335       @scroll =  FactoryGirl.create :scroll_hello, author: @author_yas
336     end
337     
338     context 'オーソドックスなデータのとき' do
339       it 'cut' do
340         expect(@model.new_t(@scroll.id)).to eq 0
341       end
342       
343     end
344     
345     context 'オーソドックスなデータのとき' do
346       it 'cut' do
347         @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
348         expect(@model.new_t(@scroll.id)).to eq 1
349       end
350       
351       it 'cut' do
352         @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
353         @item =  FactoryGirl.create :scroll_panel_plain, z: 2, t: 1, scroll: @scroll, panel: @panel
354         @item =  FactoryGirl.create :scroll_panel_plain, z: 3, t: 2, scroll: @scroll, panel: @panel
355         expect(@model.new_t(@scroll.id)).to eq 3
356       end
357       
358     end
359     
360   end
361   
362   describe 'collect_tに於いて' do
363     before do
364       @dummy =  FactoryGirl.create :scroll_great_with_scroll_panels, author: @author_yas
365       @scroll =  FactoryGirl.create :scroll_hello, author: @author_yas
366     end
367     
368     context 'オーソドックスなデータのとき' do
369       it 'cut' do
370         expect(@model.collect_t(@scroll.id)).to be_empty
371       end
372       
373     end
374     
375     context 'オーソドックスなデータのとき' do
376       it 'cut' do
377         @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
378         expect(@model.collect_t(@scroll.id)).to eq [0]
379       end
380       
381       it 'cut' do
382         @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
383         @item =  FactoryGirl.create :scroll_panel_plain, z: 2, t: 1, scroll: @scroll, panel: @panel
384         @item =  FactoryGirl.create :scroll_panel_plain, z: 3, t: 2, scroll: @scroll, panel: @panel
385         expect(@model.collect_t(@scroll.id)).to eq [0, 1, 2]
386       end
387       
388     end
389     
390   end
391   
392   describe 'tチェックに於いて' do
393     before do
394       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
395       @panel = FactoryGirl.create :panel, :author_id => @author.id
396       @item = FactoryGirl.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
397     end
398     context 'つつがなく終わるとき' do
399       it 't収集を依頼している' do
400         SheetPanel.should_receive(:collect_t).with(any_args).exactly(1)
401         SheetPanel.stub(:collect_t).with(any_args).and_return([])
402         SheetPanel.stub(:serial?).with(any_args).and_return(true)
403         r = SheetPanel.validate_t @item
404       end
405       it '収集したtをシリアライズチェック依頼している' do
406         SheetPanel.stub(:collect_t).with(any_args).and_return([])
407         SheetPanel.should_receive(:serial?).with(any_args).exactly(1)
408         SheetPanel.stub(:serial?).with(any_args).and_return(true)
409         r = SheetPanel.validate_t @item
410       end
411     end
412     #実データでチェック
413     #依頼チェックだけでは不安なので最低限のチェックを
414     context '新規のとき' do
415       it '一件だけで正常通過している' do
416         @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
417         r = SheetPanel.validate_t @item
418         r.should be_true 
419       end
420     end
421     context '既存のとき' do
422       it '2件目を作っても正常通過している' do
423         @item = FactoryGirl.create :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
424         @item2 = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
425         r = SheetPanel.validate_t @item2
426         r.should be_true 
427       end
428     end
429   end
430   describe '挿入シフトに於いて' do
431     before do
432       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
433       @panel = FactoryGirl.create :panel, :author_id => @author.id
434     end
435     context '依頼チェック' do
436       #テーブルが空で0に挿入
437       it 'Updateを依頼している' do
438         SheetPanel.stub(:update_all).with(any_args)
439         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
440         @item = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
441         @item.insert_shift
442       end
443     end
444     context 'テーブルに1件(t:0)で0に挿入したとき' do
445       before do
446         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
447         @item2 = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
448       end
449       it '既存の行を1にシフトしている' do
450         @item2.insert_shift
451         l = SheetPanel.find :all
452         l.first.t.should eq 1
453       end
454       it 'これから挿入するt(0)が欠番になっている' do
455         @item2.insert_shift
456         l = SheetPanel.find(:all).map {|s| s.t }
457         l.include?(0).should_not be_true
458       end
459     end
460     context 'テーブルに2件(t:0,1)で1に挿入したとき' do
461       before do
462         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
463         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
464         @item3 = FactoryGirl.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
465       end
466       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
467         @item3.insert_shift
468         l = SheetPanel.find(:all).map {|s| s.t }
469         l.sort.should eq [0, 2]
470       end
471     end
472     context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
473       before do
474         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
475         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
476         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
477         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
478         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
479         @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
480       end
481       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
482         @item6.insert_shift
483         l = SheetPanel.find(:all).map {|s| s.t }
484         l.sort.should eq [0, 1, 3, 4, 5]
485       end
486     end
487     context '先ほどのケース+他の用紙1件で挿入したとき' do
488       before do
489         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
490         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
491         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
492         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
493         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
494         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
495         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
496         @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
497       end
498       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
499         @item6.insert_shift
500         l = SheetPanel.find(:all, :conditions => ['sheet_id = ?', @sheet.id]).map {|s| s.t }
501         l.sort.should eq [0, 1, 3, 4, 5]
502       end
503       it '他の用紙に影響がない' do
504         ot = @itemc2.t
505         @item6.insert_shift
506         @itemc2.reload
507         @itemc2.t.should eq ot
508       end
509     end
510   end
511   describe '少ない方に移動に於いて' do
512     before do
513       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
514       @panel = FactoryGirl.create :panel, :author_id => @author.id
515     end
516     context '依頼チェック' do
517       it 'Updateを依頼している' do
518         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
519         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
520         SheetPanel.stub(:update_all).with(any_args)
521         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
522         ot = @item2.t
523         @item2.t = 0
524         @item2.lesser_shift ot
525       end
526     end
527     context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
528       before do
529         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
530         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
531         @ot = @item2.t
532         @item2.t = 0
533       end
534       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
535         #移動させたい行はそのまま残る
536         @item2.lesser_shift @ot
537         l = SheetPanel.find(:all).map {|s| s.t }
538         l.sort.should eq [1, 1]
539       end
540       it '既存のt0を1にシフトしている' do
541         @item2.lesser_shift @ot
542         @item.reload
543         @item.t.should eq 1
544       end
545     end
546     context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
547       before do
548         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
549         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
550         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
551         @ot = @item3.t
552         @item3.t = 1
553       end
554       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
555         #移動させたい行はそのまま残る
556         @item3.lesser_shift @ot
557         l = SheetPanel.find(:all).map {|s| s.t }
558         l.sort.should eq [0, 2, 2]
559       end
560       it '既存のt1を2にシフトしている' do
561         @item3.lesser_shift @ot
562         @item2.reload
563         @item2.t.should eq 2
564       end
565     end
566     context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
567       before do
568         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
569         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
570         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
571         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
572         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
573         @ot = @item4.t
574         @item4.t = 1
575       end
576       it 'これから挿入するt(1)が欠番になっている' do
577         #移動させたい行はそのまま残る
578         @item4.lesser_shift @ot
579         l = SheetPanel.find(:all).map {|s| s.t }
580         l.sort.should eq [0, 2, 3, 3, 4]
581       end
582       it '既存のt0には変化がない' do
583         @item4.lesser_shift @ot
584         @item.reload
585         @item.t.should eq 0
586       end
587       it '既存のt4には変化がない' do
588         @item4.lesser_shift @ot
589         @item5.reload
590         @item5.t.should eq 4
591       end
592       it '既存のt1を2にシフトしている' do
593         @item4.lesser_shift @ot
594         @item2.reload
595         @item2.t.should eq 2
596       end
597       it '既存のt2を3にシフトしている' do
598         @item4.lesser_shift @ot
599         @item3.reload
600         @item3.t.should eq 3
601       end
602     end
603     context '先ほどのケース+他の用紙1件で挿入したとき' do
604       before do
605         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
606         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
607         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
608         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
609         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
610         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
611         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
612         @ot = @item4.t
613         @item4.t = 1
614       end
615       it 'これから挿入するt(1)が欠番になっている' do
616         @item4.lesser_shift @ot
617         l = SheetPanel.find(:all).map {|s| s.t }
618         l.sort.should eq [0, 0, 2, 3, 3, 4]
619       end
620       it '既存のt0には変化がない' do
621         @item4.lesser_shift @ot
622         @item.reload
623         @item.t.should eq 0
624       end
625       it '既存のt4には変化がない' do
626         @item4.lesser_shift @ot
627         @item5.reload
628         @item5.t.should eq 4
629       end
630       it '既存のt1を2にシフトしている' do
631         @item4.lesser_shift @ot
632         @item2.reload
633         @item2.t.should eq 2
634       end
635       it '既存のt2を3にシフトしている' do
636         @item4.lesser_shift @ot
637         @item3.reload
638         @item3.t.should eq 3
639       end
640       it '他の用紙に影響がない' do
641         @item4.lesser_shift @ot
642         @itemc2.reload
643         @itemc2.t.should eq 0
644       end
645     end
646     #例外ケース。
647     #負のときは0として正常扱い
648     context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
649       before do
650         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
651         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
652         @ot = @item2.t
653         @item2.t = -1
654       end
655       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
656         #移動させたい行はそのまま残る
657         @item2.lesser_shift @ot
658         l = SheetPanel.find(:all).map {|s| s.t }
659         l.sort.should eq [1, 1]
660       end
661       it '既存のt0を1にシフトしている' do
662         @item2.lesser_shift @ot
663         @item.reload
664         @item.t.should eq 1
665       end
666       it '既存のt1は0に補正されている' do
667         @item2.lesser_shift @ot
668         @item2.t.should eq 0
669       end
670     end
671   end
672   describe '大きい方に移動に於いて' do
673     before do
674       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
675       @panel = FactoryGirl.create :panel, :author_id => @author.id
676     end
677     context '依頼チェック' do
678       it 'Updateを依頼している' do
679         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
680         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
681         SheetPanel.stub(:update_all).with(any_args)
682         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
683         ot = @item.t
684         @item.t = 1
685         @item.higher_shift ot
686       end
687     end
688     context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
689       before do
690         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
691         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
692         @ot = @item.t
693         @item.t = 1
694       end
695       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
696         #移動させたい行はそのまま残る
697         @item.higher_shift @ot
698         l = SheetPanel.find(:all).map {|s| s.t }
699         l.sort.should eq [0, 0]
700       end
701       it '既存のt1を0にシフトしている' do
702         @item.higher_shift @ot
703         @item2.reload
704         @item2.t.should eq 0
705       end
706     end
707     context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
708       before do
709         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
710         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
711         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
712         @ot = @item.t
713         @item.t = 1
714       end
715       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
716         #移動させたい行はそのまま残る
717         @item.higher_shift @ot
718         l = SheetPanel.find(:all).map {|s| s.t }
719         l.sort.should eq [0, 0, 2]
720       end
721       it '既存のt1を0にシフトしている' do
722         @item.higher_shift @ot
723         @item2.reload
724         @item2.t.should eq 0
725       end
726     end
727     context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
728       before do
729         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
730         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
731         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
732         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
733         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
734         @ot = @item2.t
735         @item2.t = 3
736       end
737       it 'これから挿入するt(3)が欠番になっている' do
738         #移動させたい行はそのまま残る
739         @item2.higher_shift @ot
740         l = SheetPanel.find(:all).map {|s| s.t }
741         l.sort.should eq [0, 1, 1, 2, 4]
742       end
743       it '既存のt0には変化がない' do
744         @item2.higher_shift @ot
745         @item.reload
746         @item.t.should eq 0
747       end
748       it '既存のt4には変化がない' do
749         @item2.higher_shift @ot
750         @item5.reload
751         @item5.t.should eq 4
752       end
753       it '既存のt2を1にシフトしている' do
754         @item2.higher_shift @ot
755         @item3.reload
756         @item3.t.should eq 1
757       end
758       it '既存のt3を2にシフトしている' do
759         @item2.higher_shift @ot
760         @item4.reload
761         @item4.t.should eq 2
762       end
763     end
764     context '先ほどのケース+他の用紙1件で挿入したとき' do
765       before do
766         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
767         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
768         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
769         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
770         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
771         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
772         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
773         @ot = @item2.t
774         @item2.t = 3
775       end
776       it 'これから挿入するt(3)が欠番になっている' do
777         #移動させたい行はそのまま残る
778         @item2.higher_shift @ot
779         l = SheetPanel.find(:all).map {|s| s.t }
780         l.sort.should eq [0, 0, 1, 1, 2, 4]
781       end
782       it '既存のt0には変化がない' do
783         @item2.higher_shift @ot
784         @item.reload
785         @item.t.should eq 0
786       end
787       it '既存のt4には変化がない' do
788         @item2.higher_shift @ot
789         @item5.reload
790         @item5.t.should eq 4
791       end
792       it '既存のt2を1にシフトしている' do
793         @item2.higher_shift @ot
794         @item3.reload
795         @item3.t.should eq 1
796       end
797       it '既存のt3を2にシフトしている' do
798         @item2.higher_shift @ot
799         @item4.reload
800         @item4.t.should eq 2
801       end
802       it '他の用紙に影響がない' do
803         @item2.higher_shift @ot
804         @itemc2.reload
805         @itemc2.t.should eq 0
806       end
807     end
808     #例外ケース。
809     #max超えたときはmaxとして正常扱い
810     context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
811       before do
812         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
813         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
814         @ot = @item.t
815         @item.t = 2
816       end
817       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
818         #移動させたい行はそのまま残る
819         @item.higher_shift @ot
820         l = SheetPanel.find(:all).map {|s| s.t }
821         l.sort.should eq [0, 0]
822       end
823       it '既存のt1を0にシフトしている' do
824         @item.higher_shift @ot
825         @item2.reload
826         @item2.t.should eq 0
827       end
828       it '既存のt0は1に補正されている' do
829         @item.higher_shift @ot
830         @item.t.should eq 1
831       end
832     end
833   end
834   describe '入れ替えに於いて' do
835     before do
836       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
837       @panel = FactoryGirl.create :panel, :author_id => @author.id
838       @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
839       @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
840     end
841     context '新tが旧tより小さいとき' do
842       it '少ない方に移動を依頼している' do
843         SheetPanel.any_instance.stub(:lesser_shift).with(any_args)
844         SheetPanel.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
845         ot = @item2.t
846         @item2.t = 0
847         @item2.update_shift ot
848       end
849     end
850     context '新tが旧tより大きいとき' do
851       it '大きい方に移動を依頼している' do
852         SheetPanel.any_instance.stub(:higher_shift).with(any_args)
853         SheetPanel.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
854         ot = @item.t
855         @item.t = 1
856         @item.update_shift ot
857       end
858     end
859   end
860   describe '順序入れ替えに於いて' do
861     before do
862       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
863       @panel = FactoryGirl.create :panel, :author_id => @author.id
864     end
865     context 'オブジェクトが新規でtが空のとき' do
866       it '末尾追加としてtを補充依頼している' do
867         @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
868         SheetPanel.stub(:new_t).with(any_args).and_return(0)
869         SheetPanel.should_receive(:new_t).with(any_args).exactly(1)
870         @item.t = nil
871         r = @item.rotate
872       end
873     end
874     context 'オブジェクトが新規でtが設定されているとき' do
875       it '挿入追加として挿入シフトを依頼している' do
876         @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
877         SheetPanel.any_instance.stub(:insert_shift).with(any_args)
878         SheetPanel.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
879         @item.t = 0
880         r = @item.rotate
881       end
882     end
883     context 'オブジェクトが新規でなくtが設定されているとき' do
884       it 'コマ移動として入れ替えを依頼している' do
885         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
886         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
887         SheetPanel.any_instance.stub(:update_shift).with(any_args)
888         SheetPanel.any_instance.should_receive(:update_shift).with(1).exactly(1)
889         @item2.t = 0
890         r = @item.rotate 1
891       end
892     end
893     context 'オブジェクトが新規でなくtが空のとき' do
894       it '入れ替えもシフトもせず、tを空のままにしている' do
895         #結果、tに欠番が生じてシリアライズチェックでひっかかる
896       end
897     end
898   end
899   describe '保存に於いて' do
900     before do
901       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
902       @panel = FactoryGirl.create :panel, :author_id => @author.id
903       @item = FactoryGirl.build :sheet_panel, :t => nil, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
904     end
905     context 'つつがなく終わるとき' do
906       it '編集許可チェックを依頼している' do
907         SheetPanel.any_instance.stub(:allow?).with(any_args).and_return(true)
908         SheetPanel.any_instance.should_receive(:allow?).with(any_args).exactly(1)
909         r = @item.store
910       end
911       it '順序入れ替えを依頼している' do
912         SheetPanel.any_instance.stub(:rotate).with(any_args).and_return(0)
913         SheetPanel.any_instance.should_receive(:rotate).with(any_args).exactly(1)
914         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
915         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
916         r = @item.store 
917       end
918       it '保存を依頼している' do
919         SheetPanel.stub(:new_t).with(any_args).and_return(0)
920         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
921         SheetPanel.any_instance.should_receive(:save).with(any_args).exactly(1)
922         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
923         r = @item.store
924       end
925       it 'tのシリアライズチェックを依頼している' do
926         SheetPanel.stub(:new_t).with(any_args).and_return(0)
927         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
928         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
929         SheetPanel.should_receive(:validate_t).with(any_args).exactly(1)
930         r = @item.store
931       end
932     end
933     #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
934     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したとき' do
935       before do
936         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
937         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
938         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
939         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
940         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
941         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
942         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
943         @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
944       end
945       it '既存のt0には変化がない' do
946         @item6.store
947         @item.reload
948         @item.t.should eq 0
949       end
950       it '既存のt1には変化がない' do
951         @item6.store
952         @item2.reload
953         @item2.t.should eq 1
954       end
955       it '既存のt2を3にシフトしている' do
956         @item6.store
957         @item3.reload
958         @item3.t.should eq 3
959       end
960       it '既存のt3を4にシフトしている' do
961         @item6.store
962         @item4.reload
963         @item4.t.should eq 4
964       end
965       it '既存のt5を5にシフトしている' do
966         @item6.store
967         @item5.reload
968         @item5.t.should eq 5
969       end
970       it '新規のt2が作成されている' do
971         @item6.store
972         @item6.reload
973         @item6.t.should eq 2
974       end
975       it '他の用紙に影響がない' do
976         @ot = @itemc2.t
977         @item6.store
978         @itemc2.reload
979         @itemc2.t.should eq @ot
980       end
981     end
982     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したとき' do
983       before do
984         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
985         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
986         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
987         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
988         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
989         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
990         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
991         @ot = @item4.t
992         @item4.t = 1
993       end
994       it '既存のt0には変化がない' do
995         @item4.store @ot
996         @item.reload
997         @item.t.should eq 0
998       end
999       it '既存のt4には変化がない' do
1000         @item4.store @ot
1001         @item5.reload
1002         @item5.t.should eq 4
1003       end
1004       it '既存のt1を2にシフトしている' do
1005         @item4.store @ot
1006         @item2.reload
1007         @item2.t.should eq 2
1008       end
1009       it '既存のt2を3にシフトしている' do
1010         @item4.store @ot
1011         @item3.reload
1012         @item3.t.should eq 3
1013       end
1014       it '既存のt3を1にシフトしている' do
1015         @item4.store @ot
1016         @item4.reload
1017         @item4.t.should eq 1
1018       end
1019       it '他の用紙に影響がない' do
1020         @item4.store @ot
1021         @itemc2.reload
1022         @itemc2.t.should eq 0
1023       end
1024     end
1025     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で1を3に移動したとき' do
1026       before do
1027         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1028         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1029         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1030         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1031         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1032         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1033         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1034         @ot = @item2.t
1035         @item2.t = 3
1036       end
1037       it '既存のt0には変化がない' do
1038         @item2.store @ot
1039         @item.reload
1040         @item.t.should eq 0
1041       end
1042       it '既存のt4には変化がない' do
1043         @item2.store @ot
1044         @item5.reload
1045         @item5.t.should eq 4
1046       end
1047       it '既存のt1を3にシフトしている' do
1048         @item2.store @ot
1049         @item2.reload
1050         @item2.t.should eq 3
1051       end
1052       it '既存のt2を1にシフトしている' do
1053         @item2.store @ot
1054         @item3.reload
1055         @item3.t.should eq 1
1056       end
1057       it '既存のt3を2にシフトしている' do
1058         @item2.store @ot
1059         @item4.reload
1060         @item4.t.should eq 2
1061       end
1062       it '他の用紙に影響がない' do
1063         @item2.store @ot
1064         @itemc2.reload
1065         @itemc2.t.should eq 0
1066       end
1067     end
1068     #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1069     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したが保存に失敗したとき' do
1070       before do
1071         SheetPanel.any_instance.stub(:save).with(any_args).and_return(false)
1072         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1073         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1074         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1075         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1076         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1077         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1078         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1079         @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1080       end
1081       it '既存のtに変化がない' do
1082         @item6.store
1083         @item.reload
1084         @item.t.should eq 0
1085         @item2.reload
1086         @item2.t.should eq 1
1087         @item3.reload
1088         @item3.t.should eq 2
1089         @item4.reload
1090         @item4.t.should eq 3
1091         @item5.reload
1092         @item5.t.should eq 4
1093         @itemc2.reload
1094         @itemc2.t.should eq 0
1095       end
1096       it 'falseを返す' do
1097         r = @item6.store
1098         r.should be_false
1099       end
1100     end
1101     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したがシリアルチェックに失敗したとき' do
1102       before do
1103         SheetPanel.stub(:validate_t).with(any_args).and_return(false)
1104         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1105         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1106         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1107         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1108         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1109         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1110         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1111         @ot = @item4.t
1112         @item4.t = 1
1113       end
1114       it '既存のtに変化がない' do
1115         @item4.store @ot
1116         @item.reload
1117         @item.t.should eq 0
1118         @item2.reload
1119         @item2.t.should eq 1
1120         @item3.reload
1121         @item3.t.should eq 2
1122         @item4.reload
1123         @item4.t.should eq 3
1124         @item5.reload
1125         @item5.t.should eq 4
1126         @itemc2.reload
1127         @itemc2.t.should eq 0
1128       end
1129       it 'falseを返す' do
1130         r = @item4.store @ot
1131         r.should be_false
1132       end
1133       it 'tにエラーメッセージが入っている' do
1134         @item4.store @ot
1135         @item4.errors[:t].should_not be_empty
1136         @item4.valid?.should be_true
1137       end
1138     end
1139     context '編集不可だったとき' do
1140       before do
1141         @item = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1142         SheetPanel.any_instance.stub(:allow?).and_return(false)
1143       end
1144       it '403Forbidden例外を返す' do
1145         lambda{
1146           @item.store
1147         }.should raise_error(ActiveRecord::Forbidden)
1148       end
1149     end
1150   end
1151   describe '切り詰め処理つき削除に於いて' do
1152     before do
1153       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1154       @panel = FactoryGirl.create :panel, :author_id => @author.id
1155       @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1156     end
1157     context 'つつがなく終わるとき' do
1158       it '削除される' do
1159         lambda{
1160           @item.destroy_and_shorten
1161         }.should change(SheetPanel, :count ).by(-1)
1162       end
1163       it 'Trueを返す' do
1164         r = @item.destroy_and_shorten
1165         r.should be_true 
1166       end
1167     end
1168     context '削除に失敗したとき' do
1169       before do
1170         SheetPanel.any_instance.stub(:destroy).and_return(false)
1171       end
1172       it 'ロールバックされる' do
1173         lambda{
1174           @item.destroy_and_shorten
1175         }.should_not change(SheetPanel, :count )
1176       end
1177       it 'Falseを返す' do
1178         r = @item.destroy_and_shorten
1179         r.should be_false
1180       end
1181     end
1182     #連携テスト。切り詰めが直接DBをいじる
1183     context '2件で先頭を削除したとき' do
1184       before do
1185         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1186       end
1187       it '行が削除される' do
1188         lambda{
1189           @item.destroy_and_shorten
1190         }.should change(SheetPanel, :count ).by(-1)
1191       end
1192       it '先頭は削除される' do
1193         @item.destroy_and_shorten
1194         lambda{
1195           SheetPanel.find @item.id
1196         }.should raise_error(ActiveRecord::RecordNotFound)
1197       end
1198       it '2件目は前に詰められる' do
1199         @item.destroy_and_shorten
1200         @item2.reload
1201         @item2.t.should eq 0
1202       end
1203     end
1204     context '3件で先頭を削除したとき' do
1205       before do
1206         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1207         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1208       end
1209       it '行が削除される' do
1210         lambda{
1211           @item.destroy_and_shorten
1212         }.should change(SheetPanel, :count ).by(-1)
1213       end
1214       it '先頭は削除される' do
1215         @item.destroy_and_shorten
1216         lambda{
1217           SheetPanel.find @item.id
1218         }.should raise_error(ActiveRecord::RecordNotFound)
1219       end
1220       it '2件目は前に詰められる' do
1221         @item.destroy_and_shorten
1222         @item2.reload
1223         @item2.t.should eq 0
1224       end
1225       it '3件目は前に詰められる' do
1226         @item.destroy_and_shorten
1227         @item3.reload
1228         @item3.t.should eq 1
1229       end
1230     end
1231     context '5件で3件目を削除したとき' do
1232       before do
1233         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1234         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1235         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1236         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1237       end
1238       it '行が削除される' do
1239         lambda{
1240           @item3.destroy_and_shorten
1241         }.should change(SheetPanel, :count ).by(-1)
1242       end
1243       it '1件目は変化がない' do
1244         @item3.destroy_and_shorten
1245         @item.reload
1246         @item.t.should eq 0
1247       end
1248       it '2件目は変化がない' do
1249         @item3.destroy_and_shorten
1250         @item2.reload
1251         @item2.t.should eq 1
1252       end
1253       it '3件目は削除される' do
1254         @item3.destroy_and_shorten
1255         lambda{
1256           SheetPanel.find @item3.id
1257         }.should raise_error(ActiveRecord::RecordNotFound)
1258       end
1259       it '4件目は前に詰められる' do
1260         @item3.destroy_and_shorten
1261         @item4.reload
1262         @item4.t.should eq 2
1263       end
1264       it '5件目は前に詰められる' do
1265         @item3.destroy_and_shorten
1266         @item5.reload
1267         @item5.t.should eq 3
1268       end
1269     end
1270     #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1271   end
1272 end
1273