OSDN Git Service

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