OSDN Git Service

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