OSDN Git Service

t#32046:
[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     SpeechBalloonTemplate.delete_all
7     @admin = FactoryGirl.create :admin
8     @sp = FactoryGirl.create :system_picture
9     @lg = FactoryGirl.create :license_group
10     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     SpeechBalloonTemplate.delete_all
12     @speech_balloon_template = FactoryGirl.create(:speech_balloon_template, "name" => "circle@pettan.com", "classname" => "CircleSpeechBalloon", "caption" => "cc",  "system_picture_id" => @sp.id, "settings" => '{}')
13     @writing_format = FactoryGirl.create :writing_format
14     @user = FactoryGirl.create( :user_yas)
15     @author = FactoryGirl.create :author, :user_id => @user.id
16     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
17     @other_user = FactoryGirl.create( :user_yas)
18     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
19     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
20     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
21     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
22   end
23   
24   describe '検証に於いて' do
25     before do
26       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
27       @panel = FactoryGirl.create :panel, :author_id => @author.id
28       @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
29     end
30     
31     context 'オーソドックスなデータのとき' do
32       it '下限データが通る' do
33         @sheet_panel.x = -99999
34         @sheet_panel.y = -99999
35         @sheet_panel.z = 1
36         @sheet_panel.t = 0
37         @sheet_panel.should be_valid
38       end
39       it '上限データが通る' do
40         @sheet_panel.x = 99999
41         @sheet_panel.y = 99999
42         @sheet_panel.z = 99999
43         @sheet_panel.t = 99999
44         @sheet_panel.should be_valid
45       end
46     end
47     
48     context 'sheet_idを検証するとき' do
49       it 'nullなら失敗する' do
50         @sheet_panel.sheet_id = nil
51         @sheet_panel.should_not be_valid
52       end
53       it '数値でなければ失敗する' do
54         @sheet_panel.sheet_id = 'a'
55         @sheet_panel.should_not be_valid
56       end
57       it '存在する用紙でなければ失敗する' do
58         @sheet_panel.sheet_id = 0
59         @sheet_panel.should_not be_valid
60       end
61     end
62     
63     context 'panel_idを検証するとき' do
64       it 'nullなら失敗する' do
65         @sheet_panel.panel_id = nil
66         @sheet_panel.should_not be_valid
67       end
68       it '数値でなければ失敗する' do
69         @sheet_panel.panel_id = 'a'
70         @sheet_panel.should_not be_valid
71       end
72       it '存在するコマでなければ失敗する' do
73         @sheet_panel.panel_id = 0
74         @sheet_panel.should_not be_valid
75       end
76     end
77     
78     context 'xを検証するとき' do
79       it 'nullなら失敗する' do
80         @sheet_panel.x = nil
81         @sheet_panel.should_not be_valid
82       end
83       it '数値でなければ失敗する' do
84         @sheet_panel.x = 'a'
85         @sheet_panel.should_not be_valid
86       end
87       it '0なら通る' do
88         @sheet_panel.x = '0'
89         @sheet_panel.should be_valid
90       end
91       it '負でも通る' do
92         @sheet_panel.x = -1
93         @sheet_panel.should be_valid
94       end
95     end
96     context 'yを検証するとき' do
97       it 'nullなら失敗する' do
98         @sheet_panel.y = nil
99         @sheet_panel.should_not be_valid
100       end
101       it '数値でなければ失敗する' do
102         @sheet_panel.y = 'a'
103         @sheet_panel.should_not be_valid
104       end
105       it '0なら通る' do
106         @sheet_panel.y = '0'
107         @sheet_panel.should be_valid
108       end
109       it '負でも通る' do
110         @sheet_panel.y = -1
111         @sheet_panel.should be_valid
112       end
113     end
114     context 'zを検証するとき' do
115       it 'nullなら失敗する' do
116         @sheet_panel.z = nil
117         @sheet_panel.should_not be_valid
118       end
119       it '数値でなければ失敗する' do
120         @sheet_panel.z = 'a'
121         @sheet_panel.should_not be_valid
122       end
123       it '負なら失敗する' do
124         @sheet_panel.z = -1
125         @sheet_panel.should_not be_valid
126       end
127       it '0なら失敗する' do
128         @sheet_panel.z = 0
129         @sheet_panel.should_not be_valid
130       end
131     end
132     context 'tを検証するとき' do
133       it 'nullなら失敗する' do
134         @sheet_panel.t = nil
135         @sheet_panel.should_not be_valid
136       end
137       it '数値でなければ失敗する' do
138         @sheet_panel.t = 'a'
139         @sheet_panel.should_not be_valid
140       end
141       it '負なら失敗する' do
142         @sheet_panel.t = -1
143         @sheet_panel.should_not be_valid
144       end
145     end
146     
147     context 'author_idを検証するとき' do
148       it 'nullなら失敗する' do
149         @sheet_panel.author_id = nil
150         @sheet_panel.should_not be_valid
151       end
152       it '数値でなければ失敗する' do
153         @sheet_panel.author_id = 'a'
154         @sheet_panel.should_not be_valid
155       end
156       it '存在する作家でなければ失敗する' do
157         @sheet_panel.author_id = 0
158         @sheet_panel.should_not be_valid
159       end
160     end
161     context '全体を検証するとき' do
162     end
163   end
164   
165   describe 'デフォルト値補充に於いて' do
166     before do
167       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
168       @panel = FactoryGirl.create :panel, :author_id => @author.id
169     end
170     
171     #dbのデフォルト値が0だから明示的にnilにしないと追加ができない
172     it 'tをnilにする' do
173       @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id
174       @sheet_panel.supply_default
175       @sheet_panel.t.should be_nil
176     end
177     
178   end
179   
180   describe '上書き補充に於いて' do
181     before do
182       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
183       @panel = FactoryGirl.create :panel, :author_id => @author.id
184     end
185     
186     context 'author_idを補充' do
187       it '問答無用でauthor_idを補充する' do
188         @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id
189         @sheet_panel.author_id = nil
190         @sheet_panel.overwrite @author
191         @sheet_panel.author_id.should eq @author.id
192       end
193     end
194     
195   end
196   
197   describe '所持判定に於いて' do
198     before do
199       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
200       @panel = FactoryGirl.create :panel, :author_id => @author.id
201       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
202       @sheeto = FactoryGirl.create :sheet, :author_id => @other_author.id
203       @panelo = FactoryGirl.create :panel, :author_id => @other_author.id
204       @sheet_panelo = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @sheeto.id, :panel_id => @panelo.id
205     end
206     context '事前チェックする' do
207       it '自身にロールリストからの作家取得を依頼している' do
208         SheetPanel.should_receive(:get_author_from_roles).with(any_args).exactly(1)
209         r = @sheet_panel.own?([@author])
210       end
211     end
212     context 'ロール内作家が取得できるとき' do
213       before do
214       end
215       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
216         SheetPanel.stub(:get_author_from_roles).with(any_args).and_return(@author)
217         r = @sheet_panel.own?([@author])
218         r.should be_true
219       end
220       it 'ロール内作家のidが自身の作家idと一致しないならno' do
221         SheetPanel.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
222         @sheet_panel.own?(@other_author).should be_false
223       end
224     end
225     context 'ロール内作家が取得できないとき' do
226       before do
227         SheetPanel.stub(:get_author_from_roles).with(any_args).and_return(nil)
228       end
229       it 'Falseを返す' do
230         r = @sheet_panel.own?([@author])
231         r.should be_false
232       end
233     end
234   end
235   
236   describe '閲覧許可に於いて' do
237     before do
238       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
239       @panel = FactoryGirl.create :panel, :author_id => @author.id
240       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
241     end
242     context 'オープンモードのとき' do
243       before do
244         MagicNumber['run_mode'] = 0
245       end
246       it '自身にゲスト用ロールチェックを問い合わせしている' do
247         SheetPanel.any_instance.stub(:guest_role_check).and_return(true)
248         SheetPanel.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
249         r = @sheet_panel.visible?([@author])
250       end
251       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
252         SheetPanel.any_instance.stub(:guest_role_check).and_return(false)
253         r = @sheet_panel.visible?([@author])
254         r.should be_false
255       end
256     end
257     context 'クローズドモードのとき' do
258       before do
259         MagicNumber['run_mode'] = 1
260       end
261       it '自身に読者用ロールチェックを問い合わせしている' do
262         SheetPanel.any_instance.stub(:reader_role_check).and_return(true)
263         SheetPanel.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
264         r = @sheet_panel.visible?([@author])
265       end
266       it '読者用ロールチェックが失敗したとき、falseを返す' do
267         SheetPanel.any_instance.stub(:reader_role_check).and_return(false)
268         r = @sheet_panel.visible?([@author])
269         r.should be_false
270       end
271     end
272     context '事前チェックする' do
273       before do
274         MagicNumber['run_mode'] = 1
275         SheetPanel.any_instance.stub(:reader_role_check).and_return(true)
276       end
277       it '自身の用紙に所持判定を問い合わせしている' do
278         Sheet.any_instance.stub(:own?).and_return(true)
279         Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
280         r = @sheet_panel.visible?([@author])
281       end
282       it '自身の用紙に閲覧許可を問い合わせしている' do
283         Sheet.any_instance.stub(:own?).and_return(false)
284         Sheet.any_instance.stub(:visible?).and_return(true)
285         Sheet.any_instance.should_receive(:visible?).with(any_args).exactly(1)
286         r = @sheet_panel.visible?([@author])
287       end
288     end
289     context 'つつがなく終わるとき' do
290       before do
291         MagicNumber['run_mode'] = 1
292         SheetPanel.any_instance.stub(:reader_role_check).and_return(true)
293       end
294       it '自分の用紙の紙コマなら許可する' do
295         Sheet.any_instance.stub(:own?).and_return(true)
296         Sheet.any_instance.stub(:visible).and_return(0)
297         r = @sheet_panel.visible?([@author])
298         r.should be_true
299       end
300       it '他人の非公開用紙の紙コマなら許可しない' do
301         Sheet.any_instance.stub(:own?).and_return(false)
302         Sheet.any_instance.stub(:visible).and_return(0)
303         r = @sheet_panel.visible?([@author])
304         r.should be_false
305       end
306       it '他人の用紙の紙コマでも公開なら許可する' do
307         Sheet.any_instance.stub(:own?).and_return(false)
308         Sheet.any_instance.stub(:visible).and_return(1)
309         r = @sheet_panel.visible?([@author])
310         r.should be_true
311       end
312     end
313   end
314   
315   describe 'プレイリスト取得に於いて' do
316     before do
317       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
318       @panel = FactoryGirl.create :panel, :author_id => @author.id
319       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
320       @panel2 = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
321       @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
322       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
323     end
324     context 'offset補正について' do
325       it '文字列から数値に変換される' do
326         SheetPanel.offset(100, '8').should eq 8
327       end
328       it 'nilの場合は0になる' do
329         SheetPanel.offset(100).should eq 0
330       end
331       #投稿されたコマ数以上の値が指定されたときは、最後のコマだけになる
332       #最後のコマとは、コマ数‐1.
333       it '1件のときオフセット1なら0になる' do
334         SheetPanel.offset(1, '1').should eq 0
335       end
336       it '5件のときオフセット5なら4になる' do
337         SheetPanel.offset(5, '5').should eq 4
338       end
339       # 負の値が指定されたときは、最後のコマから数えてコマを飛ばして表示する。
340       #-4のときは、最後から4つのコマを表示する。 
341       it '2件のときオフセット-1なら1になる' do
342         SheetPanel.offset(2, '-1').should eq 1
343       end
344       it '5件のときオフセット-2なら3になる' do
345         SheetPanel.offset(5, '-2').should eq 3
346       end
347       # 最終的なが負になるなど、不正な値が入ったときは0となる。 
348       it '2件のときオフセット-5なら0になる' do
349         SheetPanel.offset(2, '-5').should eq 0
350       end
351     end
352     context 'panel_count補正について' do
353       it '文字列から数値に変換される' do
354         SheetPanel.panel_count(100, '7').should eq 7
355       end
356       it 'nilの場合はSheetPanel.default_panel_sizeになる' do
357         SheetPanel.panel_count(100).should eq SheetPanel.default_panel_size
358       end
359       it '0以下の場合はSheetPanel.default_panel_sizeになる' do
360         SheetPanel.panel_count(100, '0').should eq SheetPanel.default_panel_size
361       end
362       it 'SheetPanel.max_panel_sizeを超えた場合はSheetPanel.max_panel_sizeになる' do
363         SheetPanel.panel_count(100, '1000').should eq SheetPanel.max_panel_size
364       end
365     end
366     it 'リストを返す' do
367       c = SheetPanel.play_list @sheet, @author
368       c.should eq [@sheet_panel]
369     end
370     it 't順で並んでいる' do
371       #公開用紙の公開コマは(他人の用紙であっても)含んでいる
372       v = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1
373       c = SheetPanel.play_list @sheet, @author
374       c.should eq [ @sheet_panel, v]
375     end
376     it '非公開のコマは含んでいる' do
377       h = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel2.id, :t => 1
378       c = SheetPanel.play_list @sheet, @author
379       c.should eq [ @sheet_panel, h]
380     end
381     context 'DBに5件あって1ページの件数を2件に変えたとして' do
382       before do
383         @sheet_panel2 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1
384         @sheet_panel3 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 2
385         @sheet_panel4 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 3
386         @sheet_panel5 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 4
387       end
388       it 'offset=0なら末尾2件を返す' do
389         #時系列で並んでいる
390         c = SheetPanel.play_list( @sheet, @author, 0, 2)
391         c.should eq [@sheet_panel, @sheet_panel2]
392       end
393       it 'offset=2なら中間2件を返す' do
394         c = SheetPanel.play_list(@sheet, @author, 2, 2)
395         c.should eq [@sheet_panel3, @sheet_panel4]
396       end
397       it 'offset=4なら先頭1件を返す' do
398         c = SheetPanel.play_list(@sheet, @author, 4, 2)
399         c.should eq [@sheet_panel5]
400       end
401     end
402   end
403   
404   describe '一覧取得に於いて' do
405     before do
406       @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
407       @panel = FactoryGirl.create :panel, :author_id => @author.id
408       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
409       @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
410       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
411       @hidden_sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0
412     end
413     context 'page補正について' do
414       it '文字列から数値に変換される' do
415         SheetPanel.page('8').should eq 8
416       end
417       it 'nilの場合は1になる' do
418         SheetPanel.page().should eq 1
419       end
420       it '0以下の場合は1になる' do
421         SheetPanel.page('0').should eq 1
422       end
423     end
424     context 'page_size補正について' do
425       it '文字列から数値に変換される' do
426         SheetPanel.page_size('7').should eq 7
427       end
428       it 'nilの場合はSheetPanel.default_page_sizeになる' do
429         SheetPanel.page_size().should eq SheetPanel.default_page_size
430       end
431       it '0以下の場合はSheetPanel.default_page_sizeになる' do
432         SheetPanel.page_size('0').should eq SheetPanel.default_page_size
433       end
434       it 'SheetPanel.max_page_sizeを超えた場合はSheetPanel.max_page_sizeになる' do
435         SheetPanel.page_size('1000').should eq SheetPanel.max_page_size
436       end
437     end
438     it 'リストを返す' do
439       c = SheetPanel.list
440       c.should eq [@sheet_panel]
441     end
442     it '時系列で並んでいる' do
443       #公開用紙のSheetPanelは(他人のSheetPanelであっても)含んでいる
444       v = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 0, :updated_at => Time.now + 100
445       c = SheetPanel.list 
446       c.should eq [ v, @sheet_panel]
447     end
448     it '非公開の紙コマは(自分の紙コマであっても)含まない' do
449       h = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @hidden_sheet.id, :panel_id => @panel.id, :t => 0
450       c = SheetPanel.list 
451       c.should eq [ @sheet_panel]
452     end
453     context 'DBに5件あって1ページの件数を2件に変えたとして' do
454       before do
455         @sheet_panel2 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
456         @sheet_panel3 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 2, :updated_at => Time.now + 200
457         @sheet_panel4 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 3, :updated_at => Time.now + 300
458         @sheet_panel5 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 4, :updated_at => Time.now + 400
459       end
460       it '通常は2件を返す' do
461         l = SheetPanel.list 1, 2
462         l.should have(2).items 
463       end
464       it 'page=1なら末尾2件を返す' do
465         #時系列で並んでいる
466         l = SheetPanel.list 1, 2
467         l.should eq [@sheet_panel5, @sheet_panel4]
468       end
469       it 'page=2なら中間2件を返す' do
470         l = SheetPanel.list 2, 2
471         l.should eq [@sheet_panel3, @sheet_panel2]
472       end
473       it 'page=3なら先頭1件を返す' do
474         l = SheetPanel.list 3, 2
475         l.should eq [@sheet_panel]
476       end
477     end
478   end
479   
480   describe '自分の紙コマ一覧取得に於いて' do
481     before do
482       @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
483       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
484       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
485       @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
486       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
487       @hsheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0
488       @hpanel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
489     end
490     context 'つつがなく終わるとき' do
491       it '一覧取得オプションを利用している' do
492         SheetPanel.stub(:list_opt).with(any_args).and_return({})
493         SheetPanel.should_receive(:list_opt).with(any_args).exactly(1)
494         r = SheetPanel.mylist @author
495       end
496     end
497     it 'リストを返す' do
498       s = SheetPanel.mylist @author
499       s.should eq [@sheet_panel]
500     end
501     it '時系列で並んでいる' do
502       ns = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
503       sl = SheetPanel.mylist @author
504       sl.should eq [ns, @sheet_panel]
505     end
506     it '他人の紙コマはコマ用紙ともに公開でも含まない' do
507       so = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id
508       sl = SheetPanel.mylist @author
509       sl.should eq [@sheet_panel]
510     end
511     it '自分の紙コマはコマ用紙ともに非公開でも含んでいる' do
512       hs = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @hsheet.id, :panel_id => @hpanel.id, :updated_at => Time.now + 100
513       sl = SheetPanel.mylist @author
514       sl.should eq [hs, @sheet_panel]
515     end
516     context 'DBに5件あって1ページの件数を2件に変えたとして' do
517       before do
518         @sheet_panel2 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
519         @sheet_panel3 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 2, :updated_at => Time.now + 200
520         @sheet_panel4 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 3, :updated_at => Time.now + 300
521         @sheet_panel5 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 4, :updated_at => Time.now + 400
522         SheetPanel.stub(:default_page_size).and_return(2)
523       end
524       it '通常は2件を返す' do
525         l = SheetPanel.mylist @author, 1, 2
526         l.should have(2).items 
527       end
528       it 'page=1なら末尾2件を返す' do
529         #時系列で並んでいる
530         l = SheetPanel.mylist @author, 1, 2
531         l.should eq [@sheet_panel5, @sheet_panel4]
532       end
533       it 'page=2なら中間2件を返す' do
534         l = SheetPanel.mylist @author, 2, 2
535         l.should eq [@sheet_panel3, @sheet_panel2]
536       end
537       it 'page=3なら先頭1件を返す' do
538         l = SheetPanel.mylist @author, 3, 2
539         l.should eq [@sheet_panel]
540       end
541     end
542   end
543   
544   describe '他作家の紙コマ一覧取得に於いて' do
545     before do
546       @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
547       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
548       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
549       @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
550       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
551       @other_sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id
552     end
553     it 'リストを返す' do
554       r = SheetPanel.himlist @other_author
555       r.should eq [@other_sheet_panel]
556     end
557     it '時系列で並んでいる' do
558       ns = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 1, :updated_at => Time.now + 100
559       r = SheetPanel.himlist @other_author
560       r.should eq [ns, @other_sheet_panel]
561     end
562     it '公開用紙の紙コマ' do
563       @hsheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 0
564       ns = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @hsheet.id, :panel_id => @other_panel.id, :t => 1, :updated_at => Time.now + 100
565       r = SheetPanel.himlist @other_author
566       r.should eq [@other_sheet_panel]
567     end
568     context 'DBに5件あって1ページの件数を2件に変えたとして' do
569       before do
570         @other_sheet_panel2 = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 1, :updated_at => Time.now + 100
571         @other_sheet_panel3 = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 2, :updated_at => Time.now + 200
572         @other_sheet_panel4 = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 3, :updated_at => Time.now + 300
573         @other_sheet_panel5 = FactoryGirl.create :sheet_panel, :author_id => @other_author.id, :sheet_id => @other_sheet.id, :panel_id => @other_panel.id, :t => 4, :updated_at => Time.now + 400
574         SheetPanel.stub(:default_page_size).and_return(2)
575       end
576       it '通常は2件を返す' do
577         l = SheetPanel.himlist @other_author, 1, 2
578         l.should have(2).items 
579       end
580       it 'page=1なら末尾2件を返す' do
581         #時系列で並んでいる
582         l = SheetPanel.himlist @other_author, 1, 2
583         l.should eq [@other_sheet_panel5, @other_sheet_panel4]
584       end
585       it 'page=2なら中間2件を返す' do
586         l = SheetPanel.himlist @other_author, 2, 2
587         l.should eq [@other_sheet_panel3, @other_sheet_panel2]
588       end
589       it 'page=3なら先頭1件を返す' do
590         l = SheetPanel.himlist @other_author, 3, 2
591         l.should eq [@other_sheet_panel]
592       end
593     end
594   end
595   
596   describe '紙コマ一覧ページ制御に於いて' do
597     before do
598       SheetPanel.stub(:count).with(any_args).and_return(100)
599     end
600     it 'ページ制御を返す' do
601       r = SheetPanel.list_paginate 
602       r.is_a?(Kaminari::PaginatableArray).should be_true
603     end
604     it '紙コマ一覧の取得条件を利用している' do
605       SheetPanel.stub(:list_where).with(any_args).and_return('')
606       SheetPanel.should_receive(:list_where).with(any_args).exactly(1)
607       r = SheetPanel.list_paginate 
608     end
609     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
610       r = SheetPanel.list_paginate 3, 10
611       r.limit_value.should eq 10
612       r.offset_value.should eq 20
613     end
614   end
615   
616   describe '自分の紙コマ一覧ページ制御に於いて' do
617     before do
618       SheetPanel.stub(:count).with(any_args).and_return(100)
619     end
620     it 'ページ制御を返す' do
621       r = SheetPanel.mylist_paginate @author
622       r.is_a?(Kaminari::PaginatableArray).should be_true
623     end
624     it '自分の紙コマ一覧の取得条件を利用している' do
625       SheetPanel.stub(:mylist_where).with(any_args).and_return('')
626       SheetPanel.should_receive(:mylist_where).with(any_args).exactly(1)
627       r = SheetPanel.mylist_paginate @author
628     end
629     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
630       r = SheetPanel.mylist_paginate @author, 3, 10
631       r.limit_value.should eq 10
632       r.offset_value.should eq 20
633     end
634   end
635   
636   describe '他作家の紙コマ一覧ページ制御に於いて' do
637     before do
638       SheetPanel.stub(:count).with(any_args).and_return(100)
639     end
640     it 'ページ制御を返す' do
641       r = SheetPanel.himlist_paginate @other_author
642       r.is_a?(Kaminari::PaginatableArray).should be_true
643     end
644     it '他作家の紙コマ一覧の取得条件を利用している' do
645       SheetPanel.stub(:himlist_where).with(any_args).and_return('')
646       SheetPanel.should_receive(:himlist_where).with(any_args).exactly(1)
647       r = SheetPanel.himlist_paginate @other_author
648     end
649     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
650       r = SheetPanel.himlist_paginate @other_author, 3, 10
651       r.limit_value.should eq 10
652       r.offset_value.should eq 20
653     end
654   end
655   
656   describe 'list関連テーブルプションに於いて' do
657     it '3つの項目を含んでいる' do
658       r = SheetPanel.list_opt
659       r.should have(3).items
660     end
661     it '用紙を含んでいる' do
662       r = SheetPanel.list_opt
663       r.has_key?(:sheet).should be_true
664     end
665       it '用紙は作家を含んでいる' do
666         r = SheetPanel.list_opt
667         r[:sheet].has_key?(:author).should be_true
668       end
669     it '作家を含んでいる' do
670       r = SheetPanel.list_opt
671       r.has_key?(:author).should be_true
672     end
673     it 'コマを含んでいる' do
674       r = SheetPanel.list_opt
675       r.has_key?(:panel).should be_true
676     end
677       it 'コマは作家を含んでいる' do
678         r = SheetPanel.list_opt
679         r[:panel].has_key?(:author).should be_true
680       end
681       it 'コマはコマ絵を含んでいる' do
682         r = SheetPanel.list_opt
683         r[:panel].has_key?(:panel_pictures).should be_true
684       end
685         it 'コマ絵は実素材を含んでいる' do
686           r = SheetPanel.list_opt
687           r[:panel][:panel_pictures].has_key?(:picture).should be_true
688         end
689           it '実素材は絵師を含んでいる' do
690             r = SheetPanel.list_opt
691             r[:panel][:panel_pictures][:picture].has_key?(:artist).should be_true
692           end
693           it '実素材はライセンスを含んでいる' do
694             r = SheetPanel.list_opt
695             r[:panel][:panel_pictures][:picture].has_key?(:license).should be_true
696           end
697       it 'コマはフキダシを含んでいる' do
698         r = SheetPanel.list_opt
699         r[:panel].has_key?(:speech_balloons).should be_true
700       end
701         it 'フキダシはフキダシ枠を含んでいる' do
702           r = SheetPanel.list_opt
703           r[:panel][:speech_balloons].has_key?(:balloon).should be_true
704         end
705         it 'フキダシはセリフを含んでいる' do
706           r = SheetPanel.list_opt
707           r[:panel][:speech_balloons].has_key?(:speech).should be_true
708         end
709   end
710   describe 'json一覧出力オプションに於いて' do
711   end
712   
713   describe '単体取得に於いて' do
714     before do
715       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
716       @panel = FactoryGirl.create :panel, :author_id => @author.id
717       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
718     end
719     context 'つつがなく終わるとき' do
720       it '単体取得オプションを利用している' do
721         SheetPanel.stub(:show_opt).with(any_args).and_return({})
722         SheetPanel.should_receive(:show_opt).with(any_args).exactly(1)
723         r = SheetPanel.show @sheet_panel.id, @author
724       end
725       it '閲覧許可を問い合わせている' do
726         SheetPanel.any_instance.stub(:visible?).with(@author).and_return(true)
727         SheetPanel.any_instance.should_receive(:visible?).with(@author).exactly(1)
728         r = SheetPanel.show @sheet_panel.id, @author
729       end
730     end
731     it '指定の紙コマを返す' do
732       l = SheetPanel.show @sheet_panel.id, @author
733       l.should eq @sheet_panel
734     end
735     context '他人の紙コマを開こうとしたとき' do
736       it '403Forbidden例外を返す' do
737         SheetPanel.any_instance.stub(:visible?).with(@other_author).and_return(false)
738         lambda{
739           SheetPanel.show @sheet_panel.id, @other_author
740         }.should raise_error(ActiveRecord::Forbidden)
741       end
742     end
743     context '存在しない紙コマを開こうとしたとき' do
744       it '404RecordNotFound例外を返す' do
745         lambda{
746           SheetPanel.show 110, @author
747         }.should raise_error(ActiveRecord::RecordNotFound)
748       end
749     end
750   end
751   
752   describe '編集取得に於いて' do
753     before do
754       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
755       @panel = FactoryGirl.create :panel, :author_id => @author.id
756       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
757     end
758     context 'つつがなく終わるとき' do
759       it '単体取得オプションを利用している' do
760         SheetPanel.stub(:show_opt).with(any_args).and_return({})
761         SheetPanel.should_receive(:show_opt).with(any_args).exactly(1)
762         r = SheetPanel.edit @sheet_panel.id, @author
763       end
764       it '所持判定を問い合わせている' do
765         SheetPanel.any_instance.stub(:own?).with(any_args).and_return(true)
766         SheetPanel.any_instance.should_receive(:own?).with(any_args).exactly(1)
767         r = SheetPanel.edit @sheet_panel.id, @author
768       end
769     end
770     it '指定の紙コマを返す' do
771       l = SheetPanel.edit @sheet_panel.id, @author
772       l.should eq @sheet_panel
773     end
774     context '他人の紙コマを開こうとしたとき' do
775       it '403Forbidden例外を返す' do
776         SheetPanel.any_instance.stub(:own?).and_return(false)
777         lambda{
778           SheetPanel.edit @sheet_panel.id, @author
779         }.should raise_error(ActiveRecord::Forbidden)
780       end
781     end
782     context '存在しない紙コマを開こうとしたとき' do
783       it '404RecordNotFound例外を返す' do
784         lambda{
785           SheetPanel.edit 110, @author
786         }.should raise_error(ActiveRecord::RecordNotFound)
787       end
788     end
789   end
790   
791   describe '単体取得オプションに於いて' do
792     it 'includeキーを含んでいる' do
793       r = SheetPanel.show_opt
794       r.has_key?(:include).should be_true
795     end
796     it '3つの項目を含んでいる' do
797       r = SheetPanel.show_opt[:include]
798       r.should have(3).items
799     end
800     it '用紙を含んでいる' do
801       r = SheetPanel.show_opt[:include]
802       r.has_key?(:sheet).should be_true
803     end
804       it '用紙は作家を含んでいる' do
805         r = SheetPanel.show_opt[:include]
806         r[:sheet].has_key?(:author).should be_true
807       end
808     it '作家を含んでいる' do
809       r = SheetPanel.show_opt[:include]
810       r.has_key?(:author).should be_true
811     end
812     it 'コマを含んでいる' do
813       r = SheetPanel.show_opt[:include]
814       r.has_key?(:panel).should be_true
815     end
816       it 'コマは作家を含んでいる' do
817         r = SheetPanel.show_opt[:include]
818         r[:panel].has_key?(:author).should be_true
819       end
820       it 'コマはコマ絵を含んでいる' do
821         r = SheetPanel.show_opt[:include]
822         r[:panel].has_key?(:panel_pictures).should be_true
823       end
824         it 'コマ絵は実素材を含んでいる' do
825           r = SheetPanel.show_opt[:include]
826           r[:panel][:panel_pictures].has_key?(:picture).should be_true
827         end
828           it '実素材は絵師を含んでいる' do
829             r = SheetPanel.show_opt[:include]
830             r[:panel][:panel_pictures][:picture].has_key?(:artist).should be_true
831           end
832           it '実素材はライセンスを含んでいる' do
833             r = SheetPanel.show_opt[:include]
834             r[:panel][:panel_pictures][:picture].has_key?(:license).should be_true
835           end
836       it 'コマはフキダシを含んでいる' do
837         r = SheetPanel.show_opt[:include]
838         r[:panel].has_key?(:speech_balloons).should be_true
839       end
840         it 'フキダシはフキダシ枠を含んでいる' do
841           r = SheetPanel.show_opt[:include]
842           r[:panel][:speech_balloons].has_key?(:balloon).should be_true
843         end
844         it 'フキダシはセリフを含んでいる' do
845           r = SheetPanel.show_opt[:include]
846           r[:panel][:speech_balloons].has_key?(:speech).should be_true
847         end
848   end
849   describe 'json単体取得オプションに於いて' do
850   end
851   
852   describe '紙コマのjson出力に於いて' do
853     before do
854       #コマを作成しておく。
855       @panel = FactoryGirl.create :panel, :author_id => @author.id
856       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height
857       @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
858       @speech = @sb.build_speech(
859         FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id)
860       )
861       @balloon = @sb.build_balloon(
862         FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id)
863       )
864       @sb.boost
865       @sb.save!
866       @gc = @panel.ground_colors.create(
867         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id)
868       )
869       @gp = @panel.ground_pictures.create(
870         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)
871       )
872       @panel.reload
873       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
874       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
875     end
876     context '事前チェックする' do
877       before do
878         Panel.any_instance.stub(:elements).and_return('{}')
879       end
880       it 'コマ要素のjson出力を依頼している' do
881         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
882         Panel.any_instance.should_receive(:elements).with(any_args).exactly(1)
883         r = @sheet_panel.sheet_panel_as_json @author
884       end
885     end
886     it 'json textを返している' do
887       r = JSON.parse @sheet_panel.sheet_panel_as_json(@author)
888       r.is_a?(Hash).should be_true
889     end
890     it 'sheet,author,panel,コマ要素を含んでいる' do
891       r = JSON.parse @sheet_panel.sheet_panel_as_json(@author)
892       r.has_key?('sheet').should be_true
893       r['sheet'].has_key?('author').should be_true
894       r.has_key?('author').should be_true
895       r.has_key?('panel').should be_true
896       r['panel'].has_key?('author').should be_true
897     end
898     context 'コマ閲覧許可のとき' do
899       before do
900         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
901       end
902       it 'コマ要素にコマ要素を追加している' do
903         r = JSON.parse @sheet_panel.sheet_panel_as_json(@author)
904         r['panel'].has_key?('elements').should be_true
905         r['panel']['elements'].should_not be_empty
906       end
907     end
908     context 'コマ閲覧不許可のとき' do
909       before do
910         Panel.any_instance.stub(:visible?).with(any_args).and_return(false)
911       end
912       it 'コマ要素にデータを含ませない' do
913         r = JSON.parse @sheet_panel.sheet_panel_as_json(@author)
914         r['panel'].has_key?('elements').should be_false
915       end
916     end
917   end
918   
919   describe '紙コマリストのjson出力に於いて' do
920     before do
921       @panel = FactoryGirl.create :panel, :author_id => @author.id
922       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
923       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
924       SheetPanel.any_instance.stub(:sheet_panel_as_json).with(@author).and_return('{"s": 5}')
925     end
926     context 'つつがなく終わるとき' do
927       it '紙コマのjson出力を依頼している' do
928         SheetPanel.any_instance.should_receive(:sheet_panel_as_json).with(@author).exactly(1)
929         r = SheetPanel.list_as_json_text [@sheet_panel], @author
930       end
931     end
932     it 'json textを返している' do
933       r = SheetPanel.list_as_json_text [@sheet_panel], @author
934       j = JSON.parse r
935       j.is_a?(Array).should be_true
936     end
937     it '紙コマを含んでいる' do
938       r = SheetPanel.list_as_json_text [@sheet_panel], @author
939       j = JSON.parse r
940       j.first.has_key?('s').should be_true
941     end
942   end
943   
944   describe 'ライセンス素材に於いて' do
945     before do
946       #コマを作成しておく。
947       @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
948       @p2 = FactoryGirl.create :picture, :original_picture_id => @op2.id, :license_id => @license.id, :artist_id => @artist.id
949       @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p2.id
950       @panel = FactoryGirl.create :panel, :author_id => @author.id
951       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 0, :width => @p.width, :height => @p.height, :picture_id => @p.id
952       @panel.reload
953       @panel2 = FactoryGirl.create :panel, :author_id => @author.id
954       @pp2= FactoryGirl.create :panel_picture, :panel_id => @panel2.id, :t => 0, :width => @p2.width, :height => @p2.height, :picture_id => @p2.id
955       @panel2.reload
956       
957       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
958       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
959       @sheet_panel2 = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel2.id
960     end
961     context '事前チェック' do
962     end
963     context 'つつがなく終わるとき' do
964     end
965     it '連想配列(値は実素材、キーは実素材id)を返している' do
966       r = SheetPanel.licensed_pictures [@sheet_panel, @sheet_panel2]
967       r.is_a?(Hash).should be_true
968       r.should have(2).items
969       r[@pp.picture_id].should eq @p
970       r[@pp2.picture_id].should eq @p2
971     end
972     context 'コマが削除されているときき' do
973       before do
974         @panel2.destroy
975       end
976       it '削除されているコマは無視する' do
977         r = SheetPanel.licensed_pictures [@sheet_panel, @sheet_panel2]
978         r.is_a?(Hash).should be_true
979         r.should have(1).items
980         r[@pp.picture_id].should eq @p
981         r[@pp2.picture_id].should be_nil
982       end
983     end
984   end
985   
986   describe 't補充値に於いて' do
987     before do
988       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
989       @panel = FactoryGirl.create :panel, :author_id => @author.id
990     end
991     
992     context '用紙初のコマなら' do
993       it '0を補充値とする' do
994         @sheet_panel = FactoryGirl.build :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
995         @sheet_panel.t = nil
996         r = SheetPanel.new_t @sheet_panel.sheet_id
997         r.should eq 0
998       end
999     end
1000     context '用紙に一個コマがあるとき' do
1001       it '1を補充値とする' do
1002         FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0
1003         @sheet_panel = FactoryGirl.build :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
1004         @sheet_panel.t = nil
1005         r = SheetPanel.new_t @sheet_panel.sheet_id
1006         r.should eq 1
1007       end
1008     end
1009     context '用紙に2個コマがあるとき' do
1010       it '2を補充値とする' do
1011         FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0
1012         FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1
1013         @sheet_panel = FactoryGirl.build :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
1014         @sheet_panel.t = nil
1015         r = SheetPanel.new_t @sheet_panel.sheet_id
1016         r.should eq 2
1017       end
1018     end
1019   end
1020   describe 'シリアライズチェックに於いて' do
1021     context 'つつがなく終わるとき' do
1022       it '0からシリアライズされているならTrueを返す' do
1023         r = SheetPanel.serial? [0, 1, 2]
1024         r.should be_true
1025       end
1026       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
1027         r = SheetPanel.serial? [0, 2, 1]
1028         r.should be_true
1029       end
1030       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
1031         r = SheetPanel.serial? [ 2, 1, 4, 3, 0]
1032         r.should be_true
1033       end
1034     end
1035     context '異常なとき' do
1036       it '0から始まらないならFalseを返す' do
1037         r = SheetPanel.serial? [1, 2, 3]
1038         r.should be_false
1039       end
1040       it '連続していないならFalseを返す' do
1041         r = SheetPanel.serial? [0, 1, 2, 4]
1042         r.should be_false
1043       end
1044       it '連続していないならFalseを返す' do
1045         r = SheetPanel.serial? [0, 1, 2, 4, 5]
1046         r.should be_false
1047       end
1048     end
1049   end
1050   describe 't収集に於いて' do
1051     before do
1052       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1053       @panel = FactoryGirl.create :panel, :author_id => @author.id
1054       @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1055       @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1056       @c2sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1057     end
1058     context 'つつがなく終わるとき' do
1059       it '紙コマから同一用紙のtだけを収集している' do
1060         r = SheetPanel.collect_t @sheet_panel
1061         r.should eq [0]
1062       end
1063     end
1064     context '複数コマのとき' do
1065       it '紙コマから同一用紙のtだけを収集している' do
1066         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1067         r = SheetPanel.collect_t @sheet_panel
1068         r.sort.should eq [0, 1]
1069       end
1070     end
1071     context '複数コマでヨソの用紙も混じっているとき' do
1072       it '紙コマから同一用紙のtだけを収集している' do
1073         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1074         r = SheetPanel.collect_t @sheet_panel
1075         r.sort.should eq [0, 1]
1076       end
1077     end
1078   end
1079   describe 'tチェックに於いて' do
1080     before do
1081       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1082       @panel = FactoryGirl.create :panel, :author_id => @author.id
1083       @sheet_panel = FactoryGirl.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1084     end
1085     context 'つつがなく終わるとき' do
1086       it 't収集を依頼している' do
1087         SheetPanel.should_receive(:collect_t).with(any_args).exactly(1)
1088         SheetPanel.stub(:collect_t).with(any_args).and_return([])
1089         SheetPanel.stub(:serial?).with(any_args).and_return(true)
1090         r = SheetPanel.validate_t @sheet_panel
1091       end
1092       it '収集したtをシリアライズチェック依頼している' do
1093         SheetPanel.stub(:collect_t).with(any_args).and_return([])
1094         SheetPanel.should_receive(:serial?).with(any_args).exactly(1)
1095         SheetPanel.stub(:serial?).with(any_args).and_return(true)
1096         r = SheetPanel.validate_t @sheet_panel
1097       end
1098     end
1099     #実データでチェック
1100     #依頼チェックだけでは不安なので最低限のチェックを
1101     context '新規のとき' do
1102       it '一件だけで正常通過している' do
1103         @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
1104         r = SheetPanel.validate_t @sheet_panel
1105         r.should be_true 
1106       end
1107     end
1108     context '既存のとき' do
1109       it '2件目を作っても正常通過している' do
1110         @sheet_panel = FactoryGirl.create :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
1111         @sheet_panel2 = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
1112         r = SheetPanel.validate_t @sheet_panel2
1113         r.should be_true 
1114       end
1115     end
1116   end
1117   describe '挿入シフトに於いて' do
1118     before do
1119       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1120       @panel = FactoryGirl.create :panel, :author_id => @author.id
1121     end
1122     context '依頼チェック' do
1123       #テーブルが空で0に挿入
1124       it 'Updateを依頼している' do
1125         SheetPanel.stub(:update_all).with(any_args)
1126         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
1127         @sheet_panel = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1128         @sheet_panel.insert_shift
1129       end
1130     end
1131     context 'テーブルに1件(t:0)で0に挿入したとき' do
1132       before do
1133         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1134         @sheet_panel2 = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1135       end
1136       it '既存の行を1にシフトしている' do
1137         @sheet_panel2.insert_shift
1138         l = SheetPanel.find :all
1139         l.first.t.should eq 1
1140       end
1141       it 'これから挿入するt(0)が欠番になっている' do
1142         @sheet_panel2.insert_shift
1143         l = SheetPanel.find(:all).map {|s| s.t }
1144         l.include?(0).should_not be_true
1145       end
1146     end
1147     context 'テーブルに2件(t:0,1)で1に挿入したとき' do
1148       before do
1149         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1150         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1151         @sheet_panel3 = FactoryGirl.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1152       end
1153       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1154         @sheet_panel3.insert_shift
1155         l = SheetPanel.find(:all).map {|s| s.t }
1156         l.sort.should eq [0, 2]
1157       end
1158     end
1159     context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
1160       before do
1161         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1162         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1163         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1164         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1165         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1166         @sheet_panel6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1167       end
1168       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1169         @sheet_panel6.insert_shift
1170         l = SheetPanel.find(:all).map {|s| s.t }
1171         l.sort.should eq [0, 1, 3, 4, 5]
1172       end
1173     end
1174     context '先ほどのケース+他の用紙1件で挿入したとき' do
1175       before do
1176         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1177         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1178         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1179         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1180         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1181         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1182         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1183         @sheet_panel6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1184       end
1185       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1186         @sheet_panel6.insert_shift
1187         l = SheetPanel.find(:all, :conditions => ['sheet_id = ?', @sheet.id]).map {|s| s.t }
1188         l.sort.should eq [0, 1, 3, 4, 5]
1189       end
1190       it '他の用紙に影響がない' do
1191         ot = @sheet_panelc2.t
1192         @sheet_panel6.insert_shift
1193         @sheet_panelc2.reload
1194         @sheet_panelc2.t.should eq ot
1195       end
1196     end
1197   end
1198   describe '少ない方に移動に於いて' do
1199     before do
1200       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1201       @panel = FactoryGirl.create :panel, :author_id => @author.id
1202     end
1203     context '依頼チェック' do
1204       it 'Updateを依頼している' do
1205         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1206         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1207         SheetPanel.stub(:update_all).with(any_args)
1208         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
1209         ot = @sheet_panel2.t
1210         @sheet_panel2.t = 0
1211         @sheet_panel2.lesser_shift ot
1212       end
1213     end
1214     context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
1215       before do
1216         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1217         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1218         @ot = @sheet_panel2.t
1219         @sheet_panel2.t = 0
1220       end
1221       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
1222         #移動させたい行はそのまま残る
1223         @sheet_panel2.lesser_shift @ot
1224         l = SheetPanel.find(:all).map {|s| s.t }
1225         l.sort.should eq [1, 1]
1226       end
1227       it '既存のt0を1にシフトしている' do
1228         @sheet_panel2.lesser_shift @ot
1229         @sheet_panel.reload
1230         @sheet_panel.t.should eq 1
1231       end
1232     end
1233     context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
1234       before do
1235         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1236         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1237         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1238         @ot = @sheet_panel3.t
1239         @sheet_panel3.t = 1
1240       end
1241       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1242         #移動させたい行はそのまま残る
1243         @sheet_panel3.lesser_shift @ot
1244         l = SheetPanel.find(:all).map {|s| s.t }
1245         l.sort.should eq [0, 2, 2]
1246       end
1247       it '既存のt1を2にシフトしている' do
1248         @sheet_panel3.lesser_shift @ot
1249         @sheet_panel2.reload
1250         @sheet_panel2.t.should eq 2
1251       end
1252     end
1253     context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
1254       before do
1255         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1256         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1257         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1258         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1259         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1260         @ot = @sheet_panel4.t
1261         @sheet_panel4.t = 1
1262       end
1263       it 'これから挿入するt(1)が欠番になっている' do
1264         #移動させたい行はそのまま残る
1265         @sheet_panel4.lesser_shift @ot
1266         l = SheetPanel.find(:all).map {|s| s.t }
1267         l.sort.should eq [0, 2, 3, 3, 4]
1268       end
1269       it '既存のt0には変化がない' do
1270         @sheet_panel4.lesser_shift @ot
1271         @sheet_panel.reload
1272         @sheet_panel.t.should eq 0
1273       end
1274       it '既存のt4には変化がない' do
1275         @sheet_panel4.lesser_shift @ot
1276         @sheet_panel5.reload
1277         @sheet_panel5.t.should eq 4
1278       end
1279       it '既存のt1を2にシフトしている' do
1280         @sheet_panel4.lesser_shift @ot
1281         @sheet_panel2.reload
1282         @sheet_panel2.t.should eq 2
1283       end
1284       it '既存のt2を3にシフトしている' do
1285         @sheet_panel4.lesser_shift @ot
1286         @sheet_panel3.reload
1287         @sheet_panel3.t.should eq 3
1288       end
1289     end
1290     context '先ほどのケース+他の用紙1件で挿入したとき' do
1291       before do
1292         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1293         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1294         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1295         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1296         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1297         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1298         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1299         @ot = @sheet_panel4.t
1300         @sheet_panel4.t = 1
1301       end
1302       it 'これから挿入するt(1)が欠番になっている' do
1303         @sheet_panel4.lesser_shift @ot
1304         l = SheetPanel.find(:all).map {|s| s.t }
1305         l.sort.should eq [0, 0, 2, 3, 3, 4]
1306       end
1307       it '既存のt0には変化がない' do
1308         @sheet_panel4.lesser_shift @ot
1309         @sheet_panel.reload
1310         @sheet_panel.t.should eq 0
1311       end
1312       it '既存のt4には変化がない' do
1313         @sheet_panel4.lesser_shift @ot
1314         @sheet_panel5.reload
1315         @sheet_panel5.t.should eq 4
1316       end
1317       it '既存のt1を2にシフトしている' do
1318         @sheet_panel4.lesser_shift @ot
1319         @sheet_panel2.reload
1320         @sheet_panel2.t.should eq 2
1321       end
1322       it '既存のt2を3にシフトしている' do
1323         @sheet_panel4.lesser_shift @ot
1324         @sheet_panel3.reload
1325         @sheet_panel3.t.should eq 3
1326       end
1327       it '他の用紙に影響がない' do
1328         @sheet_panel4.lesser_shift @ot
1329         @sheet_panelc2.reload
1330         @sheet_panelc2.t.should eq 0
1331       end
1332     end
1333     #例外ケース。
1334     #負のときは0として正常扱い
1335     context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
1336       before do
1337         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1338         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1339         @ot = @sheet_panel2.t
1340         @sheet_panel2.t = -1
1341       end
1342       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
1343         #移動させたい行はそのまま残る
1344         @sheet_panel2.lesser_shift @ot
1345         l = SheetPanel.find(:all).map {|s| s.t }
1346         l.sort.should eq [1, 1]
1347       end
1348       it '既存のt0を1にシフトしている' do
1349         @sheet_panel2.lesser_shift @ot
1350         @sheet_panel.reload
1351         @sheet_panel.t.should eq 1
1352       end
1353       it '既存のt1は0に補正されている' do
1354         @sheet_panel2.lesser_shift @ot
1355         @sheet_panel2.t.should eq 0
1356       end
1357     end
1358   end
1359   describe '大きい方に移動に於いて' do
1360     before do
1361       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1362       @panel = FactoryGirl.create :panel, :author_id => @author.id
1363     end
1364     context '依頼チェック' do
1365       it 'Updateを依頼している' do
1366         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1367         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1368         SheetPanel.stub(:update_all).with(any_args)
1369         SheetPanel.should_receive(:update_all).with(any_args).exactly(1)
1370         ot = @sheet_panel.t
1371         @sheet_panel.t = 1
1372         @sheet_panel.higher_shift ot
1373       end
1374     end
1375     context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
1376       before do
1377         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1378         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1379         @ot = @sheet_panel.t
1380         @sheet_panel.t = 1
1381       end
1382       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1383         #移動させたい行はそのまま残る
1384         @sheet_panel.higher_shift @ot
1385         l = SheetPanel.find(:all).map {|s| s.t }
1386         l.sort.should eq [0, 0]
1387       end
1388       it '既存のt1を0にシフトしている' do
1389         @sheet_panel.higher_shift @ot
1390         @sheet_panel2.reload
1391         @sheet_panel2.t.should eq 0
1392       end
1393     end
1394     context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
1395       before do
1396         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1397         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1398         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1399         @ot = @sheet_panel.t
1400         @sheet_panel.t = 1
1401       end
1402       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1403         #移動させたい行はそのまま残る
1404         @sheet_panel.higher_shift @ot
1405         l = SheetPanel.find(:all).map {|s| s.t }
1406         l.sort.should eq [0, 0, 2]
1407       end
1408       it '既存のt1を0にシフトしている' do
1409         @sheet_panel.higher_shift @ot
1410         @sheet_panel2.reload
1411         @sheet_panel2.t.should eq 0
1412       end
1413     end
1414     context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
1415       before do
1416         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1417         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1418         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1419         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1420         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1421         @ot = @sheet_panel2.t
1422         @sheet_panel2.t = 3
1423       end
1424       it 'これから挿入するt(3)が欠番になっている' do
1425         #移動させたい行はそのまま残る
1426         @sheet_panel2.higher_shift @ot
1427         l = SheetPanel.find(:all).map {|s| s.t }
1428         l.sort.should eq [0, 1, 1, 2, 4]
1429       end
1430       it '既存のt0には変化がない' do
1431         @sheet_panel2.higher_shift @ot
1432         @sheet_panel.reload
1433         @sheet_panel.t.should eq 0
1434       end
1435       it '既存のt4には変化がない' do
1436         @sheet_panel2.higher_shift @ot
1437         @sheet_panel5.reload
1438         @sheet_panel5.t.should eq 4
1439       end
1440       it '既存のt2を1にシフトしている' do
1441         @sheet_panel2.higher_shift @ot
1442         @sheet_panel3.reload
1443         @sheet_panel3.t.should eq 1
1444       end
1445       it '既存のt3を2にシフトしている' do
1446         @sheet_panel2.higher_shift @ot
1447         @sheet_panel4.reload
1448         @sheet_panel4.t.should eq 2
1449       end
1450     end
1451     context '先ほどのケース+他の用紙1件で挿入したとき' do
1452       before do
1453         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1454         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1455         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1456         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1457         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1458         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1459         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1460         @ot = @sheet_panel2.t
1461         @sheet_panel2.t = 3
1462       end
1463       it 'これから挿入するt(3)が欠番になっている' do
1464         #移動させたい行はそのまま残る
1465         @sheet_panel2.higher_shift @ot
1466         l = SheetPanel.find(:all).map {|s| s.t }
1467         l.sort.should eq [0, 0, 1, 1, 2, 4]
1468       end
1469       it '既存のt0には変化がない' do
1470         @sheet_panel2.higher_shift @ot
1471         @sheet_panel.reload
1472         @sheet_panel.t.should eq 0
1473       end
1474       it '既存のt4には変化がない' do
1475         @sheet_panel2.higher_shift @ot
1476         @sheet_panel5.reload
1477         @sheet_panel5.t.should eq 4
1478       end
1479       it '既存のt2を1にシフトしている' do
1480         @sheet_panel2.higher_shift @ot
1481         @sheet_panel3.reload
1482         @sheet_panel3.t.should eq 1
1483       end
1484       it '既存のt3を2にシフトしている' do
1485         @sheet_panel2.higher_shift @ot
1486         @sheet_panel4.reload
1487         @sheet_panel4.t.should eq 2
1488       end
1489       it '他の用紙に影響がない' do
1490         @sheet_panel2.higher_shift @ot
1491         @sheet_panelc2.reload
1492         @sheet_panelc2.t.should eq 0
1493       end
1494     end
1495     #例外ケース。
1496     #max超えたときはmaxとして正常扱い
1497     context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
1498       before do
1499         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1500         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1501         @ot = @sheet_panel.t
1502         @sheet_panel.t = 2
1503       end
1504       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1505         #移動させたい行はそのまま残る
1506         @sheet_panel.higher_shift @ot
1507         l = SheetPanel.find(:all).map {|s| s.t }
1508         l.sort.should eq [0, 0]
1509       end
1510       it '既存のt1を0にシフトしている' do
1511         @sheet_panel.higher_shift @ot
1512         @sheet_panel2.reload
1513         @sheet_panel2.t.should eq 0
1514       end
1515       it '既存のt0は1に補正されている' do
1516         @sheet_panel.higher_shift @ot
1517         @sheet_panel.t.should eq 1
1518       end
1519     end
1520   end
1521   describe '入れ替えに於いて' do
1522     before do
1523       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1524       @panel = FactoryGirl.create :panel, :author_id => @author.id
1525       @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1526       @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1527     end
1528     context '新tが旧tより小さいとき' do
1529       it '少ない方に移動を依頼している' do
1530         SheetPanel.any_instance.stub(:lesser_shift).with(any_args)
1531         SheetPanel.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
1532         ot = @sheet_panel2.t
1533         @sheet_panel2.t = 0
1534         @sheet_panel2.update_shift ot
1535       end
1536     end
1537     context '新tが旧tより大きいとき' do
1538       it '大きい方に移動を依頼している' do
1539         SheetPanel.any_instance.stub(:higher_shift).with(any_args)
1540         SheetPanel.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
1541         ot = @sheet_panel.t
1542         @sheet_panel.t = 1
1543         @sheet_panel.update_shift ot
1544       end
1545     end
1546   end
1547   describe '順序入れ替えに於いて' do
1548     before do
1549       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1550       @panel = FactoryGirl.create :panel, :author_id => @author.id
1551     end
1552     context 'オブジェクトが新規でtが空のとき' do
1553       it '末尾追加としてtを補充依頼している' do
1554         @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1555         SheetPanel.stub(:new_t).with(any_args).and_return(0)
1556         SheetPanel.should_receive(:new_t).with(any_args).exactly(1)
1557         @sheet_panel.t = nil
1558         r = @sheet_panel.rotate
1559       end
1560     end
1561     context 'オブジェクトが新規でtが設定されているとき' do
1562       it '挿入追加として挿入シフトを依頼している' do
1563         @sheet_panel = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1564         SheetPanel.any_instance.stub(:insert_shift).with(any_args)
1565         SheetPanel.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
1566         @sheet_panel.t = 0
1567         r = @sheet_panel.rotate
1568       end
1569     end
1570     context 'オブジェクトが新規でなくtが設定されているとき' do
1571       it 'コマ移動として入れ替えを依頼している' do
1572         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1573         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1574         SheetPanel.any_instance.stub(:update_shift).with(any_args)
1575         SheetPanel.any_instance.should_receive(:update_shift).with(1).exactly(1)
1576         @sheet_panel2.t = 0
1577         r = @sheet_panel.rotate 1
1578       end
1579     end
1580     context 'オブジェクトが新規でなくtが空のとき' do
1581       it '入れ替えもシフトもせず、tを空のままにしている' do
1582         #結果、tに欠番が生じてシリアライズチェックでひっかかる
1583       end
1584     end
1585   end
1586   describe '編集許可に於いて' do
1587     before do
1588       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1589       @panel = FactoryGirl.create :panel, :author_id => @author.id
1590       @sheet_panel = FactoryGirl.build :sheet_panel, :t => nil, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1591     end
1592     context 'つつがなく終わるとき' do
1593       it 'trueを返す' do
1594         r = @sheet_panel.allow?
1595         r.should be_true
1596       end
1597     end
1598     context '用紙で引っかかるとき' do
1599       it 'falseを返す' do
1600         Panel.any_instance.stub(:usable?).with(any_args).and_return(true)
1601         Sheet.any_instance.stub(:own?).with(any_args).and_return(false)
1602         r = @sheet_panel.allow?
1603         r.should be_false
1604       end
1605     end
1606     context 'コマで引っかかるとき' do
1607       it 'falseを返す' do
1608         Sheet.any_instance.stub(:own?).with(any_args).and_return(true)
1609         Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
1610         r = @sheet_panel.allow?
1611         r.should be_false
1612       end
1613     end
1614     context '用紙またはコマが指定されていなかったとき' do
1615       it 'nilを返す' do
1616         Sheet.any_instance.stub(:own?).with(any_args).and_return(true)
1617         @sheet_panel.panel_id = nil
1618         r = @sheet_panel.allow?
1619         r.should eq nil
1620       end
1621       it 'nilを返す' do
1622         Panel.any_instance.stub(:usable?).with(any_args).and_return(true)
1623         @sheet_panel.sheet_id = nil
1624         r = @sheet_panel.allow?
1625         r.should eq nil
1626       end
1627     end
1628   end
1629   describe '保存に於いて' do
1630     before do
1631       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1632       @panel = FactoryGirl.create :panel, :author_id => @author.id
1633       @sheet_panel = FactoryGirl.build :sheet_panel, :t => nil, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1634     end
1635     context 'つつがなく終わるとき' do
1636       it '編集許可チェックを依頼している' do
1637         SheetPanel.any_instance.stub(:allow?).with(any_args).and_return(true)
1638         SheetPanel.any_instance.should_receive(:allow?).with(any_args).exactly(1)
1639         r = @sheet_panel.store
1640       end
1641       it '順序入れ替えを依頼している' do
1642         SheetPanel.any_instance.stub(:rotate).with(any_args).and_return(0)
1643         SheetPanel.any_instance.should_receive(:rotate).with(any_args).exactly(1)
1644         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
1645         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
1646         r = @sheet_panel.store 
1647       end
1648       it '保存を依頼している' do
1649         SheetPanel.stub(:new_t).with(any_args).and_return(0)
1650         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
1651         SheetPanel.any_instance.should_receive(:save).with(any_args).exactly(1)
1652         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
1653         r = @sheet_panel.store
1654       end
1655       it 'tのシリアライズチェックを依頼している' do
1656         SheetPanel.stub(:new_t).with(any_args).and_return(0)
1657         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
1658         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
1659         SheetPanel.should_receive(:validate_t).with(any_args).exactly(1)
1660         r = @sheet_panel.store
1661       end
1662     end
1663     #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1664     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したとき' do
1665       before do
1666         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1667         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1668         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1669         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1670         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1671         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1672         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1673         @sheet_panel6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1674       end
1675       it '既存のt0には変化がない' do
1676         @sheet_panel6.store
1677         @sheet_panel.reload
1678         @sheet_panel.t.should eq 0
1679       end
1680       it '既存のt1には変化がない' do
1681         @sheet_panel6.store
1682         @sheet_panel2.reload
1683         @sheet_panel2.t.should eq 1
1684       end
1685       it '既存のt2を3にシフトしている' do
1686         @sheet_panel6.store
1687         @sheet_panel3.reload
1688         @sheet_panel3.t.should eq 3
1689       end
1690       it '既存のt3を4にシフトしている' do
1691         @sheet_panel6.store
1692         @sheet_panel4.reload
1693         @sheet_panel4.t.should eq 4
1694       end
1695       it '既存のt5を5にシフトしている' do
1696         @sheet_panel6.store
1697         @sheet_panel5.reload
1698         @sheet_panel5.t.should eq 5
1699       end
1700       it '新規のt2が作成されている' do
1701         @sheet_panel6.store
1702         @sheet_panel6.reload
1703         @sheet_panel6.t.should eq 2
1704       end
1705       it '他の用紙に影響がない' do
1706         @ot = @sheet_panelc2.t
1707         @sheet_panel6.store
1708         @sheet_panelc2.reload
1709         @sheet_panelc2.t.should eq @ot
1710       end
1711     end
1712     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したとき' do
1713       before do
1714         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1715         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1716         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1717         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1718         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1719         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1720         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1721         @ot = @sheet_panel4.t
1722         @sheet_panel4.t = 1
1723       end
1724       it '既存のt0には変化がない' do
1725         @sheet_panel4.store @ot
1726         @sheet_panel.reload
1727         @sheet_panel.t.should eq 0
1728       end
1729       it '既存のt4には変化がない' do
1730         @sheet_panel4.store @ot
1731         @sheet_panel5.reload
1732         @sheet_panel5.t.should eq 4
1733       end
1734       it '既存のt1を2にシフトしている' do
1735         @sheet_panel4.store @ot
1736         @sheet_panel2.reload
1737         @sheet_panel2.t.should eq 2
1738       end
1739       it '既存のt2を3にシフトしている' do
1740         @sheet_panel4.store @ot
1741         @sheet_panel3.reload
1742         @sheet_panel3.t.should eq 3
1743       end
1744       it '既存のt3を1にシフトしている' do
1745         @sheet_panel4.store @ot
1746         @sheet_panel4.reload
1747         @sheet_panel4.t.should eq 1
1748       end
1749       it '他の用紙に影響がない' do
1750         @sheet_panel4.store @ot
1751         @sheet_panelc2.reload
1752         @sheet_panelc2.t.should eq 0
1753       end
1754     end
1755     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で1を3に移動したとき' do
1756       before do
1757         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1758         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1759         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1760         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1761         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1762         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1763         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1764         @ot = @sheet_panel2.t
1765         @sheet_panel2.t = 3
1766       end
1767       it '既存のt0には変化がない' do
1768         @sheet_panel2.store @ot
1769         @sheet_panel.reload
1770         @sheet_panel.t.should eq 0
1771       end
1772       it '既存のt4には変化がない' do
1773         @sheet_panel2.store @ot
1774         @sheet_panel5.reload
1775         @sheet_panel5.t.should eq 4
1776       end
1777       it '既存のt1を3にシフトしている' do
1778         @sheet_panel2.store @ot
1779         @sheet_panel2.reload
1780         @sheet_panel2.t.should eq 3
1781       end
1782       it '既存のt2を1にシフトしている' do
1783         @sheet_panel2.store @ot
1784         @sheet_panel3.reload
1785         @sheet_panel3.t.should eq 1
1786       end
1787       it '既存のt3を2にシフトしている' do
1788         @sheet_panel2.store @ot
1789         @sheet_panel4.reload
1790         @sheet_panel4.t.should eq 2
1791       end
1792       it '他の用紙に影響がない' do
1793         @sheet_panel2.store @ot
1794         @sheet_panelc2.reload
1795         @sheet_panelc2.t.should eq 0
1796       end
1797     end
1798     #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1799     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したが保存に失敗したとき' do
1800       before do
1801         SheetPanel.any_instance.stub(:save).with(any_args).and_return(false)
1802         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1803         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1804         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1805         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1806         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1807         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1808         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1809         @sheet_panel6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1810       end
1811       it '既存のtに変化がない' do
1812         @sheet_panel6.store
1813         @sheet_panel.reload
1814         @sheet_panel.t.should eq 0
1815         @sheet_panel2.reload
1816         @sheet_panel2.t.should eq 1
1817         @sheet_panel3.reload
1818         @sheet_panel3.t.should eq 2
1819         @sheet_panel4.reload
1820         @sheet_panel4.t.should eq 3
1821         @sheet_panel5.reload
1822         @sheet_panel5.t.should eq 4
1823         @sheet_panelc2.reload
1824         @sheet_panelc2.t.should eq 0
1825       end
1826       it 'falseを返す' do
1827         r = @sheet_panel6.store
1828         r.should be_false
1829       end
1830     end
1831     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したがシリアルチェックに失敗したとき' do
1832       before do
1833         SheetPanel.stub(:validate_t).with(any_args).and_return(false)
1834         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
1835         @sheet_panelc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
1836         @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1837         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1838         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1839         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1840         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1841         @ot = @sheet_panel4.t
1842         @sheet_panel4.t = 1
1843       end
1844       it '既存のtに変化がない' do
1845         @sheet_panel4.store @ot
1846         @sheet_panel.reload
1847         @sheet_panel.t.should eq 0
1848         @sheet_panel2.reload
1849         @sheet_panel2.t.should eq 1
1850         @sheet_panel3.reload
1851         @sheet_panel3.t.should eq 2
1852         @sheet_panel4.reload
1853         @sheet_panel4.t.should eq 3
1854         @sheet_panel5.reload
1855         @sheet_panel5.t.should eq 4
1856         @sheet_panelc2.reload
1857         @sheet_panelc2.t.should eq 0
1858       end
1859       it 'falseを返す' do
1860         r = @sheet_panel4.store @ot
1861         r.should be_false
1862       end
1863       it 'tにエラーメッセージが入っている' do
1864         @sheet_panel4.store @ot
1865         @sheet_panel4.errors[:t].should_not be_empty
1866         @sheet_panel4.valid?.should be_true
1867       end
1868     end
1869     context '編集不可だったとき' do
1870       before do
1871         @sheet_panel = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1872         SheetPanel.any_instance.stub(:allow?).and_return(false)
1873       end
1874       it '403Forbidden例外を返す' do
1875         lambda{
1876           @sheet_panel.store
1877         }.should raise_error(ActiveRecord::Forbidden)
1878       end
1879     end
1880   end
1881   describe '切り詰め処理つき削除に於いて' do
1882     before do
1883       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1884       @panel = FactoryGirl.create :panel, :author_id => @author.id
1885       @sheet_panel = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1886     end
1887     context 'つつがなく終わるとき' do
1888       it '削除される' do
1889         lambda{
1890           @sheet_panel.destroy_and_shorten
1891         }.should change(SheetPanel, :count ).by(-1)
1892       end
1893       it 'Trueを返す' do
1894         r = @sheet_panel.destroy_and_shorten
1895         r.should be_true 
1896       end
1897     end
1898     context '削除に失敗したとき' do
1899       before do
1900         SheetPanel.any_instance.stub(:destroy).and_return(false)
1901       end
1902       it 'ロールバックされる' do
1903         lambda{
1904           @sheet_panel.destroy_and_shorten
1905         }.should_not change(SheetPanel, :count )
1906       end
1907       it 'Falseを返す' do
1908         r = @sheet_panel.destroy_and_shorten
1909         r.should be_false
1910       end
1911     end
1912     #連携テスト。切り詰めが直接DBをいじる
1913     context '2件で先頭を削除したとき' do
1914       before do
1915         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1916       end
1917       it '行が削除される' do
1918         lambda{
1919           @sheet_panel.destroy_and_shorten
1920         }.should change(SheetPanel, :count ).by(-1)
1921       end
1922       it '先頭は削除される' do
1923         @sheet_panel.destroy_and_shorten
1924         lambda{
1925           SheetPanel.find @sheet_panel.id
1926         }.should raise_error(ActiveRecord::RecordNotFound)
1927       end
1928       it '2件目は前に詰められる' do
1929         @sheet_panel.destroy_and_shorten
1930         @sheet_panel2.reload
1931         @sheet_panel2.t.should eq 0
1932       end
1933     end
1934     context '3件で先頭を削除したとき' do
1935       before do
1936         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1937         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1938       end
1939       it '行が削除される' do
1940         lambda{
1941           @sheet_panel.destroy_and_shorten
1942         }.should change(SheetPanel, :count ).by(-1)
1943       end
1944       it '先頭は削除される' do
1945         @sheet_panel.destroy_and_shorten
1946         lambda{
1947           SheetPanel.find @sheet_panel.id
1948         }.should raise_error(ActiveRecord::RecordNotFound)
1949       end
1950       it '2件目は前に詰められる' do
1951         @sheet_panel.destroy_and_shorten
1952         @sheet_panel2.reload
1953         @sheet_panel2.t.should eq 0
1954       end
1955       it '3件目は前に詰められる' do
1956         @sheet_panel.destroy_and_shorten
1957         @sheet_panel3.reload
1958         @sheet_panel3.t.should eq 1
1959       end
1960     end
1961     context '5件で3件目を削除したとき' do
1962       before do
1963         @sheet_panel2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1964         @sheet_panel3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1965         @sheet_panel4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1966         @sheet_panel5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
1967       end
1968       it '行が削除される' do
1969         lambda{
1970           @sheet_panel3.destroy_and_shorten
1971         }.should change(SheetPanel, :count ).by(-1)
1972       end
1973       it '1件目は変化がない' do
1974         @sheet_panel3.destroy_and_shorten
1975         @sheet_panel.reload
1976         @sheet_panel.t.should eq 0
1977       end
1978       it '2件目は変化がない' do
1979         @sheet_panel3.destroy_and_shorten
1980         @sheet_panel2.reload
1981         @sheet_panel2.t.should eq 1
1982       end
1983       it '3件目は削除される' do
1984         @sheet_panel3.destroy_and_shorten
1985         lambda{
1986           SheetPanel.find @sheet_panel3.id
1987         }.should raise_error(ActiveRecord::RecordNotFound)
1988       end
1989       it '4件目は前に詰められる' do
1990         @sheet_panel3.destroy_and_shorten
1991         @sheet_panel4.reload
1992         @sheet_panel4.t.should eq 2
1993       end
1994       it '5件目は前に詰められる' do
1995         @sheet_panel3.destroy_and_shorten
1996         @sheet_panel5.reload
1997         @sheet_panel5.t.should eq 3
1998       end
1999     end
2000     #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
2001   end
2002 end