OSDN Git Service

t#32046:
[pettanr/pettanr.git] / spec / models / sheet_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #用紙
4
5 describe Sheet do
6   before do
7     SpeechBalloonTemplate.delete_all
8     @admin = FactoryGirl.create :admin
9     @demand_user = FactoryGirl.create :demand_user
10     @sp = FactoryGirl.create :system_picture
11     @lg = FactoryGirl.create :license_group
12     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
13     @user = FactoryGirl.create( :user_yas)
14     @author = FactoryGirl.create :author, :user_id => @user.id
15     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
16     @other_user = FactoryGirl.create( :user_yas)
17     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
18     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
19   end
20   
21   describe '検証に於いて' do
22     before do
23       @sheet = FactoryGirl.build :sheet, :author_id => @author.id
24     end
25     
26     context 'オーソドックスなデータのとき' do
27       it '下限データが通る' do
28         @sheet.caption = 'a'
29         @sheet.visible = 0
30         @sheet.should be_valid
31       end
32       it '上限データが通る' do
33         @sheet.caption = 'a'*100
34         @sheet.visible = 1
35         @sheet.should be_valid
36       end
37     end
38     
39     context 'captionを検証するとき' do
40       it 'nullなら失敗する' do
41         @sheet.caption = nil
42         @sheet.should_not be_valid
43       end
44       it '100文字以上なら失敗する' do
45         @sheet.caption = 'a'*101
46         @sheet.should_not be_valid
47       end
48     end
49     context 'visibleを検証するとき' do
50       it 'nullなら失敗する' do
51         @sheet.visible = nil
52         @sheet.should_not be_valid
53       end
54       it '負なら失敗する' do
55         @sheet.visible = -1
56         @sheet.should_not be_valid
57       end
58       it '2以上なら失敗する' do
59         @sheet.visible = 2
60         @sheet.should_not be_valid
61       end
62     end
63   end
64   
65   describe '文字コード検証に於いて' do
66     before do
67       @sheet = FactoryGirl.build :sheet, :author_id => @author.id
68     end
69     
70     context 'captionを検証するとき' do
71       it 'Shift JISなら失敗する' do
72         @sheet.caption = "\x83G\x83r\x83]\x83D"
73         lambda{
74           @sheet.valid_encode
75         }.should raise_error(Pettanr::BadRequest)
76       end
77     end
78     
79   end
80   
81   describe 'デフォルト値補充に於いて' do
82     it 'visibleが0になっている' do
83       @sheet = FactoryGirl.build :sheet, :visible => nil
84       @sheet.supply_default
85       @sheet.visible.should eq 0
86     end
87   end
88   
89   describe '上書き補充に於いて' do
90     it '作家idが設定されている' do
91       @sheet = FactoryGirl.build :sheet, :author_id => nil
92       @sheet.overwrite @author
93       @sheet.author_id.should eq @author.id
94     end
95   end
96   
97   describe '所持判定に於いて' do
98     before do
99       @sheet = FactoryGirl.build :sheet, :author_id => @author.id
100     end
101     context '事前チェックする' do
102       it '自身にロールリストからの作家取得を依頼している' do
103         Sheet.should_receive(:get_author_from_roles).with(any_args).exactly(1)
104         r = @sheet.own?([@author])
105       end
106     end
107     context 'ロール内作家が取得できるとき' do
108       before do
109       end
110       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
111         Sheet.stub(:get_author_from_roles).with(any_args).and_return(@author)
112         r = @sheet.own?([@author])
113         r.should be_true
114       end
115       it 'ロール内作家のidが自身の作家idと一致しないならno' do
116         Sheet.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
117         @sheet.own?(@other_author).should be_false
118       end
119     end
120     context 'ロール内作家が取得できないとき' do
121       before do
122         Sheet.stub(:get_author_from_roles).with(any_args).and_return(nil)
123       end
124       it 'Falseを返す' do
125         r = @sheet.own?([@author])
126         r.should be_false
127       end
128     end
129   end
130   
131   describe '閲覧許可に於いて' do
132     before do
133       @sheet = FactoryGirl.build :sheet, :author_id => @author.id
134     end
135     context 'オープンモードのとき' do
136       before do
137         MagicNumber['run_mode'] = 0
138       end
139       it '自身にゲスト用ロールチェックを問い合わせしている' do
140         Sheet.any_instance.stub(:guest_role_check).and_return(true)
141         Sheet.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
142         r = @sheet.visible?([@author])
143       end
144       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
145         Sheet.any_instance.stub(:guest_role_check).and_return(false)
146         r = @sheet.visible?([@author])
147         r.should be_false
148       end
149     end
150     context 'クローズドモードのとき' do
151       before do
152         MagicNumber['run_mode'] = 1
153       end
154       it '自身に読者用ロールチェックを問い合わせしている' do
155         Sheet.any_instance.stub(:reader_role_check).and_return(true)
156         Sheet.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
157         r = @sheet.visible?([@author])
158       end
159       it '読者用ロールチェックが失敗したとき、falseを返す' do
160         Sheet.any_instance.stub(:reader_role_check).and_return(false)
161         r = @sheet.visible?([@author])
162         r.should be_false
163       end
164     end
165     context '事前チェックする' do
166       before do
167         MagicNumber['run_mode'] = 1
168         Sheet.any_instance.stub(:reader_role_check).and_return(true)
169         Sheet.any_instance.stub(:own?).and_return(true)
170       end
171       it '自身に所持判定を問い合わせしている' do
172         Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
173         r = @sheet.visible?([@author])
174       end
175     end
176     context 'つつがなく終わるとき' do
177       before do
178         MagicNumber['run_mode'] = 1
179         Sheet.any_instance.stub(:reader_role_check).and_return(true)
180       end
181       it '自分の用紙なら許可する' do
182         Sheet.any_instance.stub(:own?).and_return(true)
183         Sheet.any_instance.stub(:visible).and_return(0)
184         r = @sheet.visible?([@author])
185         r.should be_true
186       end
187       it '他人の非公開用紙なら許可しない' do
188         Sheet.any_instance.stub(:own?).and_return(false)
189         Sheet.any_instance.stub(:visible).and_return(0)
190         r = @sheet.visible?([@author])
191         r.should be_false
192       end
193       it '他人の用紙でも公開なら許可する' do
194         Sheet.any_instance.stub(:own?).and_return(false)
195         Sheet.any_instance.stub(:visible).and_return(1)
196         r = @sheet.visible?([@author])
197         r.should be_true
198       end
199     end
200   end
201   
202   describe '一覧取得に於いて' do
203     before do
204       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
205     end
206     context 'page補正について' do
207       it '文字列から数値に変換される' do
208         Sheet.page('8').should eq 8
209       end
210       it 'nilの場合は1になる' do
211         Sheet.page().should eq 1
212       end
213       it '0以下の場合は1になる' do
214         Sheet.page('0').should eq 1
215       end
216     end
217     context 'page_size補正について' do
218       it '文字列から数値に変換される' do
219         Sheet.page_size('7').should eq 7
220       end
221       it 'nilの場合はSheet.default_page_sizeになる' do
222         Sheet.page_size().should eq Sheet.default_page_size
223       end
224       it '0以下の場合はSheet.default_page_sizeになる' do
225         Sheet.page_size('0').should eq Sheet.default_page_size
226       end
227       it 'Sheet.max_page_sizeを超えた場合はSheet.max_page_sizeになる' do
228         Sheet.page_size('1000').should eq Sheet.max_page_size
229       end
230     end
231     context 'つつがなく終わるとき' do
232       it '一覧取得オプションを利用している' do
233         Sheet.stub(:list_opt).with(any_args).and_return({})
234         Sheet.should_receive(:list_opt).with(any_args).exactly(1)
235         r = Sheet.list
236       end
237     end
238     it 'リストを返す' do
239       c = Sheet.list
240       c.should eq [@sheet]
241     end
242     it '非公開用紙は(自分の用紙であっても)含んでいない' do
243       FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0
244       c = Sheet.list
245       c.should eq [@sheet]
246     end
247     it '時系列で並んでいる' do
248       #公開用紙は(他人の用紙であっても)含んでいる
249       v = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
250       c = Sheet.list
251       c.should eq [v, @sheet]
252     end
253     context 'DBに5件あって1ページの件数を2件に変えたとして' do
254       before do
255         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
256         @sheet3 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 200
257         @sheet4 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 300
258         @sheet5 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 400
259         Sheet.stub(:default_page_size).and_return(2)
260       end
261       it '通常は2件を返す' do
262         c = Sheet.list
263         c.should have(2).items 
264       end
265       it 'page=1なら末尾2件を返す' do
266         #時系列で並んでいる
267         c = Sheet.list(1)
268         c.should eq [@sheet5, @sheet4]
269       end
270       it 'page=2なら中間2件を返す' do
271         c = Sheet.list(2)
272         c.should eq [@sheet3, @sheet2]
273       end
274       it 'page=3なら先頭1件を返す' do
275         c = Sheet.list(3)
276         c.should eq [@sheet]
277       end
278     end
279   end
280   
281   describe '自分の用紙一覧取得に於いて' do
282     before do
283       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
284     end
285     context 'つつがなく終わるとき' do
286       it '一覧取得オプションを利用している' do
287         Sheet.stub(:list_opt).with(any_args).and_return({})
288         Sheet.should_receive(:list_opt).with(any_args).exactly(1)
289         r = Sheet.mylist @author
290       end
291     end
292     it 'リストを返す' do
293       c = Sheet.mylist @author
294       c.should eq [@sheet]
295     end
296     it '時系列で並んでいる' do
297       nc = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
298       cl = Sheet.mylist @author
299       cl.should eq [nc, @sheet]
300     end
301     it '他人の用紙は公開でも含まない' do
302       nc = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
303       cl = Sheet.mylist @author
304       cl.should eq [@sheet]
305     end
306     it '自分の用紙は非公開でも含んでいる' do
307       nc = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
308       cl = Sheet.mylist @author
309       cl.should eq [nc, @sheet]
310     end
311     context 'DBに5件あって1ページの件数を2件に変えたとして' do
312       before do
313         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
314         @sheet3 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 200
315         @sheet4 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 300
316         @sheet5 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 400
317       end
318       it '通常は2件を返す' do
319         c = Sheet.mylist @author, 1, 2
320         c.should have(2).items 
321       end
322       it 'page=1なら末尾2件を返す' do
323         #時系列で並んでいる
324         c = Sheet.mylist(@author, 1, 2)
325         c.should eq [@sheet5, @sheet4]
326       end
327       it 'page=2なら中間2件を返す' do
328         c = Sheet.mylist(@author, 2, 2)
329         c.should eq [@sheet3, @sheet2]
330       end
331       it 'page=3なら先頭1件を返す' do
332         c = Sheet.mylist(@author, 3, 2)
333         c.should eq [@sheet]
334       end
335     end
336   end
337   
338   describe '他作家の用紙一覧取得に於いて' do
339     before do
340       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
341       @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
342     end
343     context 'つつがなく終わるとき' do
344       it '一覧取得オプションを利用している' do
345         Sheet.stub(:list_opt).with(any_args).and_return({})
346         Sheet.should_receive(:list_opt).with(any_args).exactly(1)
347         r = Sheet.himlist @other_author
348       end
349     end
350     it '指定した作家のリストを返す' do
351       r = Sheet.himlist @other_author
352       r.should eq [@other_sheet]
353     end
354     it '時系列で並んでいる' do
355       nc = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
356       r = Sheet.himlist @other_author
357       r.should eq [nc, @other_sheet]
358     end
359     it '公開用紙に限る ' do
360       hidden = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 0
361       r = Sheet.himlist @other_author
362       r.should eq [@other_sheet]
363     end
364     context 'DBに5件あって1ページの件数を2件に変えたとして' do
365       before do
366         @other_sheet2 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
367         @other_sheet3 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 200
368         @other_sheet4 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 300
369         @other_sheet5 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 400
370       end
371       it '通常は2件を返す' do
372         c = Sheet.himlist @other_author, 1, 2
373         c.should have(2).items 
374       end
375       it 'page=1なら末尾2件を返す' do
376         #時系列で並んでいる
377         c = Sheet.himlist(@other_author, 1, 2)
378         c.should eq [@other_sheet5, @other_sheet4]
379       end
380       it 'page=2なら中間2件を返す' do
381         c = Sheet.himlist(@other_author, 2, 2)
382         c.should eq [@other_sheet3, @other_sheet2]
383       end
384       it 'page=3なら先頭1件を返す' do
385         c = Sheet.himlist(@other_author, 3, 2)
386         c.should eq [@other_sheet]
387       end
388     end
389   end
390   
391   describe '用紙一覧ページ制御に於いて' do
392     before do
393       Sheet.stub(:count).with(any_args).and_return(100)
394     end
395     it 'ページ制御を返す' do
396       r = Sheet.list_paginate 
397       r.is_a?(Kaminari::PaginatableArray).should be_true
398     end
399     it '用紙一覧の取得条件を利用している' do
400       Sheet.stub(:list_where).with(any_args).and_return('')
401       Sheet.should_receive(:list_where).with(any_args).exactly(1)
402       r = Sheet.list_paginate 
403     end
404     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
405       r = Sheet.list_paginate 3, 10
406       r.limit_value.should eq 10
407       r.offset_value.should eq 20
408     end
409   end
410   
411   describe '自分の用紙一覧ページ制御に於いて' do
412     before do
413       Sheet.stub(:count).with(any_args).and_return(100)
414     end
415     it 'ページ制御を返す' do
416       r = Sheet.mylist_paginate @author
417       r.is_a?(Kaminari::PaginatableArray).should be_true
418     end
419     it '自分の用紙一覧の取得条件を利用している' do
420       Sheet.stub(:mylist_where).with(any_args).and_return('')
421       Sheet.should_receive(:mylist_where).with(any_args).exactly(1)
422       r = Sheet.mylist_paginate @author
423     end
424     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
425       r = Sheet.mylist_paginate @author, 3, 10
426       r.limit_value.should eq 10
427       r.offset_value.should eq 20
428     end
429   end
430   
431   describe '他作家の用紙一覧ページ制御に於いて' do
432     before do
433       Sheet.stub(:count).with(any_args).and_return(100)
434     end
435     it 'ページ制御を返す' do
436       r = Sheet.himlist_paginate @other_author
437       r.is_a?(Kaminari::PaginatableArray).should be_true
438     end
439     it '他作家の用紙一覧の取得条件を利用している' do
440       Sheet.stub(:himlist_where).with(any_args).and_return('')
441       Sheet.should_receive(:himlist_where).with(any_args).exactly(1)
442       r = Sheet.himlist_paginate @other_author
443     end
444     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
445       r = Sheet.himlist_paginate @other_author, 3, 10
446       r.limit_value.should eq 10
447       r.offset_value.should eq 20
448     end
449   end
450   
451   describe '一覧取得オプションに於いて' do
452     it '2つの項目を含んでいる' do
453       r = Sheet.list_opt
454       r.should have(2).items
455     end
456     it 'スクコマを含んでいる' do
457       r = Sheet.list_opt
458       r.has_key?(:sheet_panels).should be_true
459     end
460       it 'スクコマはコマを含んでいる' do
461         r = Sheet.list_opt
462         r[:sheet_panels].has_key?(:panel).should be_true
463       end
464     it '作家を含んでいる' do
465       r = Sheet.list_opt
466       r.has_key?(:author).should be_true
467     end
468   end
469   describe 'json一覧出力オプションに於いて' do
470     before do
471       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
472       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
473       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
474       @sbt = FactoryGirl.create :speech_balloon_template
475       @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
476       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
477       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
478     end
479     it 'スクコマを含んでいる' do
480       r = Sheet.list.to_json Sheet.list_json_opt
481       j = JSON.parse r
482       i = j.first
483       i.has_key?('sheet_panels').should be_true
484     end
485       it 'スクコマはコマを含んでいる' do
486         r = Sheet.list.to_json Sheet.list_json_opt
487         j = JSON.parse r
488         i = j.first
489         s = i['sheet_panels'].first
490         s.has_key?('panel').should be_true
491       end
492     it '作家を含んでいる' do
493       r = Sheet.list.to_json Sheet.list_json_opt
494       j = JSON.parse r
495       i = j.first
496       i.has_key?('author').should be_true
497     end
498   end
499   
500   describe '単体取得に於いて' do
501     before do
502       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
503     end
504     context 'つつがなく終わるとき' do
505       it '単体取得オプションを利用している' do
506         Sheet.stub(:show_opt).with(any_args).and_return({})
507         Sheet.should_receive(:show_opt).with(any_args).exactly(1)
508         r = Sheet.show @sheet.id, @author
509       end
510       it '閲覧許可を問い合わせている' do
511         Sheet.any_instance.stub(:visible?).with(any_args).and_return(true)
512         Sheet.any_instance.should_receive(:visible?).with(any_args).exactly(1)
513         r = Sheet.show @sheet.id, @author
514       end
515     end
516     it '指定の用紙を返す' do
517       c = Sheet.show @sheet.id, @author
518       c.should eq @sheet
519     end
520     context '閲覧許可が出なかったとき' do
521       it '403Forbidden例外を返す' do
522         Sheet.any_instance.stub(:visible?).and_return(false)
523         lambda{
524           Sheet.show @sheet.id, @author
525         }.should raise_error(ActiveRecord::Forbidden)
526       end
527     end
528     context '存在しない用紙を開こうとしたとき' do
529       it '404RecordNotFound例外を返す' do
530         lambda{
531           Sheet.show 110, @author
532         }.should raise_error(ActiveRecord::RecordNotFound)
533       end
534     end
535   end
536   
537   describe '編集取得に於いて' do
538     before do
539       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
540     end
541     context 'つつがなく終わるとき' do
542       it '単体取得オプションを利用している' do
543         Sheet.stub(:show_opt).with(any_args).and_return({})
544         Sheet.should_receive(:show_opt).with(any_args).exactly(1)
545         r = Sheet.edit @sheet.id, @author
546       end
547       it '所持判定を問い合わせている' do
548         Sheet.any_instance.stub(:own?).with(any_args).and_return(true)
549         Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
550         r = Sheet.edit @sheet.id, @author
551       end
552     end
553     it '指定の用紙を返す' do
554       Sheet.any_instance.stub(:own?).and_return(true)
555       c = Sheet.edit @sheet.id, @author.id
556       c.should eq @sheet
557     end
558     context '他人の用紙を開こうとしたとき' do
559       it '403Forbidden例外を返す' do
560         Sheet.any_instance.stub(:own?).and_return(false)
561         lambda{
562           Sheet.edit @sheet.id, @author
563         }.should raise_error(ActiveRecord::Forbidden)
564       end
565     end
566     context '存在しない用紙を開こうとしたとき' do
567       it '404RecordNotFound例外を返す' do
568         lambda{
569           Sheet.edit 110, @author
570         }.should raise_error(ActiveRecord::RecordNotFound)
571       end
572     end
573   end
574   describe '単体取得オプションに於いて' do
575     it 'includeキーを含んでいる' do
576       r = Sheet.show_opt
577       r.has_key?(:include).should be_true
578     end
579     it '2つの項目を含んでいる' do
580       r = Sheet.show_opt[:include]
581       r.should have(2).items
582     end
583     it '作家を含んでいる' do
584       r = Sheet.show_opt[:include]
585       r.has_key?(:author).should be_true
586     end
587     it 'スクコマを含んでいる' do
588       r = Sheet.show_opt[:include]
589       r.has_key?(:sheet_panels).should be_true
590     end
591       it 'スクコマはコマを含んでいる' do
592         r = Sheet.show_opt[:include]
593         r[:sheet_panels].has_key?(:panel).should be_true
594       end
595   end
596   describe 'json単体出力オプションに於いて' do
597     before do
598       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
599       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
600       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
601       @sbt = FactoryGirl.create :speech_balloon_template
602       @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
603       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
604       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
605     end
606     it 'スクコマを含んでいる' do
607       r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
608       j = JSON.parse r
609       i = j
610       i.has_key?('sheet_panels').should be_true
611     end
612       it 'スクコマはコマを含んでいる' do
613         r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
614         j = JSON.parse r
615         i = j
616         s = i['sheet_panels'].first
617         s.has_key?('panel').should be_true
618       end
619     it '作家を含んでいる' do
620       r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
621       j = JSON.parse r
622       i = j
623       i.has_key?('author').should be_true
624     end
625   end
626   
627   describe '削除に於いて' do
628     before do
629       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
630       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
631       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
632       @other_sheet = FactoryGirl.create :sheet, :author_id => @author.id
633       @other_sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @other_sheet.id, :panel_id => @panel.id
634     end
635     context 'つつがなく終わるとき' do
636       it '自身を削除する' do
637         lambda {
638           r = @sheet.destroy_with_sheet_panel
639         }.should change(Sheet, :count).by(-1)
640         lambda {
641           r = Sheet.find @sheet.id
642         }.should raise_error
643       end
644       it '自身にリンクしているスクコマをすべて削除する' do
645         lambda {
646           r = @sheet.destroy_with_sheet_panel
647         }.should change(SheetPanel, :count).by(-1)
648         lambda {
649           r = SheetPanel.find @sheet_panel.id
650         }.should raise_error
651       end
652       it 'Trueを返す' do
653         r = @sheet.destroy_with_sheet_panel
654         r.should be_true
655       end
656     end
657     context '削除に失敗したとき' do
658       before do
659         SheetPanel.any_instance.stub(:destroy).with(any_args).and_return(false)
660       end
661       it 'Falseを返す' do
662         r = @sheet.destroy_with_sheet_panel
663         r.should be_false
664       end
665       it 'ロールバックしている' do
666         lambda {
667           r = @sheet.destroy_with_sheet_panel
668         }.should_not change(Sheet, :count)
669         lambda {
670           r = @sheet.destroy_with_sheet_panel
671         }.should_not change(SheetPanel, :count)
672       end
673     end
674   end
675 end