OSDN Git Service

fix peta test
[pettanr/pettanr.git] / spec / models / sheet_panel_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #紙コマ
4 describe SheetPanel do
5   before do
6     @admin =FactoryGirl.create :admin
7     @user_yas = FactoryGirl.create :user_yas_with_owner
8     @user_rom = FactoryGirl.create :user_rom
9     @author_yas =  @user_yas.author
10   end
11   
12   describe '検証に於いて' do
13     before do
14       @sheet = FactoryGirl.create :sheet, author_id: @author_yas.id
15       @panel = FactoryGirl.create :panel, author_id: @author_yas.id
16       @item = FactoryGirl.build :sheet_panel_plain, sheet: @sheet, panel: @panel
17     end
18     
19     context 'オーソドックスなデータのとき' do
20       it '下限データが通る' do
21         @item.x = -99999
22         @item.y = -99999
23         @item.z = 1
24         @item.t = 0
25         expect(@item).to be_valid
26       end
27       it '上限データが通る' do
28         @item.x = 99999
29         @item.y = 99999
30         @item.z = 99999
31         @item.t = 99999
32         expect(@item).to be_valid
33       end
34     end
35     
36     context 'sheet_idを検証するとき' do
37       it '他人のコマや用紙を引用しているケースで、オーナーが削除することもあるので、空白でも通る' do
38         @item.sheet_id = nil
39         expect(@item).to be_valid
40       end
41     end
42     
43     context 'panel_idを検証するとき' do
44       it '他人のコマや用紙を引用しているケースで、オーナーが削除することもあるので、空白でも通る' do
45         @item.panel_id = nil
46         expect(@item).to be_valid
47       end
48     end
49     
50     context 'xを検証するとき' do
51       it 'nullなら失敗する' do
52         @item.x = nil
53         expect(@item).to_not be_valid
54       end
55       it '0なら通る' do
56         @item.x = 0
57         expect(@item).to be_valid
58       end
59       it '負でも通る' do
60         @item.x = -1
61         expect(@item).to be_valid
62       end
63     end
64     context 'yを検証するとき' do
65       it 'nullなら失敗する' do
66         @item.y = nil
67         expect(@item).to_not be_valid
68       end
69       it '0なら通る' do
70         @item.y = 0
71         expect(@item).to be_valid
72       end
73       it '負でも通る' do
74         @item.y = -1
75         expect(@item).to be_valid
76       end
77     end
78     context 'zを検証するとき' do
79       it 'nullなら失敗する' do
80         @item.z = nil
81         expect(@item).to_not be_valid
82       end
83       it '負なら失敗する' do
84         @item.z = -1
85         expect(@item).to_not be_valid
86       end
87       it '0なら失敗する' do
88         @item.z = 0
89         expect(@item).to_not be_valid
90       end
91       it '1なら通る' do
92         @item.z = 1
93         expect(@item).to be_valid
94       end
95     end
96     context 'tを検証するとき' do
97       it 'nullなら失敗する' do
98         @item.t = nil
99         expect(@item).to_not be_valid
100       end
101       it '負なら失敗する' do
102         @item.t = -1
103         expect(@item).to_not be_valid
104       end
105       it '0なら通る' do
106         @item.t = 0
107         expect(@item).to be_valid
108       end
109     end
110     
111     context '全体を検証するとき' do
112     end
113   end
114   
115   describe 'デフォルト値補充に於いて' do
116     before do
117       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
118       @panel = FactoryGirl.create :panel, :author_id => @author.id
119     end
120     
121     #dbのデフォルト値が0だから明示的にnilにしないと追加ができない
122     it 'tをnilにする' do
123       @item = FactoryGirl.build :sheet_panel, :sheet_id => nil, :panel_id => @panel.id
124       @item.supply_default
125       @item.t.should be_nil
126     end
127     
128     it '自身の用紙に最大t取得を依頼している' do
129       @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id
130       Sheet.any_instance.should_receive(:new_t).with(any_args).exactly(1)
131       @item.supply_default
132     end
133     it 'tをnilにする' do
134       @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id
135       Sheet.any_instance.stub(:new_t).with(any_args).and_return(4)
136       @item.supply_default
137       @item.t.should eq 4
138     end
139     
140   end
141   
142   describe '上書き補充に於いて' do
143     before do
144       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
145       @panel = FactoryGirl.create :panel, :author_id => @author.id
146     end
147     
148     context 'author_idを補充' do
149       it '問答無用でauthor_idを補充する' do
150         @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id
151         @item.author_id = nil
152         @item.overwrite @author
153         @item.author_id.should eq @author.id
154       end
155     end
156     
157   end
158   
159   describe '所持判定に於いて' do
160     before do
161       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
162       @panel = FactoryGirl.create :panel, :author_id => @author.id
163       @item = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
164       @sheeto = FactoryGirl.create :sheet, :author_id => @other_author.id
165       @panelo = FactoryGirl.create :panel, :author_id => @other_author.id
166       @itemo = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @sheeto.id, :panel_id => @panelo.id
167     end
168     context '事前チェックする' do
169       it '自身にロールリストからの作家取得を依頼している' do
170         SheetPanel.should_receive(:get_author_from_roles).with(any_args).exactly(1)
171         r = @item.own?([@author])
172       end
173     end
174     context 'ロール内作家が取得できるとき' do
175       before do
176       end
177       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
178         SheetPanel.stub(:get_author_from_roles).with(any_args).and_return(@author)
179         r = @item.own?([@author])
180         r.should be_true
181       end
182       it 'ロール内作家のidが自身の作家idと一致しないならno' do
183         SheetPanel.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
184         @item.own?(@other_author).should be_false
185       end
186     end
187     context 'ロール内作家が取得できないとき' do
188       before do
189         SheetPanel.stub(:get_author_from_roles).with(any_args).and_return(nil)
190       end
191       it 'Falseを返す' do
192         r = @item.own?([@author])
193         r.should be_false
194       end
195     end
196   end
197   
198   describe '閲覧許可に於いて' do
199     before do
200       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
201       @panel = FactoryGirl.create :panel, :author_id => @author.id
202       @item = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
203     end
204     context 'オープンモードのとき' do
205       before do
206         MagicNumber['run_mode'] = 0
207       end
208       it '自身にゲスト用ロールチェックを問い合わせしている' do
209         SheetPanel.any_instance.stub(:guest_role_check).and_return(true)
210         SheetPanel.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
211         r = @item.visible?([@author])
212       end
213       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
214         SheetPanel.any_instance.stub(:guest_role_check).and_return(false)
215         r = @item.visible?([@author])
216         r.should be_false
217       end
218     end
219     context 'クローズドモードのとき' do
220       before do
221         MagicNumber['run_mode'] = 1
222       end
223       it '自身に読者用ロールチェックを問い合わせしている' do
224         SheetPanel.any_instance.stub(:reader_role_check).and_return(true)
225         SheetPanel.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
226         r = @item.visible?([@author])
227       end
228       it '読者用ロールチェックが失敗したとき、falseを返す' do
229         SheetPanel.any_instance.stub(:reader_role_check).and_return(false)
230         r = @item.visible?([@author])
231         r.should be_false
232       end
233     end
234     context '事前チェックする' do
235       before do
236         MagicNumber['run_mode'] = 1
237         SheetPanel.any_instance.stub(:reader_role_check).and_return(true)
238       end
239       it '自身の用紙に所持判定を問い合わせしている' do
240         Sheet.any_instance.stub(:own?).and_return(true)
241         Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
242         r = @item.visible?([@author])
243       end
244       it '自身の用紙に閲覧許可を問い合わせしている' do
245         Sheet.any_instance.stub(:own?).and_return(false)
246         Sheet.any_instance.stub(:visible?).and_return(true)
247         Sheet.any_instance.should_receive(:visible?).with(any_args).exactly(1)
248         r = @item.visible?([@author])
249       end
250     end
251     context 'つつがなく終わるとき' do
252       before do
253         MagicNumber['run_mode'] = 1
254         SheetPanel.any_instance.stub(:reader_role_check).and_return(true)
255       end
256       it '自分の用紙の紙コマなら許可する' do
257         Sheet.any_instance.stub(:own?).and_return(true)
258         Sheet.any_instance.stub(:visible).and_return(0)
259         r = @item.visible?([@author])
260         r.should be_true
261       end
262       it '他人の非公開用紙の紙コマなら許可しない' do
263         Sheet.any_instance.stub(:own?).and_return(false)
264         Sheet.any_instance.stub(:visible).and_return(0)
265         r = @item.visible?([@author])
266         r.should be_false
267       end
268       it '他人の用紙の紙コマでも公開なら許可する' do
269         Sheet.any_instance.stub(:own?).and_return(false)
270         Sheet.any_instance.stub(:visible).and_return(1)
271         r = @item.visible?([@author])
272         r.should be_true
273       end
274     end
275   end
276   
277   
278   describe '単体取得に於いて' do
279     before do
280       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
281       @panel = FactoryGirl.create :panel, :author_id => @author.id
282       @item = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
283     end
284     context 'つつがなく終わるとき' do
285       it '単体取得オプションを利用している' do
286         SheetPanel.stub(:show_opt).with(any_args).and_return({})
287         SheetPanel.should_receive(:show_opt).with(any_args).exactly(1)
288         r = SheetPanel.show @item.id, @author
289       end
290       it '閲覧許可を問い合わせている' do
291         SheetPanel.any_instance.stub(:visible?).with(@author).and_return(true)
292         SheetPanel.any_instance.should_receive(:visible?).with(@author).exactly(1)
293         r = SheetPanel.show @item.id, @author
294       end
295     end
296     it '指定の紙コマを返す' do
297       l = SheetPanel.show @item.id, @author
298       l.should eq @item
299     end
300     context '他人の紙コマを開こうとしたとき' do
301       it '403Forbidden例外を返す' do
302         SheetPanel.any_instance.stub(:visible?).with(@other_author).and_return(false)
303         lambda{
304           SheetPanel.show @item.id, @other_author
305         }.should raise_error(ActiveRecord::Forbidden)
306       end
307     end
308     context '存在しない紙コマを開こうとしたとき' do
309       it '404RecordNotFound例外を返す' do
310         lambda{
311           SheetPanel.show 110, @author
312         }.should raise_error(ActiveRecord::RecordNotFound)
313       end
314     end
315   end
316   
317   describe '編集取得に於いて' do
318     before do
319       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
320       @panel = FactoryGirl.create :panel, :author_id => @author.id
321       @item = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
322     end
323     context 'つつがなく終わるとき' do
324       it '単体取得オプションを利用している' do
325         SheetPanel.stub(:show_opt).with(any_args).and_return({})
326         SheetPanel.should_receive(:show_opt).with(any_args).exactly(1)
327         r = SheetPanel.edit @item.id, @author
328       end
329       it '所持判定を問い合わせている' do
330         SheetPanel.any_instance.stub(:own?).with(any_args).and_return(true)
331         SheetPanel.any_instance.should_receive(:own?).with(any_args).exactly(1)
332         r = SheetPanel.edit @item.id, @author
333       end
334     end
335     it '指定の紙コマを返す' do
336       l = SheetPanel.edit @item.id, @author
337       l.should eq @item
338     end
339     context '他人の紙コマを開こうとしたとき' do
340       it '403Forbidden例外を返す' do
341         SheetPanel.any_instance.stub(:own?).and_return(false)
342         lambda{
343           SheetPanel.edit @item.id, @author
344         }.should raise_error(ActiveRecord::Forbidden)
345       end
346     end
347     context '存在しない紙コマを開こうとしたとき' do
348       it '404RecordNotFound例外を返す' do
349         lambda{
350           SheetPanel.edit 110, @author
351         }.should raise_error(ActiveRecord::RecordNotFound)
352       end
353     end
354   end
355   
356   describe '単体取得オプションに於いて' do
357     it 'includeキーを含んでいる' do
358       r = SheetPanel.show_opt
359       r.has_key?(:include).should be_true
360     end
361     it '3つの項目を含んでいる' do
362       r = SheetPanel.show_opt[:include]
363       r.should have(3).items
364     end
365     it '用紙を含んでいる' do
366       r = SheetPanel.show_opt[:include]
367       r.has_key?(:sheet).should be_true
368     end
369       it '用紙は作家を含んでいる' do
370         r = SheetPanel.show_opt[:include]
371         r[:sheet].has_key?(:author).should be_true
372       end
373     it '作家を含んでいる' do
374       r = SheetPanel.show_opt[:include]
375       r.has_key?(:author).should be_true
376     end
377     it 'コマを含んでいる' do
378       r = SheetPanel.show_opt[:include]
379       r.has_key?(:panel).should be_true
380     end
381       it 'コマは作家を含んでいる' do
382         r = SheetPanel.show_opt[:include]
383         r[:panel].has_key?(:author).should be_true
384       end
385       it 'コマはコマ絵を含んでいる' do
386         r = SheetPanel.show_opt[:include]
387         r[:panel].has_key?(:panel_pictures).should be_true
388       end
389         it 'コマ絵は実素材を含んでいる' do
390           r = SheetPanel.show_opt[:include]
391           r[:panel][:panel_pictures].has_key?(:picture).should be_true
392         end
393           it '実素材は絵師を含んでいる' do
394             r = SheetPanel.show_opt[:include]
395             r[:panel][:panel_pictures][:picture].has_key?(:artist).should be_true
396           end
397           it '実素材はライセンスを含んでいる' do
398             r = SheetPanel.show_opt[:include]
399             r[:panel][:panel_pictures][:picture].has_key?(:license).should be_true
400           end
401       it 'コマはフキダシを含んでいる' do
402         r = SheetPanel.show_opt[:include]
403         r[:panel].has_key?(:speech_balloons).should be_true
404       end
405         it 'フキダシはフキダシ枠を含んでいる' do
406           r = SheetPanel.show_opt[:include]
407           r[:panel][:speech_balloons].has_key?(:balloon).should be_true
408         end
409         it 'フキダシはセリフを含んでいる' do
410           r = SheetPanel.show_opt[:include]
411           r[:panel][:speech_balloons].has_key?(:speech).should be_true
412         end
413   end
414   describe 'json単体取得オプションに於いて' do
415   end
416   
417   describe '紙コマのjson出力に於いて' do
418     before do
419       #コマを作成しておく。
420       @panel = FactoryGirl.create :panel, :author_id => @author.id
421       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height
422       @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
423       @speech = @sb.build_speech(
424         FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id)
425       )
426       @balloon = @sb.build_balloon(
427         FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id)
428       )
429       @sb.boost
430       @sb.save!
431       @gc = @panel.ground_colors.create(
432         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)
433       )
434       @gp = @panel.ground_pictures.create(
435         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)
436       )
437       @panel.reload
438       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
439       @item = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
440     end
441     context '事前チェックする' do
442       before do
443         Panel.any_instance.stub(:elements).and_return('{}')
444       end
445       it 'コマ要素のjson出力を依頼している' do
446         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
447         Panel.any_instance.should_receive(:elements).with(any_args).exactly(1)
448         r = @item.sheet_panel_as_json @author
449       end
450     end
451     it 'json textを返している' do
452       r = JSON.parse @item.sheet_panel_as_json(@author)
453       r.is_a?(Hash).should be_true
454     end
455     it 'sheet,author,panel,コマ要素を含んでいる' do
456       r = JSON.parse @item.sheet_panel_as_json(@author)
457       r.has_key?('sheet').should be_true
458       r['sheet'].has_key?('author').should be_true
459       r.has_key?('author').should be_true
460       r.has_key?('panel').should be_true
461       r['panel'].has_key?('author').should be_true
462     end
463     context 'コマ閲覧許可のとき' do
464       before do
465         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
466       end
467       it 'コマ要素にコマ要素を追加している' do
468         r = JSON.parse @item.sheet_panel_as_json(@author)
469         r['panel'].has_key?('elements').should be_true
470         r['panel']['elements'].should_not be_empty
471       end
472     end
473     context 'コマ閲覧不許可のとき' do
474       before do
475         Panel.any_instance.stub(:visible?).with(any_args).and_return(false)
476       end
477       it 'コマ要素にデータを含ませない' do
478         r = JSON.parse @item.sheet_panel_as_json(@author)
479         r['panel'].has_key?('elements').should be_false
480       end
481     end
482   end
483   
484   describe '紙コマリストのjson出力に於いて' do
485     before do
486       @panel = FactoryGirl.create :panel, :author_id => @author.id
487       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
488       @item = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
489       SheetPanel.any_instance.stub(:sheet_panel_as_json).with(@author).and_return('{"s": 5}')
490     end
491     context 'つつがなく終わるとき' do
492       it '紙コマのjson出力を依頼している' do
493         SheetPanel.any_instance.should_receive(:sheet_panel_as_json).with(@author).exactly(1)
494         r = SheetPanel.list_as_json_text [@item], @author
495       end
496     end
497     it 'json textを返している' do
498       r = SheetPanel.list_as_json_text [@item], @author
499       j = JSON.parse r
500       j.is_a?(Array).should be_true
501     end
502     it '紙コマを含んでいる' do
503       r = SheetPanel.list_as_json_text [@item], @author
504       j = JSON.parse r
505       j.first.has_key?('s').should be_true
506     end
507   end
508   
509   describe 't補充値に於いて' do
510     before do
511       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
512       @panel = FactoryGirl.create :panel, :author_id => @author.id
513     end
514     
515     context '用紙初のコマなら' do
516       it '0を補充値とする' do
517         @item = FactoryGirl.build :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
518         @item.t = nil
519         r = SheetPanel.new_t @item.sheet_id
520         r.should eq 0
521       end
522     end
523     context '用紙に一個コマがあるとき' do
524       it '1を補充値とする' do
525         FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0
526         @item = FactoryGirl.build :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
527         @item.t = nil
528         r = SheetPanel.new_t @item.sheet_id
529         r.should eq 1
530       end
531     end
532     context '用紙に2個コマがあるとき' do
533       it '2を補充値とする' do
534         FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0
535         FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1
536         @item = FactoryGirl.build :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
537         @item.t = nil
538         r = SheetPanel.new_t @item.sheet_id
539         r.should eq 2
540       end
541     end
542   end
543   describe 'シリアライズチェックに於いて' do
544     context 'つつがなく終わるとき' do
545       it '0からシリアライズされているならTrueを返す' do
546         r = SheetPanel.serial? [0, 1, 2]
547         r.should be_true
548       end
549       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
550         r = SheetPanel.serial? [0, 2, 1]
551         r.should be_true
552       end
553       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
554         r = SheetPanel.serial? [ 2, 1, 4, 3, 0]
555         r.should be_true
556       end
557     end
558     context '異常なとき' do
559       it '0から始まらないならFalseを返す' do
560         r = SheetPanel.serial? [1, 2, 3]
561         r.should be_false
562       end
563       it '連続していないならFalseを返す' do
564         r = SheetPanel.serial? [0, 1, 2, 4]
565         r.should be_false
566       end
567       it '連続していないならFalseを返す' do
568         r = SheetPanel.serial? [0, 1, 2, 4, 5]
569         r.should be_false
570       end
571     end
572   end
573   describe 't収集に於いて' do
574     before do
575       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
576       @panel = FactoryGirl.create :panel, :author_id => @author.id
577       @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
578       @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
579       @c2sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
580     end
581     context 'つつがなく終わるとき' do
582       it '紙コマから同一用紙のtだけを収集している' do
583         r = SheetPanel.collect_t @item
584         r.should eq [0]
585       end
586     end
587     context '複数コマのとき' do
588       it '紙コマから同一用紙のtだけを収集している' do
589         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
590         r = SheetPanel.collect_t @item
591         r.sort.should eq [0, 1]
592       end
593     end
594     context '複数コマでヨソの用紙も混じっているとき' do
595       it '紙コマから同一用紙のtだけを収集している' do
596         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
597         r = SheetPanel.collect_t @item
598         r.sort.should eq [0, 1]
599       end
600     end
601   end
602   describe 'tチェックに於いて' do
603     before do
604       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
605       @panel = FactoryGirl.create :panel, :author_id => @author.id
606       @item = FactoryGirl.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
607     end
608     context 'つつがなく終わるとき' do
609       it 't収集を依頼している' do
610         SheetPanel.should_receive(:collect_t).with(any_args).exactly(1)
611         SheetPanel.stub(:collect_t).with(any_args).and_return([])
612         SheetPanel.stub(:serial?).with(any_args).and_return(true)
613         r = SheetPanel.validate_t @item
614       end
615       it '収集したtをシリアライズチェック依頼している' do
616         SheetPanel.stub(:collect_t).with(any_args).and_return([])
617         SheetPanel.should_receive(:serial?).with(any_args).exactly(1)
618         SheetPanel.stub(:serial?).with(any_args).and_return(true)
619         r = SheetPanel.validate_t @item
620       end
621     end
622     #実データでチェック
623     #依頼チェックだけでは不安なので最低限のチェックを
624     context '新規のとき' do
625       it '一件だけで正常通過している' do
626         @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
627         r = SheetPanel.validate_t @item
628         r.should be_true 
629       end
630     end
631     context '既存のとき' do
632       it '2件目を作っても正常通過している' do
633         @item = FactoryGirl.create :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
634         @item2 = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
635         r = SheetPanel.validate_t @item2
636         r.should be_true 
637       end
638     end
639   end
640   describe '挿入シフトに於いて' do
641     before do
642       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
643       @panel = FactoryGirl.create :panel, :author_id => @author.id
644     end
645     context '依頼チェック' do
646       #テーブルが空で0に挿入
647       it 'Updateを依頼している' do
648         SheetPanel.stub(:update_all).with(any_args)
649         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
650         @item = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
651         @item.insert_shift
652       end
653     end
654     context 'テーブルに1件(t:0)で0に挿入したとき' do
655       before do
656         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
657         @item2 = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
658       end
659       it '既存の行を1にシフトしている' do
660         @item2.insert_shift
661         l = SheetPanel.find :all
662         l.first.t.should eq 1
663       end
664       it 'これから挿入するt(0)が欠番になっている' do
665         @item2.insert_shift
666         l = SheetPanel.find(:all).map {|s| s.t }
667         l.include?(0).should_not be_true
668       end
669     end
670     context 'テーブルに2件(t:0,1)で1に挿入したとき' do
671       before do
672         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
673         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
674         @item3 = FactoryGirl.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
675       end
676       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
677         @item3.insert_shift
678         l = SheetPanel.find(:all).map {|s| s.t }
679         l.sort.should eq [0, 2]
680       end
681     end
682     context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
683       before do
684         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
685         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
686         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
687         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
688         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
689         @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
690       end
691       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
692         @item6.insert_shift
693         l = SheetPanel.find(:all).map {|s| s.t }
694         l.sort.should eq [0, 1, 3, 4, 5]
695       end
696     end
697     context '先ほどのケース+他の用紙1件で挿入したとき' do
698       before do
699         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
700         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
701         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
702         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
703         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
704         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
705         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
706         @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
707       end
708       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
709         @item6.insert_shift
710         l = SheetPanel.find(:all, :conditions => ['sheet_id = ?', @sheet.id]).map {|s| s.t }
711         l.sort.should eq [0, 1, 3, 4, 5]
712       end
713       it '他の用紙に影響がない' do
714         ot = @itemc2.t
715         @item6.insert_shift
716         @itemc2.reload
717         @itemc2.t.should eq ot
718       end
719     end
720   end
721   describe '少ない方に移動に於いて' do
722     before do
723       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
724       @panel = FactoryGirl.create :panel, :author_id => @author.id
725     end
726     context '依頼チェック' do
727       it 'Updateを依頼している' do
728         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
729         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
730         SheetPanel.stub(:update_all).with(any_args)
731         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
732         ot = @item2.t
733         @item2.t = 0
734         @item2.lesser_shift ot
735       end
736     end
737     context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
738       before do
739         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
740         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
741         @ot = @item2.t
742         @item2.t = 0
743       end
744       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
745         #移動させたい行はそのまま残る
746         @item2.lesser_shift @ot
747         l = SheetPanel.find(:all).map {|s| s.t }
748         l.sort.should eq [1, 1]
749       end
750       it '既存のt0を1にシフトしている' do
751         @item2.lesser_shift @ot
752         @item.reload
753         @item.t.should eq 1
754       end
755     end
756     context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
757       before do
758         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
759         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
760         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
761         @ot = @item3.t
762         @item3.t = 1
763       end
764       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
765         #移動させたい行はそのまま残る
766         @item3.lesser_shift @ot
767         l = SheetPanel.find(:all).map {|s| s.t }
768         l.sort.should eq [0, 2, 2]
769       end
770       it '既存のt1を2にシフトしている' do
771         @item3.lesser_shift @ot
772         @item2.reload
773         @item2.t.should eq 2
774       end
775     end
776     context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
777       before do
778         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
779         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
780         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
781         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
782         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
783         @ot = @item4.t
784         @item4.t = 1
785       end
786       it 'これから挿入するt(1)が欠番になっている' do
787         #移動させたい行はそのまま残る
788         @item4.lesser_shift @ot
789         l = SheetPanel.find(:all).map {|s| s.t }
790         l.sort.should eq [0, 2, 3, 3, 4]
791       end
792       it '既存のt0には変化がない' do
793         @item4.lesser_shift @ot
794         @item.reload
795         @item.t.should eq 0
796       end
797       it '既存のt4には変化がない' do
798         @item4.lesser_shift @ot
799         @item5.reload
800         @item5.t.should eq 4
801       end
802       it '既存のt1を2にシフトしている' do
803         @item4.lesser_shift @ot
804         @item2.reload
805         @item2.t.should eq 2
806       end
807       it '既存のt2を3にシフトしている' do
808         @item4.lesser_shift @ot
809         @item3.reload
810         @item3.t.should eq 3
811       end
812     end
813     context '先ほどのケース+他の用紙1件で挿入したとき' do
814       before do
815         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
816         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
817         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
818         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
819         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
820         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
821         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
822         @ot = @item4.t
823         @item4.t = 1
824       end
825       it 'これから挿入するt(1)が欠番になっている' do
826         @item4.lesser_shift @ot
827         l = SheetPanel.find(:all).map {|s| s.t }
828         l.sort.should eq [0, 0, 2, 3, 3, 4]
829       end
830       it '既存のt0には変化がない' do
831         @item4.lesser_shift @ot
832         @item.reload
833         @item.t.should eq 0
834       end
835       it '既存のt4には変化がない' do
836         @item4.lesser_shift @ot
837         @item5.reload
838         @item5.t.should eq 4
839       end
840       it '既存のt1を2にシフトしている' do
841         @item4.lesser_shift @ot
842         @item2.reload
843         @item2.t.should eq 2
844       end
845       it '既存のt2を3にシフトしている' do
846         @item4.lesser_shift @ot
847         @item3.reload
848         @item3.t.should eq 3
849       end
850       it '他の用紙に影響がない' do
851         @item4.lesser_shift @ot
852         @itemc2.reload
853         @itemc2.t.should eq 0
854       end
855     end
856     #例外ケース。
857     #負のときは0として正常扱い
858     context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
859       before do
860         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
861         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
862         @ot = @item2.t
863         @item2.t = -1
864       end
865       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
866         #移動させたい行はそのまま残る
867         @item2.lesser_shift @ot
868         l = SheetPanel.find(:all).map {|s| s.t }
869         l.sort.should eq [1, 1]
870       end
871       it '既存のt0を1にシフトしている' do
872         @item2.lesser_shift @ot
873         @item.reload
874         @item.t.should eq 1
875       end
876       it '既存のt1は0に補正されている' do
877         @item2.lesser_shift @ot
878         @item2.t.should eq 0
879       end
880     end
881   end
882   describe '大きい方に移動に於いて' do
883     before do
884       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
885       @panel = FactoryGirl.create :panel, :author_id => @author.id
886     end
887     context '依頼チェック' do
888       it 'Updateを依頼している' do
889         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
890         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
891         SheetPanel.stub(:update_all).with(any_args)
892         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
893         ot = @item.t
894         @item.t = 1
895         @item.higher_shift ot
896       end
897     end
898     context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
899       before do
900         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
901         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
902         @ot = @item.t
903         @item.t = 1
904       end
905       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
906         #移動させたい行はそのまま残る
907         @item.higher_shift @ot
908         l = SheetPanel.find(:all).map {|s| s.t }
909         l.sort.should eq [0, 0]
910       end
911       it '既存のt1を0にシフトしている' do
912         @item.higher_shift @ot
913         @item2.reload
914         @item2.t.should eq 0
915       end
916     end
917     context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
918       before do
919         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
920         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
921         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
922         @ot = @item.t
923         @item.t = 1
924       end
925       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
926         #移動させたい行はそのまま残る
927         @item.higher_shift @ot
928         l = SheetPanel.find(:all).map {|s| s.t }
929         l.sort.should eq [0, 0, 2]
930       end
931       it '既存のt1を0にシフトしている' do
932         @item.higher_shift @ot
933         @item2.reload
934         @item2.t.should eq 0
935       end
936     end
937     context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
938       before do
939         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
940         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
941         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
942         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
943         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
944         @ot = @item2.t
945         @item2.t = 3
946       end
947       it 'これから挿入するt(3)が欠番になっている' do
948         #移動させたい行はそのまま残る
949         @item2.higher_shift @ot
950         l = SheetPanel.find(:all).map {|s| s.t }
951         l.sort.should eq [0, 1, 1, 2, 4]
952       end
953       it '既存のt0には変化がない' do
954         @item2.higher_shift @ot
955         @item.reload
956         @item.t.should eq 0
957       end
958       it '既存のt4には変化がない' do
959         @item2.higher_shift @ot
960         @item5.reload
961         @item5.t.should eq 4
962       end
963       it '既存のt2を1にシフトしている' do
964         @item2.higher_shift @ot
965         @item3.reload
966         @item3.t.should eq 1
967       end
968       it '既存のt3を2にシフトしている' do
969         @item2.higher_shift @ot
970         @item4.reload
971         @item4.t.should eq 2
972       end
973     end
974     context '先ほどのケース+他の用紙1件で挿入したとき' do
975       before do
976         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
977         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
978         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
979         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
980         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
981         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
982         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
983         @ot = @item2.t
984         @item2.t = 3
985       end
986       it 'これから挿入するt(3)が欠番になっている' do
987         #移動させたい行はそのまま残る
988         @item2.higher_shift @ot
989         l = SheetPanel.find(:all).map {|s| s.t }
990         l.sort.should eq [0, 0, 1, 1, 2, 4]
991       end
992       it '既存のt0には変化がない' do
993         @item2.higher_shift @ot
994         @item.reload
995         @item.t.should eq 0
996       end
997       it '既存のt4には変化がない' do
998         @item2.higher_shift @ot
999         @item5.reload
1000         @item5.t.should eq 4
1001       end
1002       it '既存のt2を1にシフトしている' do
1003         @item2.higher_shift @ot
1004         @item3.reload
1005         @item3.t.should eq 1
1006       end
1007       it '既存のt3を2にシフトしている' do
1008         @item2.higher_shift @ot
1009         @item4.reload
1010         @item4.t.should eq 2
1011       end
1012       it '他の用紙に影響がない' do
1013         @item2.higher_shift @ot
1014         @itemc2.reload
1015         @itemc2.t.should eq 0
1016       end
1017     end
1018     #例外ケース。
1019     #max超えたときはmaxとして正常扱い
1020     context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
1021       before do
1022         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1023         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1024         @ot = @item.t
1025         @item.t = 2
1026       end
1027       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1028         #移動させたい行はそのまま残る
1029         @item.higher_shift @ot
1030         l = SheetPanel.find(:all).map {|s| s.t }
1031         l.sort.should eq [0, 0]
1032       end
1033       it '既存のt1を0にシフトしている' do
1034         @item.higher_shift @ot
1035         @item2.reload
1036         @item2.t.should eq 0
1037       end
1038       it '既存のt0は1に補正されている' do
1039         @item.higher_shift @ot
1040         @item.t.should eq 1
1041       end
1042     end
1043   end
1044   describe '入れ替えに於いて' do
1045     before do
1046       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1047       @panel = FactoryGirl.create :panel, :author_id => @author.id
1048       @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1049       @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1050     end
1051     context '新tが旧tより小さいとき' do
1052       it '少ない方に移動を依頼している' do
1053         SheetPanel.any_instance.stub(:lesser_shift).with(any_args)
1054         SheetPanel.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
1055         ot = @item2.t
1056         @item2.t = 0
1057         @item2.update_shift ot
1058       end
1059     end
1060     context '新tが旧tより大きいとき' do
1061       it '大きい方に移動を依頼している' do
1062         SheetPanel.any_instance.stub(:higher_shift).with(any_args)
1063         SheetPanel.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
1064         ot = @item.t
1065         @item.t = 1
1066         @item.update_shift ot
1067       end
1068     end
1069   end
1070   describe '順序入れ替えに於いて' do
1071     before do
1072       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1073       @panel = FactoryGirl.create :panel, :author_id => @author.id
1074     end
1075     context 'オブジェクトが新規でtが空のとき' do
1076       it '末尾追加としてtを補充依頼している' do
1077         @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1078         SheetPanel.stub(:new_t).with(any_args).and_return(0)
1079         SheetPanel.should_receive(:new_t).with(any_args).exactly(1)
1080         @item.t = nil
1081         r = @item.rotate
1082       end
1083     end
1084     context 'オブジェクトが新規でtが設定されているとき' do
1085       it '挿入追加として挿入シフトを依頼している' do
1086         @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1087         SheetPanel.any_instance.stub(:insert_shift).with(any_args)
1088         SheetPanel.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
1089         @item.t = 0
1090         r = @item.rotate
1091       end
1092     end
1093     context 'オブジェクトが新規でなくtが設定されているとき' do
1094       it 'コマ移動として入れ替えを依頼している' do
1095         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1096         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1097         SheetPanel.any_instance.stub(:update_shift).with(any_args)
1098         SheetPanel.any_instance.should_receive(:update_shift).with(1).exactly(1)
1099         @item2.t = 0
1100         r = @item.rotate 1
1101       end
1102     end
1103     context 'オブジェクトが新規でなくtが空のとき' do
1104       it '入れ替えもシフトもせず、tを空のままにしている' do
1105         #結果、tに欠番が生じてシリアライズチェックでひっかかる
1106       end
1107     end
1108   end
1109   describe '編集許可に於いて' do
1110     before do
1111       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1112       @panel = FactoryGirl.create :panel, :author_id => @author.id
1113       @item = FactoryGirl.build :sheet_panel, :t => nil, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1114     end
1115     context 'つつがなく終わるとき' do
1116       it 'trueを返す' do
1117         r = @item.allow?
1118         r.should be_true
1119       end
1120     end
1121     context '用紙で引っかかるとき' do
1122       it 'falseを返す' do
1123         Panel.any_instance.stub(:usable?).with(any_args).and_return(true)
1124         Sheet.any_instance.stub(:own?).with(any_args).and_return(false)
1125         r = @item.allow?
1126         r.should be_false
1127       end
1128     end
1129     context 'コマで引っかかるとき' do
1130       it 'falseを返す' do
1131         Sheet.any_instance.stub(:own?).with(any_args).and_return(true)
1132         Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
1133         r = @item.allow?
1134         r.should be_false
1135       end
1136     end
1137     context '用紙またはコマが指定されていなかったとき' do
1138       it 'nilを返す' do
1139         Sheet.any_instance.stub(:own?).with(any_args).and_return(true)
1140         @item.panel_id = nil
1141         r = @item.allow?
1142         r.should eq nil
1143       end
1144       it 'nilを返す' do
1145         Panel.any_instance.stub(:usable?).with(any_args).and_return(true)
1146         @item.sheet_id = nil
1147         r = @item.allow?
1148         r.should eq nil
1149       end
1150     end
1151   end
1152   describe '保存に於いて' do
1153     before do
1154       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1155       @panel = FactoryGirl.create :panel, :author_id => @author.id
1156       @item = FactoryGirl.build :sheet_panel, :t => nil, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1157     end
1158     context 'つつがなく終わるとき' do
1159       it '編集許可チェックを依頼している' do
1160         SheetPanel.any_instance.stub(:allow?).with(any_args).and_return(true)
1161         SheetPanel.any_instance.should_receive(:allow?).with(any_args).exactly(1)
1162         r = @item.store
1163       end
1164       it '順序入れ替えを依頼している' do
1165         SheetPanel.any_instance.stub(:rotate).with(any_args).and_return(0)
1166         SheetPanel.any_instance.should_receive(:rotate).with(any_args).exactly(1)
1167         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
1168         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
1169         r = @item.store 
1170       end
1171       it '保存を依頼している' do
1172         SheetPanel.stub(:new_t).with(any_args).and_return(0)
1173         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
1174         SheetPanel.any_instance.should_receive(:save).with(any_args).exactly(1)
1175         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
1176         r = @item.store
1177       end
1178       it 'tのシリアライズチェックを依頼している' do
1179         SheetPanel.stub(:new_t).with(any_args).and_return(0)
1180         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
1181         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
1182         SheetPanel.should_receive(:validate_t).with(any_args).exactly(1)
1183         r = @item.store
1184       end
1185     end
1186     #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1187     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したとき' do
1188       before do
1189         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1190         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1191         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1192         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1193         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1194         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1195         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1196         @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1197       end
1198       it '既存のt0には変化がない' do
1199         @item6.store
1200         @item.reload
1201         @item.t.should eq 0
1202       end
1203       it '既存のt1には変化がない' do
1204         @item6.store
1205         @item2.reload
1206         @item2.t.should eq 1
1207       end
1208       it '既存のt2を3にシフトしている' do
1209         @item6.store
1210         @item3.reload
1211         @item3.t.should eq 3
1212       end
1213       it '既存のt3を4にシフトしている' do
1214         @item6.store
1215         @item4.reload
1216         @item4.t.should eq 4
1217       end
1218       it '既存のt5を5にシフトしている' do
1219         @item6.store
1220         @item5.reload
1221         @item5.t.should eq 5
1222       end
1223       it '新規のt2が作成されている' do
1224         @item6.store
1225         @item6.reload
1226         @item6.t.should eq 2
1227       end
1228       it '他の用紙に影響がない' do
1229         @ot = @itemc2.t
1230         @item6.store
1231         @itemc2.reload
1232         @itemc2.t.should eq @ot
1233       end
1234     end
1235     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したとき' do
1236       before do
1237         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1238         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1239         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1240         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1241         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1242         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1243         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1244         @ot = @item4.t
1245         @item4.t = 1
1246       end
1247       it '既存のt0には変化がない' do
1248         @item4.store @ot
1249         @item.reload
1250         @item.t.should eq 0
1251       end
1252       it '既存のt4には変化がない' do
1253         @item4.store @ot
1254         @item5.reload
1255         @item5.t.should eq 4
1256       end
1257       it '既存のt1を2にシフトしている' do
1258         @item4.store @ot
1259         @item2.reload
1260         @item2.t.should eq 2
1261       end
1262       it '既存のt2を3にシフトしている' do
1263         @item4.store @ot
1264         @item3.reload
1265         @item3.t.should eq 3
1266       end
1267       it '既存のt3を1にシフトしている' do
1268         @item4.store @ot
1269         @item4.reload
1270         @item4.t.should eq 1
1271       end
1272       it '他の用紙に影響がない' do
1273         @item4.store @ot
1274         @itemc2.reload
1275         @itemc2.t.should eq 0
1276       end
1277     end
1278     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で1を3に移動したとき' do
1279       before do
1280         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1281         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1282         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1283         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1284         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1285         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1286         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1287         @ot = @item2.t
1288         @item2.t = 3
1289       end
1290       it '既存のt0には変化がない' do
1291         @item2.store @ot
1292         @item.reload
1293         @item.t.should eq 0
1294       end
1295       it '既存のt4には変化がない' do
1296         @item2.store @ot
1297         @item5.reload
1298         @item5.t.should eq 4
1299       end
1300       it '既存のt1を3にシフトしている' do
1301         @item2.store @ot
1302         @item2.reload
1303         @item2.t.should eq 3
1304       end
1305       it '既存のt2を1にシフトしている' do
1306         @item2.store @ot
1307         @item3.reload
1308         @item3.t.should eq 1
1309       end
1310       it '既存のt3を2にシフトしている' do
1311         @item2.store @ot
1312         @item4.reload
1313         @item4.t.should eq 2
1314       end
1315       it '他の用紙に影響がない' do
1316         @item2.store @ot
1317         @itemc2.reload
1318         @itemc2.t.should eq 0
1319       end
1320     end
1321     #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1322     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したが保存に失敗したとき' do
1323       before do
1324         SheetPanel.any_instance.stub(:save).with(any_args).and_return(false)
1325         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1326         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1327         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1328         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1329         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1330         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1331         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1332         @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1333       end
1334       it '既存のtに変化がない' do
1335         @item6.store
1336         @item.reload
1337         @item.t.should eq 0
1338         @item2.reload
1339         @item2.t.should eq 1
1340         @item3.reload
1341         @item3.t.should eq 2
1342         @item4.reload
1343         @item4.t.should eq 3
1344         @item5.reload
1345         @item5.t.should eq 4
1346         @itemc2.reload
1347         @itemc2.t.should eq 0
1348       end
1349       it 'falseを返す' do
1350         r = @item6.store
1351         r.should be_false
1352       end
1353     end
1354     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したがシリアルチェックに失敗したとき' do
1355       before do
1356         SheetPanel.stub(:validate_t).with(any_args).and_return(false)
1357         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1358         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1359         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1360         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1361         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1362         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1363         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1364         @ot = @item4.t
1365         @item4.t = 1
1366       end
1367       it '既存のtに変化がない' do
1368         @item4.store @ot
1369         @item.reload
1370         @item.t.should eq 0
1371         @item2.reload
1372         @item2.t.should eq 1
1373         @item3.reload
1374         @item3.t.should eq 2
1375         @item4.reload
1376         @item4.t.should eq 3
1377         @item5.reload
1378         @item5.t.should eq 4
1379         @itemc2.reload
1380         @itemc2.t.should eq 0
1381       end
1382       it 'falseを返す' do
1383         r = @item4.store @ot
1384         r.should be_false
1385       end
1386       it 'tにエラーメッセージが入っている' do
1387         @item4.store @ot
1388         @item4.errors[:t].should_not be_empty
1389         @item4.valid?.should be_true
1390       end
1391     end
1392     context '編集不可だったとき' do
1393       before do
1394         @item = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1395         SheetPanel.any_instance.stub(:allow?).and_return(false)
1396       end
1397       it '403Forbidden例外を返す' do
1398         lambda{
1399           @item.store
1400         }.should raise_error(ActiveRecord::Forbidden)
1401       end
1402     end
1403   end
1404   describe '切り詰め処理つき削除に於いて' do
1405     before do
1406       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1407       @panel = FactoryGirl.create :panel, :author_id => @author.id
1408       @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1409     end
1410     context 'つつがなく終わるとき' do
1411       it '削除される' do
1412         lambda{
1413           @item.destroy_and_shorten
1414         }.should change(SheetPanel, :count ).by(-1)
1415       end
1416       it 'Trueを返す' do
1417         r = @item.destroy_and_shorten
1418         r.should be_true 
1419       end
1420     end
1421     context '削除に失敗したとき' do
1422       before do
1423         SheetPanel.any_instance.stub(:destroy).and_return(false)
1424       end
1425       it 'ロールバックされる' do
1426         lambda{
1427           @item.destroy_and_shorten
1428         }.should_not change(SheetPanel, :count )
1429       end
1430       it 'Falseを返す' do
1431         r = @item.destroy_and_shorten
1432         r.should be_false
1433       end
1434     end
1435     #連携テスト。切り詰めが直接DBをいじる
1436     context '2件で先頭を削除したとき' do
1437       before do
1438         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1439       end
1440       it '行が削除される' do
1441         lambda{
1442           @item.destroy_and_shorten
1443         }.should change(SheetPanel, :count ).by(-1)
1444       end
1445       it '先頭は削除される' do
1446         @item.destroy_and_shorten
1447         lambda{
1448           SheetPanel.find @item.id
1449         }.should raise_error(ActiveRecord::RecordNotFound)
1450       end
1451       it '2件目は前に詰められる' do
1452         @item.destroy_and_shorten
1453         @item2.reload
1454         @item2.t.should eq 0
1455       end
1456     end
1457     context '3件で先頭を削除したとき' do
1458       before do
1459         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1460         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1461       end
1462       it '行が削除される' do
1463         lambda{
1464           @item.destroy_and_shorten
1465         }.should change(SheetPanel, :count ).by(-1)
1466       end
1467       it '先頭は削除される' do
1468         @item.destroy_and_shorten
1469         lambda{
1470           SheetPanel.find @item.id
1471         }.should raise_error(ActiveRecord::RecordNotFound)
1472       end
1473       it '2件目は前に詰められる' do
1474         @item.destroy_and_shorten
1475         @item2.reload
1476         @item2.t.should eq 0
1477       end
1478       it '3件目は前に詰められる' do
1479         @item.destroy_and_shorten
1480         @item3.reload
1481         @item3.t.should eq 1
1482       end
1483     end
1484     context '5件で3件目を削除したとき' do
1485       before do
1486         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1487         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1488         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1489         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1490       end
1491       it '行が削除される' do
1492         lambda{
1493           @item3.destroy_and_shorten
1494         }.should change(SheetPanel, :count ).by(-1)
1495       end
1496       it '1件目は変化がない' do
1497         @item3.destroy_and_shorten
1498         @item.reload
1499         @item.t.should eq 0
1500       end
1501       it '2件目は変化がない' do
1502         @item3.destroy_and_shorten
1503         @item2.reload
1504         @item2.t.should eq 1
1505       end
1506       it '3件目は削除される' do
1507         @item3.destroy_and_shorten
1508         lambda{
1509           SheetPanel.find @item3.id
1510         }.should raise_error(ActiveRecord::RecordNotFound)
1511       end
1512       it '4件目は前に詰められる' do
1513         @item3.destroy_and_shorten
1514         @item4.reload
1515         @item4.t.should eq 2
1516       end
1517       it '5件目は前に詰められる' do
1518         @item3.destroy_and_shorten
1519         @item5.reload
1520         @item5.t.should eq 3
1521       end
1522     end
1523     #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1524   end
1525 end