1 # -*- encoding: utf-8 -*-
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
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
24 it 'オーソドックスなデータなら通る' do
25 @story.should be_valid
28 context 'comic_idを検証するとき' do
30 @story.comic_id = @comic.id
31 @story.should be_valid
35 @story.should_not be_valid
39 @story.should_not be_valid
41 it '存在するコミックでなければ失敗する' do
43 @story.should_not be_valid
47 context 'panel_idを検証するとき' do
49 @story.panel_id = @panel.id
50 @story.should be_valid
54 @story.should_not be_valid
58 @story.should_not be_valid
60 it '存在するコマでなければ失敗する' do
62 @story.should_not be_valid
71 @story.should be_valid
75 @story.should_not be_valid
79 @story.should_not be_valid
83 @story.should_not be_valid
87 context 'author_idを検証するとき' do
89 @story.author_id = @author.id
90 @story.should be_valid
93 @story.author_id = nil
94 @story.should_not be_valid
97 @story.author_id = 'a'
98 @story.should_not be_valid
100 it '存在する作家でなければ失敗する' do
102 @story.should_not be_valid
105 context '全体を検証するとき' do
107 @story = Factory :story, :author_id => @author.id
112 describe 'デフォルト値補充に於いて' do
114 @comic = Factory :comic, :author_id => @author.id
115 @panel = Factory :panel, :author_id => @author.id
118 #dbのデフォルト値が0だから明示的にnilにしないと追加ができない
120 @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id
121 @story.supply_default
122 @story.t.should be_nil
127 describe '上書き補充に於いて' do
129 @comic = Factory :comic, :author_id => @author.id
130 @panel = Factory :panel, :author_id => @author.id
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
144 describe '作者判定に於いて' 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
153 it '自分のストーリーならyes' do
154 @story.own?(@author).should == true
157 @storyo.own?(@author).should == false
160 @story.own?(nil).should == false
163 describe '単体取得に於いて' 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
170 l = Story.edit @story.id, @author
173 context '他人のストーリーを開こうとしたとき' do
174 it '403Forbidden例外を返す' do
175 Story.any_instance.stub(:own?).and_return(false)
177 Story.edit @story.id, @author
178 }.should raise_error(ActiveRecord::Forbidden)
181 context '存在しないストーリーを開こうとしたとき' do
182 it '404RecordNotFound例外を返す' do
184 Story.edit 110, @author
185 }.should raise_error(ActiveRecord::RecordNotFound)
190 describe '一覧取得に於いて' 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
197 context 'offset補正について' do
198 it '文字列から数値に変換される' do
199 Story.offset(100, '8').should eq 8
202 Story.offset(100).should eq 0
204 #投稿されたコマ数以上の値が指定されたときは、最後のコマだけになる
206 it '1件のときオフセット1なら0になる' do
207 Story.offset(1, '1').should eq 0
209 it '5件のときオフセット5なら4になる' do
210 Story.offset(5, '5').should eq 4
212 # 負の値が指定されたときは、最後のコマから数えてコマを飛ばして表示する。
213 #-4のときは、最後から4つのコマを表示する。
214 it '2件のときオフセット-1なら1になる' do
215 Story.offset(2, '-1').should eq 1
217 it '5件のときオフセット-2なら3になる' do
218 Story.offset(5, '-2').should eq 3
220 # 最終的なが負になるなど、不正な値が入ったときは0となる。
221 it '2件のときオフセット-5なら0になる' do
222 Story.offset(2, '-5').should eq 0
225 context 'panel_count補正について' do
226 it '文字列から数値に変換される' do
227 Story.panel_count(100, '7').should eq 7
229 it 'nilの場合はStory.default_panel_sizeになる' do
230 Story.panel_count(100).should eq Story.default_panel_size
232 it '0以下の場合はStory.default_panel_sizeになる' do
233 Story.panel_count(100, '0').should eq Story.default_panel_size
235 it 'Story.max_panel_sizeを超えた場合はStory.max_panel_sizeになる' do
236 Story.panel_count(100, '1000').should eq Story.max_panel_size
240 c = Story.list @comic, @author
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]
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]
253 context 'DBに5件あって1ページの件数を2件に変えたとして' 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
260 it 'offset=0なら末尾2件を返す' do
262 c = Story.list( @comic, @author, 0, 2)
263 c.should eq [@story, @story2]
265 it 'offset=2なら中間2件を返す' do
266 c = Story.list(@comic, @author, 2, 2)
267 c.should eq [@story3, @story4]
269 it 'offset=4なら先頭1件を返す' do
270 c = Story.list(@comic, @author, 4, 2)
271 c.should eq [@story5]
275 describe 'list関連テーブルプションに於いて' do
276 it 'includeキーを含んでいる' do
278 r.has_key?(:include).should be_true
281 r = Story.list_opt[:include]
282 r.should have(3).items
285 r = Story.list_opt[:include]
286 r.has_key?(:comic).should be_true
288 it 'コミックは作家を含んでいる' do
289 r = Story.list_opt[:include]
290 r[:comic].has_key?(:author).should be_true
293 r = Story.list_opt[:include]
294 r.has_key?(:author).should be_true
297 r = Story.list_opt[:include]
298 r.has_key?(:panel).should be_true
301 r = Story.list_opt[:include]
302 r[:panel].has_key?(:author).should be_true
305 r = Story.list_opt[:include]
306 r[:panel].has_key?(:panel_pictures).should be_true
308 it 'コマはフキダシを含んでいる' do
309 r = Story.list_opt[:include]
310 r[:panel].has_key?(:speech_balloons).should be_true
313 describe 'json一覧出力オプションに於いて' do
314 it 'includeキーを含んでいる' do
315 r = Story.list_json_opt
316 r.has_key?(:include).should be_true
319 r = Story.list_json_opt[:include]
320 r.should have(3).items
323 r = Story.list_json_opt[:include]
324 r.has_key?(:comic).should be_true
326 it 'コミックは作家を含んでいる' do
327 r = Story.list_json_opt[:include]
328 r[:comic].has_key?(:author).should be_true
331 r = Story.list_json_opt[:include]
332 r.has_key?(:author).should be_true
335 r = Story.list_json_opt[:include]
336 r.has_key?(:panel).should be_true
339 r = Story.list_json_opt[:include]
340 r[:panel].has_key?(:author).should be_true
343 r = Story.list_json_opt[:include]
344 r[:panel].has_key?(:panel_pictures).should be_true
346 it 'コマはフキダシを含んでいる' do
347 r = Story.list_json_opt[:include]
348 r[:panel].has_key?(:speech_balloons).should be_true
351 describe 't補充値に於いて' do
353 @comic = Factory :comic, :author_id => @author.id
354 @panel = Factory :panel, :author_id => @author.id
357 context 'コミック初のコマなら' do
359 @story = Factory.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
361 r = Story.new_t @story.comic_id
365 context 'コミックに一個コマがあるとき' 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
370 r = Story.new_t @story.comic_id
374 context 'コミックに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
380 r = Story.new_t @story.comic_id
385 describe 'シリアライズチェックに於いて' do
386 context 'つつがなく終わるとき' do
387 it '0からシリアライズされているならTrueを返す' do
388 r = Story.serial? [0, 1, 2]
391 it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
392 r = Story.serial? [0, 2, 1]
395 it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
396 r = Story.serial? [ 2, 1, 4, 3, 0]
401 it '0から始まらないならFalseを返す' do
402 r = Story.serial? [1, 2, 3]
405 it '連続していないならFalseを返す' do
406 r = Story.serial? [0, 1, 2, 4]
409 it '連続していないならFalseを返す' do
410 r = Story.serial? [0, 1, 2, 4, 5]
415 describe 't収集に於いて' 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
423 context 'つつがなく終わるとき' do
424 it 'ストーリーから同一コミックのtだけを収集している' do
425 r = Story.collect_t @story
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]
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]
444 describe 'tチェックに於いて' 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
450 context 'つつがなく終わるとき' 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
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
465 #依頼チェックだけでは不安なので最低限のチェックを
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
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
482 describe '挿入シフトに於いて' do
484 @comic = Factory :comic, :author_id => @author.id
485 @panel = Factory :panel, :author_id => @author.id
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
496 context 'テーブルに1件(t:0)で0に挿入したとき' 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
501 it '既存の行を1にシフトしている' do
504 l.first.t.should eq 1
506 it 'これから挿入するt(0)が欠番になっている' do
508 l = Story.find(:all).map {|s| s.t }
509 l.include?(0).should_not be_true
512 context 'テーブルに2件(t:0,1)で1に挿入したとき' 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
518 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
520 l = Story.find(:all).map {|s| s.t }
521 l.sort.should eq [0, 2]
524 context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' 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
533 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
535 l = Story.find(:all).map {|s| s.t }
536 l.sort.should eq [0, 1, 3, 4, 5]
539 context '先ほどのケース+他のコミック1件で挿入したとき' 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
550 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
552 l = Story.find(:all, :conditions => ['comic_id = ?', @comic.id]).map {|s| s.t }
553 l.sort.should eq [0, 1, 3, 4, 5]
559 @storyc2.t.should eq ot
563 describe '少ない方に移動に於いて' do
565 @comic = Factory :comic, :author_id => @author.id
566 @panel = Factory :panel, :author_id => @author.id
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)
576 @story2.lesser_shift ot
579 context 'テーブルに2件(t:0,1)で1を0に移動したとき' 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
586 it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
588 @story2.lesser_shift @ot
589 l = Story.find(:all).map {|s| s.t }
590 l.sort.should eq [1, 1]
592 it '既存のt0を1にシフトしている' do
593 @story2.lesser_shift @ot
598 context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' 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
606 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
608 @story3.lesser_shift @ot
609 l = Story.find(:all).map {|s| s.t }
610 l.sort.should eq [0, 2, 2]
612 it '既存のt1を2にシフトしている' do
613 @story3.lesser_shift @ot
615 @story2.t.should eq 2
618 context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' 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
628 it 'これから挿入するt(1)が欠番になっている' do
630 @story4.lesser_shift @ot
631 l = Story.find(:all).map {|s| s.t }
632 l.sort.should eq [0, 2, 3, 3, 4]
635 @story4.lesser_shift @ot
640 @story4.lesser_shift @ot
642 @story5.t.should eq 4
644 it '既存のt1を2にシフトしている' do
645 @story4.lesser_shift @ot
647 @story2.t.should eq 2
649 it '既存のt2を3にシフトしている' do
650 @story4.lesser_shift @ot
652 @story3.t.should eq 3
655 context '先ほどのケース+他のコミック1件で挿入したとき' 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
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]
673 @story4.lesser_shift @ot
678 @story4.lesser_shift @ot
680 @story5.t.should eq 4
682 it '既存のt1を2にシフトしている' do
683 @story4.lesser_shift @ot
685 @story2.t.should eq 2
687 it '既存のt2を3にシフトしている' do
688 @story4.lesser_shift @ot
690 @story3.t.should eq 3
693 @story4.lesser_shift @ot
695 @storyc2.t.should eq 0
700 context 'テーブルに2件(t:0,1)で1を-1に移動したとき' 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
707 it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
709 @story2.lesser_shift @ot
710 l = Story.find(:all).map {|s| s.t }
711 l.sort.should eq [1, 1]
713 it '既存のt0を1にシフトしている' do
714 @story2.lesser_shift @ot
718 it '既存のt1は0に補正されている' do
719 @story2.lesser_shift @ot
720 @story2.t.should eq 0
724 describe '大きい方に移動に於いて' do
726 @comic = Factory :comic, :author_id => @author.id
727 @panel = Factory :panel, :author_id => @author.id
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)
737 @story.higher_shift ot
740 context 'テーブルに2件(t:0,1)で0を1に移動したとき' 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
747 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
749 @story.higher_shift @ot
750 l = Story.find(:all).map {|s| s.t }
751 l.sort.should eq [0, 0]
753 it '既存のt1を0にシフトしている' do
754 @story.higher_shift @ot
756 @story2.t.should eq 0
759 context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' 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
767 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
769 @story.higher_shift @ot
770 l = Story.find(:all).map {|s| s.t }
771 l.sort.should eq [0, 0, 2]
773 it '既存のt1を0にシフトしている' do
774 @story.higher_shift @ot
776 @story2.t.should eq 0
779 context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' 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
789 it 'これから挿入するt(3)が欠番になっている' do
791 @story2.higher_shift @ot
792 l = Story.find(:all).map {|s| s.t }
793 l.sort.should eq [0, 1, 1, 2, 4]
796 @story2.higher_shift @ot
801 @story2.higher_shift @ot
803 @story5.t.should eq 4
805 it '既存のt2を1にシフトしている' do
806 @story2.higher_shift @ot
808 @story3.t.should eq 1
810 it '既存のt3を2にシフトしている' do
811 @story2.higher_shift @ot
813 @story4.t.should eq 2
816 context '先ほどのケース+他のコミック1件で挿入したとき' 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
828 it 'これから挿入するt(3)が欠番になっている' do
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]
835 @story2.higher_shift @ot
840 @story2.higher_shift @ot
842 @story5.t.should eq 4
844 it '既存のt2を1にシフトしている' do
845 @story2.higher_shift @ot
847 @story3.t.should eq 1
849 it '既存のt3を2にシフトしている' do
850 @story2.higher_shift @ot
852 @story4.t.should eq 2
855 @story2.higher_shift @ot
857 @storyc2.t.should eq 0
862 context 'テーブルに2件(t:0,1)で0を2に移動したとき' 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
869 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
871 @story.higher_shift @ot
872 l = Story.find(:all).map {|s| s.t }
873 l.sort.should eq [0, 0]
875 it '既存のt1を0にシフトしている' do
876 @story.higher_shift @ot
878 @story2.t.should eq 0
880 it '既存のt0は1に補正されている' do
881 @story.higher_shift @ot
886 describe '入れ替えに於いて' 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
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)
899 @story2.update_shift ot
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)
908 @story.update_shift ot
912 describe '順序入れ替えに於いて' do
914 @comic = Factory :comic, :author_id => @author.id
915 @panel = Factory :panel, :author_id => @author.id
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)
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)
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)
945 context 'オブジェクトが新規でなくtが空のとき' do
946 it '入れ替えもシフトもせず、tを空のままにしている' do
947 #結果、tに欠番が生じてシリアライズチェックでひっかかる
951 describe '編集許可に於いて' 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
957 context 'つつがなく終わるとき' do
963 context 'コミックで引っかかるとき' do
965 @story.comic_id = nil
970 Comic.any_instance.stub(:own?).with(any_args).and_return(false)
975 context 'コマで引っかかるとき' do
977 @story.panel_id = nil
982 Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
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
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)
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)
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)
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)
1022 #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1023 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したとき' 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
1034 it '既存のt0には変化がない' do
1037 @story.t.should eq 0
1039 it '既存のt1には変化がない' do
1042 @story2.t.should eq 1
1044 it '既存のt2を3にシフトしている' do
1047 @story3.t.should eq 3
1049 it '既存のt3を4にシフトしている' do
1052 @story4.t.should eq 4
1054 it '既存のt5を5にシフトしている' do
1057 @story5.t.should eq 5
1059 it '新規のt2が作成されている' do
1062 @story6.t.should eq 2
1064 it '他のコミックに影響がない' do
1068 @storyc2.t.should eq @ot
1071 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したとき' 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
1083 it '既存のt0には変化がない' do
1086 @story.t.should eq 0
1088 it '既存のt4には変化がない' do
1091 @story5.t.should eq 4
1093 it '既存のt1を2にシフトしている' do
1096 @story2.t.should eq 2
1098 it '既存のt2を3にシフトしている' do
1101 @story3.t.should eq 3
1103 it '既存のt3を1にシフトしている' do
1106 @story4.t.should eq 1
1108 it '他のコミックに影響がない' do
1111 @storyc2.t.should eq 0
1114 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で1を3に移動したとき' 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
1126 it '既存のt0には変化がない' do
1129 @story.t.should eq 0
1131 it '既存のt4には変化がない' do
1134 @story5.t.should eq 4
1136 it '既存のt1を3にシフトしている' do
1139 @story2.t.should eq 3
1141 it '既存のt2を1にシフトしている' do
1144 @story3.t.should eq 1
1146 it '既存のt3を2にシフトしている' do
1149 @story4.t.should eq 2
1151 it '他のコミックに影響がない' do
1154 @storyc2.t.should eq 0
1157 #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1158 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したが保存に失敗したとき' 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
1173 @story.t.should eq 0
1175 @story2.t.should eq 1
1177 @story3.t.should eq 2
1179 @story4.t.should eq 3
1181 @story5.t.should eq 4
1183 @storyc2.t.should eq 0
1190 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したがシリアルチェックに失敗したとき' 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
1206 @story.t.should eq 0
1208 @story2.t.should eq 1
1210 @story3.t.should eq 2
1212 @story4.t.should eq 3
1214 @story5.t.should eq 4
1216 @storyc2.t.should eq 0
1219 r = @story4.store @ot
1222 it 'tにエラーメッセージが入っている' do
1224 @story4.errors[:t].should_not be_empty
1225 @story4.valid?.should be_true
1228 context '編集不可だったとき' 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)
1233 it '403Forbidden例外を返す' do
1236 }.should raise_error(ActiveRecord::Forbidden)
1240 describe '切り詰め処理つき削除に於いて' 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
1246 context 'つつがなく終わるとき' do
1249 @story.destroy_and_shorten
1250 }.should change(Story, :count ).by(-1)
1253 #連携テスト。切り詰めが直接DBをいじる
1254 context '2件で先頭を削除したとき' do
1256 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1260 @story.destroy_and_shorten
1261 }.should change(Story, :count ).by(-1)
1264 @story.destroy_and_shorten
1266 Story.find @story.id
1267 }.should raise_error(ActiveRecord::RecordNotFound)
1270 @story.destroy_and_shorten
1272 @story2.t.should eq 0
1275 context '3件で先頭を削除したとき' 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
1282 @story.destroy_and_shorten
1283 }.should change(Story, :count ).by(-1)
1286 @story.destroy_and_shorten
1288 Story.find @story.id
1289 }.should raise_error(ActiveRecord::RecordNotFound)
1292 @story.destroy_and_shorten
1294 @story2.t.should eq 0
1297 @story.destroy_and_shorten
1299 @story3.t.should eq 1
1302 context '5件で3件目を削除したとき' 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
1311 @story3.destroy_and_shorten
1312 }.should change(Story, :count ).by(-1)
1315 @story3.destroy_and_shorten
1317 @story.t.should eq 0
1320 @story3.destroy_and_shorten
1322 @story2.t.should eq 1
1325 @story3.destroy_and_shorten
1327 Story.find @story3.id
1328 }.should raise_error(ActiveRecord::RecordNotFound)
1331 @story3.destroy_and_shorten
1333 @story4.t.should eq 2
1336 @story3.destroy_and_shorten
1338 @story5.t.should eq 3
1341 #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する