OSDN Git Service

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