OSDN Git Service

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