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 #dbのデフォルト値が0だから明示的にnilにしないと追加ができない
118 @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id
119 @story.supply_default
120 @story.t.should be_nil
125 describe '上書き補充に於いて' do
127 @comic = Factory :comic, :author_id => @author.id
128 @panel = Factory :panel, :author_id => @author.id
131 context 'author_idを補充' do
132 it '問答無用でauthor_idを補充する' do
133 @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id
134 @story.author_id = nil
135 @story.overwrite @author
136 @story.author_id.should eq @author.id
142 describe '作者判定に於いて' do
144 @comic = Factory :comic, :author_id => @author.id
145 @panel = Factory :panel, :author_id => @author.id
146 @story = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
147 @comico = Factory :comic, :author_id => @other_author.id
148 @panelo = Factory :panel, :author_id => @other_author.id
149 @storyo = Factory :story, :author_id => @other_author.id, :comic_id => @comico.id, :panel_id => @panelo.id
151 it '自分のストーリーならyes' do
152 @story.own?(@author).should == true
155 @storyo.own?(@author).should == false
158 @story.own?(nil).should == false
161 describe '単体取得に於いて' do
163 @comic = Factory :comic, :author_id => @author.id
164 @panel = Factory :panel, :author_id => @author.id
165 @story = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
168 l = Story.edit @story.id, @author
171 context '他人のストーリーを開こうとしたとき' do
172 it '403Forbidden例外を返す' do
173 Story.any_instance.stub(:own?).and_return(false)
175 Story.edit @story.id, @author
176 }.should raise_error(ActiveRecord::Forbidden)
179 context '存在しないストーリーを開こうとしたとき' do
180 it '404RecordNotFound例外を返す' do
182 Story.edit 110, @author
183 }.should raise_error(ActiveRecord::RecordNotFound)
188 describe '一覧取得に於いて' do
190 @comic = Factory :comic, :author_id => @author.id
191 @panel = Factory :panel, :author_id => @author.id
192 @story = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
193 @panel2 = Factory :panel, :author_id => @author.id, :publish => 0
195 context 'offset補正について' do
196 it '文字列から数値に変換される' do
197 Story.offset(100, '8').should eq 8
200 Story.offset(100).should eq 0
202 #投稿されたコマ数以上の値が指定されたときは、最後のコマだけになる
204 it '1件のときオフセット1なら0になる' do
205 Story.offset(1, '1').should eq 0
207 it '5件のときオフセット5なら4になる' do
208 Story.offset(5, '5').should eq 4
210 # 負の値が指定されたときは、最後のコマから数えてコマを飛ばして表示する。
211 #-4のときは、最後から4つのコマを表示する。
212 it '2件のときオフセット-1なら1になる' do
213 Story.offset(2, '-1').should eq 1
215 it '5件のときオフセット-2なら3になる' do
216 Story.offset(5, '-2').should eq 3
218 # 最終的なが負になるなど、不正な値が入ったときは0となる。
219 it '2件のときオフセット-5なら0になる' do
220 Story.offset(2, '-5').should eq 0
223 context 'panel_count補正について' do
224 it '文字列から数値に変換される' do
225 Story.panel_count(100, '7').should eq 7
227 it 'nilの場合はStory.default_panel_sizeになる' do
228 Story.panel_count(100).should eq Story.default_panel_size
230 it '0以下の場合はStory.default_panel_sizeになる' do
231 Story.panel_count(100, '0').should eq Story.default_panel_size
233 it 'Story.max_panel_sizeを超えた場合はStory.max_panel_sizeになる' do
234 Story.panel_count(100, '1000').should eq Story.max_panel_size
238 c = Story.list @comic, @author
242 v = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
243 c = Story.list @comic, @author
244 c.should eq [ @story, v]
246 it '非公開のコマは含まない' do
\r
247 h = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel2.id, :t => 1
248 c = Story.list @comic, @author
249 c.should eq [ @story]
251 context 'DBに5件あって1ページの件数を2件に変えたとして' do
253 @story2 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
254 @story3 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 2
255 @story4 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 3
256 @story5 = Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 4
258 it 'offset=0なら末尾2件を返す' do
260 c = Story.list( @comic, @author, 0, 2)
261 c.should eq [@story, @story2]
263 it 'offset=2なら中間2件を返す' do
264 c = Story.list(@comic, @author, 2, 2)
265 c.should eq [@story3, @story4]
267 it 'offset=4なら先頭1件を返す' do
268 c = Story.list(@comic, @author, 4, 2)
269 c.should eq [@story5]
273 describe 'list関連テーブルプションに於いて' do
274 it 'includeキーを含んでいる' do
276 r.has_key?(:include).should be_true
279 r = Story.list_opt[:include]
280 r.should have(3).items
283 r = Story.list_opt[:include]
284 r.has_key?(:comic).should be_true
286 it 'コミックは作家を含んでいる' do
287 r = Story.list_opt[:include]
288 r[:comic].has_key?(:author).should be_true
291 r = Story.list_opt[:include]
292 r.has_key?(:author).should be_true
295 r = Story.list_opt[:include]
296 r.has_key?(:panel).should be_true
299 r = Story.list_opt[:include]
300 r[:panel].has_key?(:author).should be_true
303 r = Story.list_opt[:include]
304 r[:panel].has_key?(:panel_pictures).should be_true
306 it 'コマはフキダシを含んでいる' do
307 r = Story.list_opt[:include]
308 r[:panel].has_key?(:speech_balloons).should be_true
311 describe 'json一覧出力オプションに於いて' do
312 it 'includeキーを含んでいる' do
313 r = Story.list_json_opt
314 r.has_key?(:include).should be_true
317 r = Story.list_json_opt[:include]
318 r.should have(3).items
321 r = Story.list_json_opt[:include]
322 r.has_key?(:comic).should be_true
324 it 'コミックは作家を含んでいる' do
325 r = Story.list_json_opt[:include]
326 r[:comic].has_key?(:author).should be_true
329 r = Story.list_json_opt[:include]
330 r.has_key?(:author).should be_true
333 r = Story.list_json_opt[:include]
334 r.has_key?(:panel).should be_true
337 r = Story.list_json_opt[:include]
338 r[:panel].has_key?(:author).should be_true
341 r = Story.list_json_opt[:include]
342 r[:panel].has_key?(:panel_pictures).should be_true
344 it 'コマはフキダシを含んでいる' do
345 r = Story.list_json_opt[:include]
346 r[:panel].has_key?(:speech_balloons).should be_true
349 describe 't補充値に於いて' do
351 @comic = Factory :comic, :author_id => @author.id
352 @panel = Factory :panel, :author_id => @author.id
355 context 'コミック初のコマなら' do
357 @story = Factory.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
359 r = Story.new_t @story.comic_id
363 context 'コミックに一個コマがあるとき' do
365 Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
366 @story = Factory.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
368 r = Story.new_t @story.comic_id
372 context 'コミックに2個コマがあるとき' do
374 Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
375 Factory :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
376 @story = Factory.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
378 r = Story.new_t @story.comic_id
383 describe 'シリアライズチェックに於いて' do
384 context 'つつがなく終わるとき' do
385 it '0からシリアライズされているならTrueを返す' do
386 r = Story.serial? [0, 1, 2]
389 it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
390 r = Story.serial? [0, 2, 1]
393 it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
394 r = Story.serial? [ 2, 1, 4, 3, 0]
399 it '0から始まらないならFalseを返す' do
400 r = Story.serial? [1, 2, 3]
403 it '連続していないならFalseを返す' do
404 r = Story.serial? [0, 1, 2, 4]
407 it '連続していないならFalseを返す' do
408 r = Story.serial? [0, 1, 2, 4, 5]
413 describe 't収集に於いて' do
415 @comic = Factory :comic, :author_id => @author.id
416 @panel = Factory :panel, :author_id => @author.id
417 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
418 @comic2 = Factory :comic, :author_id => @author.id
419 @c2story = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
421 context 'つつがなく終わるとき' do
422 it 'ストーリーから同一コミックのtだけを収集している' do
423 r = Story.collect_t @story
428 it 'ストーリーから同一コミックのtだけを収集している' do
429 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
430 r = Story.collect_t @story
431 r.sort.should eq [0, 1]
434 context '複数コマでヨソのコミックも混じっているとき' do
435 it 'ストーリーから同一コミックのtだけを収集している' do
436 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
437 r = Story.collect_t @story
438 r.sort.should eq [0, 1]
442 describe 'tチェックに於いて' do
444 @comic = Factory :comic, :author_id => @author.id
445 @panel = Factory :panel, :author_id => @author.id
446 @story = Factory.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
448 context 'つつがなく終わるとき' do
450 Story.should_receive(:collect_t).with(any_args).exactly(1)
451 Story.stub(:collect_t).with(any_args).and_return([])
452 Story.stub(:serial?).with(any_args).and_return(true)
453 r = Story.validate_t @story
455 it '収集したtをシリアライズチェック依頼している' do
456 Story.stub(:collect_t).with(any_args).and_return([])
457 Story.should_receive(:serial?).with(any_args).exactly(1)
458 Story.stub(:serial?).with(any_args).and_return(true)
459 r = Story.validate_t @story
463 #依頼チェックだけでは不安なので最低限のチェックを
465 it '一件だけで正常通過している' do
466 @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
467 r = Story.validate_t @story
472 it '2件目を作っても正常通過している' do
473 @story = Factory :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
474 @story2 = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
475 r = Story.validate_t @story2
480 describe '挿入シフトに於いて' do
482 @comic = Factory :comic, :author_id => @author.id
483 @panel = Factory :panel, :author_id => @author.id
487 it 'Updateを依頼している' do
488 Story.stub(:update_all).with(any_args)
489 Story.should_receive(:update_all).with(any_args).exactly(1)
490 @story = Factory.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
494 context 'テーブルに1件(t:0)で0に挿入したとき' do
496 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
497 @story2 = Factory.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
499 it '既存の行を1にシフトしている' do
502 l.first.t.should eq 1
504 it 'これから挿入するt(0)が欠番になっている' do
506 l = Story.find(:all).map {|s| s.t }
507 l.include?(0).should_not be_true
510 context 'テーブルに2件(t:0,1)で1に挿入したとき' do
512 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
513 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
514 @story3 = Factory.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
516 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
518 l = Story.find(:all).map {|s| s.t }
519 l.sort.should eq [0, 2]
522 context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
524 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
525 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
526 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
527 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
528 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
529 @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
531 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
533 l = Story.find(:all).map {|s| s.t }
534 l.sort.should eq [0, 1, 3, 4, 5]
537 context '先ほどのケース+他のコミック1件で挿入したとき' do
539 @comic2 = Factory :comic, :author_id => @author.id
540 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
541 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
542 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
543 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
544 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
545 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
546 @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
548 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
550 l = Story.find(:all, :conditions => ['comic_id = ?', @comic.id]).map {|s| s.t }
551 l.sort.should eq [0, 1, 3, 4, 5]
557 @storyc2.t.should eq ot
561 describe '少ない方に移動に於いて' do
563 @comic = Factory :comic, :author_id => @author.id
564 @panel = Factory :panel, :author_id => @author.id
567 it 'Updateを依頼している' do
568 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
569 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
570 Story.stub(:update_all).with(any_args)
571 Story.should_receive(:update_all).with(any_args).exactly(1)
574 @story2.lesser_shift ot
577 context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
579 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
580 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
584 it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
586 @story2.lesser_shift @ot
587 l = Story.find(:all).map {|s| s.t }
588 l.sort.should eq [1, 1]
590 it '既存のt0を1にシフトしている' do
591 @story2.lesser_shift @ot
596 context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
598 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
599 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
600 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
604 it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
606 @story3.lesser_shift @ot
607 l = Story.find(:all).map {|s| s.t }
608 l.sort.should eq [0, 2, 2]
610 it '既存のt1を2にシフトしている' do
611 @story3.lesser_shift @ot
613 @story2.t.should eq 2
616 context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
618 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
619 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
620 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
621 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
622 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
626 it 'これから挿入するt(1)が欠番になっている' do
628 @story4.lesser_shift @ot
629 l = Story.find(:all).map {|s| s.t }
630 l.sort.should eq [0, 2, 3, 3, 4]
633 @story4.lesser_shift @ot
638 @story4.lesser_shift @ot
640 @story5.t.should eq 4
642 it '既存のt1を2にシフトしている' do
643 @story4.lesser_shift @ot
645 @story2.t.should eq 2
647 it '既存のt2を3にシフトしている' do
648 @story4.lesser_shift @ot
650 @story3.t.should eq 3
653 context '先ほどのケース+他のコミック1件で挿入したとき' do
655 @comic2 = Factory :comic, :author_id => @author.id
656 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
657 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
658 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
659 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
660 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
661 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
665 it 'これから挿入するt(1)が欠番になっている' do
666 @story4.lesser_shift @ot
667 l = Story.find(:all).map {|s| s.t }
668 l.sort.should eq [0, 0, 2, 3, 3, 4]
671 @story4.lesser_shift @ot
676 @story4.lesser_shift @ot
678 @story5.t.should eq 4
680 it '既存のt1を2にシフトしている' do
681 @story4.lesser_shift @ot
683 @story2.t.should eq 2
685 it '既存のt2を3にシフトしている' do
686 @story4.lesser_shift @ot
688 @story3.t.should eq 3
691 @story4.lesser_shift @ot
693 @storyc2.t.should eq 0
698 context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
700 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
701 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
705 it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
707 @story2.lesser_shift @ot
708 l = Story.find(:all).map {|s| s.t }
709 l.sort.should eq [1, 1]
711 it '既存のt0を1にシフトしている' do
712 @story2.lesser_shift @ot
716 it '既存のt1は0に補正されている' do
717 @story2.lesser_shift @ot
718 @story2.t.should eq 0
722 describe '大きい方に移動に於いて' do
724 @comic = Factory :comic, :author_id => @author.id
725 @panel = Factory :panel, :author_id => @author.id
728 it 'Updateを依頼している' do
729 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
730 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
731 Story.stub(:update_all).with(any_args)
732 Story.should_receive(:update_all).with(any_args).exactly(1)
735 @story.higher_shift ot
738 context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
740 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
741 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
745 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
747 @story.higher_shift @ot
748 l = Story.find(:all).map {|s| s.t }
749 l.sort.should eq [0, 0]
751 it '既存のt1を0にシフトしている' do
752 @story.higher_shift @ot
754 @story2.t.should eq 0
757 context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
759 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
760 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
761 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
765 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
767 @story.higher_shift @ot
768 l = Story.find(:all).map {|s| s.t }
769 l.sort.should eq [0, 0, 2]
771 it '既存のt1を0にシフトしている' do
772 @story.higher_shift @ot
774 @story2.t.should eq 0
777 context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
779 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
780 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
781 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
782 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
783 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
787 it 'これから挿入するt(3)が欠番になっている' do
789 @story2.higher_shift @ot
790 l = Story.find(:all).map {|s| s.t }
791 l.sort.should eq [0, 1, 1, 2, 4]
794 @story2.higher_shift @ot
799 @story2.higher_shift @ot
801 @story5.t.should eq 4
803 it '既存のt2を1にシフトしている' do
804 @story2.higher_shift @ot
806 @story3.t.should eq 1
808 it '既存のt3を2にシフトしている' do
809 @story2.higher_shift @ot
811 @story4.t.should eq 2
814 context '先ほどのケース+他のコミック1件で挿入したとき' do
816 @comic2 = Factory :comic, :author_id => @author.id
817 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
818 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
819 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
820 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
821 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
822 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
826 it 'これから挿入するt(3)が欠番になっている' do
828 @story2.higher_shift @ot
829 l = Story.find(:all).map {|s| s.t }
830 l.sort.should eq [0, 0, 1, 1, 2, 4]
833 @story2.higher_shift @ot
838 @story2.higher_shift @ot
840 @story5.t.should eq 4
842 it '既存のt2を1にシフトしている' do
843 @story2.higher_shift @ot
845 @story3.t.should eq 1
847 it '既存のt3を2にシフトしている' do
848 @story2.higher_shift @ot
850 @story4.t.should eq 2
853 @story2.higher_shift @ot
855 @storyc2.t.should eq 0
860 context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
862 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
863 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
867 it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
869 @story.higher_shift @ot
870 l = Story.find(:all).map {|s| s.t }
871 l.sort.should eq [0, 0]
873 it '既存のt1を0にシフトしている' do
874 @story.higher_shift @ot
876 @story2.t.should eq 0
878 it '既存のt0は1に補正されている' do
879 @story.higher_shift @ot
884 describe '入れ替えに於いて' do
886 @comic = Factory :comic, :author_id => @author.id
887 @panel = Factory :panel, :author_id => @author.id
888 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
889 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
891 context '新tが旧tより小さいとき' do
892 it '少ない方に移動を依頼している' do
893 Story.any_instance.stub(:lesser_shift).with(any_args)
894 Story.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
897 @story2.update_shift ot
900 context '新tが旧tより大きいとき' do
901 it '大きい方に移動を依頼している' do
902 Story.any_instance.stub(:higher_shift).with(any_args)
903 Story.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
906 @story.update_shift ot
910 describe '順序入れ替えに於いて' do
912 @comic = Factory :comic, :author_id => @author.id
913 @panel = Factory :panel, :author_id => @author.id
915 context 'オブジェクトが新規でtが空のとき' do
916 it '末尾追加としてtを補充依頼している' do
917 @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
918 Story.stub(:new_t).with(any_args).and_return(0)
919 Story.should_receive(:new_t).with(any_args).exactly(1)
924 context 'オブジェクトが新規でtが設定されているとき' do
925 it '挿入追加として挿入シフトを依頼している' do
926 @story = Factory.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
927 Story.any_instance.stub(:insert_shift).with(any_args)
928 Story.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
933 context 'オブジェクトが新規でなくtが設定されているとき' do
934 it 'コマ移動として入れ替えを依頼している' do
935 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
936 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
937 Story.any_instance.stub(:update_shift).with(any_args)
938 Story.any_instance.should_receive(:update_shift).with(1).exactly(1)
943 context 'オブジェクトが新規でなくtが空のとき' do
944 it '入れ替えもシフトもせず、tを空のままにしている' do
945 #結果、tに欠番が生じてシリアライズチェックでひっかかる
949 describe '編集許可に於いて' do
951 @comic = Factory :comic, :author_id => @author.id
952 @panel = Factory :panel, :author_id => @author.id
953 @story = Factory.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
955 context 'つつがなく終わるとき' do
961 context 'コミックで引っかかるとき' do
963 @story.comic_id = nil
968 Comic.any_instance.stub(:own?).with(any_args).and_return(false)
973 context 'コマで引っかかるとき' do
975 @story.panel_id = nil
980 Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
988 @comic = Factory :comic, :author_id => @author.id
989 @panel = Factory :panel, :author_id => @author.id
990 @story = Factory.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
992 context 'つつがなく終わるとき' do
993 it '編集許可チェックを依頼している' do
994 Story.any_instance.stub(:allow?).with(any_args).and_return(true)
995 Story.any_instance.should_receive(:allow?).with(any_args).exactly(1)
998 it '順序入れ替えを依頼している' do
999 Story.any_instance.stub(:rotate).with(any_args).and_return(0)
1000 Story.any_instance.should_receive(:rotate).with(any_args).exactly(1)
1001 Story.any_instance.stub(:save).with(any_args).and_return(true)
1002 Story.stub(:validate_t).with(any_args).and_return(true)
1006 Story.stub(:new_t).with(any_args).and_return(0)
1007 Story.any_instance.stub(:save).with(any_args).and_return(true)
1008 Story.any_instance.should_receive(:save).with(any_args).exactly(1)
1009 Story.stub(:validate_t).with(any_args).and_return(true)
1012 it 'tのシリアライズチェックを依頼している' do
1013 Story.stub(:new_t).with(any_args).and_return(0)
1014 Story.any_instance.stub(:save).with(any_args).and_return(true)
1015 Story.stub(:validate_t).with(any_args).and_return(true)
1016 Story.should_receive(:validate_t).with(any_args).exactly(1)
1020 #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1021 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したとき' do
1023 @comic2 = Factory :comic, :author_id => @author.id
1024 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1025 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1026 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1027 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1028 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1029 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1030 @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1032 it '既存のt0には変化がない' do
1035 @story.t.should eq 0
1037 it '既存のt1には変化がない' do
1040 @story2.t.should eq 1
1042 it '既存のt2を3にシフトしている' do
1045 @story3.t.should eq 3
1047 it '既存のt3を4にシフトしている' do
1050 @story4.t.should eq 4
1052 it '既存のt5を5にシフトしている' do
1055 @story5.t.should eq 5
1057 it '新規のt2が作成されている' do
1060 @story6.t.should eq 2
1062 it '他のコミックに影響がない' do
1066 @storyc2.t.should eq @ot
1069 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したとき' do
1071 @comic2 = Factory :comic, :author_id => @author.id
1072 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1073 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1074 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1075 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1076 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1077 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1081 it '既存のt0には変化がない' do
1084 @story.t.should eq 0
1086 it '既存のt4には変化がない' do
1089 @story5.t.should eq 4
1091 it '既存のt1を2にシフトしている' do
1094 @story2.t.should eq 2
1096 it '既存のt2を3にシフトしている' do
1099 @story3.t.should eq 3
1101 it '既存のt3を1にシフトしている' do
1104 @story4.t.should eq 1
1106 it '他のコミックに影響がない' do
1109 @storyc2.t.should eq 0
1112 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で1を3に移動したとき' do
1114 @comic2 = Factory :comic, :author_id => @author.id
1115 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1116 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1117 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1118 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1119 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1120 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1124 it '既存のt0には変化がない' do
1127 @story.t.should eq 0
1129 it '既存のt4には変化がない' do
1132 @story5.t.should eq 4
1134 it '既存のt1を3にシフトしている' do
1137 @story2.t.should eq 3
1139 it '既存のt2を1にシフトしている' do
1142 @story3.t.should eq 1
1144 it '既存のt3を2にシフトしている' do
1147 @story4.t.should eq 2
1149 it '他のコミックに影響がない' do
1152 @storyc2.t.should eq 0
1155 #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1156 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したが保存に失敗したとき' do
1158 Story.any_instance.stub(:save).with(any_args).and_return(false)
1159 @comic2 = Factory :comic, :author_id => @author.id
1160 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1161 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1162 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1163 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1164 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1165 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1166 @story6 = Factory.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1171 @story.t.should eq 0
1173 @story2.t.should eq 1
1175 @story3.t.should eq 2
1177 @story4.t.should eq 3
1179 @story5.t.should eq 4
1181 @storyc2.t.should eq 0
1188 context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したがシリアルチェックに失敗したとき' do
1190 Story.stub(:validate_t).with(any_args).and_return(false)
1191 @comic2 = Factory :comic, :author_id => @author.id
1192 @storyc2 = Factory :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1193 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1194 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1195 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1196 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1197 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1204 @story.t.should eq 0
1206 @story2.t.should eq 1
1208 @story3.t.should eq 2
1210 @story4.t.should eq 3
1212 @story5.t.should eq 4
1214 @storyc2.t.should eq 0
1217 r = @story4.store @ot
1220 it 'tにエラーメッセージが入っている' do
1222 @story4.errors[:t].should_not be_empty
1223 @story4.valid?.should be_true
1226 context '編集不可だったとき' do
1228 @story = Factory.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1229 Story.any_instance.stub(:allow?).and_return(false)
1231 it '403Forbidden例外を返す' do
1234 }.should raise_error(ActiveRecord::Forbidden)
1238 describe '切り詰め処理つき削除に於いて' do
1240 @comic = Factory :comic, :author_id => @author.id
1241 @panel = Factory :panel, :author_id => @author.id
1242 @story = Factory :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1244 context 'つつがなく終わるとき' do
1247 @story.destroy_and_shorten
1248 }.should change(Story, :count ).by(-1)
1251 #連携テスト。切り詰めが直接DBをいじる
1252 context '2件で先頭を削除したとき' do
1254 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1258 @story.destroy_and_shorten
1259 }.should change(Story, :count ).by(-1)
1262 @story.destroy_and_shorten
1264 Story.find @story.id
1265 }.should raise_error(ActiveRecord::RecordNotFound)
1268 @story.destroy_and_shorten
1270 @story2.t.should eq 0
1273 context '3件で先頭を削除したとき' do
1275 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1276 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1280 @story.destroy_and_shorten
1281 }.should change(Story, :count ).by(-1)
1284 @story.destroy_and_shorten
1286 Story.find @story.id
1287 }.should raise_error(ActiveRecord::RecordNotFound)
1290 @story.destroy_and_shorten
1292 @story2.t.should eq 0
1295 @story.destroy_and_shorten
1297 @story3.t.should eq 1
1300 context '5件で3件目を削除したとき' do
1302 @story2 = Factory :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1303 @story3 = Factory :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1304 @story4 = Factory :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1305 @story5 = Factory :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1309 @story3.destroy_and_shorten
1310 }.should change(Story, :count ).by(-1)
1313 @story3.destroy_and_shorten
1315 @story.t.should eq 0
1318 @story3.destroy_and_shorten
1320 @story2.t.should eq 1
1323 @story3.destroy_and_shorten
1325 Story.find @story3.id
1326 }.should raise_error(ActiveRecord::RecordNotFound)
1329 @story3.destroy_and_shorten
1331 @story4.t.should eq 2
1334 @story3.destroy_and_shorten
1336 @story5.t.should eq 3
1339 #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する