1 # -*- encoding: utf-8 -*-
7 @license = Factory :license
8 @user = Factory( :user_yas)
10 @artist = Factory :artist_yas, :author_id => @author.id
11 @other_user = Factory( :user_yas)
12 @other_author = @other_user.author
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
22 it 'オーソドックスなデータなら通る' do
23 @story.should be_valid
26 context 'comic_idを検証するとき' do
28 @story.comic_id = @comic.id
29 @story.should be_valid
33 @story.should_not be_valid
37 @story.should_not be_valid
39 it '存在するコミックでなければ失敗する' do
41 @story.should_not be_valid
45 context 'panel_idを検証するとき' do
47 @story.panel_id = @panel.id
48 @story.should be_valid
52 @story.should_not be_valid
56 @story.should_not be_valid
58 it '存在するコマでなければ失敗する' do
60 @story.should_not be_valid
69 @story.should be_valid
73 @story.should_not be_valid
77 @story.should_not be_valid
81 @story.should_not be_valid
85 context 'author_idを検証するとき' do
87 @story.author_id = @author.id
88 @story.should be_valid
91 @story.author_id = nil
92 @story.should_not be_valid
95 @story.author_id = 'a'
96 @story.should_not be_valid
98 it '存在する作家でなければ失敗する' do
100 @story.should_not be_valid
103 context '全体を検証するとき' do
105 @story = Factory :story, :author_id => @author.id
110 describe 'データ補充に於いて' do
112 @comic = Factory :comic, :author_id => @author.id
113 @panel = Factory :panel, :author_id => @author.id
116 context 'author_idを補充' do
117 it '問答無用でauthor_idを補充する' do
118 @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id
119 @story.author_id = nil
120 @story.supply_default @author
121 @story.author_id.should eq @author.id
127 describe '作者判定に於いて' do
129 @comic = Factory :comic, :author_id => @author.id
130 @panel = Factory :panel, :author_id => @author.id
131 @story = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
132 @comico = Factory :comic, :author_id => @other_author.id
133 @panelo = Factory :panel, :author_id => @other_author.id
134 @storyo = Factory :story, :author_id => @other_author.id, :comic_id => @comico.id, :panel_id => @panelo.id
136 it '自分のストーリーならyes' do
137 @story.own?(@author).should == true
140 @storyo.own?(@author).should == false
143 @story.own?(nil).should == false
146 describe '単体取得に於いて' do
148 @comic = Factory :comic, :author_id => @author.id
149 @panel = Factory :panel, :author_id => @author.id
150 @story = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
152 context 'オーナー指定がないとき' do
154 l = Story.show @story.id, @author
158 context 'オーナー指定のとき' do
159 it '指定のストーリーが自分のものならそれを返す' do
160 l = Story.show @story.id, @author
163 context '他人のストーリーを開こうとしたとき' do
164 it '403Forbidden例外を返す' do
165 Story.any_instance.stub(:own?).and_return(false)
167 Story.show @story.id, @author
168 }.should raise_error(ActiveRecord::Forbidden)
172 context '存在しないストーリーを開こうとしたとき' do
173 it '404RecordNotFound例外を返す' do
175 Story.show 110, @author
176 }.should raise_error(ActiveRecord::RecordNotFound)
181 describe '一覧取得に於いて' do
183 @comic = Factory :comic, :author_id => @author.id
184 @panel = Factory :panel, :author_id => @author.id
185 @story = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
187 context 'offset補正について' do
188 it '文字列から数値に変換される' do
189 Story.offset(100, '8').should eq 8
192 Story.offset(100).should eq 0
194 #投稿されたコマ数以上の値が指定されたときは、最後のコマだけになる
196 it '1件のときオフセット1なら0になる' do
197 Story.offset(1, '1').should eq 0
199 it '5件のときオフセット5なら4になる' do
200 Story.offset(5, '5').should eq 4
202 # 負の値が指定されたときは、最後のコマから数えてコマを飛ばして表示する。
203 #-4のときは、最後から4つのコマを表示する。
204 it '2件のときオフセット-1なら1になる' do
205 Story.offset(2, '-1').should eq 1
207 it '5件のときオフセット-2なら3になる' do
208 Story.offset(5, '-2').should eq 3
210 # 最終的なが負になるなど、不正な値が入ったときは0となる。
211 it '2件のときオフセット-5なら0になる' do
212 Story.offset(2, '-5').should eq 0
215 context 'panel_count補正について' do
216 it '文字列から数値に変換される' do
217 Story.panel_count(100, '7').should eq 7
219 it 'nilの場合はStory.default_panel_sizeになる' do
220 Story.panel_count(100).should eq Story.default_panel_size
222 it '0以下の場合はStory.default_panel_sizeになる' do
223 Story.panel_count(100, '0').should eq Story.default_panel_size
225 it 'Story.max_panel_sizeを超えた場合はStory.max_panel_sizeになる' do
226 Story.panel_count(100, '1000').should eq Story.max_panel_size
230 c = Story.list @comic, @author
234 v = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
235 c = Story.list @comic, @author
236 c.should eq [ @story, v]
238 context 'DBに5件あって1ページの件数を2件に変えたとして' do
240 @story2 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
241 @story3 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 2
242 @story4 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 3
243 @story5 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 4
245 it 'offset=0なら末尾2件を返す' do
247 c = Story.list( @comic, @author, 0, 2)
248 c.should eq [@story, @story2]
250 it 'offset=2なら中間2件を返す' do
251 c = Story.list(@comic, @author, 2, 2)
252 c.should eq [@story3, @story4]
254 it 'offset=4なら先頭1件を返す' do
255 c = Story.list(@comic, @author, 4, 2)
256 c.should eq [@story5]
260 describe 'list関連テーブルプションに於いて' do
261 it 'includeキーを含んでいる' do
263 r.has_key?(:include).should be_true
266 r = Story.list_opt[:include]
267 r.should have(3).items
270 r = Story.list_opt[:include]
271 r.has_key?(:comic).should be_true
273 it 'コミックは作家を含んでいる' do
274 r = Story.list_opt[:include]
275 r[:comic].has_key?(:author).should be_true
278 r = Story.list_opt[:include]
279 r.has_key?(:author).should be_true
282 r = Story.list_opt[:include]
283 r.has_key?(:panel).should be_true
286 r = Story.list_opt[:include]
287 r[:panel].has_key?(:author).should be_true
290 r = Story.list_opt[:include]
291 r[:panel].has_key?(:panel_pictures).should be_true
293 it 'コマはフキダシを含んでいる' do
294 r = Story.list_opt[:include]
295 r[:panel].has_key?(:speech_balloons).should be_true
298 describe 'json一覧出力オプションに於いて' do
299 it 'includeキーを含んでいる' do
300 r = Story.list_json_opt
301 r.has_key?(:include).should be_true
304 r = Story.list_json_opt[:include]
305 r.should have(3).items
308 r = Story.list_json_opt[:include]
309 r.has_key?(:comic).should be_true
311 it 'コミックは作家を含んでいる' do
312 r = Story.list_json_opt[:include]
313 r[:comic].has_key?(:author).should be_true
316 r = Story.list_json_opt[:include]
317 r.has_key?(:author).should be_true
320 r = Story.list_json_opt[:include]
321 r.has_key?(:panel).should be_true
324 r = Story.list_json_opt[:include]
325 r[:panel].has_key?(:author).should be_true
328 r = Story.list_json_opt[:include]
329 r[:panel].has_key?(:panel_pictures).should be_true
331 it 'コマはフキダシを含んでいる' do
332 r = Story.list_json_opt[:include]
333 r[:panel].has_key?(:speech_balloons).should be_true
336 describe 't補充値に於いて' do
338 @comic = Factory :comic, :author_id => @author.id
339 @panel = Factory :panel, :author_id => @author.id
342 context 'コミック初のコマなら' do
344 @story = Factory.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
346 r = Story.new_t @story.comic_id
350 context 'コミックに一個コマがあるとき' do
352 Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
353 @story = Factory.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
355 r = Story.new_t @story.comic_id
359 context 'コミックに2個コマがあるとき' do
361 Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
362 Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
363 @story = Factory.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
365 r = Story.new_t @story.comic_id
370 describe 'シリアライズチェックに於いて' do
371 context 'つつがなく終わるとき' do
372 it '0からシリアライズされているならTrueを返す' do
373 r = Story.serial? [0, 1, 2]
376 it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
377 r = Story.serial? [0, 2, 1]
380 it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
381 r = Story.serial? [ 2, 1, 4, 3, 0]
386 it '0から始まらないならFalseを返す' do
387 r = Story.serial? [1, 2, 3]
390 it '連続していないならFalseを返す' do
391 r = Story.serial? [0, 1, 2, 4]
394 it '連続していないならFalseを返す' do
395 r = Story.serial? [0, 1, 2, 4, 5]
400 describe 't収集に於いて' do
402 @comic = Factory :comic, :author_id => @author.id
403 @panel = Factory :panel, :author_id => @author.id
404 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
405 @comic2 = Factory :comic, :author_id => @author.id
406 @c2story = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
408 context 'つつがなく終わるとき' do
409 it 'ストーリーから同一コミックのtだけを収集している' do
410 r = Story.collect_t @story
415 it 'ストーリーから同一コミックのtだけを収集している' do
416 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
417 r = Story.collect_t @story
418 r.sort.should eq [0, 1]
421 context '複数コマでヨソのコミックも混じっているとき' do
422 it 'ストーリーから同一コミックのtだけを収集している' do
423 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
424 r = Story.collect_t @story
425 r.sort.should eq [0, 1]
429 describe 'tチェックに於いて' do
431 @comic = Factory :comic, :author_id => @author.id
432 @panel = Factory :panel, :author_id => @author.id
433 @story = Factory.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
435 context 'つつがなく終わるとき' do
437 Story.should_receive(:collect_t).with(any_args).exactly(1)
438 Story.stub(:collect_t).with(any_args).and_return([])
439 Story.stub(:serial?).with(any_args).and_return(true)
440 r = Story.validate_t @story
442 it '収集したtをシリアライズチェック依頼している' do
443 Story.stub(:collect_t).with(any_args).and_return([])
444 Story.should_receive(:serial?).with(any_args).exactly(1)
445 Story.stub(:serial?).with(any_args).and_return(true)
446 r = Story.validate_t @story
450 #依頼チェックだけでは不安なので最低限のチェックを
452 it '一件だけで正常通過している' do
453 @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
454 r = Story.validate_t @story
459 it '2件目を作っても正常通過している' do
460 @story = Factory :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
461 @story2 = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
462 r = Story.validate_t @story2
467 describe '挿入シフトに於いて' do
469 @comic = Factory :comic, :author_id => @author.id
470 @panel = Factory :panel, :author_id => @author.id
474 it 'Updateを依頼している' do
475 Story.stub(:update_all).with(any_args)
476 Story.should_receive(:update_all).with(any_args).exactly(1)
477 @story = Factory.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
481 context 'テーブルに1件(t:0)で0に挿入したとき' do
483 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
484 @story2 = Factory.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
486 it '既存の行を1にシフトしている' do
489 l.first.t.should eq 1
491 it 'これから挿入するt(0)が欠番になっている' do
493 l = Story.find(:all).map {|s| s.t }
494 l.include?(0).should_not be_true
497 context 'テーブルに2件(t:0,1)で1に挿入したとき' do
499 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
500 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
501 @story3 = Factory.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
503 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
505 l = Story.find(:all).map {|s| s.t }
506 l.sort.should eq [0, 2]
509 context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
511 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
512 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
513 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
514 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
515 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
516 @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
518 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
520 l = Story.find(:all).map {|s| s.t }
521 l.sort.should eq [0, 1, 3, 4, 5]
524 context '先ほどのケース+他のコミック1件で挿入したとき' do
526 @comic2 = Factory :comic, :author_id => @author.id
527 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
528 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
529 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
530 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
531 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
532 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
533 @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
535 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
537 l = Story.find(:all, :conditions => ['comic_id = ?', @comic.id]).map {|s| s.t }
538 l.sort.should eq [0, 1, 3, 4, 5]
544 @storyc2.t.should eq ot
548 describe '少ない方に移動に於いて' do
550 @comic = Factory :comic, :author_id => @author.id
551 @panel = Factory :panel, :author_id => @author.id
554 it 'Updateを依頼している' do
555 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
556 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
557 Story.stub(:update_all).with(any_args)
558 Story.should_receive(:update_all).with(any_args).exactly(1)
561 @story2.lesser_shift ot
564 context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
566 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
567 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
571 it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
573 @story2.lesser_shift @ot
574 l = Story.find(:all).map {|s| s.t }
575 l.sort.should eq [1, 1]
577 it '既存のt0を1にシフトしている' do
578 @story2.lesser_shift @ot
583 context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
585 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
586 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
587 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
591 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
593 @story3.lesser_shift @ot
594 l = Story.find(:all).map {|s| s.t }
595 l.sort.should eq [0, 2, 2]
597 it '既存のt1を2にシフトしている' do
598 @story3.lesser_shift @ot
600 @story2.t.should eq 2
603 context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
605 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
606 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
607 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
608 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
609 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
613 it 'これから挿入するt(1)が欠番になっている' do
615 @story4.lesser_shift @ot
616 l = Story.find(:all).map {|s| s.t }
617 l.sort.should eq [0, 2, 3, 3, 4]
620 @story4.lesser_shift @ot
625 @story4.lesser_shift @ot
627 @story5.t.should eq 4
629 it '既存のt1を2にシフトしている' do
630 @story4.lesser_shift @ot
632 @story2.t.should eq 2
634 it '既存のt2を3にシフトしている' do
635 @story4.lesser_shift @ot
637 @story3.t.should eq 3
640 context '先ほどのケース+他のコミック1件で挿入したとき' do
642 @comic2 = Factory :comic, :author_id => @author.id
643 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
644 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
645 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
646 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
647 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
648 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
652 it 'これから挿入するt(1)が欠番になっている' do
653 @story4.lesser_shift @ot
654 l = Story.find(:all).map {|s| s.t }
655 l.sort.should eq [0, 0, 2, 3, 3, 4]
658 @story4.lesser_shift @ot
663 @story4.lesser_shift @ot
665 @story5.t.should eq 4
667 it '既存のt1を2にシフトしている' do
668 @story4.lesser_shift @ot
670 @story2.t.should eq 2
672 it '既存のt2を3にシフトしている' do
673 @story4.lesser_shift @ot
675 @story3.t.should eq 3
678 @story4.lesser_shift @ot
680 @storyc2.t.should eq 0
685 context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
687 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
688 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
692 it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
694 @story2.lesser_shift @ot
695 l = Story.find(:all).map {|s| s.t }
696 l.sort.should eq [1, 1]
698 it '既存のt0を1にシフトしている' do
699 @story2.lesser_shift @ot
703 it '既存のt1は0に補正されている' do
704 @story2.lesser_shift @ot
705 @story2.t.should eq 0
709 describe '大きい方に移動に於いて' do
711 @comic = Factory :comic, :author_id => @author.id
712 @panel = Factory :panel, :author_id => @author.id
715 it 'Updateを依頼している' do
716 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
717 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
718 Story.stub(:update_all).with(any_args)
719 Story.should_receive(:update_all).with(any_args).exactly(1)
722 @story.higher_shift ot
725 context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
727 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
728 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
732 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
734 @story.higher_shift @ot
735 l = Story.find(:all).map {|s| s.t }
736 l.sort.should eq [0, 0]
738 it '既存のt1を0にシフトしている' do
739 @story.higher_shift @ot
741 @story2.t.should eq 0
744 context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
746 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
747 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
748 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
752 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
754 @story.higher_shift @ot
755 l = Story.find(:all).map {|s| s.t }
756 l.sort.should eq [0, 0, 2]
758 it '既存のt1を0にシフトしている' do
759 @story.higher_shift @ot
761 @story2.t.should eq 0
764 context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
766 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
767 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
768 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
769 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
770 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
774 it 'これから挿入するt(3)が欠番になっている' do
776 @story2.higher_shift @ot
777 l = Story.find(:all).map {|s| s.t }
778 l.sort.should eq [0, 1, 1, 2, 4]
781 @story2.higher_shift @ot
786 @story2.higher_shift @ot
788 @story5.t.should eq 4
790 it '既存のt2を1にシフトしている' do
791 @story2.higher_shift @ot
793 @story3.t.should eq 1
795 it '既存のt3を2にシフトしている' do
796 @story2.higher_shift @ot
798 @story4.t.should eq 2
801 context '先ほどのケース+他のコミック1件で挿入したとき' do
803 @comic2 = Factory :comic, :author_id => @author.id
804 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
805 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
806 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
807 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
808 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
809 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
813 it 'これから挿入するt(3)が欠番になっている' do
815 @story2.higher_shift @ot
816 l = Story.find(:all).map {|s| s.t }
817 l.sort.should eq [0, 0, 1, 1, 2, 4]
820 @story2.higher_shift @ot
825 @story2.higher_shift @ot
827 @story5.t.should eq 4
829 it '既存のt2を1にシフトしている' do
830 @story2.higher_shift @ot
832 @story3.t.should eq 1
834 it '既存のt3を2にシフトしている' do
835 @story2.higher_shift @ot
837 @story4.t.should eq 2
840 @story2.higher_shift @ot
842 @storyc2.t.should eq 0
847 context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
849 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
850 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
854 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
856 @story.higher_shift @ot
857 l = Story.find(:all).map {|s| s.t }
858 l.sort.should eq [0, 0]
860 it '既存のt1を0にシフトしている' do
861 @story.higher_shift @ot
863 @story2.t.should eq 0
865 it '既存のt0は1に補正されている' do
866 @story.higher_shift @ot
871 describe '入れ替えに於いて' do
873 @comic = Factory :comic, :author_id => @author.id
874 @panel = Factory :panel, :author_id => @author.id
875 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
876 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
878 context '新tが旧tより小さいとき' do
879 it '少ない方に移動を依頼している' do
880 Story.any_instance.stub(:lesser_shift).with(any_args)
881 Story.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
884 @story2.update_shift ot
887 context '新tが旧tより大きいとき' do
888 it '大きい方に移動を依頼している' do
889 Story.any_instance.stub(:higher_shift).with(any_args)
890 Story.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
893 @story.update_shift ot
897 describe '順序入れ替えに於いて' do
899 @comic = Factory :comic, :author_id => @author.id
900 @panel = Factory :panel, :author_id => @author.id
902 context 'オブジェクトが新規でtが空のとき' do
903 it '末尾追加としてtを補充依頼している' do
904 @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
905 Story.stub(:new_t).with(any_args).and_return(0)
906 Story.should_receive(:new_t).with(any_args).exactly(1)
911 context 'オブジェクトが新規でtが設定されているとき' do
912 it '挿入追加として挿入シフトを依頼している' do
913 @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
914 Story.any_instance.stub(:insert_shift).with(any_args)
915 Story.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
920 context 'オブジェクトが新規でなくtが設定されているとき' do
921 it 'コマ移動として入れ替えを依頼している' do
922 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
923 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
924 Story.any_instance.stub(:update_shift).with(any_args)
925 Story.any_instance.should_receive(:update_shift).with(1).exactly(1)
930 context 'オブジェクトが新規でなくtが空のとき' do
931 it '入れ替えもシフトもせず、tを空のままにしている' do
932 #結果、tに欠番が生じてシリアライズチェックでひっかかる
936 describe '編集許可に於いて' do
938 @comic = Factory :comic, :author_id => @author.id
939 @panel = Factory :panel, :author_id => @author.id
940 @story = Factory.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
942 context 'つつがなく終わるとき' do
948 context 'コミックで引っかかるとき' do
950 @story.comic_id = nil
955 Comic.any_instance.stub(:own?).with(any_args).and_return(false)
960 context 'コマで引っかかるとき' do
962 @story.panel_id = nil
967 Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
975 @comic = Factory :comic, :author_id => @author.id
976 @panel = Factory :panel, :author_id => @author.id
977 @story = Factory.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
979 context 'つつがなく終わるとき' do
980 it '編集許可チェックを依頼している' do
981 Story.any_instance.stub(:allow?).with(any_args).and_return(true)
982 Story.any_instance.should_receive(:allow?).with(any_args).exactly(1)
985 it '順序入れ替えを依頼している' do
986 Story.any_instance.stub(:rotate).with(any_args).and_return(0)
987 Story.any_instance.should_receive(:rotate).with(any_args).exactly(1)
988 Story.any_instance.stub(:save).with(any_args).and_return(true)
989 Story.stub(:validate_t).with(any_args).and_return(true)
993 Story.stub(:new_t).with(any_args).and_return(0)
994 Story.any_instance.stub(:save).with(any_args).and_return(true)
995 Story.any_instance.should_receive(:save).with(any_args).exactly(1)
996 Story.stub(:validate_t).with(any_args).and_return(true)
999 it 'tのシリアライズチェックを依頼している' do
1000 Story.stub(:new_t).with(any_args).and_return(0)
1001 Story.any_instance.stub(:save).with(any_args).and_return(true)
1002 Story.stub(:validate_t).with(any_args).and_return(true)
1003 Story.should_receive(:validate_t).with(any_args).exactly(1)
1007 #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1008 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したとき' do
1010 @comic2 = Factory :comic, :author_id => @author.id
1011 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1012 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1013 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1014 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1015 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1016 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1017 @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1019 it '既存のt0には変化がない' do
1022 @story.t.should eq 0
1024 it '既存のt1には変化がない' do
1027 @story2.t.should eq 1
1029 it '既存のt2を3にシフトしている' do
1032 @story3.t.should eq 3
1034 it '既存のt3を4にシフトしている' do
1037 @story4.t.should eq 4
1039 it '既存のt5を5にシフトしている' do
1042 @story5.t.should eq 5
1044 it '新規のt2が作成されている' do
1047 @story6.t.should eq 2
1049 it '他のコミックに影響がない' do
1053 @storyc2.t.should eq @ot
1056 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したとき' do
1058 @comic2 = Factory :comic, :author_id => @author.id
1059 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1060 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1061 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1062 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1063 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1064 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1068 it '既存のt0には変化がない' do
1071 @story.t.should eq 0
1073 it '既存のt4には変化がない' do
1076 @story5.t.should eq 4
1078 it '既存のt1を2にシフトしている' do
1081 @story2.t.should eq 2
1083 it '既存のt2を3にシフトしている' do
1086 @story3.t.should eq 3
1088 it '既存のt3を1にシフトしている' do
1091 @story4.t.should eq 1
1093 it '他のコミックに影響がない' do
1096 @storyc2.t.should eq 0
1099 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で1を3に移動したとき' do
1101 @comic2 = Factory :comic, :author_id => @author.id
1102 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1103 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1104 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1105 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1106 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1107 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1111 it '既存のt0には変化がない' do
1114 @story.t.should eq 0
1116 it '既存のt4には変化がない' do
1119 @story5.t.should eq 4
1121 it '既存のt1を3にシフトしている' do
1124 @story2.t.should eq 3
1126 it '既存のt2を1にシフトしている' do
1129 @story3.t.should eq 1
1131 it '既存のt3を2にシフトしている' do
1134 @story4.t.should eq 2
1136 it '他のコミックに影響がない' do
1139 @storyc2.t.should eq 0
1142 #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1143 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したが保存に失敗したとき' do
1145 Story.any_instance.stub(:save).with(any_args).and_return(false)
1146 @comic2 = Factory :comic, :author_id => @author.id
1147 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1148 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1149 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1150 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1151 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1152 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1153 @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1158 @story.t.should eq 0
1160 @story2.t.should eq 1
1162 @story3.t.should eq 2
1164 @story4.t.should eq 3
1166 @story5.t.should eq 4
1168 @storyc2.t.should eq 0
1175 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したがシリアルチェックに失敗したとき' do
1177 Story.stub(:validate_t).with(any_args).and_return(false)
1178 @comic2 = Factory :comic, :author_id => @author.id
1179 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1180 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1181 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1182 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1183 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1184 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1191 @story.t.should eq 0
1193 @story2.t.should eq 1
1195 @story3.t.should eq 2
1197 @story4.t.should eq 3
1199 @story5.t.should eq 4
1201 @storyc2.t.should eq 0
1204 r = @story4.store @ot
1207 it 'tにエラーメッセージが入っている' do
1209 @story4.errors[:t].should_not be_empty
1210 @story4.valid?.should be_true
1213 context '編集不可だったとき' do
1215 @story = Factory.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1216 Story.any_instance.stub(:allow?).and_return(false)
1218 it '403Forbidden例外を返す' do
1221 }.should raise_error(ActiveRecord::Forbidden)
1225 describe '切り詰め処理つき削除に於いて' do
1227 @comic = Factory :comic, :author_id => @author.id
1228 @panel = Factory :panel, :author_id => @author.id
1229 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1231 context 'つつがなく終わるとき' do
1234 @story.destroy_and_shorten
1235 }.should change(Story, :count ).by(-1)
1238 #連携テスト。切り詰めが直接DBをいじる
1239 context '2件で先頭を削除したとき' do
1241 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1245 @story.destroy_and_shorten
1246 }.should change(Story, :count ).by(-1)
1249 @story.destroy_and_shorten
1251 Story.find @story.id
1252 }.should raise_error(ActiveRecord::RecordNotFound)
1255 @story.destroy_and_shorten
1257 @story2.t.should eq 0
1260 context '3件で先頭を削除したとき' do
1262 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1263 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1267 @story.destroy_and_shorten
1268 }.should change(Story, :count ).by(-1)
1271 @story.destroy_and_shorten
1273 Story.find @story.id
1274 }.should raise_error(ActiveRecord::RecordNotFound)
1277 @story.destroy_and_shorten
1279 @story2.t.should eq 0
1282 @story.destroy_and_shorten
1284 @story3.t.should eq 1
1287 context '5件で3件目を削除したとき' do
1289 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1290 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1291 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1292 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1296 @story3.destroy_and_shorten
1297 }.should change(Story, :count ).by(-1)
1300 @story3.destroy_and_shorten
1302 @story.t.should eq 0
1305 @story3.destroy_and_shorten
1307 @story2.t.should eq 1
1310 @story3.destroy_and_shorten
1312 Story.find @story3.id
1313 }.should raise_error(ActiveRecord::RecordNotFound)
1316 @story3.destroy_and_shorten
1318 @story4.t.should eq 2
1321 @story3.destroy_and_shorten
1323 @story5.t.should eq 3
1326 #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する