1 # -*- encoding: utf-8 -*-
7 @admin = FactoryGirl.create :admin
8 @demand_user = FactoryGirl.create :demand_user
9 @sp = FactoryGirl.create :system_picture
10 @lg = FactoryGirl.create :license_group
11 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
12 @user = FactoryGirl.create( :user_yas)
13 @author = FactoryGirl.create :author, :user_id => @user.id
14 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
15 @other_user = FactoryGirl.create( :user_yas)
16 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
17 @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
22 @comic = FactoryGirl.create :comic, :author_id => @author.id
23 @story = FactoryGirl.build :story, :comic_id => @comic.id
26 context 'オーソドックスなデータのとき' do
31 @story.should be_valid
34 @story.title = 'a'*100
37 @story.should be_valid
41 context 'comic_idを検証するとき' do
44 @story.should_not be_valid
48 @story.should_not be_valid
50 it '存在するコミックでなければ失敗する' do
52 @story.should_not be_valid
56 context 'titleを検証するとき' do
59 @story.should_not be_valid
62 @story.title = 'a'*101
63 @story.should_not be_valid
67 context 'visibleを検証するとき' do
70 @story.should_not be_valid
74 @story.should_not be_valid
78 @story.should_not be_valid
85 @story.should_not be_valid
89 @story.should_not be_valid
93 @story.should_not be_valid
98 describe '文字コード検証に於いて' do
100 @comic = FactoryGirl.create :comic, :author_id => @author.id
101 @story = FactoryGirl.build :story, :comic_id => @comic.id
104 context 'titleを検証するとき' do
105 it 'Shift JISなら失敗する' do
106 @story.title = "\x83G\x83r\x83]\x83D"
109 }.should raise_error(Pettanr::BadRequest)
113 context 'descriptionを検証するとき' do
114 it 'Shift JISなら失敗する' do
115 @story.description = "\x83G\x83r\x83]\x83D"
118 }.should raise_error(Pettanr::BadRequest)
123 describe 'デフォルト値補充に於いて' do
124 it 'visibleが0になっている' do
125 @story = FactoryGirl.build :story, :visible => nil
126 @story.supply_default
127 @story.visible.should eq 0
131 describe '上書き補充に於いて' do
134 describe '所持判定に於いて' do
136 @comic = FactoryGirl.create :comic, :author_id => @author.id
137 @story = FactoryGirl.build :story, :comic_id => @comic.id
139 context '事前チェックする' do
140 it '自身にロールリストからの作家取得を依頼している' do
141 Story.should_receive(:get_author_from_roles).with(any_args).exactly(1)
142 r = @story.own?([@author])
145 context 'ロール内作家が取得できるとき' do
148 it 'ロール内作家のidが自身のコミックの作家idと一致するなら許可する' do
149 Story.stub(:get_author_from_roles).with(any_args).and_return(@author)
150 r = @story.own?([@author])
153 it 'ロール内作家のidが自身のコミックの作家idと一致しないならno' do
154 Story.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
155 @story.own?(@other_author).should be_false
158 context 'ロール内作家が取得できないとき' do
160 Story.stub(:get_author_from_roles).with(any_args).and_return(nil)
163 r = @story.own?([@author])
169 describe '閲覧許可に於いて' do
171 @comic = FactoryGirl.create :comic, :author_id => @author.id
172 @story = FactoryGirl.build :story, :comic_id => @comic.id
174 context 'オープンモードのとき' do
176 MagicNumber['run_mode'] = 0
178 it '自身にゲスト用ロールチェックを問い合わせしている' do
179 Story.any_instance.stub(:guest_role_check).and_return(true)
180 Story.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
181 r = @story.visible?([@author])
183 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
184 Story.any_instance.stub(:guest_role_check).and_return(false)
185 r = @story.visible?([@author])
189 context 'クローズドモードのとき' do
191 MagicNumber['run_mode'] = 1
193 it '自身に読者用ロールチェックを問い合わせしている' do
194 Story.any_instance.stub(:reader_role_check).and_return(true)
195 Story.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
196 r = @story.visible?([@author])
198 it '読者用ロールチェックが失敗したとき、falseを返す' do
199 Story.any_instance.stub(:reader_role_check).and_return(false)
200 r = @story.visible?([@author])
204 context '事前チェックする' do
206 MagicNumber['run_mode'] = 1
207 Story.any_instance.stub(:reader_role_check).and_return(true)
208 Story.any_instance.stub(:own?).and_return(true)
210 it '自身に所持判定を問い合わせしている' do
211 Story.any_instance.should_receive(:own?).with(any_args).exactly(1)
212 r = @story.visible?([@author])
215 context 'つつがなく終わるとき' do
217 MagicNumber['run_mode'] = 1
218 Story.any_instance.stub(:reader_role_check).and_return(true)
220 it '自分のストーリーなら許可する' do
221 Story.any_instance.stub(:own?).and_return(true)
222 Story.any_instance.stub(:visible).and_return(0)
223 r = @story.visible?([@author])
226 it '他人の非公開ストーリーなら許可しない' do
227 Story.any_instance.stub(:own?).and_return(false)
228 Story.any_instance.stub(:visible).and_return(0)
229 r = @story.visible?([@author])
232 it '他人のストーリーでも公開なら許可する' do
233 Story.any_instance.stub(:own?).and_return(false)
234 Story.any_instance.stub(:visible).and_return(1)
235 r = @story.visible?([@author])
241 describe '一覧取得に於いて' do
243 @comic = FactoryGirl.create :comic, :author_id => @author.id
244 @story = FactoryGirl.create :story, :comic_id => @comic.id
246 context 'page補正について' do
247 it '文字列から数値に変換される' do
248 Story.page('8').should eq 8
251 Story.page().should eq 1
254 Story.page('0').should eq 1
257 context 'page_size補正について' do
258 it '文字列から数値に変換される' do
259 Story.page_size('7').should eq 7
261 it 'nilの場合はStory.default_page_sizeになる' do
262 Story.page_size().should eq Story.default_page_size
264 it '0以下の場合はStory.default_page_sizeになる' do
265 Story.page_size('0').should eq Story.default_page_size
267 it 'Story.max_page_sizeを超えた場合はStory.max_page_sizeになる' do
268 Story.page_size('1000').should eq Story.max_page_size
271 context 'つつがなく終わるとき' do
272 it '一覧取得オプションを利用している' do
273 Story.stub(:list_opt).with(any_args).and_return({})
274 Story.should_receive(:list_opt).with(any_args).exactly(1)
282 it '非公開ストーリーは(自分のストーリーであっても)含んでいない' do
283 FactoryGirl.create :story, :comic_id => @comic.id, :visible => 0
288 #公開ストーリーは(他人のストーリーであっても)含んでいる
289 other_comic = FactoryGirl.create :comic, :author_id => @other_author.id
290 v = FactoryGirl.create :story, :comic_id => other_comic.id, :updated_at => Time.now + 100
292 c.should eq [v, @story]
294 context 'DBに5件あって1ページの件数を2件に変えたとして' do
296 @story2 = FactoryGirl.create :story, :comic_id => @comic.id, :updated_at => Time.now + 100
297 @story3 = FactoryGirl.create :story, :comic_id => @comic.id, :updated_at => Time.now + 200
298 @story4 = FactoryGirl.create :story, :comic_id => @comic.id, :updated_at => Time.now + 300
299 @story5 = FactoryGirl.create :story, :comic_id => @comic.id, :updated_at => Time.now + 400
300 Story.stub(:default_page_size).and_return(2)
304 c.should have(2).items
306 it 'page=1なら末尾2件を返す' do
309 c.should eq [@story5, @story4]
311 it 'page=2なら中間2件を返す' do
313 c.should eq [@story3, @story2]
315 it 'page=3なら先頭1件を返す' do
322 describe '自分のストーリー一覧取得に於いて' do
324 @comic = FactoryGirl.create :comic, :author_id => @author.id
325 @story = FactoryGirl.create :story, :comic_id => @comic.id
327 context 'つつがなく終わるとき' do
328 it '一覧取得オプションを利用している' do
329 Story.stub(:list_opt).with(any_args).and_return({})
330 Story.should_receive(:list_opt).with(any_args).exactly(1)
331 r = Story.mylist @author
335 c = Story.mylist @author
339 nc = FactoryGirl.create :story, :comic_id => @comic.id, :updated_at => Time.now + 100
340 cl = Story.mylist @author
341 cl.should eq [nc, @story]
343 it '他人のストーリーは公開でも含まない' do
344 other_comic = FactoryGirl.create :comic, :author_id => @other_author.id
345 nc = FactoryGirl.create :story, :comic_id => other_comic.id, :visible => 1
346 cl = Story.mylist @author
347 cl.should eq [@story]
349 it '自分のストーリーは非公開でも含んでいる' do
350 nc = FactoryGirl.create :story, :comic_id => @comic.id, :visible => 0, :updated_at => Time.now + 100
351 cl = Story.mylist @author
352 cl.should eq [nc, @story]
354 context 'DBに5件あって1ページの件数を2件に変えたとして' do
356 @story2 = FactoryGirl.create :story, :comic_id => @comic.id, :updated_at => Time.now + 100
357 @story3 = FactoryGirl.create :story, :comic_id => @comic.id, :updated_at => Time.now + 200
358 @story4 = FactoryGirl.create :story, :comic_id => @comic.id, :updated_at => Time.now + 300
359 @story5 = FactoryGirl.create :story, :comic_id => @comic.id, :updated_at => Time.now + 400
362 c = Story.mylist @author, 1, 2
363 c.should have(2).items
365 it 'page=1なら末尾2件を返す' do
367 c = Story.mylist(@author, 1, 2)
368 c.should eq [@story5, @story4]
370 it 'page=2なら中間2件を返す' do
371 c = Story.mylist(@author, 2, 2)
372 c.should eq [@story3, @story2]
374 it 'page=3なら先頭1件を返す' do
375 c = Story.mylist(@author, 3, 2)
381 describe '他作家のストーリー一覧取得に於いて' do
383 @comic = FactoryGirl.create :comic, :author_id => @author.id
384 @story = FactoryGirl.create :story, :comic_id => @comic.id
385 @other_comic = FactoryGirl.create :comic, :visible => 1, :author_id => @other_author.id
386 @other_story = FactoryGirl.create :story, :visible => 1, :comic_id => @other_comic.id
388 context 'つつがなく終わるとき' do
389 it '一覧取得オプションを利用している' do
390 Story.stub(:list_opt).with(any_args).and_return({})
391 Story.should_receive(:list_opt).with(any_args).exactly(1)
392 r = Story.himlist @other_author
395 it '指定した作家のリストを返す' do
396 r = Story.himlist @other_author
397 r.should eq [@other_story]
400 nc = FactoryGirl.create :story, :comic_id => @other_comic.id, :updated_at => Time.now + 100
401 r = Story.himlist @other_author
402 r.should eq [nc, @other_story]
405 hidden = FactoryGirl.create :story, :comic_id => @other_comic.id, :visible => 0
406 r = Story.himlist @other_author
407 r.should eq [@other_story]
409 context 'DBに5件あって1ページの件数を2件に変えたとして' do
411 @other_story2 = FactoryGirl.create :story, :comic_id => @other_comic.id, :updated_at => Time.now + 100
412 @other_story3 = FactoryGirl.create :story, :comic_id => @other_comic.id, :updated_at => Time.now + 200
413 @other_story4 = FactoryGirl.create :story, :comic_id => @other_comic.id, :updated_at => Time.now + 300
414 @other_story5 = FactoryGirl.create :story, :comic_id => @other_comic.id, :updated_at => Time.now + 400
417 c = Story.himlist @other_author, 1, 2
418 c.should have(2).items
420 it 'page=1なら末尾2件を返す' do
422 c = Story.himlist(@other_author, 1, 2)
423 c.should eq [@other_story5, @other_story4]
425 it 'page=2なら中間2件を返す' do
426 c = Story.himlist(@other_author, 2, 2)
427 c.should eq [@other_story3, @other_story2]
429 it 'page=3なら先頭1件を返す' do
430 c = Story.himlist(@other_author, 3, 2)
431 c.should eq [@other_story]
436 describe 'ストーリー一覧ページ制御に於いて' do
438 Story.stub(:count).with(any_args).and_return(100)
441 r = Story.list_paginate
442 r.is_a?(Kaminari::PaginatableArray).should be_true
444 it 'ストーリー一覧の取得条件を利用している' do
445 Story.stub(:list_where).with(any_args).and_return('')
446 Story.should_receive(:list_where).with(any_args).exactly(1)
447 r = Story.list_paginate
449 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
450 r = Story.list_paginate 3, 10
451 r.limit_value.should eq 10
452 r.offset_value.should eq 20
456 describe '自分のストーリー一覧ページ制御に於いて' do
458 Story.stub(:count).with(any_args).and_return(100)
461 r = Story.mylist_paginate @author
462 r.is_a?(Kaminari::PaginatableArray).should be_true
464 it '自分のストーリー一覧の取得条件を利用している' do
465 Story.stub(:mylist_where).with(any_args).and_return('')
466 Story.should_receive(:mylist_where).with(any_args).exactly(1)
467 r = Story.mylist_paginate @author
469 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
470 r = Story.mylist_paginate @author, 3, 10
471 r.limit_value.should eq 10
472 r.offset_value.should eq 20
476 describe '他作家のストーリー一覧ページ制御に於いて' do
478 Story.stub(:count).with(any_args).and_return(100)
481 r = Story.himlist_paginate @other_author
482 r.is_a?(Kaminari::PaginatableArray).should be_true
484 it '他作家のストーリー一覧の取得条件を利用している' do
485 Story.stub(:himlist_where).with(any_args).and_return('')
486 Story.should_receive(:himlist_where).with(any_args).exactly(1)
487 r = Story.himlist_paginate @other_author
489 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
490 r = Story.himlist_paginate @other_author, 3, 10
491 r.limit_value.should eq 10
492 r.offset_value.should eq 20
496 describe '一覧取得オプションに於いて' do
499 r.should have(1).items
503 r.has_key?(:comic).should be_true
505 it 'コミックは作家を含んでいる' do
507 r[:comic].has_key?(:author).should be_true
510 describe 'json一覧出力オプションに於いて' do
512 @comic = FactoryGirl.create :comic, :author_id => @author.id
513 @story = FactoryGirl.create :story, :comic_id => @comic.id, :visible => 1
516 r = Story.list.to_json Story.list_json_opt
519 i.has_key?('comic').should be_true
521 it 'コミックは作家を含んでいる' do
522 r = Story.list.to_json Story.list_json_opt
526 s.has_key?('author').should be_true
530 describe '単体取得に於いて' do
532 @comic = FactoryGirl.create :comic, :author_id => @author.id
533 @story = FactoryGirl.create :story, :comic_id => @comic.id
535 context 'つつがなく終わるとき' do
536 it '単体取得オプションを利用している' do
537 Story.stub(:show_opt).with(any_args).and_return({})
538 Story.should_receive(:show_opt).with(any_args).exactly(1)
539 r = Story.show @story.id, @author
541 it '閲覧許可を問い合わせている' do
542 Story.any_instance.stub(:visible?).with(any_args).and_return(true)
543 Story.any_instance.should_receive(:visible?).with(any_args).exactly(1)
544 r = Story.show @story.id, @author
548 c = Story.show @story.id, @author
551 context '閲覧許可が出なかったとき' do
552 it '403Forbidden例外を返す' do
553 Story.any_instance.stub(:visible?).and_return(false)
555 Story.show @story.id, @author
556 }.should raise_error(ActiveRecord::Forbidden)
559 context '存在しないストーリーを開こうとしたとき' do
560 it '404RecordNotFound例外を返す' do
562 Story.show 110, @author
563 }.should raise_error(ActiveRecord::RecordNotFound)
568 describe '編集取得に於いて' do
570 @comic = FactoryGirl.create :comic, :author_id => @author.id
571 @story = FactoryGirl.create :story, :comic_id => @comic.id
573 context 'つつがなく終わるとき' do
574 it '単体取得オプションを利用している' do
575 Story.stub(:show_opt).with(any_args).and_return({})
576 Story.should_receive(:show_opt).with(any_args).exactly(1)
577 r = Story.edit @story.id, @author
579 it '所持判定を問い合わせている' do
580 Story.any_instance.stub(:own?).with(any_args).and_return(true)
581 Story.any_instance.should_receive(:own?).with(any_args).exactly(1)
582 r = Story.edit @story.id, @author
586 Story.any_instance.stub(:own?).and_return(true)
587 c = Story.edit @story.id, @author.id
590 context '他人のストーリーを開こうとしたとき' do
591 it '403Forbidden例外を返す' do
592 Story.any_instance.stub(:own?).and_return(false)
594 Story.edit @story.id, @author
595 }.should raise_error(ActiveRecord::Forbidden)
598 context '存在しないストーリーを開こうとしたとき' do
599 it '404RecordNotFound例外を返す' do
601 Story.edit 110, @author
602 }.should raise_error(ActiveRecord::RecordNotFound)
606 describe '単体取得オプションに於いて' do
607 it 'includeキーを含んでいる' do
609 r.has_key?(:include).should be_true
612 r = Story.show_opt[:include]
613 r.should have(1).items
616 r = Story.show_opt[:include]
617 r.has_key?(:comic).should be_true
619 it 'コミックは作家を含んでいる' do
620 r = Story.show_opt[:include]
621 r[:comic].has_key?(:author).should be_true
624 describe 'json単体出力オプションに於いて' do
626 @comic = FactoryGirl.create :comic, :author_id => @author.id
627 @story = FactoryGirl.create :story, :comic_id => @comic.id, :visible => 1
630 r = Story.show(@story.id, @author).to_json Story.show_json_opt
633 i.has_key?('comic').should be_true
635 it 'コミックは作家を含んでいる' do
636 r = Story.show(@story.id, @author).to_json Story.show_json_opt
640 s.has_key?('author').should be_true
644 describe 't補充値に於いて' do
646 @comic = FactoryGirl.create :comic, :author_id => @author.id
649 context 'コミック初のコマなら' do
651 @story = FactoryGirl.build :story, :comic_id => @comic.id
653 r = Story.new_t @story.comic_id
657 context 'コミックに一個コマがあるとき' do
659 FactoryGirl.create :story, :comic_id => @comic.id, :t => 0
660 @story = FactoryGirl.build :story, :comic_id => @comic.id
662 r = Story.new_t @story.comic_id
666 context 'コミックに2個コマがあるとき' do
668 FactoryGirl.create :story, :comic_id => @comic.id, :t => 0
669 FactoryGirl.create :story, :comic_id => @comic.id, :t => 1
670 @story = FactoryGirl.build :story, :comic_id => @comic.id
672 r = Story.new_t @story.comic_id
677 describe 'シリアライズチェックに於いて' do
678 context 'つつがなく終わるとき' do
679 it '0からシリアライズされているならTrueを返す' do
680 r = Story.serial? [0, 1, 2]
683 it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
684 r = Story.serial? [0, 2, 1]
687 it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
688 r = Story.serial? [ 2, 1, 4, 3, 0]
693 it '0から始まらないならFalseを返す' do
694 r = Story.serial? [1, 2, 3]
697 it '連続していないならFalseを返す' do
698 r = Story.serial? [0, 1, 2, 4]
701 it '連続していないならFalseを返す' do
702 r = Story.serial? [0, 1, 2, 4, 5]
707 describe 't収集に於いて' do
709 @comic = FactoryGirl.create :comic, :author_id => @author.id
710 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
711 @comic2 = FactoryGirl.create :comic, :author_id => @author.id
712 @c2story = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id
714 context 'つつがなく終わるとき' do
715 it 'ストーリーから同一コミックのtだけを収集している' do
716 r = Story.collect_t @story
721 it 'ストーリーから同一コミックのtだけを収集している' do
722 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
723 r = Story.collect_t @story
724 r.sort.should eq [0, 1]
727 context '複数ストーリーでヨソのコミックも混じっているとき' do
728 it 'ストーリーから同一コミックのtだけを収集している' do
729 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
730 r = Story.collect_t @story
731 r.sort.should eq [0, 1]
735 describe 'tチェックに於いて' do
737 @comic = FactoryGirl.create :comic, :author_id => @author.id
738 @story = FactoryGirl.build :story, :t => 1, :comic_id => @comic.id
740 context 'つつがなく終わるとき' do
742 Story.should_receive(:collect_t).with(any_args).exactly(1)
743 Story.stub(:collect_t).with(any_args).and_return([])
744 Story.stub(:serial?).with(any_args).and_return(true)
745 r = Story.validate_t @story
747 it '収集したtをシリアライズチェック依頼している' do
748 Story.stub(:collect_t).with(any_args).and_return([])
749 Story.should_receive(:serial?).with(any_args).exactly(1)
750 Story.stub(:serial?).with(any_args).and_return(true)
751 r = Story.validate_t @story
755 #依頼チェックだけでは不安なので最低限のチェックを
757 it '一件だけで正常通過している' do
758 @story = FactoryGirl.build :story, :comic_id => @comic.id, :t => 0
759 r = Story.validate_t @story
764 it '2件目を作っても正常通過している' do
765 @story = FactoryGirl.create :story, :comic_id => @comic.id, :t => 0
766 @story2 = FactoryGirl.build :story, :comic_id => @comic.id, :t => 1
767 r = Story.validate_t @story2
772 describe '挿入シフトに於いて' do
774 @comic = FactoryGirl.create :comic, :author_id => @author.id
778 it 'Updateを依頼している' do
779 Story.stub(:update_all).with(any_args)
780 Story.should_receive(:update_all).with(any_args).exactly(1)
781 @story = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id
785 context 'テーブルに1件(t:0)で0に挿入したとき' do
787 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
788 @story2 = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id
790 it '既存の行を1にシフトしている' do
793 l.first.t.should eq 1
795 it 'これから挿入するt(0)が欠番になっている' do
797 l = Story.find(:all).map {|s| s.t }
798 l.include?(0).should_not be_true
801 context 'テーブルに2件(t:0,1)で1に挿入したとき' do
803 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
804 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
805 @story3 = FactoryGirl.build :story, :t => 1, :comic_id => @comic.id
807 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
809 l = Story.find(:all).map {|s| s.t }
810 l.sort.should eq [0, 2]
813 context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
815 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
816 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
817 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
818 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
819 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
820 @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id
822 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
824 l = Story.find(:all).map {|s| s.t }
825 l.sort.should eq [0, 1, 3, 4, 5]
828 context '先ほどのケース+他のコミック1件で挿入したとき' do
830 @comic2 = FactoryGirl.create :comic, :author_id => @author.id
831 @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id
832 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
833 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
834 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
835 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
836 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
837 @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id
839 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
841 l = Story.find(:all, :conditions => ['comic_id = ?', @comic.id]).map {|s| s.t }
842 l.sort.should eq [0, 1, 3, 4, 5]
848 @storyc2.t.should eq ot
852 describe '少ない方に移動に於いて' do
854 @comic = FactoryGirl.create :comic
857 it 'Updateを依頼している' do
858 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
859 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
860 Story.stub(:update_all).with(any_args)
861 Story.should_receive(:update_all).with(any_args).exactly(1)
864 @story2.lesser_shift ot
867 context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
869 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
870 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
874 it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
876 @story2.lesser_shift @ot
877 l = Story.find(:all).map {|s| s.t }
878 l.sort.should eq [1, 1]
880 it '既存のt0を1にシフトしている' do
881 @story2.lesser_shift @ot
886 context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
888 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
889 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
890 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
894 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
896 @story3.lesser_shift @ot
897 l = Story.find(:all).map {|s| s.t }
898 l.sort.should eq [0, 2, 2]
900 it '既存のt1を2にシフトしている' do
901 @story3.lesser_shift @ot
903 @story2.t.should eq 2
906 context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
908 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
909 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
910 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
911 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
912 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
916 it 'これから挿入するt(1)が欠番になっている' do
918 @story4.lesser_shift @ot
919 l = Story.find(:all).map {|s| s.t }
920 l.sort.should eq [0, 2, 3, 3, 4]
923 @story4.lesser_shift @ot
928 @story4.lesser_shift @ot
930 @story5.t.should eq 4
932 it '既存のt1を2にシフトしている' do
933 @story4.lesser_shift @ot
935 @story2.t.should eq 2
937 it '既存のt2を3にシフトしている' do
938 @story4.lesser_shift @ot
940 @story3.t.should eq 3
943 context '先ほどのケース+他のコミック1件で挿入したとき' do
945 @comic2 = FactoryGirl.create :comic
946 @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id
947 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
948 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
949 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
950 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
951 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
955 it 'これから挿入するt(1)が欠番になっている' do
956 @story4.lesser_shift @ot
957 l = Story.find(:all).map {|s| s.t }
958 l.sort.should eq [0, 0, 2, 3, 3, 4]
961 @story4.lesser_shift @ot
966 @story4.lesser_shift @ot
968 @story5.t.should eq 4
970 it '既存のt1を2にシフトしている' do
971 @story4.lesser_shift @ot
973 @story2.t.should eq 2
975 it '既存のt2を3にシフトしている' do
976 @story4.lesser_shift @ot
978 @story3.t.should eq 3
981 @story4.lesser_shift @ot
983 @storyc2.t.should eq 0
988 context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
990 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
991 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
995 it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
997 @story2.lesser_shift @ot
998 l = Story.find(:all).map {|s| s.t }
999 l.sort.should eq [1, 1]
1001 it '既存のt0を1にシフトしている' do
1002 @story2.lesser_shift @ot
1004 @story.t.should eq 1
1006 it '既存のt1は0に補正されている' do
1007 @story2.lesser_shift @ot
1008 @story2.t.should eq 0
1012 describe '大きい方に移動に於いて' do
1014 @comic = FactoryGirl.create :comic, :author_id => @author.id
1017 it 'Updateを依頼している' do
1018 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1019 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1020 Story.stub(:update_all).with(any_args)
1021 Story.should_receive(:update_all).with(any_args).exactly(1)
1024 @story.higher_shift ot
1027 context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
1029 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1030 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1034 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1036 @story.higher_shift @ot
1037 l = Story.find(:all).map {|s| s.t }
1038 l.sort.should eq [0, 0]
1040 it '既存のt1を0にシフトしている' do
1041 @story.higher_shift @ot
1043 @story2.t.should eq 0
1046 context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
1048 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1049 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1050 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
1054 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1056 @story.higher_shift @ot
1057 l = Story.find(:all).map {|s| s.t }
1058 l.sort.should eq [0, 0, 2]
1060 it '既存のt1を0にシフトしている' do
1061 @story.higher_shift @ot
1063 @story2.t.should eq 0
1066 context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
1068 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1069 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1070 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
1071 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
1072 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
1076 it 'これから挿入するt(3)が欠番になっている' do
1078 @story2.higher_shift @ot
1079 l = Story.find(:all).map {|s| s.t }
1080 l.sort.should eq [0, 1, 1, 2, 4]
1082 it '既存のt0には変化がない' do
1083 @story2.higher_shift @ot
1085 @story.t.should eq 0
1087 it '既存のt4には変化がない' do
1088 @story2.higher_shift @ot
1090 @story5.t.should eq 4
1092 it '既存のt2を1にシフトしている' do
1093 @story2.higher_shift @ot
1095 @story3.t.should eq 1
1097 it '既存のt3を2にシフトしている' do
1098 @story2.higher_shift @ot
1100 @story4.t.should eq 2
1103 context '先ほどのケース+他のコミック1件で挿入したとき' do
1105 @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1106 @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id
1107 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1108 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1109 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
1110 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
1111 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
1115 it 'これから挿入するt(3)が欠番になっている' do
1117 @story2.higher_shift @ot
1118 l = Story.find(:all).map {|s| s.t }
1119 l.sort.should eq [0, 0, 1, 1, 2, 4]
1121 it '既存のt0には変化がない' do
1122 @story2.higher_shift @ot
1124 @story.t.should eq 0
1126 it '既存のt4には変化がない' do
1127 @story2.higher_shift @ot
1129 @story5.t.should eq 4
1131 it '既存のt2を1にシフトしている' do
1132 @story2.higher_shift @ot
1134 @story3.t.should eq 1
1136 it '既存のt3を2にシフトしている' do
1137 @story2.higher_shift @ot
1139 @story4.t.should eq 2
1141 it '他のコミックに影響がない' do
1142 @story2.higher_shift @ot
1144 @storyc2.t.should eq 0
1148 #max超えたときはmaxとして正常扱い
1149 context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
1151 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1152 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1156 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1158 @story.higher_shift @ot
1159 l = Story.find(:all).map {|s| s.t }
1160 l.sort.should eq [0, 0]
1162 it '既存のt1を0にシフトしている' do
1163 @story.higher_shift @ot
1165 @story2.t.should eq 0
1167 it '既存のt0は1に補正されている' do
1168 @story.higher_shift @ot
1169 @story.t.should eq 1
1173 describe '入れ替えに於いて' do
1175 @comic = FactoryGirl.create :comic, :author_id => @author.id
1176 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1177 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1179 context '新tが旧tより小さいとき' do
1180 it '少ない方に移動を依頼している' do
1181 Story.any_instance.stub(:lesser_shift).with(any_args)
1182 Story.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
1185 @story2.update_shift ot
1188 context '新tが旧tより大きいとき' do
1189 it '大きい方に移動を依頼している' do
1190 Story.any_instance.stub(:higher_shift).with(any_args)
1191 Story.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
1194 @story.update_shift ot
1198 describe '順序入れ替えに於いて' do
1200 @comic = FactoryGirl.create :comic, :author_id => @author.id
1202 context 'オブジェクトが新規でtが空のとき' do
1203 it '末尾追加としてtを補充依頼している' do
1204 @story = FactoryGirl.build :story, :comic_id => @comic.id
1205 Story.stub(:new_t).with(any_args).and_return(0)
1206 Story.should_receive(:new_t).with(any_args).exactly(1)
1211 context 'オブジェクトが新規でtが設定されているとき' do
1212 it '挿入追加として挿入シフトを依頼している' do
1213 @story = FactoryGirl.build :story, :comic_id => @comic.id
1214 Story.any_instance.stub(:insert_shift).with(any_args)
1215 Story.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
1220 context 'オブジェクトが新規でなくtが設定されているとき' do
1221 it '移動として入れ替えを依頼している' do
1222 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1223 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1224 Story.any_instance.stub(:update_shift).with(any_args)
1225 Story.any_instance.should_receive(:update_shift).with(1).exactly(1)
1230 context 'オブジェクトが新規でなくtが空のとき' do
1231 it '入れ替えもシフトもせず、tを空のままにしている' do
1232 #結果、tに欠番が生じてシリアライズチェックでひっかかる
1236 describe '編集許可に於いて' do
1238 @comic = FactoryGirl.create :comic, :author_id => @author.id
1239 @story = FactoryGirl.build :story, :t => nil, :comic_id => @comic.id
1241 context 'つつがなく終わるとき' do
1243 r = @story.allow? @author
1247 context 'コミックで引っかかるとき' do
1249 Comic.any_instance.stub(:own?).with(any_args).and_return(false)
1250 r = @story.allow? @other_author
1254 context 'コミックが指定されていなかったとき' do
1256 Comic.any_instance.stub(:own?).with(any_args).and_return(true)
1257 @story.comic_id = nil
1258 r = @story.allow? @author
1263 describe '保存に於いて' do
1265 @comic = FactoryGirl.create :comic, :author_id => @author.id
1266 @story = FactoryGirl.build :story, :t => nil, :comic_id => @comic.id
1268 context 'つつがなく終わるとき' do
1269 it '編集許可チェックを依頼している' do
1270 Story.any_instance.stub(:allow?).with(any_args).and_return(true)
1271 Story.any_instance.should_receive(:allow?).with(any_args).exactly(1)
1272 r = @story.store @author
1274 it '順序入れ替えを依頼している' do
1275 Story.any_instance.stub(:rotate).with(any_args).and_return(0)
1276 Story.any_instance.should_receive(:rotate).with(any_args).exactly(1)
1277 Story.any_instance.stub(:save).with(any_args).and_return(true)
1278 Story.stub(:validate_t).with(any_args).and_return(true)
1279 r = @story.store @author
1282 Story.stub(:new_t).with(any_args).and_return(0)
1283 Story.any_instance.stub(:save).with(any_args).and_return(true)
1284 Story.any_instance.should_receive(:save).with(any_args).exactly(1)
1285 Story.stub(:validate_t).with(any_args).and_return(true)
1286 r = @story.store @author
1288 it 'tのシリアライズチェックを依頼している' do
1289 Story.stub(:new_t).with(any_args).and_return(0)
1290 Story.any_instance.stub(:save).with(any_args).and_return(true)
1291 Story.stub(:validate_t).with(any_args).and_return(true)
1292 Story.should_receive(:validate_t).with(any_args).exactly(1)
1293 r = @story.store @author
1296 #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1297 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したとき' do
1299 @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1300 @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id
1301 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1302 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1303 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
1304 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
1305 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
1306 @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id
1308 it '既存のt0には変化がない' do
1309 @story6.store @author
1311 @story.t.should eq 0
1313 it '既存のt1には変化がない' do
1314 @story6.store @author
1316 @story2.t.should eq 1
1318 it '既存のt2を3にシフトしている' do
1319 @story6.store @author
1321 @story3.t.should eq 3
1323 it '既存のt3を4にシフトしている' do
1324 @story6.store @author
1326 @story4.t.should eq 4
1328 it '既存のt5を5にシフトしている' do
1329 @story6.store @author
1331 @story5.t.should eq 5
1333 it '新規のt2が作成されている' do
1334 @story6.store @author
1336 @story6.t.should eq 2
1338 it '他のコミックに影響がない' do
1340 @story6.store @author
1342 @storyc2.t.should eq @ot
1345 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したとき' do
1347 @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1348 @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id
1349 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1350 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1351 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
1352 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
1353 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
1357 it '既存のt0には変化がない' do
1358 @story4.store @author, @ot
1360 @story.t.should eq 0
1362 it '既存のt4には変化がない' do
1363 @story4.store @author, @ot
1365 @story5.t.should eq 4
1367 it '既存のt1を2にシフトしている' do
1368 @story4.store @author, @ot
1370 @story2.t.should eq 2
1372 it '既存のt2を3にシフトしている' do
1373 @story4.store @author, @ot
1375 @story3.t.should eq 3
1377 it '既存のt3を1にシフトしている' do
1378 @story4.store @author, @ot
1380 @story4.t.should eq 1
1382 it '他のコミックに影響がない' do
1383 @story4.store @author, @ot
1385 @storyc2.t.should eq 0
1388 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で1を3に移動したとき' do
1390 @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1391 @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id
1392 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1393 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1394 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
1395 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
1396 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
1400 it '既存のt0には変化がない' do
1401 @story2.store @author, @ot
1403 @story.t.should eq 0
1405 it '既存のt4には変化がない' do
1406 @story2.store @author, @ot
1408 @story5.t.should eq 4
1410 it '既存のt1を3にシフトしている' do
1411 @story2.store @author, @ot
1413 @story2.t.should eq 3
1415 it '既存のt2を1にシフトしている' do
1416 @story2.store @author, @ot
1418 @story3.t.should eq 1
1420 it '既存のt3を2にシフトしている' do
1421 @story2.store @author, @ot
1423 @story4.t.should eq 2
1425 it '他のコミックに影響がない' do
1426 @story2.store @author, @ot
1428 @storyc2.t.should eq 0
1431 #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1432 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したが保存に失敗したとき' do
1434 Story.any_instance.stub(:save).with(any_args).and_return(false)
1435 @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1436 @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id
1437 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1438 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1439 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
1440 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
1441 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
1442 @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id
1445 @story6.store @author
1447 @story.t.should eq 0
1449 @story2.t.should eq 1
1451 @story3.t.should eq 2
1453 @story4.t.should eq 3
1455 @story5.t.should eq 4
1457 @storyc2.t.should eq 0
1460 r = @story6.store @author
1464 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したがシリアルチェックに失敗したとき' do
1466 Story.stub(:validate_t).with(any_args).and_return(false)
1467 @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1468 @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id
1469 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1470 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1471 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
1472 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
1473 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
1478 @story4.store @author, @ot
1480 @story.t.should eq 0
1482 @story2.t.should eq 1
1484 @story3.t.should eq 2
1486 @story4.t.should eq 3
1488 @story5.t.should eq 4
1490 @storyc2.t.should eq 0
1493 r = @story4.store @author, @ot
1496 it 'tにエラーメッセージが入っている' do
1497 @story4.store @author, @ot
1498 @story4.errors[:t].should_not be_empty
1499 @story4.valid?.should be_true
1502 context '編集不可だったとき' do
1504 @story = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id
1505 Story.any_instance.stub(:allow?).and_return(false)
1507 it '403Forbidden例外を返す' do
1509 @story.store @author
1510 }.should raise_error(ActiveRecord::Forbidden)
1514 describe '切り詰め処理つき削除に於いて' do
1516 @comic = FactoryGirl.create :comic, :author_id => @author.id
1517 @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id
1519 context 'つつがなく終わるとき' do
1521 Story.any_instance.stub(:destroy_with_story_sheet).with(any_args).and_return(true)
1522 Story.any_instance.should_receive(:destroy_with_story_sheet).with(any_args).exactly(1)
1523 r = @story.destroy_and_shorten
1527 @story.destroy_and_shorten
1528 }.should change(Story, :count ).by(-1)
1531 r = @story.destroy_and_shorten
1535 context '削除に失敗したとき' do
1537 Story.any_instance.stub(:destroy_with_story_sheet).and_return(false)
1541 @story.destroy_and_shorten
1542 }.should_not change(Story, :count )
1545 r = @story.destroy_and_shorten
1549 #連携テスト。切り詰めが直接DBをいじる
1550 context '2件で先頭を削除したとき' do
1552 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1556 @story.destroy_and_shorten
1557 }.should change(Story, :count ).by(-1)
1560 @story.destroy_and_shorten
1562 Story.find @story.id
1563 }.should raise_error(ActiveRecord::RecordNotFound)
1566 @story.destroy_and_shorten
1568 @story2.t.should eq 0
1571 context '3件で先頭を削除したとき' do
1573 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1574 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
1578 @story.destroy_and_shorten
1579 }.should change(Story, :count ).by(-1)
1582 @story.destroy_and_shorten
1584 Story.find @story.id
1585 }.should raise_error(ActiveRecord::RecordNotFound)
1588 @story.destroy_and_shorten
1590 @story2.t.should eq 0
1593 @story.destroy_and_shorten
1595 @story3.t.should eq 1
1598 context '5件で3件目を削除したとき' do
1600 @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id
1601 @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id
1602 @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id
1603 @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id
1607 @story3.destroy_and_shorten
1608 }.should change(Story, :count ).by(-1)
1611 @story3.destroy_and_shorten
1613 @story.t.should eq 0
1616 @story3.destroy_and_shorten
1618 @story2.t.should eq 1
1621 @story3.destroy_and_shorten
1623 Story.find @story3.id
1624 }.should raise_error(ActiveRecord::RecordNotFound)
1627 @story3.destroy_and_shorten
1629 @story4.t.should eq 2
1632 @story3.destroy_and_shorten
1634 @story5.t.should eq 3
1637 #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1639 describe '削除に於いて' do
1641 @comic = FactoryGirl.create :comic, :author_id => @author.id
1642 @story = FactoryGirl.create :story, :comic_id => @comic.id
1643 @sheet = FactoryGirl.create :sheet, :author_id => @author.id
1644 @story_sheet = FactoryGirl.create :story_sheet, :author_id => @author.id, :story_id => @story.id, :sheet_id => @sheet.id
1645 @other_comic = FactoryGirl.create :comic, :author_id => @author.id
1646 @other_story = FactoryGirl.create :story, :comic_id => @other_comic.id
1647 @other_sheet = FactoryGirl.create :sheet, :author_id => @author.id
1648 @other_story_sheet = FactoryGirl.create :story_sheet, :author_id => @author.id, :story_id => @other_story.id, :sheet_id => @other_sheet.id
1650 context 'つつがなく終わるとき' do
1653 r = @story.destroy_with_story_sheet
1654 }.should change(Story, :count).by(-1)
1656 r = Story.find @story.id
1657 }.should raise_error
1659 it '自身にリンクしているスト紙をすべて削除する' do
1661 r = @story.destroy_with_story_sheet
1662 }.should change(StorySheet, :count).by(-1)
1664 r = StorySheet.find @story_sheet.id
1665 }.should raise_error
1668 r = @story.destroy_with_story_sheet
1672 context '削除に失敗したとき' do
1674 StorySheet.any_instance.stub(:destroy).with(any_args).and_return(false)
1677 r = @story.destroy_with_story_sheet
1682 r = @story.destroy_with_story_sheet
1683 }.should_not change(Story, :count)
1685 r = @story.destroy_with_story_sheet
1686 }.should_not change(StorySheet, :count)