OSDN Git Service

t#29020:modify lesser shift 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     #例外ケース。
684     #負のときは0として正常扱い
685     context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
686       before do
687         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
688         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
689         @ot = @story2.t
690         @story2.t = -1
691       end
692       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
693         #移動させたい行はそのまま残る
694         @story2.lesser_shift @ot
695         l = Story.find(:all).map {|s| s.t }
696         l.sort.should eq [1, 1]
697       end
698       it '既存のt0を1にシフトしている' do
699         @story2.lesser_shift @ot
700         @story.reload
701         @story.t.should eq 1
702       end
703       it '既存のt1は0に補正されている' do
704         @story2.lesser_shift @ot
705         @story2.t.should eq 0
706       end
707     end
708   end
709   describe '大きい方に移動に於いて' do
710     before do
711       @comic = Factory :comic, :author_id => @author.id
712       @panel = Factory :panel, :author_id => @author.id
713     end
714     context '依頼チェック' do
715       it 'Updateを依頼している' do
716         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
717         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
718         Story.stub(:update_all).with(any_args)
719         Story.should_receive(:update_all).with(any_args).exactly(1)
720         ot = @story.t
721         @story.t = 1
722         @story.higher_shift ot
723       end
724     end
725     context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
726       before do
727         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
728         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
729         @ot = @story.t
730         @story.t = 1
731       end
732       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
733         #移動させたい行はそのまま残る
734         @story.higher_shift @ot
735         l = Story.find(:all).map {|s| s.t }
736         l.sort.should eq [0, 0]
737       end
738       it '既存のt1を0にシフトしている' do
739         @story.higher_shift @ot
740         @story2.reload
741         @story2.t.should eq 0
742       end
743     end
744     context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
745       before do
746         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
747         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
748         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
749         @ot = @story.t
750         @story.t = 1
751       end
752       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
753         #移動させたい行はそのまま残る
754         @story.higher_shift @ot
755         l = Story.find(:all).map {|s| s.t }
756         l.sort.should eq [0, 0, 2]
757       end
758       it '既存のt1を0にシフトしている' do
759         @story.higher_shift @ot
760         @story2.reload
761         @story2.t.should eq 0
762       end
763     end
764     context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
765       before do
766         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
767         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
768         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
769         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
770         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
771         @ot = @story2.t
772         @story2.t = 3
773       end
774       it 'これから挿入するt(3)が欠番になっている' do
775         #移動させたい行はそのまま残る
776         @story2.higher_shift @ot
777         l = Story.find(:all).map {|s| s.t }
778         l.sort.should eq [0, 1, 1, 2, 4]
779       end
780       it '既存のt0には変化がない' do
781         @story2.higher_shift @ot
782         @story.reload
783         @story.t.should eq 0
784       end
785       it '既存のt4には変化がない' do
786         @story2.higher_shift @ot
787         @story5.reload
788         @story5.t.should eq 4
789       end
790       it '既存のt2を1にシフトしている' do
791         @story2.higher_shift @ot
792         @story3.reload
793         @story3.t.should eq 1
794       end
795       it '既存のt3を2にシフトしている' do
796         @story2.higher_shift @ot
797         @story4.reload
798         @story4.t.should eq 2
799       end
800     end
801     context '先ほどのケース+他のコミック1件で挿入したとき' do
802       before do
803         @comic2 = Factory :comic, :author_id => @author.id
804         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
805         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
806         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
807         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
808         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
809         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
810         @ot = @story2.t
811         @story2.t = 3
812       end
813       it 'これから挿入するt(3)が欠番になっている' do
814         #移動させたい行はそのまま残る
815         @story2.higher_shift @ot
816         l = Story.find(:all).map {|s| s.t }
817         l.sort.should eq [0, 0, 1, 1, 2, 4]
818       end
819       it '既存のt0には変化がない' do
820         @story2.higher_shift @ot
821         @story.reload
822         @story.t.should eq 0
823       end
824       it '既存のt4には変化がない' do
825         @story2.higher_shift @ot
826         @story5.reload
827         @story5.t.should eq 4
828       end
829       it '既存のt2を1にシフトしている' do
830         @story2.higher_shift @ot
831         @story3.reload
832         @story3.t.should eq 1
833       end
834       it '既存のt3を2にシフトしている' do
835         @story2.higher_shift @ot
836         @story4.reload
837         @story4.t.should eq 2
838       end
839       it '他のコミックに影響がない' do
840         @story2.higher_shift @ot
841         @storyc2.reload
842         @storyc2.t.should eq 0
843       end
844     end
845     #例外ケース。
846     #max超えたときはmaxとして正常扱い
847     context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
848       before do
849         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
850         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
851         @ot = @story.t
852         @story.t = 2
853       end
854       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
855         #移動させたい行はそのまま残る
856         @story.higher_shift @ot
857         l = Story.find(:all).map {|s| s.t }
858         l.sort.should eq [0, 0]
859       end
860       it '既存のt1を0にシフトしている' do
861         @story.higher_shift @ot
862         @story2.reload
863         @story2.t.should eq 0
864       end
865       it '既存のt0は1に補正されている' do
866         @story.higher_shift @ot
867         @story.t.should eq 1
868       end
869     end
870   end
871   describe '入れ替えに於いて' do
872     before do
873       @comic = Factory :comic, :author_id => @author.id
874       @panel = Factory :panel, :author_id => @author.id
875       @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
876       @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
877     end
878     context '新tが旧tより小さいとき' do
879       it '少ない方に移動を依頼している' do
880         Story.any_instance.stub(:lesser_shift).with(any_args)
881         Story.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
882         ot = @story2.t
883         @story2.t = 0
884         @story2.update_shift ot
885       end
886     end
887     context '新tが旧tより大きいとき' do
888       it '大きい方に移動を依頼している' do
889         Story.any_instance.stub(:higher_shift).with(any_args)
890         Story.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
891         ot = @story.t
892         @story.t = 1
893         @story.update_shift ot
894       end
895     end
896   end
897   describe '順序入れ替えに於いて' do
898     before do
899       @comic = Factory :comic, :author_id => @author.id
900       @panel = Factory :panel, :author_id => @author.id
901     end
902     context 'オブジェクトが新規でtが空のとき' do
903       it '末尾追加としてtを補充依頼している' do
904         @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
905         Story.stub(:new_t).with(any_args).and_return(0)
906         Story.should_receive(:new_t).with(any_args).exactly(1)
907         @story.t = nil
908         r = @story.rotate
909       end
910     end
911     context 'オブジェクトが新規でtが設定されているとき' do
912       it '挿入追加として挿入シフトを依頼している' do
913         @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
914         Story.any_instance.stub(:insert_shift).with(any_args)
915         Story.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
916         @story.t = 0
917         r = @story.rotate
918       end
919     end
920     context 'オブジェクトが新規でなくtが設定されているとき' do
921       it 'コマ移動として入れ替えを依頼している' do
922         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
923         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
924         Story.any_instance.stub(:update_shift).with(any_args)
925         Story.any_instance.should_receive(:update_shift).with(1).exactly(1)
926         @story2.t = 0
927         r = @story.rotate 1
928       end
929     end
930     context 'オブジェクトが新規でなくtが空のとき' do
931       it '入れ替えもシフトもせず、tを空のままにしている' do
932         #結果、tに欠番が生じてシリアライズチェックでひっかかる
933       end
934     end
935   end
936   describe '編集許可に於いて' do
937     before do
938       @comic = Factory :comic, :author_id => @author.id
939       @panel = Factory :panel, :author_id => @author.id
940       @story = Factory.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
941     end
942     context 'つつがなく終わるとき' do
943       it 'trueを返す' do
944         r = @story.allow?
945         r.should be_true
946       end
947     end
948     context 'コミックで引っかかるとき' do
949       it 'falseを返す' do
950         @story.comic_id = nil
951         r = @story.allow?
952         r.should be_false
953       end
954       it 'falseを返す' do
955         Comic.any_instance.stub(:own?).with(any_args).and_return(false)
956         r = @story.allow?
957         r.should be_false
958       end
959     end
960     context 'コマで引っかかるとき' do
961       it 'falseを返す' do
962         @story.panel_id = nil
963         r = @story.allow?
964         r.should be_false
965       end
966       it 'falseを返す' do
967         Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
968         r = @story.allow?
969         r.should be_false
970       end
971     end
972   end
973   describe '保存に於いて' do
974     before do
975       @comic = Factory :comic, :author_id => @author.id
976       @panel = Factory :panel, :author_id => @author.id
977       @story = Factory.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
978     end
979     context 'つつがなく終わるとき' do
980       it '編集許可チェックを依頼している' do
981         Story.any_instance.stub(:allow?).with(any_args).and_return(true)
982         Story.any_instance.should_receive(:allow?).with(any_args).exactly(1)
983         r = @story.store
984       end
985       it '順序入れ替えを依頼している' do
986         Story.any_instance.stub(:rotate).with(any_args).and_return(0)
987         Story.any_instance.should_receive(:rotate).with(any_args).exactly(1)
988         Story.any_instance.stub(:save).with(any_args).and_return(true)
989         Story.stub(:validate_t).with(any_args).and_return(true)
990         r = @story.store 
991       end
992       it '保存を依頼している' do
993         Story.stub(:new_t).with(any_args).and_return(0)
994         Story.any_instance.stub(:save).with(any_args).and_return(true)
995         Story.any_instance.should_receive(:save).with(any_args).exactly(1)
996         Story.stub(:validate_t).with(any_args).and_return(true)
997         r = @story.store
998       end
999       it 'tのシリアライズチェックを依頼している' do
1000         Story.stub(:new_t).with(any_args).and_return(0)
1001         Story.any_instance.stub(:save).with(any_args).and_return(true)
1002         Story.stub(:validate_t).with(any_args).and_return(true)
1003         Story.should_receive(:validate_t).with(any_args).exactly(1)
1004         r = @story.store
1005       end
1006     end
1007     #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1008     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したとき' do
1009       before do
1010         @comic2 = Factory :comic, :author_id => @author.id
1011         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1012         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1013         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1014         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1015         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1016         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1017         @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1018       end
1019       it '既存のt0には変化がない' do
1020         @story6.store
1021         @story.reload
1022         @story.t.should eq 0
1023       end
1024       it '既存のt1には変化がない' do
1025         @story6.store
1026         @story2.reload
1027         @story2.t.should eq 1
1028       end
1029       it '既存のt2を3にシフトしている' do
1030         @story6.store
1031         @story3.reload
1032         @story3.t.should eq 3
1033       end
1034       it '既存のt3を4にシフトしている' do
1035         @story6.store
1036         @story4.reload
1037         @story4.t.should eq 4
1038       end
1039       it '既存のt5を5にシフトしている' do
1040         @story6.store
1041         @story5.reload
1042         @story5.t.should eq 5
1043       end
1044       it '新規のt2が作成されている' do
1045         @story6.store
1046         @story6.reload
1047         @story6.t.should eq 2
1048       end
1049       it '他のコミックに影響がない' do
1050         @ot = @storyc2.t
1051         @story6.store
1052         @storyc2.reload
1053         @storyc2.t.should eq @ot
1054       end
1055     end
1056     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したとき' do
1057       before do
1058         @comic2 = Factory :comic, :author_id => @author.id
1059         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1060         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1061         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1062         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1063         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1064         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1065         @ot = @story4.t
1066         @story4.t = 1
1067       end
1068       it '既存のt0には変化がない' do
1069         @story4.store @ot
1070         @story.reload
1071         @story.t.should eq 0
1072       end
1073       it '既存のt4には変化がない' do
1074         @story4.store @ot
1075         @story5.reload
1076         @story5.t.should eq 4
1077       end
1078       it '既存のt1を2にシフトしている' do
1079         @story4.store @ot
1080         @story2.reload
1081         @story2.t.should eq 2
1082       end
1083       it '既存のt2を3にシフトしている' do
1084         @story4.store @ot
1085         @story3.reload
1086         @story3.t.should eq 3
1087       end
1088       it '既存のt3を1にシフトしている' do
1089         @story4.store @ot
1090         @story4.reload
1091         @story4.t.should eq 1
1092       end
1093       it '他のコミックに影響がない' do
1094         @story4.store @ot
1095         @storyc2.reload
1096         @storyc2.t.should eq 0
1097       end
1098     end
1099     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で1を3に移動したとき' do
1100       before do
1101         @comic2 = Factory :comic, :author_id => @author.id
1102         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1103         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1104         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1105         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1106         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1107         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1108         @ot = @story2.t
1109         @story2.t = 3
1110       end
1111       it '既存のt0には変化がない' do
1112         @story2.store @ot
1113         @story.reload
1114         @story.t.should eq 0
1115       end
1116       it '既存のt4には変化がない' do
1117         @story2.store @ot
1118         @story5.reload
1119         @story5.t.should eq 4
1120       end
1121       it '既存のt1を3にシフトしている' do
1122         @story2.store @ot
1123         @story2.reload
1124         @story2.t.should eq 3
1125       end
1126       it '既存のt2を1にシフトしている' do
1127         @story2.store @ot
1128         @story3.reload
1129         @story3.t.should eq 1
1130       end
1131       it '既存のt3を2にシフトしている' do
1132         @story2.store @ot
1133         @story4.reload
1134         @story4.t.should eq 2
1135       end
1136       it '他のコミックに影響がない' do
1137         @story2.store @ot
1138         @storyc2.reload
1139         @storyc2.t.should eq 0
1140       end
1141     end
1142     #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1143     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したが保存に失敗したとき' do
1144       before do
1145         Story.any_instance.stub(:save).with(any_args).and_return(false)
1146         @comic2 = Factory :comic, :author_id => @author.id
1147         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1148         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1149         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1150         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1151         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1152         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1153         @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1154       end
1155       it '既存のtに変化がない' do
1156         @story6.store
1157         @story.reload
1158         @story.t.should eq 0
1159         @story2.reload
1160         @story2.t.should eq 1
1161         @story3.reload
1162         @story3.t.should eq 2
1163         @story4.reload
1164         @story4.t.should eq 3
1165         @story5.reload
1166         @story5.t.should eq 4
1167         @storyc2.reload
1168         @storyc2.t.should eq 0
1169       end
1170       it 'falseを返す' do
1171         r = @story6.store
1172         r.should be_false
1173       end
1174     end
1175     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したがシリアルチェックに失敗したとき' do
1176       before do
1177         Story.stub(:validate_t).with(any_args).and_return(false)
1178         @comic2 = Factory :comic, :author_id => @author.id
1179         @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1180         @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1181         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1182         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1183         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1184         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1185         @ot = @story4.t
1186         @story4.t = 1
1187       end
1188       it '既存のtに変化がない' do
1189         @story4.store @ot
1190         @story.reload
1191         @story.t.should eq 0
1192         @story2.reload
1193         @story2.t.should eq 1
1194         @story3.reload
1195         @story3.t.should eq 2
1196         @story4.reload
1197         @story4.t.should eq 3
1198         @story5.reload
1199         @story5.t.should eq 4
1200         @storyc2.reload
1201         @storyc2.t.should eq 0
1202       end
1203       it 'falseを返す' do
1204         r = @story4.store @ot
1205         r.should be_false
1206       end
1207       it 'tにエラーメッセージが入っている' do
1208         @story4.store @ot
1209         @story4.errors[:t].should_not be_empty
1210         @story4.valid?.should be_true
1211       end
1212     end
1213     context '編集不可だったとき' do
1214       before do
1215         @story = Factory.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1216         Story.any_instance.stub(:allow?).and_return(false)
1217       end
1218       it '403Forbidden例外を返す' do
1219         lambda{
1220           @story.store
1221         }.should raise_error(ActiveRecord::Forbidden)
1222       end
1223     end
1224   end
1225   describe '切り詰め処理つき削除に於いて' do
1226     before do
1227       @comic = Factory :comic, :author_id => @author.id
1228       @panel = Factory :panel, :author_id => @author.id
1229       @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1230     end
1231     context 'つつがなく終わるとき' do
1232       it '削除される' do
1233         lambda{
1234           @story.destroy_and_shorten
1235         }.should change(Story, :count ).by(-1)
1236       end
1237     end
1238     #連携テスト。切り詰めが直接DBをいじる
1239     context '2件で先頭を削除したとき' do
1240       before do
1241         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1242       end
1243       it '行が削除される' do
1244         lambda{
1245           @story.destroy_and_shorten
1246         }.should change(Story, :count ).by(-1)
1247       end
1248       it '先頭は削除される' do
1249         @story.destroy_and_shorten
1250         lambda{
1251           Story.find @story.id
1252         }.should raise_error(ActiveRecord::RecordNotFound)
1253       end
1254       it '2件目は前に詰められる' do
1255         @story.destroy_and_shorten
1256         @story2.reload
1257         @story2.t.should eq 0
1258       end
1259     end
1260     context '3件で先頭を削除したとき' do
1261       before do
1262         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1263         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1264       end
1265       it '行が削除される' do
1266         lambda{
1267           @story.destroy_and_shorten
1268         }.should change(Story, :count ).by(-1)
1269       end
1270       it '先頭は削除される' do
1271         @story.destroy_and_shorten
1272         lambda{
1273           Story.find @story.id
1274         }.should raise_error(ActiveRecord::RecordNotFound)
1275       end
1276       it '2件目は前に詰められる' do
1277         @story.destroy_and_shorten
1278         @story2.reload
1279         @story2.t.should eq 0
1280       end
1281       it '3件目は前に詰められる' do
1282         @story.destroy_and_shorten
1283         @story3.reload
1284         @story3.t.should eq 1
1285       end
1286     end
1287     context '5件で3件目を削除したとき' do
1288       before do
1289         @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1290         @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1291         @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1292         @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1293       end
1294       it '行が削除される' do
1295         lambda{
1296           @story3.destroy_and_shorten
1297         }.should change(Story, :count ).by(-1)
1298       end
1299       it '1件目は変化がない' do
1300         @story3.destroy_and_shorten
1301         @story.reload
1302         @story.t.should eq 0
1303       end
1304       it '2件目は変化がない' do
1305         @story3.destroy_and_shorten
1306         @story2.reload
1307         @story2.t.should eq 1
1308       end
1309       it '3件目は削除される' do
1310         @story3.destroy_and_shorten
1311         lambda{
1312           Story.find @story3.id
1313         }.should raise_error(ActiveRecord::RecordNotFound)
1314       end
1315       it '4件目は前に詰められる' do
1316         @story3.destroy_and_shorten
1317         @story4.reload
1318         @story4.t.should eq 2
1319       end
1320       it '5件目は前に詰められる' do
1321         @story3.destroy_and_shorten
1322         @story5.reload
1323         @story5.t.should eq 3
1324       end
1325     end
1326     #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1327   end
1328 end