OSDN Git Service

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