OSDN Git Service

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