OSDN Git Service

t#29018:modify collect t on story
[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     Factory :admin
7     @license = Factory :license
8     @user = Factory( :user_yas)
9     @author = @user.author
10     @artist = Factory :artist_yas, :author_id => @author.id
11     @other_user = Factory( :user_yas)
12     @other_author = @other_user.author
13   end
14   
15   describe '検証に於いて' do
16     before do
17       @comic = Factory :comic, :author_id => @author.id
18       @panel = Factory :panel, :author_id => @author.id
19       @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
20     end
21     
22     it 'オーソドックスなデータなら通る' do
23       @story.should be_valid
24     end
25     
26     context 'comic_idを検証するとき' do
27       it 'テストデータの確認' do
28         @story.comic_id = @comic.id
29         @story.should be_valid
30       end
31       it 'nullなら失敗する' do
32         @story.comic_id = nil
33         @story.should_not be_valid
34       end
35       it '数値でなければ失敗する' do
36         @story.comic_id = 'a'
37         @story.should_not be_valid
38       end
39       it '存在するコミックでなければ失敗する' do
40         @story.comic_id = 0
41         @story.should_not be_valid
42       end
43     end
44     
45     context 'panel_idを検証するとき' do
46       it 'テストデータの確認' do
47         @story.panel_id = @panel.id
48         @story.should be_valid
49       end
50       it 'nullなら失敗する' do
51         @story.panel_id = nil
52         @story.should_not be_valid
53       end
54       it '数値でなければ失敗する' do
55         @story.panel_id = 'a'
56         @story.should_not be_valid
57       end
58       it '存在するコマでなければ失敗する' do
59         @story.panel_id = 0
60         @story.should_not be_valid
61       end
62     end
63     
64     context 'tを検証するとき' do
65       before do
66       end
67       it 'テストデータの確認' do
68         @story.t = 0
69         @story.should be_valid
70       end
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 'テストデータの確認' do
87         @story.author_id = @author.id
88         @story.should be_valid
89       end
90       it 'nullなら失敗する' do
91         @story.author_id = nil
92         @story.should_not be_valid
93       end
94       it '数値でなければ失敗する' do
95         @story.author_id = 'a'
96         @story.should_not be_valid
97       end
98       it '存在する作家でなければ失敗する' do
99         @story.author_id = 0
100         @story.should_not be_valid
101       end
102     end
103     context '全体を検証するとき' do
104       before do
105         @story = Factory :story, :author_id => @author.id
106       end
107     end
108   end
109   
110   describe 'データ補充に於いて' do
111     before do
112       @comic = Factory :comic, :author_id => @author.id
113       @panel = Factory :panel, :author_id => @author.id
114     end
115     
116     context 'author_idを補充' do
117       it '問答無用でauthor_idを補充する' do
118         @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id
119         @story.author_id = nil
120         @story.supply_default @author
121         @story.author_id.should eq @author.id
122       end
123     end
124     
125   end
126   
127   describe '作者判定に於いて' do
128     before do
129       @comic = Factory :comic, :author_id => @author.id
130       @panel = Factory :panel, :author_id => @author.id
131       @story = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
132       @comico = Factory :comic, :author_id => @other_author.id
133       @panelo = Factory :panel, :author_id => @other_author.id
134       @storyo = Factory :story, :author_id => @other_author.id, :comic_id => @comico.id, :panel_id => @panelo.id
135     end
136     it '自分のストーリーならyes' do
137       @story.own?(@author).should == true
138     end
139     it '他人のストーリーならno' do
140       @storyo.own?(@author).should == false
141     end
142     it '作家が不明ならno' do
143       @story.own?(nil).should == false
144     end
145   end
146   describe '単体取得に於いて' do
147     before do
148       @comic = Factory :comic, :author_id => @author.id
149       @panel = Factory :panel, :author_id => @author.id
150       @story = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
151     end
152     context 'オーナー指定がないとき' do
153       it '指定のストーリーを返す' do
154         l = Story.show @story.id, @author
155         l.should eq @story
156       end
157     end
158     context 'オーナー指定のとき' do
159       it '指定のストーリーが自分のものならそれを返す' do
160         l = Story.show @story.id, @author
161         l.should eq @story
162       end
163       context '他人のストーリーを開こうとしたとき' do
164         it '403Forbidden例外を返す' do
165           Story.any_instance.stub(:own?).and_return(false)
166           lambda{
167             Story.show @story.id, @author
168           }.should raise_error(ActiveRecord::Forbidden)
169         end
170       end
171     end
172     context '存在しないストーリーを開こうとしたとき' do
173       it '404RecordNotFound例外を返す' do
174         lambda{
175           Story.show 110, @author
176         }.should raise_error(ActiveRecord::RecordNotFound)
177       end
178     end
179   end
180   
181   describe '一覧取得に於いて' do
182     before do
183       @comic = Factory :comic, :author_id => @author.id
184       @panel = Factory :panel, :author_id => @author.id
185       @story = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
186     end
187     context 'offset補正について' do
188       it '文字列から数値に変換される' do
189         Story.offset(100, '8').should eq 8
190       end
191       it 'nilの場合は0になる' do
192         Story.offset(100).should eq 0
193       end
194       #投稿されたコマ数以上の値が指定されたときは、最後のコマだけになる
195       #最後のコマとは、コマ数‐1.
196       it '1件のときオフセット1なら0になる' do
197         Story.offset(1, '1').should eq 0
198       end
199       it '5件のときオフセット5なら4になる' do
200         Story.offset(5, '5').should eq 4
201       end
202       # 負の値が指定されたときは、最後のコマから数えてコマを飛ばして表示する。
203       #-4のときは、最後から4つのコマを表示する。 
204       it '2件のときオフセット-1なら1になる' do
205         Story.offset(2, '-1').should eq 1
206       end
207       it '5件のときオフセット-2なら3になる' do
208         Story.offset(5, '-2').should eq 3
209       end
210       # 最終的なが負になるなど、不正な値が入ったときは0となる。 
211       it '2件のときオフセット-5なら0になる' do
212         Story.offset(2, '-5').should eq 0
213       end
214     end
215     context 'panel_count補正について' do
216       it '文字列から数値に変換される' do
217         Story.panel_count(100, '7').should eq 7
218       end
219       it 'nilの場合はStory.default_panel_sizeになる' do
220         Story.panel_count(100).should eq Story.default_panel_size
221       end
222       it '0以下の場合はStory.default_panel_sizeになる' do
223         Story.panel_count(100, '0').should eq Story.default_panel_size
224       end
225       it 'Story.max_panel_sizeを超えた場合はStory.max_panel_sizeになる' do
226         Story.panel_count(100, '1000').should eq Story.max_panel_size
227       end
228     end
229     it 'リストを返す' do
230       c = Story.list @comic, @author
231       c.should eq [@story]
232     end
233     it 't順で並んでいる' do
234       v = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
235       c = Story.list @comic, @author
236       c.should eq [ @story, v]
237     end
238     context 'DBに5件あって1ページの件数を2件に変えたとして' do
239       before do
240         @story2 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
241         @story3 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 2
242         @story4 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 3
243         @story5 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 4
244       end
245       it 'offset=0なら末尾2件を返す' do
246         #時系列で並んでいる
247         c = Story.list( @comic, @author, 0, 2)
248         c.should eq [@story, @story2]
249       end
250       it 'offset=2なら中間2件を返す' do
251         c = Story.list(@comic, @author, 2, 2)
252         c.should eq [@story3, @story4]
253       end
254       it 'offset=4なら先頭1件を返す' do
255         c = Story.list(@comic, @author, 4, 2)
256         c.should eq [@story5]
257       end
258     end
259   end
260   describe 'list関連テーブルプションに於いて' do
261     it 'includeキーを含んでいる' do
262       r = Story.list_opt
263       r.has_key?(:include).should be_true
264     end
265     it '3つの項目を含んでいる' do
266       r = Story.list_opt[:include]
267       r.should have(3).items
268     end
269     it 'コミックを含んでいる' do
270       r = Story.list_opt[:include]
271       r.has_key?(:comic).should be_true
272     end
273       it 'コミックは作家を含んでいる' do
274         r = Story.list_opt[:include]
275         r[:comic].has_key?(:author).should be_true
276       end
277     it '作家を含んでいる' do
278       r = Story.list_opt[:include]
279       r.has_key?(:author).should be_true
280     end
281     it 'コマを含んでいる' do
282       r = Story.list_opt[:include]
283       r.has_key?(:panel).should be_true
284     end
285       it 'コマは作家を含んでいる' do
286         r = Story.list_opt[:include]
287         r[:panel].has_key?(:author).should be_true
288       end
289       it 'コマはコマ絵を含んでいる' do
290         r = Story.list_opt[:include]
291         r[:panel].has_key?(:panel_pictures).should be_true
292       end
293       it 'コマはフキダシを含んでいる' do
294         r = Story.list_opt[:include]
295         r[:panel].has_key?(:speech_balloons).should be_true
296       end
297   end
298   describe 'json一覧出力オプションに於いて' do
299     it 'includeキーを含んでいる' do
300       r = Story.list_json_opt
301       r.has_key?(:include).should be_true
302     end
303     it '3つの項目を含んでいる' do
304       r = Story.list_json_opt[:include]
305       r.should have(3).items
306     end
307     it 'コミックを含んでいる' do
308       r = Story.list_json_opt[:include]
309       r.has_key?(:comic).should be_true
310     end
311       it 'コミックは作家を含んでいる' do
312         r = Story.list_json_opt[:include]
313         r[:comic].has_key?(:author).should be_true
314       end
315     it '作家を含んでいる' do
316       r = Story.list_json_opt[:include]
317       r.has_key?(:author).should be_true
318     end
319     it 'コマを含んでいる' do
320       r = Story.list_json_opt[:include]
321       r.has_key?(:panel).should be_true
322     end
323       it 'コマは作家を含んでいる' do
324         r = Story.list_json_opt[:include]
325         r[:panel].has_key?(:author).should be_true
326       end
327       it 'コマはコマ絵を含んでいる' do
328         r = Story.list_json_opt[:include]
329         r[:panel].has_key?(:panel_pictures).should be_true
330       end
331       it 'コマはフキダシを含んでいる' do
332         r = Story.list_json_opt[:include]
333         r[:panel].has_key?(:speech_balloons).should be_true
334       end
335   end
336   describe 't補充値に於いて' do
337     before do
338       @comic = Factory :comic, :author_id => @author.id
339       @panel = Factory :panel, :author_id => @author.id
340     end
341     
342     context 'コミック初のコマなら' do
343       it '0を補充値とする' do
344         @story = Factory.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
345         @story.t = nil
346         r = Story.new_t @story.comic_id
347         r.should eq 0
348       end
349     end
350     context 'コミックに一個コマがあるとき' do
351       it '1を補充値とする' do
352         Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
353         @story = Factory.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
354         @story.t = nil
355         r = Story.new_t @story.comic_id
356         r.should eq 1
357       end
358     end
359     context 'コミックに2個コマがあるとき' do
360       it '2を補充値とする' do
361         Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
362         Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
363         @story = Factory.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
364         @story.t = nil
365         r = Story.new_t @story.comic_id
366         r.should eq 2
367       end
368     end
369   end
370   describe 'シリアライズチェックに於いて' do
371     context 'つつがなく終わるとき' do
372       it '0からシリアライズされているならTrueを返す' do
373         r = Story.serial? [0, 1, 2]
374         r.should be_true
375       end
376       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
377         r = Story.serial? [0, 2, 1]
378         r.should be_true
379       end
380       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
381         r = Story.serial? [ 2, 1, 4, 3, 0]
382         r.should be_true
383       end
384     end
385     context '異常なとき' do
386       it '0から始まらないならFalseを返す' do
387         r = Story.serial? [1, 2, 3]
388         r.should be_false
389       end
390       it '連続していないならFalseを返す' do
391         r = Story.serial? [0, 1, 2, 4]
392         r.should be_false
393       end
394       it '連続していないならFalseを返す' do
395         r = Story.serial? [0, 1, 2, 4, 5]
396         r.should be_false
397       end
398     end
399   end
400   describe 't収集に於いて' do
401     before do
402       @comic = Factory :comic, :author_id => @author.id
403       @panel = Factory :panel, :author_id => @author.id
404       @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
405       @comic2 = Factory :comic, :author_id => @author.id
406       @c2story = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
407     end
408     context 'つつがなく終わるとき' do
409       it 'ストーリーから同一コミックのtだけを収集している' do
410         r = Story.collect_t @story
411         r.should eq [0]
412       end
413     end
414     context '複数コマのとき' do
415       it 'ストーリーから同一コミックのtだけを収集している' do
416         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
417         r = Story.collect_t @story
418         r.sort.should eq [0, 1]
419       end
420     end
421     context '複数コマでヨソのコミックも混じっているとき' do
422       it 'ストーリーから同一コミックのtだけを収集している' do
423         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
424         r = Story.collect_t @story
425         r.sort.should eq [0, 1]
426       end
427     end
428   end
429   describe 'tチェックに於いて' do
430     before do
431       @comic = Factory :comic, :author_id => @author.id
432       @panel = Factory :panel, :author_id => @author.id
433       @story = Factory.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
434     end
435     context 'つつがなく終わるとき' do
436       it 't収集を依頼している' do
437         Story.should_receive(:collect_t).with(any_args).exactly(1)
438         Story.stub(:collect_t).with(any_args).and_return([])
439         Story.stub(:serial?).with(any_args).and_return(true)
440         r = Story.validate_t @story
441       end
442       it '収集したtをシリアライズチェック依頼している' do
443         Story.stub(:collect_t).with(any_args).and_return([])
444         Story.should_receive(:serial?).with(any_args).exactly(1)
445         Story.stub(:serial?).with(any_args).and_return(true)
446         r = Story.validate_t @story
447       end
448     end
449     #実データでチェック
450     #依頼チェックだけでは不安なので最低限のチェックを
451     context '新規のとき' do
452       it '一件だけで正常通過している' do
453         @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
454         r = Story.validate_t @story
455         r.should be_true 
456       end
457     end
458     context '既存のとき' do
459       it '2件目を作っても正常通過している' do
460         @story = Factory :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
461         @story2 = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
462         r = Story.validate_t @story2
463         r.should be_true 
464       end
465     end
466   end
467   describe '挿入シフトに於いて' do
468     before do
469       @comic = Factory :comic, :author_id => @author.id
470       @panel = Factory :panel, :author_id => @author.id
471     end
472     context '依頼チェック' do
473       #テーブルが空で0に挿入
474       it 'Updateを依頼している' do
475         Story.stub(:update_all).with(any_args)
476         Story.should_receive(:update_all).with(any_args).exactly(1)
477         @story = Factory.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
478         @story.insert_shift
479       end
480     end
481     context 'テーブルに1件(t:0)で0に挿入したとき' do
482       before do
483         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
484         @story2 = Factory.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
485       end
486       it '既存の行を1にシフトしている' do
487         @story2.insert_shift
488         l = Story.find :all
489         l.first.t.should eq 1
490       end
491       it 'これから挿入するt(0)が欠番になっている' do
492         @story2.insert_shift
493         l = Story.find(:all).map {|s| s.t }
494         l.include?(0).should_not be_true
495       end
496     end
497     context 'テーブルに2件(t:0,1)で1に挿入したとき' do
498       before do
499         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
500         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
501         @story3 = Factory.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
502       end
503       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
504         @story3.insert_shift
505         l = Story.find(:all).map {|s| s.t }
506         l.sort.should eq [0, 2]
507       end
508     end
509     context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
510       before do
511         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
512         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
513         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
514         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
515         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
516         @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
517       end
518       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
519         @story6.insert_shift
520         l = Story.find(:all).map {|s| s.t }
521         l.sort.should eq [0, 1, 3, 4, 5]
522       end
523     end
524     context '先ほどのケース+他のコミック1件で挿入したとき' do
525       before do
526         @comic2 = Factory :comic, :author_id => @author.id
527         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
528         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
529         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
530         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
531         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
532         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
533         @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
534       end
535       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
536         @story6.insert_shift
537         l = Story.find(:all, :conditions => ['comic_id = ?', @comic.id]).map {|s| s.t }
538         l.sort.should eq [0, 1, 3, 4, 5]
539       end
540       it '他のコミックに影響がない' do
541         ot = @storyc2.t
542         @story6.insert_shift
543         @storyc2.reload
544         @storyc2.t.should eq ot
545       end
546     end
547   end
548   describe '少ない方に移動に於いて' do
549     before do
550       @comic = Factory :comic, :author_id => @author.id
551       @panel = Factory :panel, :author_id => @author.id
552     end
553     context '依頼チェック' do
554       it 'Updateを依頼している' do
555         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
556         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
557         Story.stub(:update_all).with(any_args)
558         Story.should_receive(:update_all).with(any_args).exactly(1)
559         ot = @story2.t
560         @story2.t = 0
561         @story2.lesser_shift ot
562       end
563     end
564     context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
565       before do
566         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
567         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
568         @ot = @story2.t
569         @story2.t = 0
570       end
571       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
572         #移動させたい行はそのまま残る
573         @story2.lesser_shift @ot
574         l = Story.find(:all).map {|s| s.t }
575         l.sort.should eq [1, 1]
576       end
577       it '既存のt0を1にシフトしている' do
578         @story2.lesser_shift @ot
579         @story.reload
580         @story.t.should eq 1
581       end
582     end
583     context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
584       before do
585         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
586         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
587         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
588         @ot = @story3.t
589         @story3.t = 1
590       end
591       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
592         #移動させたい行はそのまま残る
593         @story3.lesser_shift @ot
594         l = Story.find(:all).map {|s| s.t }
595         l.sort.should eq [0, 2, 2]
596       end
597       it '既存のt1を2にシフトしている' do
598         @story3.lesser_shift @ot
599         @story2.reload
600         @story2.t.should eq 2
601       end
602     end
603     context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
604       before do
605         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
606         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
607         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
608         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
609         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
610         @ot = @story4.t
611         @story4.t = 1
612       end
613       it 'これから挿入するt(1)が欠番になっている' do
614         #移動させたい行はそのまま残る
615         @story4.lesser_shift @ot
616         l = Story.find(:all).map {|s| s.t }
617         l.sort.should eq [0, 2, 3, 3, 4]
618       end
619       it '既存のt0には変化がない' do
620         @story4.lesser_shift @ot
621         @story.reload
622         @story.t.should eq 0
623       end
624       it '既存のt4には変化がない' do
625         @story4.lesser_shift @ot
626         @story5.reload
627         @story5.t.should eq 4
628       end
629       it '既存のt1を2にシフトしている' do
630         @story4.lesser_shift @ot
631         @story2.reload
632         @story2.t.should eq 2
633       end
634       it '既存のt2を3にシフトしている' do
635         @story4.lesser_shift @ot
636         @story3.reload
637         @story3.t.should eq 3
638       end
639     end
640     context '先ほどのケース+他のコミック1件で挿入したとき' do
641       before do
642         @comic2 = Factory :comic, :author_id => @author.id
643         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
644         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
645         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
646         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
647         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
648         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
649         @ot = @story4.t
650         @story4.t = 1
651       end
652       it 'これから挿入するt(1)が欠番になっている' do
653         @story4.lesser_shift @ot
654         l = Story.find(:all).map {|s| s.t }
655         l.sort.should eq [0, 0, 2, 3, 3, 4]
656       end
657       it '既存のt0には変化がない' do
658         @story4.lesser_shift @ot
659         @story.reload
660         @story.t.should eq 0
661       end
662       it '既存のt4には変化がない' do
663         @story4.lesser_shift @ot
664         @story5.reload
665         @story5.t.should eq 4
666       end
667       it '既存のt1を2にシフトしている' do
668         @story4.lesser_shift @ot
669         @story2.reload
670         @story2.t.should eq 2
671       end
672       it '既存のt2を3にシフトしている' do
673         @story4.lesser_shift @ot
674         @story3.reload
675         @story3.t.should eq 3
676       end
677       it '他のコミックに影響がない' do
678         @story4.lesser_shift @ot
679         @storyc2.reload
680         @storyc2.t.should eq 0
681       end
682     end
683   end
684   describe '大きい方に移動に於いて' do
685     before do
686       @comic = Factory :comic, :author_id => @author.id
687       @panel = Factory :panel, :author_id => @author.id
688     end
689     context '依頼チェック' do
690       it 'Updateを依頼している' do
691         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
692         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
693         Story.stub(:update_all).with(any_args)
694         Story.should_receive(:update_all).with(any_args).exactly(1)
695         ot = @story.t
696         @story.t = 1
697         @story.higher_shift ot
698       end
699     end
700     context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
701       before do
702         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
703         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
704         @ot = @story.t
705         @story.t = 1
706       end
707       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
708         #移動させたい行はそのまま残る
709         @story.higher_shift @ot
710         l = Story.find(:all).map {|s| s.t }
711         l.sort.should eq [0, 0]
712       end
713       it '既存のt1を0にシフトしている' do
714         @story.higher_shift @ot
715         @story2.reload
716         @story2.t.should eq 0
717       end
718     end
719     context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
720       before do
721         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
722         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
723         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
724         @ot = @story.t
725         @story.t = 1
726       end
727       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
728         #移動させたい行はそのまま残る
729         @story.higher_shift @ot
730         l = Story.find(:all).map {|s| s.t }
731         l.sort.should eq [0, 0, 2]
732       end
733       it '既存のt1を0にシフトしている' do
734         @story.higher_shift @ot
735         @story2.reload
736         @story2.t.should eq 0
737       end
738     end
739     context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
740       before do
741         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
742         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
743         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
744         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
745         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
746         @ot = @story2.t
747         @story2.t = 3
748       end
749       it 'これから挿入するt(3)が欠番になっている' do
750         #移動させたい行はそのまま残る
751         @story2.higher_shift @ot
752         l = Story.find(:all).map {|s| s.t }
753         l.sort.should eq [0, 1, 1, 2, 4]
754       end
755       it '既存のt0には変化がない' do
756         @story2.higher_shift @ot
757         @story.reload
758         @story.t.should eq 0
759       end
760       it '既存のt4には変化がない' do
761         @story2.higher_shift @ot
762         @story5.reload
763         @story5.t.should eq 4
764       end
765       it '既存のt2を1にシフトしている' do
766         @story2.higher_shift @ot
767         @story3.reload
768         @story3.t.should eq 1
769       end
770       it '既存のt3を2にシフトしている' do
771         @story2.higher_shift @ot
772         @story4.reload
773         @story4.t.should eq 2
774       end
775     end
776     context '先ほどのケース+他のコミック1件で挿入したとき' do
777       before do
778         @comic2 = Factory :comic, :author_id => @author.id
779         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
780         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
781         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
782         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
783         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
784         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
785         @ot = @story2.t
786         @story2.t = 3
787       end
788       it 'これから挿入するt(3)が欠番になっている' do
789         #移動させたい行はそのまま残る
790         @story2.higher_shift @ot
791         l = Story.find(:all).map {|s| s.t }
792         l.sort.should eq [0, 0, 1, 1, 2, 4]
793       end
794       it '既存のt0には変化がない' do
795         @story2.higher_shift @ot
796         @story.reload
797         @story.t.should eq 0
798       end
799       it '既存のt4には変化がない' do
800         @story2.higher_shift @ot
801         @story5.reload
802         @story5.t.should eq 4
803       end
804       it '既存のt2を1にシフトしている' do
805         @story2.higher_shift @ot
806         @story3.reload
807         @story3.t.should eq 1
808       end
809       it '既存のt3を2にシフトしている' do
810         @story2.higher_shift @ot
811         @story4.reload
812         @story4.t.should eq 2
813       end
814       it '他のコミックに影響がない' do
815         @story2.higher_shift @ot
816         @storyc2.reload
817         @storyc2.t.should eq 0
818       end
819     end
820   end
821   describe '入れ替えに於いて' do
822     before do
823       @comic = Factory :comic, :author_id => @author.id
824       @panel = Factory :panel, :author_id => @author.id
825       @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
826       @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
827     end
828     context '新tが旧tより小さいとき' do
829       it '少ない方に移動を依頼している' do
830         Story.any_instance.stub(:lesser_shift).with(any_args)
831         Story.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
832         ot = @story2.t
833         @story2.t = 0
834         @story2.update_shift ot
835       end
836     end
837     context '新tが旧tより大きいとき' do
838       it '大きい方に移動を依頼している' do
839         Story.any_instance.stub(:higher_shift).with(any_args)
840         Story.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
841         ot = @story.t
842         @story.t = 1
843         @story.update_shift ot
844       end
845     end
846   end
847   describe '順序入れ替えに於いて' do
848     before do
849       @comic = Factory :comic, :author_id => @author.id
850       @panel = Factory :panel, :author_id => @author.id
851     end
852     context 'オブジェクトが新規でtが空のとき' do
853       it '末尾追加としてtを補充依頼している' do
854         @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
855         Story.stub(:new_t).with(any_args).and_return(0)
856         Story.should_receive(:new_t).with(any_args).exactly(1)
857         @story.t = nil
858         r = @story.rotate
859       end
860     end
861     context 'オブジェクトが新規でtが設定されているとき' do
862       it '挿入追加として挿入シフトを依頼している' do
863         @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
864         Story.any_instance.stub(:insert_shift).with(any_args)
865         Story.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
866         @story.t = 0
867         r = @story.rotate
868       end
869     end
870     context 'オブジェクトが新規でなくtが設定されているとき' do
871       it 'コマ移動として入れ替えを依頼している' do
872         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
873         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
874         Story.any_instance.stub(:update_shift).with(any_args)
875         Story.any_instance.should_receive(:update_shift).with(1).exactly(1)
876         @story2.t = 0
877         r = @story.rotate 1
878       end
879     end
880     context 'オブジェクトが新規でなくtが空のとき' do
881       it '入れ替えもシフトもせず、tを空のままにしている' do
882         #結果、tに欠番が生じてシリアライズチェックでひっかかる
883       end
884     end
885   end
886   describe '編集許可に於いて' do
887     before do
888       @comic = Factory :comic, :author_id => @author.id
889       @panel = Factory :panel, :author_id => @author.id
890       @story = Factory.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
891     end
892     context 'つつがなく終わるとき' do
893       it 'trueを返す' do
894         r = @story.allow?
895         r.should be_true
896       end
897     end
898     context 'コミックで引っかかるとき' do
899       it 'falseを返す' do
900         @story.comic_id = nil
901         r = @story.allow?
902         r.should be_false
903       end
904       it 'falseを返す' do
905         Comic.any_instance.stub(:own?).with(any_args).and_return(false)
906         r = @story.allow?
907         r.should be_false
908       end
909     end
910     context 'コマで引っかかるとき' do
911       it 'falseを返す' do
912         @story.panel_id = nil
913         r = @story.allow?
914         r.should be_false
915       end
916       it 'falseを返す' do
917         Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
918         r = @story.allow?
919         r.should be_false
920       end
921     end
922   end
923   describe '保存に於いて' do
924     before do
925       @comic = Factory :comic, :author_id => @author.id
926       @panel = Factory :panel, :author_id => @author.id
927       @story = Factory.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
928     end
929     context 'つつがなく終わるとき' do
930       it '編集許可チェックを依頼している' do
931         Story.any_instance.stub(:allow?).with(any_args).and_return(true)
932         Story.any_instance.should_receive(:allow?).with(any_args).exactly(1)
933         r = @story.store
934       end
935       it '順序入れ替えを依頼している' do
936         Story.any_instance.stub(:rotate).with(any_args).and_return(0)
937         Story.any_instance.should_receive(:rotate).with(any_args).exactly(1)
938         Story.any_instance.stub(:save).with(any_args).and_return(true)
939         Story.stub(:validate_t).with(any_args).and_return(true)
940         r = @story.store 
941       end
942       it '保存を依頼している' do
943         Story.stub(:new_t).with(any_args).and_return(0)
944         Story.any_instance.stub(:save).with(any_args).and_return(true)
945         Story.any_instance.should_receive(:save).with(any_args).exactly(1)
946         Story.stub(:validate_t).with(any_args).and_return(true)
947         r = @story.store
948       end
949       it 'tのシリアライズチェックを依頼している' do
950         Story.stub(:new_t).with(any_args).and_return(0)
951         Story.any_instance.stub(:save).with(any_args).and_return(true)
952         Story.stub(:validate_t).with(any_args).and_return(true)
953         Story.should_receive(:validate_t).with(any_args).exactly(1)
954         r = @story.store
955       end
956     end
957     #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
958     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したとき' do
959       before do
960         @comic2 = Factory :comic, :author_id => @author.id
961         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
962         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
963         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
964         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
965         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
966         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
967         @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
968       end
969       it '既存のt0には変化がない' do
970         @story6.store
971         @story.reload
972         @story.t.should eq 0
973       end
974       it '既存のt1には変化がない' do
975         @story6.store
976         @story2.reload
977         @story2.t.should eq 1
978       end
979       it '既存のt2を3にシフトしている' do
980         @story6.store
981         @story3.reload
982         @story3.t.should eq 3
983       end
984       it '既存のt3を4にシフトしている' do
985         @story6.store
986         @story4.reload
987         @story4.t.should eq 4
988       end
989       it '既存のt5を5にシフトしている' do
990         @story6.store
991         @story5.reload
992         @story5.t.should eq 5
993       end
994       it '新規のt2が作成されている' do
995         @story6.store
996         @story6.reload
997         @story6.t.should eq 2
998       end
999       it '他のコミックに影響がない' do
1000         @ot = @storyc2.t
1001         @story6.store
1002         @storyc2.reload
1003         @storyc2.t.should eq @ot
1004       end
1005     end
1006     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したとき' do
1007       before do
1008         @comic2 = Factory :comic, :author_id => @author.id
1009         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1010         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1011         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1012         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1013         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1014         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1015         @ot = @story4.t
1016         @story4.t = 1
1017       end
1018       it '既存のt0には変化がない' do
1019         @story4.store @ot
1020         @story.reload
1021         @story.t.should eq 0
1022       end
1023       it '既存のt4には変化がない' do
1024         @story4.store @ot
1025         @story5.reload
1026         @story5.t.should eq 4
1027       end
1028       it '既存のt1を2にシフトしている' do
1029         @story4.store @ot
1030         @story2.reload
1031         @story2.t.should eq 2
1032       end
1033       it '既存のt2を3にシフトしている' do
1034         @story4.store @ot
1035         @story3.reload
1036         @story3.t.should eq 3
1037       end
1038       it '既存のt3を1にシフトしている' do
1039         @story4.store @ot
1040         @story4.reload
1041         @story4.t.should eq 1
1042       end
1043       it '他のコミックに影響がない' do
1044         @story4.store @ot
1045         @storyc2.reload
1046         @storyc2.t.should eq 0
1047       end
1048     end
1049     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で1を3に移動したとき' do
1050       before do
1051         @comic2 = Factory :comic, :author_id => @author.id
1052         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1053         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1054         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1055         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1056         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1057         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1058         @ot = @story2.t
1059         @story2.t = 3
1060       end
1061       it '既存のt0には変化がない' do
1062         @story2.store @ot
1063         @story.reload
1064         @story.t.should eq 0
1065       end
1066       it '既存のt4には変化がない' do
1067         @story2.store @ot
1068         @story5.reload
1069         @story5.t.should eq 4
1070       end
1071       it '既存のt1を3にシフトしている' do
1072         @story2.store @ot
1073         @story2.reload
1074         @story2.t.should eq 3
1075       end
1076       it '既存のt2を1にシフトしている' do
1077         @story2.store @ot
1078         @story3.reload
1079         @story3.t.should eq 1
1080       end
1081       it '既存のt3を2にシフトしている' do
1082         @story2.store @ot
1083         @story4.reload
1084         @story4.t.should eq 2
1085       end
1086       it '他のコミックに影響がない' do
1087         @story2.store @ot
1088         @storyc2.reload
1089         @storyc2.t.should eq 0
1090       end
1091     end
1092     #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1093     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したが保存に失敗したとき' do
1094       before do
1095         Story.any_instance.stub(:save).with(any_args).and_return(false)
1096         @comic2 = Factory :comic, :author_id => @author.id
1097         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1098         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1099         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1100         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1101         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1102         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1103         @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1104       end
1105       it '既存のtに変化がない' do
1106         @story6.store
1107         @story.reload
1108         @story.t.should eq 0
1109         @story2.reload
1110         @story2.t.should eq 1
1111         @story3.reload
1112         @story3.t.should eq 2
1113         @story4.reload
1114         @story4.t.should eq 3
1115         @story5.reload
1116         @story5.t.should eq 4
1117         @storyc2.reload
1118         @storyc2.t.should eq 0
1119       end
1120       it 'falseを返す' do
1121         r = @story6.store
1122         r.should be_false
1123       end
1124     end
1125     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したがシリアルチェックに失敗したとき' do
1126       before do
1127         Story.stub(:validate_t).with(any_args).and_return(false)
1128         @comic2 = Factory :comic, :author_id => @author.id
1129         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1130         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1131         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1132         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1133         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1134         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1135         @ot = @story4.t
1136         @story4.t = 1
1137       end
1138       it '既存のtに変化がない' do
1139         @story4.store @ot
1140         @story.reload
1141         @story.t.should eq 0
1142         @story2.reload
1143         @story2.t.should eq 1
1144         @story3.reload
1145         @story3.t.should eq 2
1146         @story4.reload
1147         @story4.t.should eq 3
1148         @story5.reload
1149         @story5.t.should eq 4
1150         @storyc2.reload
1151         @storyc2.t.should eq 0
1152       end
1153       it 'falseを返す' do
1154         r = @story4.store @ot
1155         r.should be_false
1156       end
1157       it 'tにエラーメッセージが入っている' do
1158         @story4.store @ot
1159         @story4.errors[:t].should_not be_empty
1160         @story4.valid?.should be_true
1161       end
1162     end
1163     context '編集不可だったとき' do
1164       before do
1165         @story = Factory.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1166         Story.any_instance.stub(:allow?).and_return(false)
1167       end
1168       it '403Forbidden例外を返す' do
1169         lambda{
1170           @story.store
1171         }.should raise_error(ActiveRecord::Forbidden)
1172       end
1173     end
1174   end
1175   describe '切り詰め処理つき削除に於いて' do
1176     before do
1177       @comic = Factory :comic, :author_id => @author.id
1178       @panel = Factory :panel, :author_id => @author.id
1179       @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1180     end
1181     context 'つつがなく終わるとき' do
1182       it '削除される' do
1183         lambda{
1184           @story.destroy_and_shorten
1185         }.should change(Story, :count ).by(-1)
1186       end
1187     end
1188     #連携テスト。切り詰めが直接DBをいじる
1189     context '2件で先頭を削除したとき' do
1190       before do
1191         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1192       end
1193       it '行が削除される' do
1194         lambda{
1195           @story.destroy_and_shorten
1196         }.should change(Story, :count ).by(-1)
1197       end
1198       it '先頭は削除される' do
1199         @story.destroy_and_shorten
1200         lambda{
1201           Story.find @story.id
1202         }.should raise_error(ActiveRecord::RecordNotFound)
1203       end
1204       it '2件目は前に詰められる' do
1205         @story.destroy_and_shorten
1206         @story2.reload
1207         @story2.t.should eq 0
1208       end
1209     end
1210     context '3件で先頭を削除したとき' do
1211       before do
1212         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1213         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1214       end
1215       it '行が削除される' do
1216         lambda{
1217           @story.destroy_and_shorten
1218         }.should change(Story, :count ).by(-1)
1219       end
1220       it '先頭は削除される' do
1221         @story.destroy_and_shorten
1222         lambda{
1223           Story.find @story.id
1224         }.should raise_error(ActiveRecord::RecordNotFound)
1225       end
1226       it '2件目は前に詰められる' do
1227         @story.destroy_and_shorten
1228         @story2.reload
1229         @story2.t.should eq 0
1230       end
1231       it '3件目は前に詰められる' do
1232         @story.destroy_and_shorten
1233         @story3.reload
1234         @story3.t.should eq 1
1235       end
1236     end
1237     context '5件で3件目を削除したとき' do
1238       before do
1239         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1240         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1241         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1242         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1243       end
1244       it '行が削除される' do
1245         lambda{
1246           @story3.destroy_and_shorten
1247         }.should change(Story, :count ).by(-1)
1248       end
1249       it '1件目は変化がない' do
1250         @story3.destroy_and_shorten
1251         @story.reload
1252         @story.t.should eq 0
1253       end
1254       it '2件目は変化がない' do
1255         @story3.destroy_and_shorten
1256         @story2.reload
1257         @story2.t.should eq 1
1258       end
1259       it '3件目は削除される' do
1260         @story3.destroy_and_shorten
1261         lambda{
1262           Story.find @story3.id
1263         }.should raise_error(ActiveRecord::RecordNotFound)
1264       end
1265       it '4件目は前に詰められる' do
1266         @story3.destroy_and_shorten
1267         @story4.reload
1268         @story4.t.should eq 2
1269       end
1270       it '5件目は前に詰められる' do
1271         @story3.destroy_and_shorten
1272         @story5.reload
1273         @story5.t.should eq 3
1274       end
1275     end
1276     #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1277   end
1278 end