OSDN Git Service

t#31297:fix author's list
[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   
491   describe '自分のストーリー一覧取得に於いて' do
492     before do
493       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
494       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
495       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
496       @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
497       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
498       @hcomic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
499       @hpanel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
500     end
501     context 'つつがなく終わるとき' do
502       it '一覧取得オプションを利用している' do
503         Story.stub(:list_opt).with(any_args).and_return({})
504         Story.should_receive(:list_opt).with(any_args).exactly(1)
505         r = Story.mylist @author
506       end
507     end
508     it 'リストを返す' do
509       s = Story.mylist @author
510       s.should eq [@story]
511     end
512     it '時系列で並んでいる' do
513       ns = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
514       sl = Story.mylist @author
515       sl.should eq [ns, @story]
516     end
517     it '他人のストーリーはコマコミックともに公開でも含まない' do
518       so = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id
519       sl = Story.mylist @author
520       sl.should eq [@story]
521     end
522     it '自分のストーリーはコマコミックともに非公開でも含んでいる' do
523       hs = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @hcomic.id, :panel_id => @hpanel.id, :updated_at => Time.now + 100
524       sl = Story.mylist @author
525       sl.should eq [hs, @story]
526     end
527     context 'DBに5件あって1ページの件数を2件に変えたとして' do
528       before do
529         @story2 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
530         @story3 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 2, :updated_at => Time.now + 200
531         @story4 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 3, :updated_at => Time.now + 300
532         @story5 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 4, :updated_at => Time.now + 400
533         Story.stub(:default_page_size).and_return(2)
534       end
535       it '通常は2件を返す' do
536         l = Story.mylist @author, 1, 2
537         l.should have(2).items 
538       end
539       it 'page=1なら末尾2件を返す' do
540         #時系列で並んでいる
541         l = Story.mylist @author, 1, 2
542         l.should eq [@story5, @story4]
543       end
544       it 'page=2なら中間2件を返す' do
545         l = Story.mylist @author, 2, 2
546         l.should eq [@story3, @story2]
547       end
548       it 'page=3なら先頭1件を返す' do
549         l = Story.mylist @author, 3, 2
550         l.should eq [@story]
551       end
552     end
553     context 'DBに5件あって1ページの件数を2件に変えたとして' do
554       before do
555         @story2 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1, :updated_at => Time.now + 100
556         @story3 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 2, :updated_at => Time.now + 200
557         @story4 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 3, :updated_at => Time.now + 300
558         @story5 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 4, :updated_at => Time.now + 400
559         Author.stub(:default_story_page_size).and_return(2)
560       end
561       it '件数0は全件(5件)を返す' do
562         r = Story.mylist @author, 5, 0
563         r.should have(5).items 
564       end
565     end
566   end
567   
568   describe '他作家のストーリー一覧取得に於いて' do
569     before do
570       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
571       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
572       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
573       @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
574       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
575       @other_story = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id
576     end
577     it 'リストを返す' do
578       r = Story.himlist @other_author
579       r.should eq [@other_story]
580     end
581     it '時系列で並んでいる' do
582       ns = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id, :t => 1, :updated_at => Time.now + 100
583       r = Story.himlist @other_author
584       r.should eq [ns, @other_story]
585     end
586     it '公開コミックのストーリー' do
587       @hcomic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 0
588       ns = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @hcomic.id, :panel_id => @other_panel.id, :t => 1, :updated_at => Time.now + 100
589       r = Story.himlist @other_author
590       r.should eq [@other_story]
591     end
592     context 'DBに5件あって1ページの件数を2件に変えたとして' do
593       before do
594         @other_story2 = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id, :t => 1, :updated_at => Time.now + 100
595         @other_story3 = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id, :t => 2, :updated_at => Time.now + 200
596         @other_story4 = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id, :t => 3, :updated_at => Time.now + 300
597         @other_story5 = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id, :t => 4, :updated_at => Time.now + 400
598         Story.stub(:default_page_size).and_return(2)
599       end
600       it '通常は2件を返す' do
601         l = Story.himlist @other_author, 1, 2
602         l.should have(2).items 
603       end
604       it 'page=1なら末尾2件を返す' do
605         #時系列で並んでいる
606         l = Story.himlist @other_author, 1, 2
607         l.should eq [@other_story5, @other_story4]
608       end
609       it 'page=2なら中間2件を返す' do
610         l = Story.himlist @other_author, 2, 2
611         l.should eq [@other_story3, @other_story2]
612       end
613       it 'page=3なら先頭1件を返す' do
614         l = Story.himlist @other_author, 3, 2
615         l.should eq [@other_story]
616       end
617     end
618     context 'DBに5件あって1ページの件数を2件に変えたとして' do
619       before do
620         @other_story2 = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id, :t => 1, :updated_at => Time.now + 100
621         @other_story3 = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id, :t => 2, :updated_at => Time.now + 200
622         @other_story4 = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id, :t => 3, :updated_at => Time.now + 300
623         @other_story5 = FactoryGirl.create :story, :author_id => @other_author.id, :comic_id => @other_comic.id, :panel_id => @other_panel.id, :t => 4, :updated_at => Time.now + 400
624         Author.stub(:default_story_page_size).and_return(2)
625       end
626       it '件数0は全件(5件)を返す' do
627         r = Story.himlist @other_author, 5, 0
628         r.should have(5).items 
629       end
630     end
631   end
632   
633   describe '単体取得に於いて' do
634     before do
635       @comic = FactoryGirl.create :comic, :author_id => @author.id
636       @panel = FactoryGirl.create :panel, :author_id => @author.id
637       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
638     end
639     context 'つつがなく終わるとき' do
640       it '単体取得オプションを利用している' do
641         Story.stub(:show_opt).with(any_args).and_return({})
642         Story.should_receive(:show_opt).with(any_args).exactly(1)
643         r = Story.show @story.id, @author
644       end
645       it '閲覧許可を問い合わせている' do
646         Story.any_instance.stub(:visible?).with(@author).and_return(true)
647         Story.any_instance.should_receive(:visible?).with(@author).exactly(1)
648         r = Story.show @story.id, @author
649       end
650     end
651     it '指定のストーリーを返す' do
652       l = Story.show @story.id, @author
653       l.should eq @story
654     end
655     context '他人のストーリーを開こうとしたとき' do
656       it '403Forbidden例外を返す' do
657         Story.any_instance.stub(:visible?).with(@other_author).and_return(false)
658         lambda{
659           Story.show @story.id, @other_author
660         }.should raise_error(ActiveRecord::Forbidden)
661       end
662     end
663     context '存在しないストーリーを開こうとしたとき' do
664       it '404RecordNotFound例外を返す' do
665         lambda{
666           Story.show 110, @author
667         }.should raise_error(ActiveRecord::RecordNotFound)
668       end
669     end
670   end
671   
672   describe '編集取得に於いて' do
673     before do
674       @comic = FactoryGirl.create :comic, :author_id => @author.id
675       @panel = FactoryGirl.create :panel, :author_id => @author.id
676       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
677     end
678     context 'つつがなく終わるとき' do
679       it '単体取得オプションを利用している' do
680         Story.stub(:show_opt).with(any_args).and_return({})
681         Story.should_receive(:show_opt).with(any_args).exactly(1)
682         r = Story.edit @story.id, @author
683       end
684       it '所持判定を問い合わせている' do
685         Story.any_instance.stub(:own?).with(any_args).and_return(true)
686         Story.any_instance.should_receive(:own?).with(any_args).exactly(1)
687         r = Story.edit @story.id, @author
688       end
689     end
690     it '指定のストーリーを返す' do
691       l = Story.edit @story.id, @author
692       l.should eq @story
693     end
694     context '他人のストーリーを開こうとしたとき' do
695       it '403Forbidden例外を返す' do
696         Story.any_instance.stub(:own?).and_return(false)
697         lambda{
698           Story.edit @story.id, @author
699         }.should raise_error(ActiveRecord::Forbidden)
700       end
701     end
702     context '存在しないストーリーを開こうとしたとき' do
703       it '404RecordNotFound例外を返す' do
704         lambda{
705           Story.edit 110, @author
706         }.should raise_error(ActiveRecord::RecordNotFound)
707       end
708     end
709   end
710   
711   describe '単体取得オプションに於いて' do
712     it 'includeキーを含んでいる' do
713       r = Story.show_opt
714       r.has_key?(:include).should be_true
715     end
716     it '3つの項目を含んでいる' do
717       r = Story.show_opt[:include]
718       r.should have(3).items
719     end
720     it 'コミックを含んでいる' do
721       r = Story.show_opt[:include]
722       r.has_key?(:comic).should be_true
723     end
724       it 'コミックは作家を含んでいる' do
725         r = Story.show_opt[:include]
726         r[:comic].has_key?(:author).should be_true
727       end
728     it '作家を含んでいる' do
729       r = Story.show_opt[:include]
730       r.has_key?(:author).should be_true
731     end
732     it 'コマを含んでいる' do
733       r = Story.show_opt[:include]
734       r.has_key?(:panel).should be_true
735     end
736       it 'コマは作家を含んでいる' do
737         r = Story.show_opt[:include]
738         r[:panel].has_key?(:author).should be_true
739       end
740       it 'コマはコマ絵を含んでいる' do
741         r = Story.show_opt[:include]
742         r[:panel].has_key?(:panel_pictures).should be_true
743       end
744         it 'コマ絵は実素材を含んでいる' do
745           r = Story.show_opt[:include]
746           r[:panel][:panel_pictures].has_key?(:picture).should be_true
747         end
748           it '実素材は絵師を含んでいる' do
749             r = Story.show_opt[:include]
750             r[:panel][:panel_pictures][:picture].has_key?(:artist).should be_true
751           end
752           it '実素材はライセンスを含んでいる' do
753             r = Story.show_opt[:include]
754             r[:panel][:panel_pictures][:picture].has_key?(:license).should be_true
755           end
756       it 'コマはフキダシを含んでいる' do
757         r = Story.show_opt[:include]
758         r[:panel].has_key?(:speech_balloons).should be_true
759       end
760         it 'フキダシはフキダシ枠を含んでいる' do
761           r = Story.show_opt[:include]
762           r[:panel][:speech_balloons].has_key?(:balloons).should be_true
763         end
764         it 'フキダシはセリフを含んでいる' do
765           r = Story.show_opt[:include]
766           r[:panel][:speech_balloons].has_key?(:speeches).should be_true
767         end
768   end
769   describe 'json単体取得オプションに於いて' do
770   end
771   
772   describe 'ストーリーのjson出力に於いて' do
773     before do
774       #コマを作成しておく。
775       @panel = FactoryGirl.create :panel, :author_id => @author.id
776       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :width => @p.width, :height => @p.height
777       @sb = @panel.speech_balloons.create(
778         FactoryGirl.attributes_for(:speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @sbt.id, :t => 0)
779       )
780       @gc = @panel.ground_colors.create(
781         FactoryGirl.attributes_for(:ground_color, :panel_id => @panel.id, :color_id => @color.id)
782       )
783       @gp = @panel.ground_pictures.create(
784         FactoryGirl.attributes_for(:ground_picture, :panel_id => @panel.id, :picture_id => @p.id)
785       )
786       @pc = @panel.panel_colors.create(
787         FactoryGirl.attributes_for(:panel_color, :panel_id => @panel.id, :code => 0xff0000)
788       )
789       @panel.reload
790       @comic = FactoryGirl.create :comic, :author_id => @author.id
791       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
792     end
793     context '事前チェックする' do
794       before do
795         Panel.any_instance.stub(:elements).and_return('{}')
796       end
797       it 'コマ要素のjson出力を依頼している' do
798         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
799         Panel.any_instance.should_receive(:elements).with(any_args).exactly(1)
800         r = @story.story_as_json @author
801       end
802     end
803     it 'json textを返している' do
804       r = JSON.parse @story.story_as_json(@author)
805       r.is_a?(Hash).should be_true
806     end
807     it 'comic,author,panel,コマ要素を含んでいる' do
808       r = JSON.parse @story.story_as_json(@author)
809       r.has_key?('comic').should be_true
810       r['comic'].has_key?('author').should be_true
811       r.has_key?('author').should be_true
812       r.has_key?('panel').should be_true
813       r['panel'].has_key?('author').should be_true
814     end
815     context 'コマ閲覧許可のとき' do
816       before do
817         Panel.any_instance.stub(:visible?).with(any_args).and_return(true)
818       end
819       it 'コマ要素にコマ要素を追加している' do
820         r = JSON.parse @story.story_as_json(@author)
821         r['panel'].has_key?('elements').should be_true
822         r['panel']['elements'].should_not be_empty
823       end
824     end
825     context 'コマ閲覧不許可のとき' do
826       before do
827         Panel.any_instance.stub(:visible?).with(any_args).and_return(false)
828       end
829       it 'コマ要素にデータを含ませない' do
830         r = JSON.parse @story.story_as_json(@author)
831         r['panel'].has_key?('elements').should be_false
832       end
833     end
834   end
835   
836   describe 'ストーリーリストのjson出力に於いて' do
837     before do
838       @panel = FactoryGirl.create :panel, :author_id => @author.id
839       @comic = FactoryGirl.create :comic, :author_id => @author.id
840       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
841       Story.any_instance.stub(:story_as_json).with(@author).and_return('{"s": 5}')
842     end
843     context 'つつがなく終わるとき' do
844       it 'ストーリーのjson出力を依頼している' do
845         Story.any_instance.should_receive(:story_as_json).with(@author).exactly(1)
846         r = Story.list_as_json_text [@story], @author
847       end
848     end
849     it 'json textを返している' do
850       r = Story.list_as_json_text [@story], @author
851       j = JSON.parse r
852       j.is_a?(Array).should be_true
853     end
854     it 'ストーリーを含んでいる' do
855       r = Story.list_as_json_text [@story], @author
856       j = JSON.parse r
857       j.first.has_key?('s').should be_true
858     end
859   end
860   
861   describe 'ライセンス素材に於いて' do
862     before do
863       #コマを作成しておく。
864       @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
865       @p2 = FactoryGirl.create :picture, :original_picture_id => @op2.id, :license_id => @license.id, :artist_id => @artist.id
866       @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op2.id, :picture_id => @p2.id
867       @panel = FactoryGirl.create :panel, :author_id => @author.id
868       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 0, :width => @p.width, :height => @p.height, :picture_id => @p.id
869       @panel.reload
870       @panel2 = FactoryGirl.create :panel, :author_id => @author.id
871       @pp2= FactoryGirl.create :panel_picture, :panel_id => @panel2.id, :t => 0, :width => @p2.width, :height => @p2.height, :picture_id => @p2.id
872       @panel2.reload
873       
874       @comic = FactoryGirl.create :comic, :author_id => @author.id
875       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
876       @story2 = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel2.id
877     end
878     context '事前チェック' do
879     end
880     context 'つつがなく終わるとき' do
881     end
882     it '連想配列(値は実素材、キーは実素材id)を返している' do
883       r = Story.licensed_pictures [@story, @story2]
884       r.is_a?(Hash).should be_true
885       r.should have(2).items
886       r[@pp.picture_id].should eq @p
887       r[@pp2.picture_id].should eq @p2
888     end
889     context 'コマが削除されているときき' do
890       before do
891         @panel2.destroy
892       end
893       it '削除されているコマは無視する' do
894         r = Story.licensed_pictures [@story, @story2]
895         r.is_a?(Hash).should be_true
896         r.should have(1).items
897         r[@pp.picture_id].should eq @p
898         r[@pp2.picture_id].should be_nil
899       end
900     end
901   end
902   
903   describe 't補充値に於いて' do
904     before do
905       @comic = FactoryGirl.create :comic, :author_id => @author.id
906       @panel = FactoryGirl.create :panel, :author_id => @author.id
907     end
908     
909     context 'コミック初のコマなら' do
910       it '0を補充値とする' do
911         @story = FactoryGirl.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
912         @story.t = nil
913         r = Story.new_t @story.comic_id
914         r.should eq 0
915       end
916     end
917     context 'コミックに一個コマがあるとき' do
918       it '1を補充値とする' do
919         FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
920         @story = FactoryGirl.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
921         @story.t = nil
922         r = Story.new_t @story.comic_id
923         r.should eq 1
924       end
925     end
926     context 'コミックに2個コマがあるとき' do
927       it '2を補充値とする' do
928         FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 0
929         FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id, :t => 1
930         @story = FactoryGirl.build :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
931         @story.t = nil
932         r = Story.new_t @story.comic_id
933         r.should eq 2
934       end
935     end
936   end
937   describe 'シリアライズチェックに於いて' do
938     context 'つつがなく終わるとき' do
939       it '0からシリアライズされているならTrueを返す' do
940         r = Story.serial? [0, 1, 2]
941         r.should be_true
942       end
943       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
944         r = Story.serial? [0, 2, 1]
945         r.should be_true
946       end
947       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
948         r = Story.serial? [ 2, 1, 4, 3, 0]
949         r.should be_true
950       end
951     end
952     context '異常なとき' do
953       it '0から始まらないならFalseを返す' do
954         r = Story.serial? [1, 2, 3]
955         r.should be_false
956       end
957       it '連続していないならFalseを返す' do
958         r = Story.serial? [0, 1, 2, 4]
959         r.should be_false
960       end
961       it '連続していないならFalseを返す' do
962         r = Story.serial? [0, 1, 2, 4, 5]
963         r.should be_false
964       end
965     end
966   end
967   describe 't収集に於いて' do
968     before do
969       @comic = FactoryGirl.create :comic, :author_id => @author.id
970       @panel = FactoryGirl.create :panel, :author_id => @author.id
971       @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
972       @comic2 = FactoryGirl.create :comic, :author_id => @author.id
973       @c2story = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
974     end
975     context 'つつがなく終わるとき' do
976       it 'ストーリーから同一コミックのtだけを収集している' do
977         r = Story.collect_t @story
978         r.should eq [0]
979       end
980     end
981     context '複数コマのとき' do
982       it 'ストーリーから同一コミックのtだけを収集している' do
983         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
984         r = Story.collect_t @story
985         r.sort.should eq [0, 1]
986       end
987     end
988     context '複数コマでヨソのコミックも混じっているとき' do
989       it 'ストーリーから同一コミックのtだけを収集している' do
990         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
991         r = Story.collect_t @story
992         r.sort.should eq [0, 1]
993       end
994     end
995   end
996   describe 'tチェックに於いて' do
997     before do
998       @comic = FactoryGirl.create :comic, :author_id => @author.id
999       @panel = FactoryGirl.create :panel, :author_id => @author.id
1000       @story = FactoryGirl.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1001     end
1002     context 'つつがなく終わるとき' do
1003       it 't収集を依頼している' do
1004         Story.should_receive(:collect_t).with(any_args).exactly(1)
1005         Story.stub(:collect_t).with(any_args).and_return([])
1006         Story.stub(:serial?).with(any_args).and_return(true)
1007         r = Story.validate_t @story
1008       end
1009       it '収集したtをシリアライズチェック依頼している' do
1010         Story.stub(:collect_t).with(any_args).and_return([])
1011         Story.should_receive(:serial?).with(any_args).exactly(1)
1012         Story.stub(:serial?).with(any_args).and_return(true)
1013         r = Story.validate_t @story
1014       end
1015     end
1016     #実データでチェック
1017     #依頼チェックだけでは不安なので最低限のチェックを
1018     context '新規のとき' do
1019       it '一件だけで正常通過している' do
1020         @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
1021         r = Story.validate_t @story
1022         r.should be_true 
1023       end
1024     end
1025     context '既存のとき' do
1026       it '2件目を作っても正常通過している' do
1027         @story = FactoryGirl.create :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
1028         @story2 = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
1029         r = Story.validate_t @story2
1030         r.should be_true 
1031       end
1032     end
1033   end
1034   describe '挿入シフトに於いて' do
1035     before do
1036       @comic = FactoryGirl.create :comic, :author_id => @author.id
1037       @panel = FactoryGirl.create :panel, :author_id => @author.id
1038     end
1039     context '依頼チェック' do
1040       #テーブルが空で0に挿入
1041       it 'Updateを依頼している' do
1042         Story.stub(:update_all).with(any_args)
1043         Story.should_receive(:update_all).with(any_args).exactly(1)
1044         @story = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1045         @story.insert_shift
1046       end
1047     end
1048     context 'テーブルに1件(t:0)で0に挿入したとき' do
1049       before do
1050         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1051         @story2 = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1052       end
1053       it '既存の行を1にシフトしている' do
1054         @story2.insert_shift
1055         l = Story.find :all
1056         l.first.t.should eq 1
1057       end
1058       it 'これから挿入するt(0)が欠番になっている' do
1059         @story2.insert_shift
1060         l = Story.find(:all).map {|s| s.t }
1061         l.include?(0).should_not be_true
1062       end
1063     end
1064     context 'テーブルに2件(t:0,1)で1に挿入したとき' do
1065       before do
1066         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1067         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1068         @story3 = FactoryGirl.build :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1069       end
1070       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1071         @story3.insert_shift
1072         l = Story.find(:all).map {|s| s.t }
1073         l.sort.should eq [0, 2]
1074       end
1075     end
1076     context 'テーブルに5件(t:0,1,2,3,4)で2に挿入したとき' do
1077       before do
1078         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1079         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1080         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1081         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1082         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1083         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1084       end
1085       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1086         @story6.insert_shift
1087         l = Story.find(:all).map {|s| s.t }
1088         l.sort.should eq [0, 1, 3, 4, 5]
1089       end
1090     end
1091     context '先ほどのケース+他のコミック1件で挿入したとき' do
1092       before do
1093         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1094         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1095         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1096         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1097         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1098         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1099         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1100         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1101       end
1102       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1103         @story6.insert_shift
1104         l = Story.find(:all, :conditions => ['comic_id = ?', @comic.id]).map {|s| s.t }
1105         l.sort.should eq [0, 1, 3, 4, 5]
1106       end
1107       it '他のコミックに影響がない' do
1108         ot = @storyc2.t
1109         @story6.insert_shift
1110         @storyc2.reload
1111         @storyc2.t.should eq ot
1112       end
1113     end
1114   end
1115   describe '少ない方に移動に於いて' do
1116     before do
1117       @comic = FactoryGirl.create :comic, :author_id => @author.id
1118       @panel = FactoryGirl.create :panel, :author_id => @author.id
1119     end
1120     context '依頼チェック' do
1121       it 'Updateを依頼している' do
1122         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1123         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1124         Story.stub(:update_all).with(any_args)
1125         Story.should_receive(:update_all).with(any_args).exactly(1)
1126         ot = @story2.t
1127         @story2.t = 0
1128         @story2.lesser_shift ot
1129       end
1130     end
1131     context 'テーブルに2件(t:0,1)で1を0に移動したとき' do
1132       before do
1133         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1134         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1135         @ot = @story2.t
1136         @story2.t = 0
1137       end
1138       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
1139         #移動させたい行はそのまま残る
1140         @story2.lesser_shift @ot
1141         l = Story.find(:all).map {|s| s.t }
1142         l.sort.should eq [1, 1]
1143       end
1144       it '既存のt0を1にシフトしている' do
1145         @story2.lesser_shift @ot
1146         @story.reload
1147         @story.t.should eq 1
1148       end
1149     end
1150     context 'テーブルに3件(t:0,1,2)で2を1に移動したとき' do
1151       before do
1152         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1153         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1154         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1155         @ot = @story3.t
1156         @story3.t = 1
1157       end
1158       it '既存のt1を2にシフトしてこれから挿入するt(1)が欠番になっている' do
1159         #移動させたい行はそのまま残る
1160         @story3.lesser_shift @ot
1161         l = Story.find(:all).map {|s| s.t }
1162         l.sort.should eq [0, 2, 2]
1163       end
1164       it '既存のt1を2にシフトしている' do
1165         @story3.lesser_shift @ot
1166         @story2.reload
1167         @story2.t.should eq 2
1168       end
1169     end
1170     context 'テーブルに5件(t:0,1,2,3,4)で3を1に移動したとき' do
1171       before do
1172         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1173         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1174         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1175         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1176         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1177         @ot = @story4.t
1178         @story4.t = 1
1179       end
1180       it 'これから挿入するt(1)が欠番になっている' do
1181         #移動させたい行はそのまま残る
1182         @story4.lesser_shift @ot
1183         l = Story.find(:all).map {|s| s.t }
1184         l.sort.should eq [0, 2, 3, 3, 4]
1185       end
1186       it '既存のt0には変化がない' do
1187         @story4.lesser_shift @ot
1188         @story.reload
1189         @story.t.should eq 0
1190       end
1191       it '既存のt4には変化がない' do
1192         @story4.lesser_shift @ot
1193         @story5.reload
1194         @story5.t.should eq 4
1195       end
1196       it '既存のt1を2にシフトしている' do
1197         @story4.lesser_shift @ot
1198         @story2.reload
1199         @story2.t.should eq 2
1200       end
1201       it '既存のt2を3にシフトしている' do
1202         @story4.lesser_shift @ot
1203         @story3.reload
1204         @story3.t.should eq 3
1205       end
1206     end
1207     context '先ほどのケース+他のコミック1件で挿入したとき' do
1208       before do
1209         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1210         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1211         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1212         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1213         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1214         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1215         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1216         @ot = @story4.t
1217         @story4.t = 1
1218       end
1219       it 'これから挿入するt(1)が欠番になっている' do
1220         @story4.lesser_shift @ot
1221         l = Story.find(:all).map {|s| s.t }
1222         l.sort.should eq [0, 0, 2, 3, 3, 4]
1223       end
1224       it '既存のt0には変化がない' do
1225         @story4.lesser_shift @ot
1226         @story.reload
1227         @story.t.should eq 0
1228       end
1229       it '既存のt4には変化がない' do
1230         @story4.lesser_shift @ot
1231         @story5.reload
1232         @story5.t.should eq 4
1233       end
1234       it '既存のt1を2にシフトしている' do
1235         @story4.lesser_shift @ot
1236         @story2.reload
1237         @story2.t.should eq 2
1238       end
1239       it '既存のt2を3にシフトしている' do
1240         @story4.lesser_shift @ot
1241         @story3.reload
1242         @story3.t.should eq 3
1243       end
1244       it '他のコミックに影響がない' do
1245         @story4.lesser_shift @ot
1246         @storyc2.reload
1247         @storyc2.t.should eq 0
1248       end
1249     end
1250     #例外ケース。
1251     #負のときは0として正常扱い
1252     context 'テーブルに2件(t:0,1)で1を-1に移動したとき' do
1253       before do
1254         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1255         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1256         @ot = @story2.t
1257         @story2.t = -1
1258       end
1259       it '既存のt0を1にシフトしてこれから挿入するt(0)が欠番になっている' do
1260         #移動させたい行はそのまま残る
1261         @story2.lesser_shift @ot
1262         l = Story.find(:all).map {|s| s.t }
1263         l.sort.should eq [1, 1]
1264       end
1265       it '既存のt0を1にシフトしている' do
1266         @story2.lesser_shift @ot
1267         @story.reload
1268         @story.t.should eq 1
1269       end
1270       it '既存のt1は0に補正されている' do
1271         @story2.lesser_shift @ot
1272         @story2.t.should eq 0
1273       end
1274     end
1275   end
1276   describe '大きい方に移動に於いて' do
1277     before do
1278       @comic = FactoryGirl.create :comic, :author_id => @author.id
1279       @panel = FactoryGirl.create :panel, :author_id => @author.id
1280     end
1281     context '依頼チェック' do
1282       it 'Updateを依頼している' do
1283         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1284         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1285         Story.stub(:update_all).with(any_args)
1286         Story.should_receive(:update_all).with(any_args).exactly(1)
1287         ot = @story.t
1288         @story.t = 1
1289         @story.higher_shift ot
1290       end
1291     end
1292     context 'テーブルに2件(t:0,1)で0を1に移動したとき' do
1293       before do
1294         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1295         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1296         @ot = @story.t
1297         @story.t = 1
1298       end
1299       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1300         #移動させたい行はそのまま残る
1301         @story.higher_shift @ot
1302         l = Story.find(:all).map {|s| s.t }
1303         l.sort.should eq [0, 0]
1304       end
1305       it '既存のt1を0にシフトしている' do
1306         @story.higher_shift @ot
1307         @story2.reload
1308         @story2.t.should eq 0
1309       end
1310     end
1311     context 'テーブルに3件(t:0,1,2)で0を1に移動したとき' do
1312       before do
1313         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1314         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1315         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1316         @ot = @story.t
1317         @story.t = 1
1318       end
1319       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1320         #移動させたい行はそのまま残る
1321         @story.higher_shift @ot
1322         l = Story.find(:all).map {|s| s.t }
1323         l.sort.should eq [0, 0, 2]
1324       end
1325       it '既存のt1を0にシフトしている' do
1326         @story.higher_shift @ot
1327         @story2.reload
1328         @story2.t.should eq 0
1329       end
1330     end
1331     context 'テーブルに5件(t:0,1,2,3,4)で1を3に移動したとき' do
1332       before do
1333         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1334         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1335         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1336         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1337         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1338         @ot = @story2.t
1339         @story2.t = 3
1340       end
1341       it 'これから挿入するt(3)が欠番になっている' do
1342         #移動させたい行はそのまま残る
1343         @story2.higher_shift @ot
1344         l = Story.find(:all).map {|s| s.t }
1345         l.sort.should eq [0, 1, 1, 2, 4]
1346       end
1347       it '既存のt0には変化がない' do
1348         @story2.higher_shift @ot
1349         @story.reload
1350         @story.t.should eq 0
1351       end
1352       it '既存のt4には変化がない' do
1353         @story2.higher_shift @ot
1354         @story5.reload
1355         @story5.t.should eq 4
1356       end
1357       it '既存のt2を1にシフトしている' do
1358         @story2.higher_shift @ot
1359         @story3.reload
1360         @story3.t.should eq 1
1361       end
1362       it '既存のt3を2にシフトしている' do
1363         @story2.higher_shift @ot
1364         @story4.reload
1365         @story4.t.should eq 2
1366       end
1367     end
1368     context '先ほどのケース+他のコミック1件で挿入したとき' do
1369       before do
1370         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1371         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1372         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1373         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1374         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1375         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1376         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1377         @ot = @story2.t
1378         @story2.t = 3
1379       end
1380       it 'これから挿入するt(3)が欠番になっている' do
1381         #移動させたい行はそのまま残る
1382         @story2.higher_shift @ot
1383         l = Story.find(:all).map {|s| s.t }
1384         l.sort.should eq [0, 0, 1, 1, 2, 4]
1385       end
1386       it '既存のt0には変化がない' do
1387         @story2.higher_shift @ot
1388         @story.reload
1389         @story.t.should eq 0
1390       end
1391       it '既存のt4には変化がない' do
1392         @story2.higher_shift @ot
1393         @story5.reload
1394         @story5.t.should eq 4
1395       end
1396       it '既存のt2を1にシフトしている' do
1397         @story2.higher_shift @ot
1398         @story3.reload
1399         @story3.t.should eq 1
1400       end
1401       it '既存のt3を2にシフトしている' do
1402         @story2.higher_shift @ot
1403         @story4.reload
1404         @story4.t.should eq 2
1405       end
1406       it '他のコミックに影響がない' do
1407         @story2.higher_shift @ot
1408         @storyc2.reload
1409         @storyc2.t.should eq 0
1410       end
1411     end
1412     #例外ケース。
1413     #max超えたときはmaxとして正常扱い
1414     context 'テーブルに2件(t:0,1)で0を2に移動したとき' do
1415       before do
1416         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1417         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1418         @ot = @story.t
1419         @story.t = 2
1420       end
1421       it '既存のt1を0にシフトしてこれから挿入するt(1)が欠番になっている' do
1422         #移動させたい行はそのまま残る
1423         @story.higher_shift @ot
1424         l = Story.find(:all).map {|s| s.t }
1425         l.sort.should eq [0, 0]
1426       end
1427       it '既存のt1を0にシフトしている' do
1428         @story.higher_shift @ot
1429         @story2.reload
1430         @story2.t.should eq 0
1431       end
1432       it '既存のt0は1に補正されている' do
1433         @story.higher_shift @ot
1434         @story.t.should eq 1
1435       end
1436     end
1437   end
1438   describe '入れ替えに於いて' do
1439     before do
1440       @comic = FactoryGirl.create :comic, :author_id => @author.id
1441       @panel = FactoryGirl.create :panel, :author_id => @author.id
1442       @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1443       @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1444     end
1445     context '新tが旧tより小さいとき' do
1446       it '少ない方に移動を依頼している' do
1447         Story.any_instance.stub(:lesser_shift).with(any_args)
1448         Story.any_instance.should_receive(:lesser_shift).with(any_args).exactly(1)
1449         ot = @story2.t
1450         @story2.t = 0
1451         @story2.update_shift ot
1452       end
1453     end
1454     context '新tが旧tより大きいとき' do
1455       it '大きい方に移動を依頼している' do
1456         Story.any_instance.stub(:higher_shift).with(any_args)
1457         Story.any_instance.should_receive(:higher_shift).with(any_args).exactly(1)
1458         ot = @story.t
1459         @story.t = 1
1460         @story.update_shift ot
1461       end
1462     end
1463   end
1464   describe '順序入れ替えに於いて' do
1465     before do
1466       @comic = FactoryGirl.create :comic, :author_id => @author.id
1467       @panel = FactoryGirl.create :panel, :author_id => @author.id
1468     end
1469     context 'オブジェクトが新規でtが空のとき' do
1470       it '末尾追加としてtを補充依頼している' do
1471         @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1472         Story.stub(:new_t).with(any_args).and_return(0)
1473         Story.should_receive(:new_t).with(any_args).exactly(1)
1474         @story.t = nil
1475         r = @story.rotate
1476       end
1477     end
1478     context 'オブジェクトが新規でtが設定されているとき' do
1479       it '挿入追加として挿入シフトを依頼している' do
1480         @story = FactoryGirl.build :story, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1481         Story.any_instance.stub(:insert_shift).with(any_args)
1482         Story.any_instance.should_receive(:insert_shift).with(any_args).exactly(1)
1483         @story.t = 0
1484         r = @story.rotate
1485       end
1486     end
1487     context 'オブジェクトが新規でなくtが設定されているとき' do
1488       it 'コマ移動として入れ替えを依頼している' do
1489         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1490         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1491         Story.any_instance.stub(:update_shift).with(any_args)
1492         Story.any_instance.should_receive(:update_shift).with(1).exactly(1)
1493         @story2.t = 0
1494         r = @story.rotate 1
1495       end
1496     end
1497     context 'オブジェクトが新規でなくtが空のとき' do
1498       it '入れ替えもシフトもせず、tを空のままにしている' do
1499         #結果、tに欠番が生じてシリアライズチェックでひっかかる
1500       end
1501     end
1502   end
1503   describe '編集許可に於いて' do
1504     before do
1505       @comic = FactoryGirl.create :comic, :author_id => @author.id
1506       @panel = FactoryGirl.create :panel, :author_id => @author.id
1507       @story = FactoryGirl.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1508     end
1509     context 'つつがなく終わるとき' do
1510       it 'trueを返す' do
1511         r = @story.allow?
1512         r.should be_true
1513       end
1514     end
1515     context 'コミックで引っかかるとき' do
1516       it 'falseを返す' do
1517         Panel.any_instance.stub(:usable?).with(any_args).and_return(true)
1518         Comic.any_instance.stub(:own?).with(any_args).and_return(false)
1519         r = @story.allow?
1520         r.should be_false
1521       end
1522     end
1523     context 'コマで引っかかるとき' do
1524       it 'falseを返す' do
1525         Comic.any_instance.stub(:own?).with(any_args).and_return(true)
1526         Panel.any_instance.stub(:usable?).with(any_args).and_return(false)
1527         r = @story.allow?
1528         r.should be_false
1529       end
1530     end
1531     context 'コミックまたはコマが指定されていなかったとき' do
1532       it 'nilを返す' do
1533         Comic.any_instance.stub(:own?).with(any_args).and_return(true)
1534         @story.panel_id = nil
1535         r = @story.allow?
1536         r.should eq nil
1537       end
1538       it 'nilを返す' do
1539         Panel.any_instance.stub(:usable?).with(any_args).and_return(true)
1540         @story.comic_id = nil
1541         r = @story.allow?
1542         r.should eq nil
1543       end
1544     end
1545   end
1546   describe '保存に於いて' do
1547     before do
1548       @comic = FactoryGirl.create :comic, :author_id => @author.id
1549       @panel = FactoryGirl.create :panel, :author_id => @author.id
1550       @story = FactoryGirl.build :story, :t => nil, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1551     end
1552     context 'つつがなく終わるとき' do
1553       it '編集許可チェックを依頼している' do
1554         Story.any_instance.stub(:allow?).with(any_args).and_return(true)
1555         Story.any_instance.should_receive(:allow?).with(any_args).exactly(1)
1556         r = @story.store
1557       end
1558       it '順序入れ替えを依頼している' do
1559         Story.any_instance.stub(:rotate).with(any_args).and_return(0)
1560         Story.any_instance.should_receive(:rotate).with(any_args).exactly(1)
1561         Story.any_instance.stub(:save).with(any_args).and_return(true)
1562         Story.stub(:validate_t).with(any_args).and_return(true)
1563         r = @story.store 
1564       end
1565       it '保存を依頼している' do
1566         Story.stub(:new_t).with(any_args).and_return(0)
1567         Story.any_instance.stub(:save).with(any_args).and_return(true)
1568         Story.any_instance.should_receive(:save).with(any_args).exactly(1)
1569         Story.stub(:validate_t).with(any_args).and_return(true)
1570         r = @story.store
1571       end
1572       it 'tのシリアライズチェックを依頼している' do
1573         Story.stub(:new_t).with(any_args).and_return(0)
1574         Story.any_instance.stub(:save).with(any_args).and_return(true)
1575         Story.stub(:validate_t).with(any_args).and_return(true)
1576         Story.should_receive(:validate_t).with(any_args).exactly(1)
1577         r = @story.store
1578       end
1579     end
1580     #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
1581     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したとき' do
1582       before do
1583         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1584         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1585         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1586         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1587         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1588         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1589         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1590         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1591       end
1592       it '既存のt0には変化がない' do
1593         @story6.store
1594         @story.reload
1595         @story.t.should eq 0
1596       end
1597       it '既存のt1には変化がない' do
1598         @story6.store
1599         @story2.reload
1600         @story2.t.should eq 1
1601       end
1602       it '既存のt2を3にシフトしている' do
1603         @story6.store
1604         @story3.reload
1605         @story3.t.should eq 3
1606       end
1607       it '既存のt3を4にシフトしている' do
1608         @story6.store
1609         @story4.reload
1610         @story4.t.should eq 4
1611       end
1612       it '既存のt5を5にシフトしている' do
1613         @story6.store
1614         @story5.reload
1615         @story5.t.should eq 5
1616       end
1617       it '新規のt2が作成されている' do
1618         @story6.store
1619         @story6.reload
1620         @story6.t.should eq 2
1621       end
1622       it '他のコミックに影響がない' do
1623         @ot = @storyc2.t
1624         @story6.store
1625         @storyc2.reload
1626         @storyc2.t.should eq @ot
1627       end
1628     end
1629     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したとき' do
1630       before do
1631         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1632         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1633         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1634         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1635         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1636         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1637         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1638         @ot = @story4.t
1639         @story4.t = 1
1640       end
1641       it '既存のt0には変化がない' do
1642         @story4.store @ot
1643         @story.reload
1644         @story.t.should eq 0
1645       end
1646       it '既存のt4には変化がない' do
1647         @story4.store @ot
1648         @story5.reload
1649         @story5.t.should eq 4
1650       end
1651       it '既存のt1を2にシフトしている' do
1652         @story4.store @ot
1653         @story2.reload
1654         @story2.t.should eq 2
1655       end
1656       it '既存のt2を3にシフトしている' do
1657         @story4.store @ot
1658         @story3.reload
1659         @story3.t.should eq 3
1660       end
1661       it '既存のt3を1にシフトしている' do
1662         @story4.store @ot
1663         @story4.reload
1664         @story4.t.should eq 1
1665       end
1666       it '他のコミックに影響がない' do
1667         @story4.store @ot
1668         @storyc2.reload
1669         @storyc2.t.should eq 0
1670       end
1671     end
1672     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で1を3に移動したとき' do
1673       before do
1674         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1675         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1676         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1677         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1678         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1679         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1680         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1681         @ot = @story2.t
1682         @story2.t = 3
1683       end
1684       it '既存のt0には変化がない' do
1685         @story2.store @ot
1686         @story.reload
1687         @story.t.should eq 0
1688       end
1689       it '既存のt4には変化がない' do
1690         @story2.store @ot
1691         @story5.reload
1692         @story5.t.should eq 4
1693       end
1694       it '既存のt1を3にシフトしている' do
1695         @story2.store @ot
1696         @story2.reload
1697         @story2.t.should eq 3
1698       end
1699       it '既存のt2を1にシフトしている' do
1700         @story2.store @ot
1701         @story3.reload
1702         @story3.t.should eq 1
1703       end
1704       it '既存のt3を2にシフトしている' do
1705         @story2.store @ot
1706         @story4.reload
1707         @story4.t.should eq 2
1708       end
1709       it '他のコミックに影響がない' do
1710         @story2.store @ot
1711         @storyc2.reload
1712         @storyc2.t.should eq 0
1713       end
1714     end
1715     #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1716     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で2に挿入したが保存に失敗したとき' do
1717       before do
1718         Story.any_instance.stub(:save).with(any_args).and_return(false)
1719         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1720         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1721         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1722         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1723         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1724         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1725         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1726         @story6 = FactoryGirl.build :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1727       end
1728       it '既存のtに変化がない' do
1729         @story6.store
1730         @story.reload
1731         @story.t.should eq 0
1732         @story2.reload
1733         @story2.t.should eq 1
1734         @story3.reload
1735         @story3.t.should eq 2
1736         @story4.reload
1737         @story4.t.should eq 3
1738         @story5.reload
1739         @story5.t.should eq 4
1740         @storyc2.reload
1741         @storyc2.t.should eq 0
1742       end
1743       it 'falseを返す' do
1744         r = @story6.store
1745         r.should be_false
1746       end
1747     end
1748     context 'テーブルに5件(t:0,1,2,3,4)+他のコミック1件で3を1に移動したがシリアルチェックに失敗したとき' do
1749       before do
1750         Story.stub(:validate_t).with(any_args).and_return(false)
1751         @comic2 = FactoryGirl.create :comic, :author_id => @author.id
1752         @storyc2 = FactoryGirl.create :story, :t => 0, :comic_id => @comic2.id, :panel_id => @panel.id, :author_id => @author.id
1753         @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1754         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1755         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1756         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1757         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1758         @ot = @story4.t
1759         @story4.t = 1
1760       end
1761       it '既存のtに変化がない' do
1762         @story4.store @ot
1763         @story.reload
1764         @story.t.should eq 0
1765         @story2.reload
1766         @story2.t.should eq 1
1767         @story3.reload
1768         @story3.t.should eq 2
1769         @story4.reload
1770         @story4.t.should eq 3
1771         @story5.reload
1772         @story5.t.should eq 4
1773         @storyc2.reload
1774         @storyc2.t.should eq 0
1775       end
1776       it 'falseを返す' do
1777         r = @story4.store @ot
1778         r.should be_false
1779       end
1780       it 'tにエラーメッセージが入っている' do
1781         @story4.store @ot
1782         @story4.errors[:t].should_not be_empty
1783         @story4.valid?.should be_true
1784       end
1785     end
1786     context '編集不可だったとき' do
1787       before do
1788         @story = FactoryGirl.build :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1789         Story.any_instance.stub(:allow?).and_return(false)
1790       end
1791       it '403Forbidden例外を返す' do
1792         lambda{
1793           @story.store
1794         }.should raise_error(ActiveRecord::Forbidden)
1795       end
1796     end
1797   end
1798   describe '切り詰め処理つき削除に於いて' do
1799     before do
1800       @comic = FactoryGirl.create :comic, :author_id => @author.id
1801       @panel = FactoryGirl.create :panel, :author_id => @author.id
1802       @story = FactoryGirl.create :story, :t => 0, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1803     end
1804     context 'つつがなく終わるとき' do
1805       it '削除される' do
1806         lambda{
1807           @story.destroy_and_shorten
1808         }.should change(Story, :count ).by(-1)
1809       end
1810       it 'Trueを返す' do
1811         r = @story.destroy_and_shorten
1812         r.should be_true 
1813       end
1814     end
1815     context '削除に失敗したとき' do
1816       before do
1817         Story.any_instance.stub(:destroy).and_return(false)
1818       end
1819       it 'ロールバックされる' do
1820         lambda{
1821           @story.destroy_and_shorten
1822         }.should_not change(Story, :count )
1823       end
1824       it 'Falseを返す' do
1825         r = @story.destroy_and_shorten
1826         r.should be_false
1827       end
1828     end
1829     #連携テスト。切り詰めが直接DBをいじる
1830     context '2件で先頭を削除したとき' do
1831       before do
1832         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1833       end
1834       it '行が削除される' do
1835         lambda{
1836           @story.destroy_and_shorten
1837         }.should change(Story, :count ).by(-1)
1838       end
1839       it '先頭は削除される' do
1840         @story.destroy_and_shorten
1841         lambda{
1842           Story.find @story.id
1843         }.should raise_error(ActiveRecord::RecordNotFound)
1844       end
1845       it '2件目は前に詰められる' do
1846         @story.destroy_and_shorten
1847         @story2.reload
1848         @story2.t.should eq 0
1849       end
1850     end
1851     context '3件で先頭を削除したとき' do
1852       before do
1853         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1854         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1855       end
1856       it '行が削除される' do
1857         lambda{
1858           @story.destroy_and_shorten
1859         }.should change(Story, :count ).by(-1)
1860       end
1861       it '先頭は削除される' do
1862         @story.destroy_and_shorten
1863         lambda{
1864           Story.find @story.id
1865         }.should raise_error(ActiveRecord::RecordNotFound)
1866       end
1867       it '2件目は前に詰められる' do
1868         @story.destroy_and_shorten
1869         @story2.reload
1870         @story2.t.should eq 0
1871       end
1872       it '3件目は前に詰められる' do
1873         @story.destroy_and_shorten
1874         @story3.reload
1875         @story3.t.should eq 1
1876       end
1877     end
1878     context '5件で3件目を削除したとき' do
1879       before do
1880         @story2 = FactoryGirl.create :story, :t => 1, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1881         @story3 = FactoryGirl.create :story, :t => 2, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1882         @story4 = FactoryGirl.create :story, :t => 3, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1883         @story5 = FactoryGirl.create :story, :t => 4, :comic_id => @comic.id, :panel_id => @panel.id, :author_id => @author.id
1884       end
1885       it '行が削除される' do
1886         lambda{
1887           @story3.destroy_and_shorten
1888         }.should change(Story, :count ).by(-1)
1889       end
1890       it '1件目は変化がない' do
1891         @story3.destroy_and_shorten
1892         @story.reload
1893         @story.t.should eq 0
1894       end
1895       it '2件目は変化がない' do
1896         @story3.destroy_and_shorten
1897         @story2.reload
1898         @story2.t.should eq 1
1899       end
1900       it '3件目は削除される' do
1901         @story3.destroy_and_shorten
1902         lambda{
1903           Story.find @story3.id
1904         }.should raise_error(ActiveRecord::RecordNotFound)
1905       end
1906       it '4件目は前に詰められる' do
1907         @story3.destroy_and_shorten
1908         @story4.reload
1909         @story4.t.should eq 2
1910       end
1911       it '5件目は前に詰められる' do
1912         @story3.destroy_and_shorten
1913         @story5.reload
1914         @story5.t.should eq 3
1915       end
1916     end
1917     #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
1918   end
1919 end