OSDN Git Service

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