OSDN Git Service

t#30200:update i18n devise
[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 = FactoryGirl.create :author, :user_id => @user.id
13     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
14     @other_user = FactoryGirl.create( :user_yas)
15     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
16     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
17     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
18     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
19     @sbt = FactoryGirl.create :speech_balloon_template
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
156     before do
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
161     context '検査対象がnil(ゲスト)のとき' do
162       context 'クローズドモードのとき' do
163         before do
164           MagicNumber['run_mode'] = 1
165         end
166         it '不許可を返す。' do
167           r = @story.visible?(nil)
168           r.should be_false
169         end
170       end
171       context 'オープンモードのとき' do
172         before do
173           MagicNumber['run_mode'] = 0
174         end
175         it '公開コミックのストーリーなら許可する' do
176           Comic.any_instance.stub(:visible?).with(nil).and_return(true)
177           r = @story.visible?(nil)
178           r.should be_true
179         end
180         it '非公開コミックのストーリーなら許可しない' do
181           Comic.any_instance.stub(:visible?).with(nil).and_return(false)
182           r = @story.visible?(nil)
183           r.should be_false
184         end
185       end
186     end
187     context '検査対象が作家のとき' do
188       it '自分のコミックのストーリーなら許可する' do
189         Comic.any_instance.stub(:own?).with(@author).and_return(true)
190         Comic.any_instance.stub(:visible?).with(@author).and_return(true)
191         r = @story.visible?(@author)
192         r.should == true
193       end
194       it '他人の非公開コミックのストーリーなら許可しない' do
195         Comic.any_instance.stub(:own?).with(@other_author).and_return(false)
196         Comic.any_instance.stub(:visible?).with(@other_author).and_return(false)
197         r = @story.visible?(@other_author)
198         r.should == false
199       end
200       it '他人のコミックのストーリーでも公開なら許可する' do
201         Comic.any_instance.stub(:own?).with(@other_author).and_return(false)
202         Comic.any_instance.stub(:visible?).with(@other_author).and_return(true)
203         r = @story.visible?(@other_author)
204         r.should == true
205       end
206     end
207     context '検査対象がそれ以外のとき' do
208       it '不許可を返す。' do
209         r = @story.visible?(@admin)
210         r.should be_false
211       end
212     end
213   end
214   
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
375       it '一覧取得オプションを利用している' do
376         Story.stub(:list_opt).with(any_args).and_return({})
377         Story.should_receive(:list_opt).with(any_args).exactly(1)
378         r = Story.mylist @author
379       end
380     end
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)
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
447       it '単体取得オプションを利用している' do
448         Story.stub(:show_opt).with(any_args).and_return({})
449         Story.should_receive(:show_opt).with(any_args).exactly(1)
450         r = Story.show @story.id, @author
451       end
452       it '閲覧許可を問い合わせている' do
453         Story.any_instance.stub(:visible?).with(@author).and_return(true)
454         Story.any_instance.should_receive(:visible?).with(@author).exactly(1)
455         r = Story.show @story.id, @author
456       end
457     end
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
486       it '単体取得オプションを利用している' do
487         Story.stub(:show_opt).with(any_args).and_return({})
488         Story.should_receive(:show_opt).with(any_args).exactly(1)
489         r = Story.edit @story.id, @author
490       end
491       it '所持判定を問い合わせている' do
492         Story.any_instance.stub(:own?).with(any_args).and_return(true)
493         Story.any_instance.should_receive(:own?).with(any_args).exactly(1)
494         r = Story.edit @story.id, @author
495       end
496     end
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
581       #コマを作成しておく。
582       @panel = FactoryGirl.create :panel, :author_id => @author.id
583       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height
584       @sb = @panel.speech_balloons.create(
585         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)
586       )
587       @gc = @panel.ground_colors.create(
588         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :color_id => @color.id)
589       )
590       @gp = @panel.ground_pictures.create(
591         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)
592       )
593       @pc = @panel.panel_colors.create(
594         FactoryGirl.attributes_for(:panel_color, :panel_id => @panel.id, :code => 0xff0000)
595       )
596       @panel.reload
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
600     context '事前チェックする' do
601       before do
602         Panel.any_instance.stub(:elements).and_return('{}')
603       end
604       it 'コマ要素のjson出力を依頼している' do
605         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
606         Panel.any_instance.should_receive(:elements).with(any_args).exactly(1)
607         r = @story.story_as_json @author
608       end
609     end
610     it 'json textを返している' do
611       r = JSON.parse @story.story_as_json(@author)
612       r.is_a?(Hash).should be_true
613     end
614     it 'comic,author,panel,コマ要素を含んでいる' do
615       r = JSON.parse @story.story_as_json(@author)
616       r.has_key?('comic').should be_true
617       r['comic'].has_key?('author').should be_true
618       r.has_key?('author').should be_true
619       r.has_key?('panel').should be_true
620       r['panel'].has_key?('author').should be_true
621     end
622     context 'コマ閲覧許可のとき' do
623       before do
624         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
625       end
626       it 'コマ要素にコマ要素を追加している' do
627         r = JSON.parse @story.story_as_json(@author)
628         r['panel'].has_key?('elements').should be_true
629         r['panel']['elements'].should_not be_empty
630       end
631     end
632     context 'コマ閲覧不許可のとき' do
633       before do
634         Panel.any_instance.stub(:visible?).with(any_args).and_return(false)
635       end
636       it 'コマ要素にデータを含ませない' do
637         r = JSON.parse @story.story_as_json(@author)
638         r['panel'].has_key?('elements').should be_false
639       end
640     end
641   end
642   
643   describe 'ストーリーリストのjson出力に於いて' do
644     before do
645       @panel = FactoryGirl.create :panel, :author_id => @author.id
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}')
649     end
650     context 'つつがなく終わるとき' do
651       it 'ストーリーのjson出力を依頼している' do
652         Story.any_instance.should_receive(:story_as_json).with(@author).exactly(1)
653         r = Story.list_as_json_text [@story], @author
654       end
655     end
656     it 'json textを返している' do
657       r = Story.list_as_json_text [@story], @author
658       j = JSON.parse r
659       j.is_a?(Array).should be_true
660     end
661     it 'ストーリーを含んでいる' do
662       r = Story.list_as_json_text [@story], @author
663       j = JSON.parse r
664       j.first.has_key?('s').should be_true
665     end
666   end
667   
668   describe 'ライセンス素材に於いて' do
669     before do
670       #コマを作成しておく。
671       @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
672       @p2 = FactoryGirl.create :picture, :original_picture_id => @op2.id, :license_id => @license.id, :artist_id => @artist.id
673       @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p2.id
674       @panel = FactoryGirl.create :panel, :author_id => @author.id
675       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 0, :width => @p.width, :height => @p.height, :picture_id => @p.id
676       @panel.reload
677       @panel2 = FactoryGirl.create :panel, :author_id => @author.id
678       @pp2= FactoryGirl.create :panel_picture, :panel_id => @panel2.id, :t => 0, :width => @p2.width, :height => @p2.height, :picture_id => @p2.id
679       @panel2.reload
680       
681       @comic = FactoryGirl.create :comic, :author_id => @author.id
682       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
683       @story2 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel2.id
684     end
685     context '事前チェック' do
686     end
687     context 'つつがなく終わるとき' do
688     end
689     it '連想配列(値は実素材、キーは実素材id)を返している' do
690       r = Story.licensed_pictures [@story, @story2]
691       r.is_a?(Hash).should be_true
692       r.should have(2).items
693       r[@pp.picture_id].should eq @p
694       r[@pp2.picture_id].should eq @p2
695     end
696   end
697   
698   describe 't補充値に於いて' do
699     before do
700       @comic = FactoryGirl.create :comic, :author_id => @author.id
701       @panel = FactoryGirl.create :panel, :author_id => @author.id
702     end
703     
704     context 'コミック初のコマなら' do
705       it '0を補充値とする' do
706         @story = FactoryGirl.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
707         @story.t = nil
708         r = Story.new_t @story.comic_id
709         r.should eq 0
710       end
711     end
712     context 'コミックに一個コマがあるとき' do
713       it '1を補充値とする' do
714         FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
715         @story = FactoryGirl.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
716         @story.t = nil
717         r = Story.new_t @story.comic_id
718         r.should eq 1
719       end
720     end
721     context 'コミックに2個コマがあるとき' do
722       it '2を補充値とする' do
723         FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
724         FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
725         @story = FactoryGirl.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
726         @story.t = nil
727         r = Story.new_t @story.comic_id
728         r.should eq 2
729       end
730     end
731   end
732   describe 'シリアライズチェックに於いて' do
733     context 'つつがなく終わるとき' do
734       it '0からシリアライズされているならTrueを返す' do
735         r = Story.serial? [0, 1, 2]
736         r.should be_true
737       end
738       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
739         r = Story.serial? [0, 2, 1]
740         r.should be_true
741       end
742       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
743         r = Story.serial? [ 2, 1, 4, 3, 0]
744         r.should be_true
745       end
746     end
747     context '異常なとき' do
748       it '0から始まらないならFalseを返す' do
749         r = Story.serial? [1, 2, 3]
750         r.should be_false
751       end
752       it '連続していないならFalseを返す' do
753         r = Story.serial? [0, 1, 2, 4]
754         r.should be_false
755       end
756       it '連続していないならFalseを返す' do
757         r = Story.serial? [0, 1, 2, 4, 5]
758         r.should be_false
759       end
760     end
761   end
762   describe 't収集に於いて' do
763     before do
764       @comic = FactoryGirl.create :comic, :author_id => @author.id
765       @panel = FactoryGirl.create :panel, :author_id => @author.id
766       @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
767       @comic2 = FactoryGirl.create :comic, :author_id => @author.id
768       @c2story = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
769     end
770     context 'つつがなく終わるとき' do
771       it 'ストーリーから同一コミックのtだけを収集している' do
772         r = Story.collect_t @story
773         r.should eq [0]
774       end
775     end
776     context '複数コマのとき' do
777       it 'ストーリーから同一コミックのtだけを収集している' do
778         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
779         r = Story.collect_t @story
780         r.sort.should eq [0, 1]
781       end
782     end
783     context '複数コマでヨソのコミックも混じっているとき' do
784       it 'ストーリーから同一コミックのtだけを収集している' do
785         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
786         r = Story.collect_t @story
787         r.sort.should eq [0, 1]
788       end
789     end
790   end
791   describe 'tチェックに於いて' do
792     before do
793       @comic = FactoryGirl.create :comic, :author_id => @author.id
794       @panel = FactoryGirl.create :panel, :author_id => @author.id
795       @story = FactoryGirl.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
796     end
797     context 'つつがなく終わるとき' do
798       it 't収集を依頼している' do
799         Story.should_receive(:collect_t).with(any_args).exactly(1)
800         Story.stub(:collect_t).with(any_args).and_return([])
801         Story.stub(:serial?).with(any_args).and_return(true)
802         r = Story.validate_t @story
803       end
804       it '収集したtをシリアライズチェック依頼している' do
805         Story.stub(:collect_t).with(any_args).and_return([])
806         Story.should_receive(:serial?).with(any_args).exactly(1)
807         Story.stub(:serial?).with(any_args).and_return(true)
808         r = Story.validate_t @story
809       end
810     end
811     #実データでチェック
812     #依頼チェックだけでは不安なので最低限のチェックを
813     context '新規のとき' do
814       it '一件だけで正常通過している' do
815         @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
816         r = Story.validate_t @story
817         r.should be_true 
818       end
819     end
820     context '既存のとき' do
821       it '2件目を作っても正常通過している' do
822         @story = FactoryGirl.create :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
823         @story2 = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
824         r = Story.validate_t @story2
825         r.should be_true 
826       end
827     end
828   end
829   describe '挿入シフトに於いて' do
830     before do
831       @comic = FactoryGirl.create :comic, :author_id => @author.id
832       @panel = FactoryGirl.create :panel, :author_id => @author.id
833     end
834     context '依頼チェック' do
835       #テーブルが空で0に挿入
836       it 'Updateを依頼している' do
837         Story.stub(:update_all).with(any_args)
838         Story.should_receive(:update_all).with(any_args).exactly(1)
839         @story = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
840         @story.insert_shift
841       end
842     end
843     context 'テーブルに1件(t:0)で0に挿入したとき' do
844       before do
845         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
846         @story2 = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
847       end
848       it '既存の行を1にシフトしている' do
849         @story2.insert_shift
850         l = Story.find :all
851         l.first.t.should eq 1
852       end
853       it 'これから挿入するt(0)が欠番になっている' do
854         @story2.insert_shift
855         l = Story.find(:all).map {|s| s.t }
856         l.include?(0).should_not be_true
857       end
858     end
859     context 'テーブルに2件(t:0,1)で1に挿入したとき' do
860       before do
861         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
862         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
863         @story3 = FactoryGirl.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
864       end
865       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
866         @story3.insert_shift
867         l = Story.find(:all).map {|s| s.t }
868         l.sort.should eq [0, 2]
869       end
870     end
871     context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
872       before do
873         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
874         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
875         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
876         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
877         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
878         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
879       end
880       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
881         @story6.insert_shift
882         l = Story.find(:all).map {|s| s.t }
883         l.sort.should eq [0, 1, 3, 4, 5]
884       end
885     end
886     context '先ほどのケース+他のコミック1件で挿入したとき' do
887       before do
888         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
889         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
890         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
891         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
892         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
893         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
894         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
895         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
896       end
897       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
898         @story6.insert_shift
899         l = Story.find(:all, :conditions => ['comic_id = ?', @comic.id]).map {|s| s.t }
900         l.sort.should eq [0, 1, 3, 4, 5]
901       end
902       it '他のコミックに影響がない' do
903         ot = @storyc2.t
904         @story6.insert_shift
905         @storyc2.reload
906         @storyc2.t.should eq ot
907       end
908     end
909   end
910   describe '少ない方に移動に於いて' do
911     before do
912       @comic = FactoryGirl.create :comic, :author_id => @author.id
913       @panel = FactoryGirl.create :panel, :author_id => @author.id
914     end
915     context '依頼チェック' do
916       it 'Updateを依頼している' 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         Story.stub(:update_all).with(any_args)
920         Story.should_receive(:update_all).with(any_args).exactly(1)
921         ot = @story2.t
922         @story2.t = 0
923         @story2.lesser_shift ot
924       end
925     end
926     context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
927       before do
928         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
929         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
930         @ot = @story2.t
931         @story2.t = 0
932       end
933       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
934         #移動させたい行はそのまま残る
935         @story2.lesser_shift @ot
936         l = Story.find(:all).map {|s| s.t }
937         l.sort.should eq [1, 1]
938       end
939       it '既存のt0を1にシフトしている' do
940         @story2.lesser_shift @ot
941         @story.reload
942         @story.t.should eq 1
943       end
944     end
945     context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
946       before do
947         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
948         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
949         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
950         @ot = @story3.t
951         @story3.t = 1
952       end
953       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
954         #移動させたい行はそのまま残る
955         @story3.lesser_shift @ot
956         l = Story.find(:all).map {|s| s.t }
957         l.sort.should eq [0, 2, 2]
958       end
959       it '既存のt1を2にシフトしている' do
960         @story3.lesser_shift @ot
961         @story2.reload
962         @story2.t.should eq 2
963       end
964     end
965     context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
966       before do
967         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
968         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
969         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
970         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
971         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
972         @ot = @story4.t
973         @story4.t = 1
974       end
975       it 'これから挿入するt(1)が欠番になっている' do
976         #移動させたい行はそのまま残る
977         @story4.lesser_shift @ot
978         l = Story.find(:all).map {|s| s.t }
979         l.sort.should eq [0, 2, 3, 3, 4]
980       end
981       it '既存のt0には変化がない' do
982         @story4.lesser_shift @ot
983         @story.reload
984         @story.t.should eq 0
985       end
986       it '既存のt4には変化がない' do
987         @story4.lesser_shift @ot
988         @story5.reload
989         @story5.t.should eq 4
990       end
991       it '既存のt1を2にシフトしている' do
992         @story4.lesser_shift @ot
993         @story2.reload
994         @story2.t.should eq 2
995       end
996       it '既存のt2を3にシフトしている' do
997         @story4.lesser_shift @ot
998         @story3.reload
999         @story3.t.should eq 3
1000       end
1001     end
1002     context '先ほどのケース+他のコミック1件で挿入したとき' do
1003       before do
1004         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1005         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1006         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1007         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1008         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1009         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1010         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1011         @ot = @story4.t
1012         @story4.t = 1
1013       end
1014       it 'これから挿入するt(1)が欠番になっている' do
1015         @story4.lesser_shift @ot
1016         l = Story.find(:all).map {|s| s.t }
1017         l.sort.should eq [0, 0, 2, 3, 3, 4]
1018       end
1019       it '既存のt0には変化がない' do
1020         @story4.lesser_shift @ot
1021         @story.reload
1022         @story.t.should eq 0
1023       end
1024       it '既存のt4には変化がない' do
1025         @story4.lesser_shift @ot
1026         @story5.reload
1027         @story5.t.should eq 4
1028       end
1029       it '既存のt1を2にシフトしている' do
1030         @story4.lesser_shift @ot
1031         @story2.reload
1032         @story2.t.should eq 2
1033       end
1034       it '既存のt2を3にシフトしている' do
1035         @story4.lesser_shift @ot
1036         @story3.reload
1037         @story3.t.should eq 3
1038       end
1039       it '他のコミックに影響がない' do
1040         @story4.lesser_shift @ot
1041         @storyc2.reload
1042         @storyc2.t.should eq 0
1043       end
1044     end
1045     #例外ケース。
1046     #負のときは0として正常扱い
1047     context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
1048       before do
1049         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1050         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1051         @ot = @story2.t
1052         @story2.t = -1
1053       end
1054       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
1055         #移動させたい行はそのまま残る
1056         @story2.lesser_shift @ot
1057         l = Story.find(:all).map {|s| s.t }
1058         l.sort.should eq [1, 1]
1059       end
1060       it '既存のt0を1にシフトしている' do
1061         @story2.lesser_shift @ot
1062         @story.reload
1063         @story.t.should eq 1
1064       end
1065       it '既存のt1は0に補正されている' do
1066         @story2.lesser_shift @ot
1067         @story2.t.should eq 0
1068       end
1069     end
1070   end
1071   describe '大きい方に移動に於いて' do
1072     before do
1073       @comic = FactoryGirl.create :comic, :author_id => @author.id
1074       @panel = FactoryGirl.create :panel, :author_id => @author.id
1075     end
1076     context '依頼チェック' do
1077       it 'Updateを依頼している' 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         Story.stub(:update_all).with(any_args)
1081         Story.should_receive(:update_all).with(any_args).exactly(1)
1082         ot = @story.t
1083         @story.t = 1
1084         @story.higher_shift ot
1085       end
1086     end
1087     context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
1088       before do
1089         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1090         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1091         @ot = @story.t
1092         @story.t = 1
1093       end
1094       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1095         #移動させたい行はそのまま残る
1096         @story.higher_shift @ot
1097         l = Story.find(:all).map {|s| s.t }
1098         l.sort.should eq [0, 0]
1099       end
1100       it '既存のt1を0にシフトしている' do
1101         @story.higher_shift @ot
1102         @story2.reload
1103         @story2.t.should eq 0
1104       end
1105     end
1106     context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
1107       before do
1108         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1109         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1110         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1111         @ot = @story.t
1112         @story.t = 1
1113       end
1114       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1115         #移動させたい行はそのまま残る
1116         @story.higher_shift @ot
1117         l = Story.find(:all).map {|s| s.t }
1118         l.sort.should eq [0, 0, 2]
1119       end
1120       it '既存のt1を0にシフトしている' do
1121         @story.higher_shift @ot
1122         @story2.reload
1123         @story2.t.should eq 0
1124       end
1125     end
1126     context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
1127       before do
1128         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1129         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1130         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1131         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1132         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1133         @ot = @story2.t
1134         @story2.t = 3
1135       end
1136       it 'これから挿入するt(3)が欠番になっている' do
1137         #移動させたい行はそのまま残る
1138         @story2.higher_shift @ot
1139         l = Story.find(:all).map {|s| s.t }
1140         l.sort.should eq [0, 1, 1, 2, 4]
1141       end
1142       it '既存のt0には変化がない' do
1143         @story2.higher_shift @ot
1144         @story.reload
1145         @story.t.should eq 0
1146       end
1147       it '既存のt4には変化がない' do
1148         @story2.higher_shift @ot
1149         @story5.reload
1150         @story5.t.should eq 4
1151       end
1152       it '既存のt2を1にシフトしている' do
1153         @story2.higher_shift @ot
1154         @story3.reload
1155         @story3.t.should eq 1
1156       end
1157       it '既存のt3を2にシフトしている' do
1158         @story2.higher_shift @ot
1159         @story4.reload
1160         @story4.t.should eq 2
1161       end
1162     end
1163     context '先ほどのケース+他のコミック1件で挿入したとき' do
1164       before do
1165         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1166         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1167         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1168         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1169         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1170         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1171         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1172         @ot = @story2.t
1173         @story2.t = 3
1174       end
1175       it 'これから挿入するt(3)が欠番になっている' do
1176         #移動させたい行はそのまま残る
1177         @story2.higher_shift @ot
1178         l = Story.find(:all).map {|s| s.t }
1179         l.sort.should eq [0, 0, 1, 1, 2, 4]
1180       end
1181       it '既存のt0には変化がない' do
1182         @story2.higher_shift @ot
1183         @story.reload
1184         @story.t.should eq 0
1185       end
1186       it '既存のt4には変化がない' do
1187         @story2.higher_shift @ot
1188         @story5.reload
1189         @story5.t.should eq 4
1190       end
1191       it '既存のt2を1にシフトしている' do
1192         @story2.higher_shift @ot
1193         @story3.reload
1194         @story3.t.should eq 1
1195       end
1196       it '既存のt3を2にシフトしている' do
1197         @story2.higher_shift @ot
1198         @story4.reload
1199         @story4.t.should eq 2
1200       end
1201       it '他のコミックに影響がない' do
1202         @story2.higher_shift @ot
1203         @storyc2.reload
1204         @storyc2.t.should eq 0
1205       end
1206     end
1207     #例外ケース。
1208     #max超えたときはmaxとして正常扱い
1209     context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
1210       before do
1211         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1212         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1213         @ot = @story.t
1214         @story.t = 2
1215       end
1216       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1217         #移動させたい行はそのまま残る
1218         @story.higher_shift @ot
1219         l = Story.find(:all).map {|s| s.t }
1220         l.sort.should eq [0, 0]
1221       end
1222       it '既存のt1を0にシフトしている' do
1223         @story.higher_shift @ot
1224         @story2.reload
1225         @story2.t.should eq 0
1226       end
1227       it '既存のt0は1に補正されている' do
1228         @story.higher_shift @ot
1229         @story.t.should eq 1
1230       end
1231     end
1232   end
1233   describe '入れ替えに於いて' do
1234     before do
1235       @comic = FactoryGirl.create :comic, :author_id => @author.id
1236       @panel = FactoryGirl.create :panel, :author_id => @author.id
1237       @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1238       @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1239     end
1240     context '新tが旧tより小さいとき' do
1241       it '少ない方に移動を依頼している' do
1242         Story.any_instance.stub(:lesser_shift).with(any_args)
1243         Story.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
1244         ot = @story2.t
1245         @story2.t = 0
1246         @story2.update_shift ot
1247       end
1248     end
1249     context '新tが旧tより大きいとき' do
1250       it '大きい方に移動を依頼している' do
1251         Story.any_instance.stub(:higher_shift).with(any_args)
1252         Story.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
1253         ot = @story.t
1254         @story.t = 1
1255         @story.update_shift ot
1256       end
1257     end
1258   end
1259   describe '順序入れ替えに於いて' do
1260     before do
1261       @comic = FactoryGirl.create :comic, :author_id => @author.id
1262       @panel = FactoryGirl.create :panel, :author_id => @author.id
1263     end
1264     context 'オブジェクトが新規でtが空のとき' do
1265       it '末尾追加としてtを補充依頼している' do
1266         @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1267         Story.stub(:new_t).with(any_args).and_return(0)
1268         Story.should_receive(:new_t).with(any_args).exactly(1)
1269         @story.t = nil
1270         r = @story.rotate
1271       end
1272     end
1273     context 'オブジェクトが新規でtが設定されているとき' do
1274       it '挿入追加として挿入シフトを依頼している' do
1275         @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1276         Story.any_instance.stub(:insert_shift).with(any_args)
1277         Story.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
1278         @story.t = 0
1279         r = @story.rotate
1280       end
1281     end
1282     context 'オブジェクトが新規でなくtが設定されているとき' do
1283       it 'コマ移動として入れ替えを依頼している' do
1284         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1285         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1286         Story.any_instance.stub(:update_shift).with(any_args)
1287         Story.any_instance.should_receive(:update_shift).with(1).exactly(1)
1288         @story2.t = 0
1289         r = @story.rotate 1
1290       end
1291     end
1292     context 'オブジェクトが新規でなくtが空のとき' do
1293       it '入れ替えもシフトもせず、tを空のままにしている' do
1294         #結果、tに欠番が生じてシリアライズチェックでひっかかる
1295       end
1296     end
1297   end
1298   describe '編集許可に於いて' do
1299     before do
1300       @comic = FactoryGirl.create :comic, :author_id => @author.id
1301       @panel = FactoryGirl.create :panel, :author_id => @author.id
1302       @story = FactoryGirl.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1303     end
1304     context 'つつがなく終わるとき' do
1305       it 'trueを返す' do
1306         r = @story.allow?
1307         r.should be_true
1308       end
1309     end
1310     context 'コミックで引っかかるとき' do
1311       it 'falseを返す' do
1312         @story.comic_id = nil
1313         r = @story.allow?
1314         r.should be_false
1315       end
1316       it 'falseを返す' do
1317         Comic.any_instance.stub(:own?).with(any_args).and_return(false)
1318         r = @story.allow?
1319         r.should be_false
1320       end
1321     end
1322     context 'コマで引っかかるとき' do
1323       it 'falseを返す' do
1324         @story.panel_id = nil
1325         r = @story.allow?
1326         r.should be_false
1327       end
1328       it 'falseを返す' do
1329         Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
1330         r = @story.allow?
1331         r.should be_false
1332       end
1333     end
1334   end
1335   describe '保存に於いて' do
1336     before do
1337       @comic = FactoryGirl.create :comic, :author_id => @author.id
1338       @panel = FactoryGirl.create :panel, :author_id => @author.id
1339       @story = FactoryGirl.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1340     end
1341     context 'つつがなく終わるとき' do
1342       it '編集許可チェックを依頼している' do
1343         Story.any_instance.stub(:allow?).with(any_args).and_return(true)
1344         Story.any_instance.should_receive(:allow?).with(any_args).exactly(1)
1345         r = @story.store
1346       end
1347       it '順序入れ替えを依頼している' do
1348         Story.any_instance.stub(:rotate).with(any_args).and_return(0)
1349         Story.any_instance.should_receive(:rotate).with(any_args).exactly(1)
1350         Story.any_instance.stub(:save).with(any_args).and_return(true)
1351         Story.stub(:validate_t).with(any_args).and_return(true)
1352         r = @story.store 
1353       end
1354       it '保存を依頼している' do
1355         Story.stub(:new_t).with(any_args).and_return(0)
1356         Story.any_instance.stub(:save).with(any_args).and_return(true)
1357         Story.any_instance.should_receive(:save).with(any_args).exactly(1)
1358         Story.stub(:validate_t).with(any_args).and_return(true)
1359         r = @story.store
1360       end
1361       it 'tのシリアライズチェックを依頼している' do
1362         Story.stub(:new_t).with(any_args).and_return(0)
1363         Story.any_instance.stub(:save).with(any_args).and_return(true)
1364         Story.stub(:validate_t).with(any_args).and_return(true)
1365         Story.should_receive(:validate_t).with(any_args).exactly(1)
1366         r = @story.store
1367       end
1368     end
1369     #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1370     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したとき' do
1371       before do
1372         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1373         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1374         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1375         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1376         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1377         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1378         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1379         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1380       end
1381       it '既存のt0には変化がない' do
1382         @story6.store
1383         @story.reload
1384         @story.t.should eq 0
1385       end
1386       it '既存のt1には変化がない' do
1387         @story6.store
1388         @story2.reload
1389         @story2.t.should eq 1
1390       end
1391       it '既存のt2を3にシフトしている' do
1392         @story6.store
1393         @story3.reload
1394         @story3.t.should eq 3
1395       end
1396       it '既存のt3を4にシフトしている' do
1397         @story6.store
1398         @story4.reload
1399         @story4.t.should eq 4
1400       end
1401       it '既存のt5を5にシフトしている' do
1402         @story6.store
1403         @story5.reload
1404         @story5.t.should eq 5
1405       end
1406       it '新規のt2が作成されている' do
1407         @story6.store
1408         @story6.reload
1409         @story6.t.should eq 2
1410       end
1411       it '他のコミックに影響がない' do
1412         @ot = @storyc2.t
1413         @story6.store
1414         @storyc2.reload
1415         @storyc2.t.should eq @ot
1416       end
1417     end
1418     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したとき' do
1419       before do
1420         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1421         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1422         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1423         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1424         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1425         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1426         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1427         @ot = @story4.t
1428         @story4.t = 1
1429       end
1430       it '既存のt0には変化がない' do
1431         @story4.store @ot
1432         @story.reload
1433         @story.t.should eq 0
1434       end
1435       it '既存のt4には変化がない' do
1436         @story4.store @ot
1437         @story5.reload
1438         @story5.t.should eq 4
1439       end
1440       it '既存のt1を2にシフトしている' do
1441         @story4.store @ot
1442         @story2.reload
1443         @story2.t.should eq 2
1444       end
1445       it '既存のt2を3にシフトしている' do
1446         @story4.store @ot
1447         @story3.reload
1448         @story3.t.should eq 3
1449       end
1450       it '既存のt3を1にシフトしている' do
1451         @story4.store @ot
1452         @story4.reload
1453         @story4.t.should eq 1
1454       end
1455       it '他のコミックに影響がない' do
1456         @story4.store @ot
1457         @storyc2.reload
1458         @storyc2.t.should eq 0
1459       end
1460     end
1461     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で1を3に移動したとき' do
1462       before do
1463         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1464         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1465         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1466         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1467         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1468         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1469         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1470         @ot = @story2.t
1471         @story2.t = 3
1472       end
1473       it '既存のt0には変化がない' do
1474         @story2.store @ot
1475         @story.reload
1476         @story.t.should eq 0
1477       end
1478       it '既存のt4には変化がない' do
1479         @story2.store @ot
1480         @story5.reload
1481         @story5.t.should eq 4
1482       end
1483       it '既存のt1を3にシフトしている' do
1484         @story2.store @ot
1485         @story2.reload
1486         @story2.t.should eq 3
1487       end
1488       it '既存のt2を1にシフトしている' do
1489         @story2.store @ot
1490         @story3.reload
1491         @story3.t.should eq 1
1492       end
1493       it '既存のt3を2にシフトしている' do
1494         @story2.store @ot
1495         @story4.reload
1496         @story4.t.should eq 2
1497       end
1498       it '他のコミックに影響がない' do
1499         @story2.store @ot
1500         @storyc2.reload
1501         @storyc2.t.should eq 0
1502       end
1503     end
1504     #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1505     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したが保存に失敗したとき' do
1506       before do
1507         Story.any_instance.stub(:save).with(any_args).and_return(false)
1508         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1509         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1510         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1511         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1512         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1513         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1514         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1515         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1516       end
1517       it '既存のtに変化がない' do
1518         @story6.store
1519         @story.reload
1520         @story.t.should eq 0
1521         @story2.reload
1522         @story2.t.should eq 1
1523         @story3.reload
1524         @story3.t.should eq 2
1525         @story4.reload
1526         @story4.t.should eq 3
1527         @story5.reload
1528         @story5.t.should eq 4
1529         @storyc2.reload
1530         @storyc2.t.should eq 0
1531       end
1532       it 'falseを返す' do
1533         r = @story6.store
1534         r.should be_false
1535       end
1536     end
1537     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したがシリアルチェックに失敗したとき' do
1538       before do
1539         Story.stub(:validate_t).with(any_args).and_return(false)
1540         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1541         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1542         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1543         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1544         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1545         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1546         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1547         @ot = @story4.t
1548         @story4.t = 1
1549       end
1550       it '既存のtに変化がない' do
1551         @story4.store @ot
1552         @story.reload
1553         @story.t.should eq 0
1554         @story2.reload
1555         @story2.t.should eq 1
1556         @story3.reload
1557         @story3.t.should eq 2
1558         @story4.reload
1559         @story4.t.should eq 3
1560         @story5.reload
1561         @story5.t.should eq 4
1562         @storyc2.reload
1563         @storyc2.t.should eq 0
1564       end
1565       it 'falseを返す' do
1566         r = @story4.store @ot
1567         r.should be_false
1568       end
1569       it 'tにエラーメッセージが入っている' do
1570         @story4.store @ot
1571         @story4.errors[:t].should_not be_empty
1572         @story4.valid?.should be_true
1573       end
1574     end
1575     context '編集不可だったとき' do
1576       before do
1577         @story = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1578         Story.any_instance.stub(:allow?).and_return(false)
1579       end
1580       it '403Forbidden例外を返す' do
1581         lambda{
1582           @story.store
1583         }.should raise_error(ActiveRecord::Forbidden)
1584       end
1585     end
1586   end
1587   describe '切り詰め処理つき削除に於いて' do
1588     before do
1589       @comic = FactoryGirl.create :comic, :author_id => @author.id
1590       @panel = FactoryGirl.create :panel, :author_id => @author.id
1591       @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1592     end
1593     context 'つつがなく終わるとき' do
1594       it '削除される' do
1595         lambda{
1596           @story.destroy_and_shorten
1597         }.should change(Story, :count ).by(-1)
1598       end
1599     end
1600     #連携テスト。切り詰めが直接DBをいじる
1601     context '2件で先頭を削除したとき' do
1602       before do
1603         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1604       end
1605       it '行が削除される' do
1606         lambda{
1607           @story.destroy_and_shorten
1608         }.should change(Story, :count ).by(-1)
1609       end
1610       it '先頭は削除される' do
1611         @story.destroy_and_shorten
1612         lambda{
1613           Story.find @story.id
1614         }.should raise_error(ActiveRecord::RecordNotFound)
1615       end
1616       it '2件目は前に詰められる' do
1617         @story.destroy_and_shorten
1618         @story2.reload
1619         @story2.t.should eq 0
1620       end
1621     end
1622     context '3件で先頭を削除したとき' do
1623       before do
1624         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1625         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1626       end
1627       it '行が削除される' do
1628         lambda{
1629           @story.destroy_and_shorten
1630         }.should change(Story, :count ).by(-1)
1631       end
1632       it '先頭は削除される' do
1633         @story.destroy_and_shorten
1634         lambda{
1635           Story.find @story.id
1636         }.should raise_error(ActiveRecord::RecordNotFound)
1637       end
1638       it '2件目は前に詰められる' do
1639         @story.destroy_and_shorten
1640         @story2.reload
1641         @story2.t.should eq 0
1642       end
1643       it '3件目は前に詰められる' do
1644         @story.destroy_and_shorten
1645         @story3.reload
1646         @story3.t.should eq 1
1647       end
1648     end
1649     context '5件で3件目を削除したとき' do
1650       before do
1651         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1652         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1653         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1654         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1655       end
1656       it '行が削除される' do
1657         lambda{
1658           @story3.destroy_and_shorten
1659         }.should change(Story, :count ).by(-1)
1660       end
1661       it '1件目は変化がない' do
1662         @story3.destroy_and_shorten
1663         @story.reload
1664         @story.t.should eq 0
1665       end
1666       it '2件目は変化がない' do
1667         @story3.destroy_and_shorten
1668         @story2.reload
1669         @story2.t.should eq 1
1670       end
1671       it '3件目は削除される' do
1672         @story3.destroy_and_shorten
1673         lambda{
1674           Story.find @story3.id
1675         }.should raise_error(ActiveRecord::RecordNotFound)
1676       end
1677       it '4件目は前に詰められる' do
1678         @story3.destroy_and_shorten
1679         @story4.reload
1680         @story4.t.should eq 2
1681       end
1682       it '5件目は前に詰められる' do
1683         @story3.destroy_and_shorten
1684         @story5.reload
1685         @story5.t.should eq 3
1686       end
1687     end
1688     #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1689   end
1690 end