OSDN Git Service

Merge branch 'v05' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05client
[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     @admin = 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     @color = FactoryGirl.create :color
11     @user = FactoryGirl.create( :user_yas)
12     @author = FactoryGirl.create :author, :user_id => @user.id
13     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
14     @other_user = FactoryGirl.create( :user_yas)
15     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
16     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
17     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
18     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
19     @sbt = FactoryGirl.create :speech_balloon_template
20   end
21   
22   describe '検証に於いて' do
23     before do
24       @comic = FactoryGirl.create :comic, :author_id => @author.id
25       @panel = FactoryGirl.create :panel, :author_id => @author.id
26       @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
27     end
28     
29     context 'オーソドックスなデータのとき' do
30       it '下限データが通る' do
31         @story.t = 0
32         @story.should be_valid
33       end
34       it '上限データが通る' do
35         @story.t = 99999
36         @story.should be_valid
37       end
38     end
39     
40     context 'comic_idを検証するとき' do
41       it 'nullなら失敗する' do
42         @story.comic_id = nil
43         @story.should_not be_valid
44       end
45       it '数値でなければ失敗する' do
46         @story.comic_id = 'a'
47         @story.should_not be_valid
48       end
49       it '存在するコミックでなければ失敗する' do
50         @story.comic_id = 0
51         @story.should_not be_valid
52       end
53     end
54     
55     context 'panel_idを検証するとき' do
56       it 'nullなら失敗する' do
57         @story.panel_id = nil
58         @story.should_not be_valid
59       end
60       it '数値でなければ失敗する' do
61         @story.panel_id = 'a'
62         @story.should_not be_valid
63       end
64       it '存在するコマでなければ失敗する' do
65         @story.panel_id = 0
66         @story.should_not be_valid
67       end
68     end
69     
70     context 'tを検証するとき' do
71       it 'nullなら失敗する' do
72         @story.t = nil
73         @story.should_not be_valid
74       end
75       it '数値でなければ失敗する' do
76         @story.t = 'a'
77         @story.should_not be_valid
78       end
79       it '負なら失敗する' do
80         @story.t = -1
81         @story.should_not be_valid
82       end
83     end
84     
85     context 'author_idを検証するとき' do
86       it 'nullなら失敗する' do
87         @story.author_id = nil
88         @story.should_not be_valid
89       end
90       it '数値でなければ失敗する' do
91         @story.author_id = 'a'
92         @story.should_not be_valid
93       end
94       it '存在する作家でなければ失敗する' do
95         @story.author_id = 0
96         @story.should_not be_valid
97       end
98     end
99     context '全体を検証するとき' do
100     end
101   end
102   
103   describe 'デフォルト値補充に於いて' do
104     before do
105       @comic = FactoryGirl.create :comic, :author_id => @author.id
106       @panel = FactoryGirl.create :panel, :author_id => @author.id
107     end
108     
109     #dbのデフォルト値が0だから明示的にnilにしないと追加ができない
110     it 'tをnilにする' do
111       @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id
112       @story.supply_default
113       @story.t.should be_nil
114     end
115     
116   end
117   
118   describe '上書き補充に於いて' do
119     before do
120       @comic = FactoryGirl.create :comic, :author_id => @author.id
121       @panel = FactoryGirl.create :panel, :author_id => @author.id
122     end
123     
124     context 'author_idを補充' do
125       it '問答無用でauthor_idを補充する' do
126         @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id
127         @story.author_id = nil
128         @story.overwrite @author
129         @story.author_id.should eq @author.id
130       end
131     end
132     
133   end
134   
135   describe '所持判定に於いて' do
136     before do
137       @comic = FactoryGirl.create :comic, :author_id => @author.id
138       @panel = FactoryGirl.create :panel, :author_id => @author.id
139       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
140       @comico = FactoryGirl.create :comic, :author_id => @other_author.id
141       @panelo = FactoryGirl.create :panel, :author_id => @other_author.id
142       @storyo = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @comico.id, :panel_id => @panelo.id
143     end
144     context '事前チェックする' do
145       it '自身にロールリストからの作家取得を依頼している' do
146         Story.should_receive(:get_author_from_roles).with(any_args).exactly(1)
147         r = @story.own?([@author])
148       end
149     end
150     context 'ロール内作家が取得できるとき' do
151       before do
152       end
153       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
154         Story.stub(:get_author_from_roles).with(any_args).and_return(@author)
155         r = @story.own?([@author])
156         r.should be_true
157       end
158       it 'ロール内作家のidが自身の作家idと一致しないならno' do
159         Story.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
160         @story.own?(@other_author).should be_false
161       end
162     end
163     context 'ロール内作家が取得できないとき' do
164       before do
165         Story.stub(:get_author_from_roles).with(any_args).and_return(nil)
166       end
167       it 'Falseを返す' do
168         r = @story.own?([@author])
169         r.should be_false
170       end
171     end
172   end
173   
174   describe '閲覧許可に於いて' do
175     before do
176       @comic = FactoryGirl.create :comic, :author_id => @author.id
177       @panel = FactoryGirl.create :panel, :author_id => @author.id
178       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
179     end
180     context 'オープンモードのとき' do
181       before do
182         MagicNumber['run_mode'] = 0
183       end
184       it '自身にゲスト用ロールチェックを問い合わせしている' do
185         Story.any_instance.stub(:guest_role_check).and_return(true)
186         Story.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
187         r = @story.visible?([@author])
188       end
189       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
190         Story.any_instance.stub(:guest_role_check).and_return(false)
191         r = @story.visible?([@author])
192         r.should be_false
193       end
194     end
195     context 'クローズドモードのとき' do
196       before do
197         MagicNumber['run_mode'] = 1
198       end
199       it '自身に読者用ロールチェックを問い合わせしている' do
200         Story.any_instance.stub(:reader_role_check).and_return(true)
201         Story.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
202         r = @story.visible?([@author])
203       end
204       it '読者用ロールチェックが失敗したとき、falseを返す' do
205         Story.any_instance.stub(:reader_role_check).and_return(false)
206         r = @story.visible?([@author])
207         r.should be_false
208       end
209     end
210     context '事前チェックする' do
211       before do
212         MagicNumber['run_mode'] = 1
213         Story.any_instance.stub(:reader_role_check).and_return(true)
214       end
215       it '自身のコミックに所持判定を問い合わせしている' do
216         Comic.any_instance.stub(:own?).and_return(true)
217         Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)
218         r = @story.visible?([@author])
219       end
220       it '自身のコミックに閲覧許可を問い合わせしている' do
221         Comic.any_instance.stub(:own?).and_return(false)
222         Comic.any_instance.stub(:visible?).and_return(true)
223         Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)
224         r = @story.visible?([@author])
225       end
226     end
227     context 'つつがなく終わるとき' do
228       before do
229         MagicNumber['run_mode'] = 1
230         Story.any_instance.stub(:reader_role_check).and_return(true)
231       end
232       it '自分のコミックのストーリーなら許可する' do
233         Comic.any_instance.stub(:own?).and_return(true)
234         Comic.any_instance.stub(:visible).and_return(0)
235         r = @story.visible?([@author])
236         r.should be_true
237       end
238       it '他人の非公開コミックのストーリーなら許可しない' do
239         Comic.any_instance.stub(:own?).and_return(false)
240         Comic.any_instance.stub(:visible).and_return(0)
241         r = @story.visible?([@author])
242         r.should be_false
243       end
244       it '他人のコミックのストーリーでも公開なら許可する' do
245         Comic.any_instance.stub(:own?).and_return(false)
246         Comic.any_instance.stub(:visible).and_return(1)
247         r = @story.visible?([@author])
248         r.should be_true
249       end
250     end
251   end
252   
253   describe 'プレイリスト取得に於いて' do
254     before do
255       @comic = FactoryGirl.create :comic, :author_id => @author.id
256       @panel = FactoryGirl.create :panel, :author_id => @author.id
257       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
258       @panel2 = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
259       @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
260       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
261     end
262     context 'offset補正について' do
263       it '文字列から数値に変換される' do
264         Story.offset(100, '8').should eq 8
265       end
266       it 'nilの場合は0になる' do
267         Story.offset(100).should eq 0
268       end
269       #投稿されたコマ数以上の値が指定されたときは、最後のコマだけになる
270       #最後のコマとは、コマ数‐1.
271       it '1件のときオフセット1なら0になる' do
272         Story.offset(1, '1').should eq 0
273       end
274       it '5件のときオフセット5なら4になる' do
275         Story.offset(5, '5').should eq 4
276       end
277       # 負の値が指定されたときは、最後のコマから数えてコマを飛ばして表示する。
278       #-4のときは、最後から4つのコマを表示する。 
279       it '2件のときオフセット-1なら1になる' do
280         Story.offset(2, '-1').should eq 1
281       end
282       it '5件のときオフセット-2なら3になる' do
283         Story.offset(5, '-2').should eq 3
284       end
285       # 最終的なが負になるなど、不正な値が入ったときは0となる。 
286       it '2件のときオフセット-5なら0になる' do
287         Story.offset(2, '-5').should eq 0
288       end
289     end
290     context 'panel_count補正について' do
291       it '文字列から数値に変換される' do
292         Story.panel_count(100, '7').should eq 7
293       end
294       it 'nilの場合はStory.default_panel_sizeになる' do
295         Story.panel_count(100).should eq Story.default_panel_size
296       end
297       it '0以下の場合はStory.default_panel_sizeになる' do
298         Story.panel_count(100, '0').should eq Story.default_panel_size
299       end
300       it 'Story.max_panel_sizeを超えた場合はStory.max_panel_sizeになる' do
301         Story.panel_count(100, '1000').should eq Story.max_panel_size
302       end
303     end
304     it 'リストを返す' do
305       c = Story.play_list @comic, @author
306       c.should eq [@story]
307     end
308     it 't順で並んでいる' do
309       #公開コミックの公開コマは(他人のコミックであっても)含んでいる
310       v = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
311       c = Story.play_list @comic, @author
312       c.should eq [ @story, v]
313     end
314     it '非公開のコマは含んでいる' do
315       h = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel2.id, :t => 1
316       c = Story.play_list @comic, @author
317       c.should eq [ @story, h]
318     end
319     context 'DBに5件あって1ページの件数を2件に変えたとして' do
320       before do
321         @story2 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
322         @story3 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 2
323         @story4 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 3
324         @story5 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 4
325       end
326       it 'offset=0なら末尾2件を返す' do
327         #時系列で並んでいる
328         c = Story.play_list( @comic, @author, 0, 2)
329         c.should eq [@story, @story2]
330       end
331       it 'offset=2なら中間2件を返す' do
332         c = Story.play_list(@comic, @author, 2, 2)
333         c.should eq [@story3, @story4]
334       end
335       it 'offset=4なら先頭1件を返す' do
336         c = Story.play_list(@comic, @author, 4, 2)
337         c.should eq [@story5]
338       end
339     end
340   end
341   describe 'list関連テーブルプションに於いて' do
342     it 'includeキーを含んでいる' do
343       r = Story.list_opt
344       r.has_key?(:include).should be_true
345     end
346     it '3つの項目を含んでいる' do
347       r = Story.list_opt[:include]
348       r.should have(3).items
349     end
350     it 'コミックを含んでいる' do
351       r = Story.list_opt[:include]
352       r.has_key?(:comic).should be_true
353     end
354       it 'コミックは作家を含んでいる' do
355         r = Story.list_opt[:include]
356         r[:comic].has_key?(:author).should be_true
357       end
358     it '作家を含んでいる' do
359       r = Story.list_opt[:include]
360       r.has_key?(:author).should be_true
361     end
362     it 'コマを含んでいる' do
363       r = Story.list_opt[:include]
364       r.has_key?(:panel).should be_true
365     end
366       it 'コマは作家を含んでいる' do
367         r = Story.list_opt[:include]
368         r[:panel].has_key?(:author).should be_true
369       end
370       it 'コマはコマ絵を含んでいる' do
371         r = Story.list_opt[:include]
372         r[:panel].has_key?(:panel_pictures).should be_true
373       end
374         it 'コマ絵は実素材を含んでいる' do
375           r = Story.list_opt[:include]
376           r[:panel][:panel_pictures].has_key?(:picture).should be_true
377         end
378           it '実素材は絵師を含んでいる' do
379             r = Story.list_opt[:include]
380             r[:panel][:panel_pictures][:picture].has_key?(:artist).should be_true
381           end
382           it '実素材はライセンスを含んでいる' do
383             r = Story.list_opt[:include]
384             r[:panel][:panel_pictures][:picture].has_key?(:license).should be_true
385           end
386       it 'コマはフキダシを含んでいる' do
387         r = Story.list_opt[:include]
388         r[:panel].has_key?(:speech_balloons).should be_true
389       end
390         it 'フキダシはフキダシ枠を含んでいる' do
391           r = Story.list_opt[:include]
392           r[:panel][:speech_balloons].has_key?(:balloons).should be_true
393         end
394         it 'フキダシはセリフを含んでいる' do
395           r = Story.list_opt[:include]
396           r[:panel][:speech_balloons].has_key?(:speeches).should be_true
397         end
398   end
399   describe 'json一覧出力オプションに於いて' do
400   end
401   
402   describe '一覧取得に於いて' do
403     before do
404       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
405       @panel = FactoryGirl.create :panel, :author_id => @author.id
406       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
407       @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
408       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
409       @hidden_comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
410     end
411     context 'page補正について' do
412       it '文字列から数値に変換される' do
413         Story.page('8').should eq 8
414       end
415       it 'nilの場合は1になる' do
416         Story.page().should eq 1
417       end
418       it '0以下の場合は1になる' do
419         Story.page('0').should eq 1
420       end
421     end
422     context 'page_size補正について' do
423       it '文字列から数値に変換される' do
424         Story.page_size('7').should eq 7
425       end
426       it 'nilの場合はStory.default_page_sizeになる' do
427         Story.page_size().should eq Story.default_page_size
428       end
429       it '0以下の場合はStory.default_page_sizeになる' do
430         Story.page_size('0').should eq Story.default_page_size
431       end
432       it 'Story.max_page_sizeを超えた場合はStory.max_page_sizeになる' do
433         Story.page_size('1000').should eq Story.max_page_size
434       end
435     end
436     it 'リストを返す' do
437       c = Story.list
438       c.should eq [@story]
439     end
440     it '時系列で並んでいる' do
441       #公開コミックのStoryは(他人のStoryであっても)含んでいる
442       v = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id, :t => 0, :updated_at => Time.now + 100
443       c = Story.list 
444       c.should eq [ v, @story]
445     end
446     it '非公開のストーリーは(自分のストーリーであっても)含まない' do
447       h = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @hidden_comic.id, :panel_id => @panel.id, :t => 0
448       c = Story.list 
449       c.should eq [ @story]
450     end
451     context 'DBに5件あって1ページの件数を2件に変えたとして' do
452       before do
453         @story2 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
454         @story3 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 2, :updated_at => Time.now + 200
455         @story4 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 3, :updated_at => Time.now + 300
456         @story5 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 4, :updated_at => Time.now + 400
457       end
458       it '通常は2件を返す' do
459         l = Story.list 1, 2
460         l.should have(2).items 
461       end
462       it 'page=1なら末尾2件を返す' do
463         #時系列で並んでいる
464         l = Story.list 1, 2
465         l.should eq [@story5, @story4]
466       end
467       it 'page=2なら中間2件を返す' do
468         l = Story.list 2, 2
469         l.should eq [@story3, @story2]
470       end
471       it 'page=3なら先頭1件を返す' do
472         l = Story.list 3, 2
473         l.should eq [@story]
474       end
475     end
476     context 'DBに5件あって1ページの件数を2件に変えたとして' do
477       before do
478         @story2 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
479         @story3 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 2, :updated_at => Time.now + 200
480         @story4 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 3, :updated_at => Time.now + 300
481         @story5 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 4, :updated_at => Time.now + 400
482         Story.stub(:default_page_size).and_return(2)
483       end
484       it '件数0は全件(5件)を返す' do
485         r = Story.list 5, 0
486         r.should have(5).items 
487       end
488     end
489   end
490   describe '自分のストーリー一覧取得に於いて' do
491     before do
492       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
493       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
494       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
495       @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
496       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
497       @hcomic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
498       @hpanel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
499     end
500     context 'つつがなく終わるとき' do
501       it '一覧取得オプションを利用している' do
502         Story.stub(:list_opt).with(any_args).and_return({})
503         Story.should_receive(:list_opt).with(any_args).exactly(1)
504         r = Story.mylist @author
505       end
506     end
507     it 'リストを返す' do
508       s = Story.mylist @author
509       s.should eq [@story]
510     end
511     it '時系列で並んでいる' do
512       ns = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
513       sl = Story.mylist @author
514       sl.should eq [ns, @story]
515     end
516     it '他人のストーリーはコマコミックともに公開でも含まない' do
517       so = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id
518       sl = Story.mylist @author
519       sl.should eq [@story]
520     end
521     it '自分のストーリーはコマコミックともに非公開でも含んでいる' do
522       hs = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @hcomic.id, :panel_id => @hpanel.id, :updated_at => Time.now + 100
523       sl = Story.mylist @author
524       sl.should eq [hs, @story]
525     end
526     context 'DBに5件あって1ページの件数を2件に変えたとして' do
527       before do
528         @story2 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
529         @story3 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 2, :updated_at => Time.now + 200
530         @story4 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 3, :updated_at => Time.now + 300
531         @story5 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 4, :updated_at => Time.now + 400
532         Story.stub(:default_page_size).and_return(2)
533       end
534       it '通常は2件を返す' do
535         l = Story.mylist @author, 1, 2
536         l.should have(2).items 
537       end
538       it 'page=1なら末尾2件を返す' do
539         #時系列で並んでいる
540         l = Story.mylist @author, 1, 2
541         l.should eq [@story5, @story4]
542       end
543       it 'page=2なら中間2件を返す' do
544         l = Story.mylist @author, 2, 2
545         l.should eq [@story3, @story2]
546       end
547       it 'page=3なら先頭1件を返す' do
548         l = Story.mylist @author, 3, 2
549         l.should eq [@story]
550       end
551     end
552     context 'DBに5件あって1ページの件数を2件に変えたとして' do
553       before do
554         @story2 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
555         @story3 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 2, :updated_at => Time.now + 200
556         @story4 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 3, :updated_at => Time.now + 300
557         @story5 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 4, :updated_at => Time.now + 400
558         Author.stub(:default_story_page_size).and_return(2)
559       end
560       it '件数0は全件(5件)を返す' do
561         r = Story.mylist @author, 5, 0
562         r.should have(5).items 
563       end
564     end
565   end
566   
567   describe '単体取得に於いて' do
568     before do
569       @comic = FactoryGirl.create :comic, :author_id => @author.id
570       @panel = FactoryGirl.create :panel, :author_id => @author.id
571       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
572     end
573     context 'つつがなく終わるとき' do
574       it '単体取得オプションを利用している' do
575         Story.stub(:show_opt).with(any_args).and_return({})
576         Story.should_receive(:show_opt).with(any_args).exactly(1)
577         r = Story.show @story.id, @author
578       end
579       it '閲覧許可を問い合わせている' do
580         Story.any_instance.stub(:visible?).with(@author).and_return(true)
581         Story.any_instance.should_receive(:visible?).with(@author).exactly(1)
582         r = Story.show @story.id, @author
583       end
584     end
585     it '指定のストーリーを返す' do
586       l = Story.show @story.id, @author
587       l.should eq @story
588     end
589     context '他人のストーリーを開こうとしたとき' do
590       it '403Forbidden例外を返す' do
591         Story.any_instance.stub(:visible?).with(@other_author).and_return(false)
592         lambda{
593           Story.show @story.id, @other_author
594         }.should raise_error(ActiveRecord::Forbidden)
595       end
596     end
597     context '存在しないストーリーを開こうとしたとき' do
598       it '404RecordNotFound例外を返す' do
599         lambda{
600           Story.show 110, @author
601         }.should raise_error(ActiveRecord::RecordNotFound)
602       end
603     end
604   end
605   
606   describe '編集取得に於いて' do
607     before do
608       @comic = FactoryGirl.create :comic, :author_id => @author.id
609       @panel = FactoryGirl.create :panel, :author_id => @author.id
610       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
611     end
612     context 'つつがなく終わるとき' do
613       it '単体取得オプションを利用している' do
614         Story.stub(:show_opt).with(any_args).and_return({})
615         Story.should_receive(:show_opt).with(any_args).exactly(1)
616         r = Story.edit @story.id, @author
617       end
618       it '所持判定を問い合わせている' do
619         Story.any_instance.stub(:own?).with(any_args).and_return(true)
620         Story.any_instance.should_receive(:own?).with(any_args).exactly(1)
621         r = Story.edit @story.id, @author
622       end
623     end
624     it '指定のストーリーを返す' do
625       l = Story.edit @story.id, @author
626       l.should eq @story
627     end
628     context '他人のストーリーを開こうとしたとき' do
629       it '403Forbidden例外を返す' do
630         Story.any_instance.stub(:own?).and_return(false)
631         lambda{
632           Story.edit @story.id, @author
633         }.should raise_error(ActiveRecord::Forbidden)
634       end
635     end
636     context '存在しないストーリーを開こうとしたとき' do
637       it '404RecordNotFound例外を返す' do
638         lambda{
639           Story.edit 110, @author
640         }.should raise_error(ActiveRecord::RecordNotFound)
641       end
642     end
643   end
644   
645   describe '単体取得オプションに於いて' do
646     it 'includeキーを含んでいる' do
647       r = Story.show_opt
648       r.has_key?(:include).should be_true
649     end
650     it '3つの項目を含んでいる' do
651       r = Story.show_opt[:include]
652       r.should have(3).items
653     end
654     it 'コミックを含んでいる' do
655       r = Story.show_opt[:include]
656       r.has_key?(:comic).should be_true
657     end
658       it 'コミックは作家を含んでいる' do
659         r = Story.show_opt[:include]
660         r[:comic].has_key?(:author).should be_true
661       end
662     it '作家を含んでいる' do
663       r = Story.show_opt[:include]
664       r.has_key?(:author).should be_true
665     end
666     it 'コマを含んでいる' do
667       r = Story.show_opt[:include]
668       r.has_key?(:panel).should be_true
669     end
670       it 'コマは作家を含んでいる' do
671         r = Story.show_opt[:include]
672         r[:panel].has_key?(:author).should be_true
673       end
674       it 'コマはコマ絵を含んでいる' do
675         r = Story.show_opt[:include]
676         r[:panel].has_key?(:panel_pictures).should be_true
677       end
678         it 'コマ絵は実素材を含んでいる' do
679           r = Story.show_opt[:include]
680           r[:panel][:panel_pictures].has_key?(:picture).should be_true
681         end
682           it '実素材は絵師を含んでいる' do
683             r = Story.show_opt[:include]
684             r[:panel][:panel_pictures][:picture].has_key?(:artist).should be_true
685           end
686           it '実素材はライセンスを含んでいる' do
687             r = Story.show_opt[:include]
688             r[:panel][:panel_pictures][:picture].has_key?(:license).should be_true
689           end
690       it 'コマはフキダシを含んでいる' do
691         r = Story.show_opt[:include]
692         r[:panel].has_key?(:speech_balloons).should be_true
693       end
694         it 'フキダシはフキダシ枠を含んでいる' do
695           r = Story.show_opt[:include]
696           r[:panel][:speech_balloons].has_key?(:balloons).should be_true
697         end
698         it 'フキダシはセリフを含んでいる' do
699           r = Story.show_opt[:include]
700           r[:panel][:speech_balloons].has_key?(:speeches).should be_true
701         end
702   end
703   describe 'json単体取得オプションに於いて' do
704   end
705   
706   describe 'ストーリーのjson出力に於いて' do
707     before do
708       #コマを作成しておく。
709       @panel = FactoryGirl.create :panel, :author_id => @author.id
710       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height
711       @sb = @panel.speech_balloons.create(
712         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)
713       )
714       @gc = @panel.ground_colors.create(
715         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :color_id => @color.id)
716       )
717       @gp = @panel.ground_pictures.create(
718         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)
719       )
720       @pc = @panel.panel_colors.create(
721         FactoryGirl.attributes_for(:panel_color, :panel_id => @panel.id, :code => 0xff0000)
722       )
723       @panel.reload
724       @comic = FactoryGirl.create :comic, :author_id => @author.id
725       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
726     end
727     context '事前チェックする' do
728       before do
729         Panel.any_instance.stub(:elements).and_return('{}')
730       end
731       it 'コマ要素のjson出力を依頼している' do
732         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
733         Panel.any_instance.should_receive(:elements).with(any_args).exactly(1)
734         r = @story.story_as_json @author
735       end
736     end
737     it 'json textを返している' do
738       r = JSON.parse @story.story_as_json(@author)
739       r.is_a?(Hash).should be_true
740     end
741     it 'comic,author,panel,コマ要素を含んでいる' do
742       r = JSON.parse @story.story_as_json(@author)
743       r.has_key?('comic').should be_true
744       r['comic'].has_key?('author').should be_true
745       r.has_key?('author').should be_true
746       r.has_key?('panel').should be_true
747       r['panel'].has_key?('author').should be_true
748     end
749     context 'コマ閲覧許可のとき' do
750       before do
751         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
752       end
753       it 'コマ要素にコマ要素を追加している' do
754         r = JSON.parse @story.story_as_json(@author)
755         r['panel'].has_key?('elements').should be_true
756         r['panel']['elements'].should_not be_empty
757       end
758     end
759     context 'コマ閲覧不許可のとき' do
760       before do
761         Panel.any_instance.stub(:visible?).with(any_args).and_return(false)
762       end
763       it 'コマ要素にデータを含ませない' do
764         r = JSON.parse @story.story_as_json(@author)
765         r['panel'].has_key?('elements').should be_false
766       end
767     end
768   end
769   
770   describe 'ストーリーリストのjson出力に於いて' do
771     before do
772       @panel = FactoryGirl.create :panel, :author_id => @author.id
773       @comic = FactoryGirl.create :comic, :author_id => @author.id
774       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
775       Story.any_instance.stub(:story_as_json).with(@author).and_return('{"s": 5}')
776     end
777     context 'つつがなく終わるとき' do
778       it 'ストーリーのjson出力を依頼している' do
779         Story.any_instance.should_receive(:story_as_json).with(@author).exactly(1)
780         r = Story.list_as_json_text [@story], @author
781       end
782     end
783     it 'json textを返している' do
784       r = Story.list_as_json_text [@story], @author
785       j = JSON.parse r
786       j.is_a?(Array).should be_true
787     end
788     it 'ストーリーを含んでいる' do
789       r = Story.list_as_json_text [@story], @author
790       j = JSON.parse r
791       j.first.has_key?('s').should be_true
792     end
793   end
794   
795   describe 'ライセンス素材に於いて' do
796     before do
797       #コマを作成しておく。
798       @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
799       @p2 = FactoryGirl.create :picture, :original_picture_id => @op2.id, :license_id => @license.id, :artist_id => @artist.id
800       @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p2.id
801       @panel = FactoryGirl.create :panel, :author_id => @author.id
802       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 0, :width => @p.width, :height => @p.height, :picture_id => @p.id
803       @panel.reload
804       @panel2 = FactoryGirl.create :panel, :author_id => @author.id
805       @pp2= FactoryGirl.create :panel_picture, :panel_id => @panel2.id, :t => 0, :width => @p2.width, :height => @p2.height, :picture_id => @p2.id
806       @panel2.reload
807       
808       @comic = FactoryGirl.create :comic, :author_id => @author.id
809       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
810       @story2 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel2.id
811     end
812     context '事前チェック' do
813     end
814     context 'つつがなく終わるとき' do
815     end
816     it '連想配列(値は実素材、キーは実素材id)を返している' do
817       r = Story.licensed_pictures [@story, @story2]
818       r.is_a?(Hash).should be_true
819       r.should have(2).items
820       r[@pp.picture_id].should eq @p
821       r[@pp2.picture_id].should eq @p2
822     end
823     context 'コマが削除されているときき' do
824       before do
825         @panel2.destroy
826       end
827       it '削除されているコマは無視する' do
828         r = Story.licensed_pictures [@story, @story2]
829         r.is_a?(Hash).should be_true
830         r.should have(1).items
831         r[@pp.picture_id].should eq @p
832         r[@pp2.picture_id].should be_nil
833       end
834     end
835   end
836   
837   describe 't補充値に於いて' do
838     before do
839       @comic = FactoryGirl.create :comic, :author_id => @author.id
840       @panel = FactoryGirl.create :panel, :author_id => @author.id
841     end
842     
843     context 'コミック初のコマなら' do
844       it '0を補充値とする' do
845         @story = FactoryGirl.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
846         @story.t = nil
847         r = Story.new_t @story.comic_id
848         r.should eq 0
849       end
850     end
851     context 'コミックに一個コマがあるとき' do
852       it '1を補充値とする' do
853         FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
854         @story = FactoryGirl.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
855         @story.t = nil
856         r = Story.new_t @story.comic_id
857         r.should eq 1
858       end
859     end
860     context 'コミックに2個コマがあるとき' do
861       it '2を補充値とする' do
862         FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
863         FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
864         @story = FactoryGirl.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
865         @story.t = nil
866         r = Story.new_t @story.comic_id
867         r.should eq 2
868       end
869     end
870   end
871   describe 'シリアライズチェックに於いて' do
872     context 'つつがなく終わるとき' do
873       it '0からシリアライズされているならTrueを返す' do
874         r = Story.serial? [0, 1, 2]
875         r.should be_true
876       end
877       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
878         r = Story.serial? [0, 2, 1]
879         r.should be_true
880       end
881       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
882         r = Story.serial? [ 2, 1, 4, 3, 0]
883         r.should be_true
884       end
885     end
886     context '異常なとき' do
887       it '0から始まらないならFalseを返す' do
888         r = Story.serial? [1, 2, 3]
889         r.should be_false
890       end
891       it '連続していないならFalseを返す' do
892         r = Story.serial? [0, 1, 2, 4]
893         r.should be_false
894       end
895       it '連続していないならFalseを返す' do
896         r = Story.serial? [0, 1, 2, 4, 5]
897         r.should be_false
898       end
899     end
900   end
901   describe 't収集に於いて' do
902     before do
903       @comic = FactoryGirl.create :comic, :author_id => @author.id
904       @panel = FactoryGirl.create :panel, :author_id => @author.id
905       @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
906       @comic2 = FactoryGirl.create :comic, :author_id => @author.id
907       @c2story = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
908     end
909     context 'つつがなく終わるとき' do
910       it 'ストーリーから同一コミックのtだけを収集している' do
911         r = Story.collect_t @story
912         r.should eq [0]
913       end
914     end
915     context '複数コマのとき' do
916       it 'ストーリーから同一コミックのtだけを収集している' do
917         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
918         r = Story.collect_t @story
919         r.sort.should eq [0, 1]
920       end
921     end
922     context '複数コマでヨソのコミックも混じっているとき' do
923       it 'ストーリーから同一コミックのtだけを収集している' do
924         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
925         r = Story.collect_t @story
926         r.sort.should eq [0, 1]
927       end
928     end
929   end
930   describe 'tチェックに於いて' do
931     before do
932       @comic = FactoryGirl.create :comic, :author_id => @author.id
933       @panel = FactoryGirl.create :panel, :author_id => @author.id
934       @story = FactoryGirl.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
935     end
936     context 'つつがなく終わるとき' do
937       it 't収集を依頼している' do
938         Story.should_receive(:collect_t).with(any_args).exactly(1)
939         Story.stub(:collect_t).with(any_args).and_return([])
940         Story.stub(:serial?).with(any_args).and_return(true)
941         r = Story.validate_t @story
942       end
943       it '収集したtをシリアライズチェック依頼している' do
944         Story.stub(:collect_t).with(any_args).and_return([])
945         Story.should_receive(:serial?).with(any_args).exactly(1)
946         Story.stub(:serial?).with(any_args).and_return(true)
947         r = Story.validate_t @story
948       end
949     end
950     #実データでチェック
951     #依頼チェックだけでは不安なので最低限のチェックを
952     context '新規のとき' do
953       it '一件だけで正常通過している' do
954         @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
955         r = Story.validate_t @story
956         r.should be_true 
957       end
958     end
959     context '既存のとき' do
960       it '2件目を作っても正常通過している' do
961         @story = FactoryGirl.create :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
962         @story2 = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
963         r = Story.validate_t @story2
964         r.should be_true 
965       end
966     end
967   end
968   describe '挿入シフトに於いて' do
969     before do
970       @comic = FactoryGirl.create :comic, :author_id => @author.id
971       @panel = FactoryGirl.create :panel, :author_id => @author.id
972     end
973     context '依頼チェック' do
974       #テーブルが空で0に挿入
975       it 'Updateを依頼している' do
976         Story.stub(:update_all).with(any_args)
977         Story.should_receive(:update_all).with(any_args).exactly(1)
978         @story = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
979         @story.insert_shift
980       end
981     end
982     context 'テーブルに1件(t:0)で0に挿入したとき' do
983       before do
984         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
985         @story2 = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
986       end
987       it '既存の行を1にシフトしている' do
988         @story2.insert_shift
989         l = Story.find :all
990         l.first.t.should eq 1
991       end
992       it 'これから挿入するt(0)が欠番になっている' do
993         @story2.insert_shift
994         l = Story.find(:all).map {|s| s.t }
995         l.include?(0).should_not be_true
996       end
997     end
998     context 'テーブルに2件(t:0,1)で1に挿入したとき' do
999       before do
1000         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1001         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1002         @story3 = FactoryGirl.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1003       end
1004       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1005         @story3.insert_shift
1006         l = Story.find(:all).map {|s| s.t }
1007         l.sort.should eq [0, 2]
1008       end
1009     end
1010     context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
1011       before do
1012         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1013         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1014         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1015         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1016         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1017         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1018       end
1019       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1020         @story6.insert_shift
1021         l = Story.find(:all).map {|s| s.t }
1022         l.sort.should eq [0, 1, 3, 4, 5]
1023       end
1024     end
1025     context '先ほどのケース+他のコミック1件で挿入したとき' do
1026       before do
1027         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1028         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1029         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1030         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1031         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1032         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1033         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1034         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1035       end
1036       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1037         @story6.insert_shift
1038         l = Story.find(:all, :conditions => ['comic_id = ?', @comic.id]).map {|s| s.t }
1039         l.sort.should eq [0, 1, 3, 4, 5]
1040       end
1041       it '他のコミックに影響がない' do
1042         ot = @storyc2.t
1043         @story6.insert_shift
1044         @storyc2.reload
1045         @storyc2.t.should eq ot
1046       end
1047     end
1048   end
1049   describe '少ない方に移動に於いて' do
1050     before do
1051       @comic = FactoryGirl.create :comic, :author_id => @author.id
1052       @panel = FactoryGirl.create :panel, :author_id => @author.id
1053     end
1054     context '依頼チェック' do
1055       it 'Updateを依頼している' do
1056         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1057         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1058         Story.stub(:update_all).with(any_args)
1059         Story.should_receive(:update_all).with(any_args).exactly(1)
1060         ot = @story2.t
1061         @story2.t = 0
1062         @story2.lesser_shift ot
1063       end
1064     end
1065     context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
1066       before do
1067         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1068         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1069         @ot = @story2.t
1070         @story2.t = 0
1071       end
1072       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
1073         #移動させたい行はそのまま残る
1074         @story2.lesser_shift @ot
1075         l = Story.find(:all).map {|s| s.t }
1076         l.sort.should eq [1, 1]
1077       end
1078       it '既存のt0を1にシフトしている' do
1079         @story2.lesser_shift @ot
1080         @story.reload
1081         @story.t.should eq 1
1082       end
1083     end
1084     context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
1085       before do
1086         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1087         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1088         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1089         @ot = @story3.t
1090         @story3.t = 1
1091       end
1092       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1093         #移動させたい行はそのまま残る
1094         @story3.lesser_shift @ot
1095         l = Story.find(:all).map {|s| s.t }
1096         l.sort.should eq [0, 2, 2]
1097       end
1098       it '既存のt1を2にシフトしている' do
1099         @story3.lesser_shift @ot
1100         @story2.reload
1101         @story2.t.should eq 2
1102       end
1103     end
1104     context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
1105       before do
1106         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1107         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1108         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1109         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1110         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1111         @ot = @story4.t
1112         @story4.t = 1
1113       end
1114       it 'これから挿入するt(1)が欠番になっている' do
1115         #移動させたい行はそのまま残る
1116         @story4.lesser_shift @ot
1117         l = Story.find(:all).map {|s| s.t }
1118         l.sort.should eq [0, 2, 3, 3, 4]
1119       end
1120       it '既存のt0には変化がない' do
1121         @story4.lesser_shift @ot
1122         @story.reload
1123         @story.t.should eq 0
1124       end
1125       it '既存のt4には変化がない' do
1126         @story4.lesser_shift @ot
1127         @story5.reload
1128         @story5.t.should eq 4
1129       end
1130       it '既存のt1を2にシフトしている' do
1131         @story4.lesser_shift @ot
1132         @story2.reload
1133         @story2.t.should eq 2
1134       end
1135       it '既存のt2を3にシフトしている' do
1136         @story4.lesser_shift @ot
1137         @story3.reload
1138         @story3.t.should eq 3
1139       end
1140     end
1141     context '先ほどのケース+他のコミック1件で挿入したとき' do
1142       before do
1143         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1144         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1145         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1146         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1147         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1148         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1149         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1150         @ot = @story4.t
1151         @story4.t = 1
1152       end
1153       it 'これから挿入するt(1)が欠番になっている' do
1154         @story4.lesser_shift @ot
1155         l = Story.find(:all).map {|s| s.t }
1156         l.sort.should eq [0, 0, 2, 3, 3, 4]
1157       end
1158       it '既存のt0には変化がない' do
1159         @story4.lesser_shift @ot
1160         @story.reload
1161         @story.t.should eq 0
1162       end
1163       it '既存のt4には変化がない' do
1164         @story4.lesser_shift @ot
1165         @story5.reload
1166         @story5.t.should eq 4
1167       end
1168       it '既存のt1を2にシフトしている' do
1169         @story4.lesser_shift @ot
1170         @story2.reload
1171         @story2.t.should eq 2
1172       end
1173       it '既存のt2を3にシフトしている' do
1174         @story4.lesser_shift @ot
1175         @story3.reload
1176         @story3.t.should eq 3
1177       end
1178       it '他のコミックに影響がない' do
1179         @story4.lesser_shift @ot
1180         @storyc2.reload
1181         @storyc2.t.should eq 0
1182       end
1183     end
1184     #例外ケース。
1185     #負のときは0として正常扱い
1186     context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
1187       before do
1188         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1189         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1190         @ot = @story2.t
1191         @story2.t = -1
1192       end
1193       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
1194         #移動させたい行はそのまま残る
1195         @story2.lesser_shift @ot
1196         l = Story.find(:all).map {|s| s.t }
1197         l.sort.should eq [1, 1]
1198       end
1199       it '既存のt0を1にシフトしている' do
1200         @story2.lesser_shift @ot
1201         @story.reload
1202         @story.t.should eq 1
1203       end
1204       it '既存のt1は0に補正されている' do
1205         @story2.lesser_shift @ot
1206         @story2.t.should eq 0
1207       end
1208     end
1209   end
1210   describe '大きい方に移動に於いて' do
1211     before do
1212       @comic = FactoryGirl.create :comic, :author_id => @author.id
1213       @panel = FactoryGirl.create :panel, :author_id => @author.id
1214     end
1215     context '依頼チェック' do
1216       it 'Updateを依頼している' do
1217         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1218         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1219         Story.stub(:update_all).with(any_args)
1220         Story.should_receive(:update_all).with(any_args).exactly(1)
1221         ot = @story.t
1222         @story.t = 1
1223         @story.higher_shift ot
1224       end
1225     end
1226     context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
1227       before do
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         @ot = @story.t
1231         @story.t = 1
1232       end
1233       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1234         #移動させたい行はそのまま残る
1235         @story.higher_shift @ot
1236         l = Story.find(:all).map {|s| s.t }
1237         l.sort.should eq [0, 0]
1238       end
1239       it '既存のt1を0にシフトしている' do
1240         @story.higher_shift @ot
1241         @story2.reload
1242         @story2.t.should eq 0
1243       end
1244     end
1245     context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
1246       before do
1247         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1248         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1249         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1250         @ot = @story.t
1251         @story.t = 1
1252       end
1253       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1254         #移動させたい行はそのまま残る
1255         @story.higher_shift @ot
1256         l = Story.find(:all).map {|s| s.t }
1257         l.sort.should eq [0, 0, 2]
1258       end
1259       it '既存のt1を0にシフトしている' do
1260         @story.higher_shift @ot
1261         @story2.reload
1262         @story2.t.should eq 0
1263       end
1264     end
1265     context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
1266       before do
1267         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1268         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1269         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1270         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1271         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1272         @ot = @story2.t
1273         @story2.t = 3
1274       end
1275       it 'これから挿入するt(3)が欠番になっている' do
1276         #移動させたい行はそのまま残る
1277         @story2.higher_shift @ot
1278         l = Story.find(:all).map {|s| s.t }
1279         l.sort.should eq [0, 1, 1, 2, 4]
1280       end
1281       it '既存のt0には変化がない' do
1282         @story2.higher_shift @ot
1283         @story.reload
1284         @story.t.should eq 0
1285       end
1286       it '既存のt4には変化がない' do
1287         @story2.higher_shift @ot
1288         @story5.reload
1289         @story5.t.should eq 4
1290       end
1291       it '既存のt2を1にシフトしている' do
1292         @story2.higher_shift @ot
1293         @story3.reload
1294         @story3.t.should eq 1
1295       end
1296       it '既存のt3を2にシフトしている' do
1297         @story2.higher_shift @ot
1298         @story4.reload
1299         @story4.t.should eq 2
1300       end
1301     end
1302     context '先ほどのケース+他のコミック1件で挿入したとき' do
1303       before do
1304         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1305         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1306         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1307         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1308         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1309         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1310         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1311         @ot = @story2.t
1312         @story2.t = 3
1313       end
1314       it 'これから挿入するt(3)が欠番になっている' do
1315         #移動させたい行はそのまま残る
1316         @story2.higher_shift @ot
1317         l = Story.find(:all).map {|s| s.t }
1318         l.sort.should eq [0, 0, 1, 1, 2, 4]
1319       end
1320       it '既存のt0には変化がない' do
1321         @story2.higher_shift @ot
1322         @story.reload
1323         @story.t.should eq 0
1324       end
1325       it '既存のt4には変化がない' do
1326         @story2.higher_shift @ot
1327         @story5.reload
1328         @story5.t.should eq 4
1329       end
1330       it '既存のt2を1にシフトしている' do
1331         @story2.higher_shift @ot
1332         @story3.reload
1333         @story3.t.should eq 1
1334       end
1335       it '既存のt3を2にシフトしている' do
1336         @story2.higher_shift @ot
1337         @story4.reload
1338         @story4.t.should eq 2
1339       end
1340       it '他のコミックに影響がない' do
1341         @story2.higher_shift @ot
1342         @storyc2.reload
1343         @storyc2.t.should eq 0
1344       end
1345     end
1346     #例外ケース。
1347     #max超えたときはmaxとして正常扱い
1348     context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
1349       before do
1350         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1351         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1352         @ot = @story.t
1353         @story.t = 2
1354       end
1355       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1356         #移動させたい行はそのまま残る
1357         @story.higher_shift @ot
1358         l = Story.find(:all).map {|s| s.t }
1359         l.sort.should eq [0, 0]
1360       end
1361       it '既存のt1を0にシフトしている' do
1362         @story.higher_shift @ot
1363         @story2.reload
1364         @story2.t.should eq 0
1365       end
1366       it '既存のt0は1に補正されている' do
1367         @story.higher_shift @ot
1368         @story.t.should eq 1
1369       end
1370     end
1371   end
1372   describe '入れ替えに於いて' do
1373     before do
1374       @comic = FactoryGirl.create :comic, :author_id => @author.id
1375       @panel = FactoryGirl.create :panel, :author_id => @author.id
1376       @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1377       @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1378     end
1379     context '新tが旧tより小さいとき' do
1380       it '少ない方に移動を依頼している' do
1381         Story.any_instance.stub(:lesser_shift).with(any_args)
1382         Story.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
1383         ot = @story2.t
1384         @story2.t = 0
1385         @story2.update_shift ot
1386       end
1387     end
1388     context '新tが旧tより大きいとき' do
1389       it '大きい方に移動を依頼している' do
1390         Story.any_instance.stub(:higher_shift).with(any_args)
1391         Story.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
1392         ot = @story.t
1393         @story.t = 1
1394         @story.update_shift ot
1395       end
1396     end
1397   end
1398   describe '順序入れ替えに於いて' do
1399     before do
1400       @comic = FactoryGirl.create :comic, :author_id => @author.id
1401       @panel = FactoryGirl.create :panel, :author_id => @author.id
1402     end
1403     context 'オブジェクトが新規でtが空のとき' do
1404       it '末尾追加としてtを補充依頼している' do
1405         @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1406         Story.stub(:new_t).with(any_args).and_return(0)
1407         Story.should_receive(:new_t).with(any_args).exactly(1)
1408         @story.t = nil
1409         r = @story.rotate
1410       end
1411     end
1412     context 'オブジェクトが新規でtが設定されているとき' do
1413       it '挿入追加として挿入シフトを依頼している' do
1414         @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1415         Story.any_instance.stub(:insert_shift).with(any_args)
1416         Story.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
1417         @story.t = 0
1418         r = @story.rotate
1419       end
1420     end
1421     context 'オブジェクトが新規でなくtが設定されているとき' do
1422       it 'コマ移動として入れ替えを依頼している' do
1423         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1424         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1425         Story.any_instance.stub(:update_shift).with(any_args)
1426         Story.any_instance.should_receive(:update_shift).with(1).exactly(1)
1427         @story2.t = 0
1428         r = @story.rotate 1
1429       end
1430     end
1431     context 'オブジェクトが新規でなくtが空のとき' do
1432       it '入れ替えもシフトもせず、tを空のままにしている' do
1433         #結果、tに欠番が生じてシリアライズチェックでひっかかる
1434       end
1435     end
1436   end
1437   describe '編集許可に於いて' do
1438     before do
1439       @comic = FactoryGirl.create :comic, :author_id => @author.id
1440       @panel = FactoryGirl.create :panel, :author_id => @author.id
1441       @story = FactoryGirl.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1442     end
1443     context 'つつがなく終わるとき' do
1444       it 'trueを返す' do
1445         r = @story.allow?
1446         r.should be_true
1447       end
1448     end
1449     context 'コミックで引っかかるとき' do
1450       it 'falseを返す' do
1451         @story.comic_id = nil
1452         r = @story.allow?
1453         r.should be_false
1454       end
1455       it 'falseを返す' do
1456         Comic.any_instance.stub(:own?).with(any_args).and_return(false)
1457         r = @story.allow?
1458         r.should be_false
1459       end
1460     end
1461     context 'コマで引っかかるとき' do
1462       it 'falseを返す' do
1463         @story.panel_id = nil
1464         r = @story.allow?
1465         r.should be_false
1466       end
1467       it 'falseを返す' do
1468         Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
1469         r = @story.allow?
1470         r.should be_false
1471       end
1472     end
1473   end
1474   describe '保存に於いて' do
1475     before do
1476       @comic = FactoryGirl.create :comic, :author_id => @author.id
1477       @panel = FactoryGirl.create :panel, :author_id => @author.id
1478       @story = FactoryGirl.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1479     end
1480     context 'つつがなく終わるとき' do
1481       it '編集許可チェックを依頼している' do
1482         Story.any_instance.stub(:allow?).with(any_args).and_return(true)
1483         Story.any_instance.should_receive(:allow?).with(any_args).exactly(1)
1484         r = @story.store
1485       end
1486       it '順序入れ替えを依頼している' do
1487         Story.any_instance.stub(:rotate).with(any_args).and_return(0)
1488         Story.any_instance.should_receive(:rotate).with(any_args).exactly(1)
1489         Story.any_instance.stub(:save).with(any_args).and_return(true)
1490         Story.stub(:validate_t).with(any_args).and_return(true)
1491         r = @story.store 
1492       end
1493       it '保存を依頼している' do
1494         Story.stub(:new_t).with(any_args).and_return(0)
1495         Story.any_instance.stub(:save).with(any_args).and_return(true)
1496         Story.any_instance.should_receive(:save).with(any_args).exactly(1)
1497         Story.stub(:validate_t).with(any_args).and_return(true)
1498         r = @story.store
1499       end
1500       it 'tのシリアライズチェックを依頼している' do
1501         Story.stub(:new_t).with(any_args).and_return(0)
1502         Story.any_instance.stub(:save).with(any_args).and_return(true)
1503         Story.stub(:validate_t).with(any_args).and_return(true)
1504         Story.should_receive(:validate_t).with(any_args).exactly(1)
1505         r = @story.store
1506       end
1507     end
1508     #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1509     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したとき' do
1510       before do
1511         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1512         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1513         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1514         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1515         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1516         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1517         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1518         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1519       end
1520       it '既存のt0には変化がない' do
1521         @story6.store
1522         @story.reload
1523         @story.t.should eq 0
1524       end
1525       it '既存のt1には変化がない' do
1526         @story6.store
1527         @story2.reload
1528         @story2.t.should eq 1
1529       end
1530       it '既存のt2を3にシフトしている' do
1531         @story6.store
1532         @story3.reload
1533         @story3.t.should eq 3
1534       end
1535       it '既存のt3を4にシフトしている' do
1536         @story6.store
1537         @story4.reload
1538         @story4.t.should eq 4
1539       end
1540       it '既存のt5を5にシフトしている' do
1541         @story6.store
1542         @story5.reload
1543         @story5.t.should eq 5
1544       end
1545       it '新規のt2が作成されている' do
1546         @story6.store
1547         @story6.reload
1548         @story6.t.should eq 2
1549       end
1550       it '他のコミックに影響がない' do
1551         @ot = @storyc2.t
1552         @story6.store
1553         @storyc2.reload
1554         @storyc2.t.should eq @ot
1555       end
1556     end
1557     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したとき' do
1558       before do
1559         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1560         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1561         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1562         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1563         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1564         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1565         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1566         @ot = @story4.t
1567         @story4.t = 1
1568       end
1569       it '既存のt0には変化がない' do
1570         @story4.store @ot
1571         @story.reload
1572         @story.t.should eq 0
1573       end
1574       it '既存のt4には変化がない' do
1575         @story4.store @ot
1576         @story5.reload
1577         @story5.t.should eq 4
1578       end
1579       it '既存のt1を2にシフトしている' do
1580         @story4.store @ot
1581         @story2.reload
1582         @story2.t.should eq 2
1583       end
1584       it '既存のt2を3にシフトしている' do
1585         @story4.store @ot
1586         @story3.reload
1587         @story3.t.should eq 3
1588       end
1589       it '既存のt3を1にシフトしている' do
1590         @story4.store @ot
1591         @story4.reload
1592         @story4.t.should eq 1
1593       end
1594       it '他のコミックに影響がない' do
1595         @story4.store @ot
1596         @storyc2.reload
1597         @storyc2.t.should eq 0
1598       end
1599     end
1600     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で1を3に移動したとき' do
1601       before do
1602         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1603         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1604         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1605         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1606         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1607         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1608         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1609         @ot = @story2.t
1610         @story2.t = 3
1611       end
1612       it '既存のt0には変化がない' do
1613         @story2.store @ot
1614         @story.reload
1615         @story.t.should eq 0
1616       end
1617       it '既存のt4には変化がない' do
1618         @story2.store @ot
1619         @story5.reload
1620         @story5.t.should eq 4
1621       end
1622       it '既存のt1を3にシフトしている' do
1623         @story2.store @ot
1624         @story2.reload
1625         @story2.t.should eq 3
1626       end
1627       it '既存のt2を1にシフトしている' do
1628         @story2.store @ot
1629         @story3.reload
1630         @story3.t.should eq 1
1631       end
1632       it '既存のt3を2にシフトしている' do
1633         @story2.store @ot
1634         @story4.reload
1635         @story4.t.should eq 2
1636       end
1637       it '他のコミックに影響がない' do
1638         @story2.store @ot
1639         @storyc2.reload
1640         @storyc2.t.should eq 0
1641       end
1642     end
1643     #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1644     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したが保存に失敗したとき' do
1645       before do
1646         Story.any_instance.stub(:save).with(any_args).and_return(false)
1647         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1648         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1649         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1650         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1651         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1652         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1653         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1654         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1655       end
1656       it '既存のtに変化がない' do
1657         @story6.store
1658         @story.reload
1659         @story.t.should eq 0
1660         @story2.reload
1661         @story2.t.should eq 1
1662         @story3.reload
1663         @story3.t.should eq 2
1664         @story4.reload
1665         @story4.t.should eq 3
1666         @story5.reload
1667         @story5.t.should eq 4
1668         @storyc2.reload
1669         @storyc2.t.should eq 0
1670       end
1671       it 'falseを返す' do
1672         r = @story6.store
1673         r.should be_false
1674       end
1675     end
1676     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したがシリアルチェックに失敗したとき' do
1677       before do
1678         Story.stub(:validate_t).with(any_args).and_return(false)
1679         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1680         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1681         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1682         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1683         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1684         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1685         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1686         @ot = @story4.t
1687         @story4.t = 1
1688       end
1689       it '既存のtに変化がない' do
1690         @story4.store @ot
1691         @story.reload
1692         @story.t.should eq 0
1693         @story2.reload
1694         @story2.t.should eq 1
1695         @story3.reload
1696         @story3.t.should eq 2
1697         @story4.reload
1698         @story4.t.should eq 3
1699         @story5.reload
1700         @story5.t.should eq 4
1701         @storyc2.reload
1702         @storyc2.t.should eq 0
1703       end
1704       it 'falseを返す' do
1705         r = @story4.store @ot
1706         r.should be_false
1707       end
1708       it 'tにエラーメッセージが入っている' do
1709         @story4.store @ot
1710         @story4.errors[:t].should_not be_empty
1711         @story4.valid?.should be_true
1712       end
1713     end
1714     context '編集不可だったとき' do
1715       before do
1716         @story = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1717         Story.any_instance.stub(:allow?).and_return(false)
1718       end
1719       it '403Forbidden例外を返す' do
1720         lambda{
1721           @story.store
1722         }.should raise_error(ActiveRecord::Forbidden)
1723       end
1724     end
1725   end
1726   describe '切り詰め処理つき削除に於いて' do
1727     before do
1728       @comic = FactoryGirl.create :comic, :author_id => @author.id
1729       @panel = FactoryGirl.create :panel, :author_id => @author.id
1730       @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1731     end
1732     context 'つつがなく終わるとき' do
1733       it '削除される' do
1734         lambda{
1735           @story.destroy_and_shorten
1736         }.should change(Story, :count ).by(-1)
1737       end
1738       it 'Trueを返す' do
1739         r = @story.destroy_and_shorten
1740         r.should be_true 
1741       end
1742     end
1743     context '削除に失敗したとき' do
1744       before do
1745         Story.any_instance.stub(:destroy).and_return(false)
1746       end
1747       it 'ロールバックされる' do
1748         lambda{
1749           @story.destroy_and_shorten
1750         }.should_not change(Story, :count )
1751       end
1752       it 'Falseを返す' do
1753         r = @story.destroy_and_shorten
1754         r.should be_false
1755       end
1756     end
1757     #連携テスト。切り詰めが直接DBをいじる
1758     context '2件で先頭を削除したとき' do
1759       before do
1760         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1761       end
1762       it '行が削除される' do
1763         lambda{
1764           @story.destroy_and_shorten
1765         }.should change(Story, :count ).by(-1)
1766       end
1767       it '先頭は削除される' do
1768         @story.destroy_and_shorten
1769         lambda{
1770           Story.find @story.id
1771         }.should raise_error(ActiveRecord::RecordNotFound)
1772       end
1773       it '2件目は前に詰められる' do
1774         @story.destroy_and_shorten
1775         @story2.reload
1776         @story2.t.should eq 0
1777       end
1778     end
1779     context '3件で先頭を削除したとき' do
1780       before do
1781         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1782         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1783       end
1784       it '行が削除される' do
1785         lambda{
1786           @story.destroy_and_shorten
1787         }.should change(Story, :count ).by(-1)
1788       end
1789       it '先頭は削除される' do
1790         @story.destroy_and_shorten
1791         lambda{
1792           Story.find @story.id
1793         }.should raise_error(ActiveRecord::RecordNotFound)
1794       end
1795       it '2件目は前に詰められる' do
1796         @story.destroy_and_shorten
1797         @story2.reload
1798         @story2.t.should eq 0
1799       end
1800       it '3件目は前に詰められる' do
1801         @story.destroy_and_shorten
1802         @story3.reload
1803         @story3.t.should eq 1
1804       end
1805     end
1806     context '5件で3件目を削除したとき' do
1807       before do
1808         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1809         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1810         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1811         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1812       end
1813       it '行が削除される' do
1814         lambda{
1815           @story3.destroy_and_shorten
1816         }.should change(Story, :count ).by(-1)
1817       end
1818       it '1件目は変化がない' do
1819         @story3.destroy_and_shorten
1820         @story.reload
1821         @story.t.should eq 0
1822       end
1823       it '2件目は変化がない' do
1824         @story3.destroy_and_shorten
1825         @story2.reload
1826         @story2.t.should eq 1
1827       end
1828       it '3件目は削除される' do
1829         @story3.destroy_and_shorten
1830         lambda{
1831           Story.find @story3.id
1832         }.should raise_error(ActiveRecord::RecordNotFound)
1833       end
1834       it '4件目は前に詰められる' do
1835         @story3.destroy_and_shorten
1836         @story4.reload
1837         @story4.t.should eq 2
1838       end
1839       it '5件目は前に詰められる' do
1840         @story3.destroy_and_shorten
1841         @story5.reload
1842         @story5.t.should eq 3
1843       end
1844     end
1845     #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1846   end
1847 end