OSDN Git Service

add: social button
[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.width = 1
30         @sheet.height = 1
31         @sheet.visible = 0
32         @sheet.should be_valid
33       end
34       it '上限データが通る' do
35         @sheet.caption = 'a'*100
36         @sheet.width = 99999
37         @sheet.height = 99999
38         @sheet.visible = 1
39         @sheet.should be_valid
40       end
41     end
42     
43     context 'captionを検証するとき' do
44       it 'nullなら失敗する' do
45         @sheet.caption = nil
46         @sheet.should_not be_valid
47       end
48       it '100文字以上なら失敗する' do
49         @sheet.caption = 'a'*101
50         @sheet.should_not be_valid
51       end
52     end
53     context 'widthを検証するとき' do
54       it 'nullなら失敗する' do
55         @sheet.width = nil
56         @sheet.should_not be_valid
57       end
58       it '数値でなければ失敗する' do
59         @sheet.width = 'a'
60         @sheet.should_not be_valid
61       end
62       it '0なら失敗する' do
63         @sheet.width = '0'
64         @sheet.should_not be_valid
65       end
66       it '負でも失敗する' do
67         @sheet.width = -1
68         @sheet.should_not be_valid
69       end
70     end
71     context 'heightを検証するとき' do
72       it 'nullなら失敗する' do
73         @sheet.height = nil
74         @sheet.should_not be_valid
75       end
76       it '数値でなければ失敗する' do
77         @sheet.height = 'a'
78         @sheet.should_not be_valid
79       end
80       it '0なら失敗する' do
81         @sheet.height = '0'
82         @sheet.should_not be_valid
83       end
84       it '負でも失敗する' do
85         @sheet.height = -1
86         @sheet.should_not be_valid
87       end
88     end
89     context 'visibleを検証するとき' do
90       it 'nullなら失敗する' do
91         @sheet.visible = nil
92         @sheet.should_not be_valid
93       end
94       it '負なら失敗する' do
95         @sheet.visible = -1
96         @sheet.should_not be_valid
97       end
98       it '2以上なら失敗する' do
99         @sheet.visible = 2
100         @sheet.should_not be_valid
101       end
102     end
103   end
104   
105   describe '文字コード検証に於いて' do
106     before do
107       @sheet = FactoryGirl.build :sheet, :author_id => @author.id
108     end
109     
110     context 'captionを検証するとき' do
111       it 'Shift JISなら失敗する' do
112         @sheet.caption = "\x83G\x83r\x83]\x83D"
113         lambda{
114           @sheet.valid_encode
115         }.should raise_error(Pettanr::BadRequest)
116       end
117     end
118     
119   end
120   
121   describe 'デフォルト値補充に於いて' do
122     it 'visibleが0になっている' do
123       @sheet = FactoryGirl.build :sheet, :visible => nil
124       @sheet.supply_default
125       @sheet.visible.should eq 0
126     end
127   end
128   
129   describe '上書き補充に於いて' do
130     it '作家idが設定されている' do
131       @sheet = FactoryGirl.build :sheet, :author_id => nil
132       @sheet.overwrite @author
133       @sheet.author_id.should eq @author.id
134     end
135   end
136   
137   describe '所持判定に於いて' do
138     before do
139       @sheet = FactoryGirl.build :sheet, :author_id => @author.id
140     end
141     context '事前チェックする' do
142       it '自身にロールリストからの作家取得を依頼している' do
143         Sheet.should_receive(:get_author_from_roles).with(any_args).exactly(1)
144         r = @sheet.own?([@author])
145       end
146     end
147     context 'ロール内作家が取得できるとき' do
148       before do
149       end
150       it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
151         Sheet.stub(:get_author_from_roles).with(any_args).and_return(@author)
152         r = @sheet.own?([@author])
153         r.should be_true
154       end
155       it 'ロール内作家のidが自身の作家idと一致しないならno' do
156         Sheet.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
157         @sheet.own?(@other_author).should be_false
158       end
159     end
160     context 'ロール内作家が取得できないとき' do
161       before do
162         Sheet.stub(:get_author_from_roles).with(any_args).and_return(nil)
163       end
164       it 'Falseを返す' do
165         r = @sheet.own?([@author])
166         r.should be_false
167       end
168     end
169   end
170   
171   describe '閲覧許可に於いて' do
172     before do
173       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
174     end
175     context 'オープンモードのとき' do
176       before do
177         MagicNumber['run_mode'] = 0
178       end
179       it '自身にゲスト用ロールチェックを問い合わせしている' do
180         Sheet.any_instance.stub(:guest_role_check).and_return(true)
181         Sheet.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
182         r = @sheet.visible?([@author])
183       end
184       it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
185         Sheet.any_instance.stub(:guest_role_check).and_return(false)
186         r = @sheet.visible?([@author])
187         r.should be_false
188       end
189     end
190     context 'クローズドモードのとき' do
191       before do
192         MagicNumber['run_mode'] = 1
193       end
194       it '自身に読者用ロールチェックを問い合わせしている' do
195         Sheet.any_instance.stub(:reader_role_check).and_return(true)
196         Sheet.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
197         r = @sheet.visible?([@author])
198       end
199       it '読者用ロールチェックが失敗したとき、falseを返す' do
200         Sheet.any_instance.stub(:reader_role_check).and_return(false)
201         r = @sheet.visible?([@author])
202         r.should be_false
203       end
204     end
205     context '事前チェックする' do
206       before do
207         MagicNumber['run_mode'] = 1
208         Sheet.any_instance.stub(:reader_role_check).and_return(true)
209         Sheet.any_instance.stub(:own?).and_return(true)
210       end
211       it '自身に所持判定を問い合わせしている' do
212         Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
213         r = @sheet.visible?([@author])
214       end
215     end
216     context 'つつがなく終わるとき' do
217       before do
218         MagicNumber['run_mode'] = 1
219         Sheet.any_instance.stub(:reader_role_check).and_return(true)
220       end
221       it '自分の用紙なら許可する' do
222         Sheet.any_instance.stub(:own?).and_return(true)
223         Sheet.any_instance.stub(:visible).and_return(0)
224         r = @sheet.visible?([@author])
225         r.should be_true
226       end
227       it '他人の非公開用紙なら許可しない' do
228         Sheet.any_instance.stub(:own?).and_return(false)
229         Sheet.any_instance.stub(:visible).and_return(0)
230         r = @sheet.visible?([@author])
231         r.should be_false
232       end
233       it '他人の用紙でも公開なら許可する' do
234         Sheet.any_instance.stub(:own?).and_return(false)
235         Sheet.any_instance.stub(:visible).and_return(1)
236         r = @sheet.visible?([@author])
237         r.should be_true
238       end
239     end
240   end
241   
242   describe '一覧取得に於いて' do
243     before do
244       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
245     end
246     context 'page補正について' do
247       it '文字列から数値に変換される' do
248         Sheet.page('8').should eq 8
249       end
250       it 'nilの場合は1になる' do
251         Sheet.page().should eq 1
252       end
253       it '0以下の場合は1になる' do
254         Sheet.page('0').should eq 1
255       end
256     end
257     context 'page_size補正について' do
258       it '文字列から数値に変換される' do
259         Sheet.page_size('7').should eq 7
260       end
261       it 'nilの場合はSheet.default_page_sizeになる' do
262         Sheet.page_size().should eq Sheet.default_page_size
263       end
264       it '0以下の場合はSheet.default_page_sizeになる' do
265         Sheet.page_size('0').should eq Sheet.default_page_size
266       end
267       it 'Sheet.max_page_sizeを超えた場合はSheet.max_page_sizeになる' do
268         Sheet.page_size('1000').should eq Sheet.max_page_size
269       end
270     end
271     context 'つつがなく終わるとき' do
272       it '一覧取得オプションを利用している' do
273         Sheet.stub(:list_opt).with(any_args).and_return({})
274         Sheet.should_receive(:list_opt).with(any_args).exactly(1)
275         r = Sheet.list
276       end
277     end
278     it 'リストを返す' do
279       c = Sheet.list
280       c.should eq [@sheet]
281     end
282     it '非公開用紙は(自分の用紙であっても)含んでいない' do
283       FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0
284       c = Sheet.list
285       c.should eq [@sheet]
286     end
287     it '時系列で並んでいる' do
288       #公開用紙は(他人の用紙であっても)含んでいる
289       v = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
290       c = Sheet.list
291       c.should eq [v, @sheet]
292     end
293     context 'DBに5件あって1ページの件数を2件に変えたとして' do
294       before do
295         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
296         @sheet3 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 200
297         @sheet4 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 300
298         @sheet5 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 400
299         Sheet.stub(:default_page_size).and_return(2)
300       end
301       it '通常は2件を返す' do
302         c = Sheet.list
303         c.should have(2).items 
304       end
305       it 'page=1なら末尾2件を返す' do
306         #時系列で並んでいる
307         c = Sheet.list(1)
308         c.should eq [@sheet5, @sheet4]
309       end
310       it 'page=2なら中間2件を返す' do
311         c = Sheet.list(2)
312         c.should eq [@sheet3, @sheet2]
313       end
314       it 'page=3なら先頭1件を返す' do
315         c = Sheet.list(3)
316         c.should eq [@sheet]
317       end
318     end
319   end
320   
321   describe '自分の用紙一覧取得に於いて' do
322     before do
323       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
324     end
325     context 'つつがなく終わるとき' do
326       it '一覧取得オプションを利用している' do
327         Sheet.stub(:list_opt).with(any_args).and_return({})
328         Sheet.should_receive(:list_opt).with(any_args).exactly(1)
329         r = Sheet.mylist @author
330       end
331     end
332     it 'リストを返す' do
333       c = Sheet.mylist @author
334       c.should eq [@sheet]
335     end
336     it '時系列で並んでいる' do
337       nc = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
338       cl = Sheet.mylist @author
339       cl.should eq [nc, @sheet]
340     end
341     it '他人の用紙は公開でも含まない' do
342       nc = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
343       cl = Sheet.mylist @author
344       cl.should eq [@sheet]
345     end
346     it '自分の用紙は非公開でも含んでいる' do
347       nc = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
348       cl = Sheet.mylist @author
349       cl.should eq [nc, @sheet]
350     end
351     context 'DBに5件あって1ページの件数を2件に変えたとして' do
352       before do
353         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
354         @sheet3 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 200
355         @sheet4 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 300
356         @sheet5 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 400
357       end
358       it '通常は2件を返す' do
359         c = Sheet.mylist @author, 1, 2
360         c.should have(2).items 
361       end
362       it 'page=1なら末尾2件を返す' do
363         #時系列で並んでいる
364         c = Sheet.mylist(@author, 1, 2)
365         c.should eq [@sheet5, @sheet4]
366       end
367       it 'page=2なら中間2件を返す' do
368         c = Sheet.mylist(@author, 2, 2)
369         c.should eq [@sheet3, @sheet2]
370       end
371       it 'page=3なら先頭1件を返す' do
372         c = Sheet.mylist(@author, 3, 2)
373         c.should eq [@sheet]
374       end
375     end
376   end
377   
378   describe '他作家の用紙一覧取得に於いて' do
379     before do
380       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
381       @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
382     end
383     context 'つつがなく終わるとき' do
384       it '一覧取得オプションを利用している' do
385         Sheet.stub(:list_opt).with(any_args).and_return({})
386         Sheet.should_receive(:list_opt).with(any_args).exactly(1)
387         r = Sheet.himlist @other_author
388       end
389     end
390     it '指定した作家のリストを返す' do
391       r = Sheet.himlist @other_author
392       r.should eq [@other_sheet]
393     end
394     it '時系列で並んでいる' do
395       nc = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
396       r = Sheet.himlist @other_author
397       r.should eq [nc, @other_sheet]
398     end
399     it '公開用紙に限る ' do
400       hidden = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 0
401       r = Sheet.himlist @other_author
402       r.should eq [@other_sheet]
403     end
404     context 'DBに5件あって1ページの件数を2件に変えたとして' do
405       before do
406         @other_sheet2 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
407         @other_sheet3 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 200
408         @other_sheet4 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 300
409         @other_sheet5 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 400
410       end
411       it '通常は2件を返す' do
412         c = Sheet.himlist @other_author, 1, 2
413         c.should have(2).items 
414       end
415       it 'page=1なら末尾2件を返す' do
416         #時系列で並んでいる
417         c = Sheet.himlist(@other_author, 1, 2)
418         c.should eq [@other_sheet5, @other_sheet4]
419       end
420       it 'page=2なら中間2件を返す' do
421         c = Sheet.himlist(@other_author, 2, 2)
422         c.should eq [@other_sheet3, @other_sheet2]
423       end
424       it 'page=3なら先頭1件を返す' do
425         c = Sheet.himlist(@other_author, 3, 2)
426         c.should eq [@other_sheet]
427       end
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.list_paginate 
437       r.is_a?(Kaminari::PaginatableArray).should be_true
438     end
439     it '用紙一覧の取得条件を利用している' do
440       Sheet.stub(:list_where).with(any_args).and_return('')
441       Sheet.should_receive(:list_where).with(any_args).exactly(1)
442       r = Sheet.list_paginate 
443     end
444     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
445       r = Sheet.list_paginate 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     before do
453       Sheet.stub(:count).with(any_args).and_return(100)
454     end
455     it 'ページ制御を返す' do
456       r = Sheet.mylist_paginate @author
457       r.is_a?(Kaminari::PaginatableArray).should be_true
458     end
459     it '自分の用紙一覧の取得条件を利用している' do
460       Sheet.stub(:mylist_where).with(any_args).and_return('')
461       Sheet.should_receive(:mylist_where).with(any_args).exactly(1)
462       r = Sheet.mylist_paginate @author
463     end
464     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
465       r = Sheet.mylist_paginate @author, 3, 10
466       r.limit_value.should eq 10
467       r.offset_value.should eq 20
468     end
469   end
470   
471   describe '他作家の用紙一覧ページ制御に於いて' do
472     before do
473       Sheet.stub(:count).with(any_args).and_return(100)
474     end
475     it 'ページ制御を返す' do
476       r = Sheet.himlist_paginate @other_author
477       r.is_a?(Kaminari::PaginatableArray).should be_true
478     end
479     it '他作家の用紙一覧の取得条件を利用している' do
480       Sheet.stub(:himlist_where).with(any_args).and_return('')
481       Sheet.should_receive(:himlist_where).with(any_args).exactly(1)
482       r = Sheet.himlist_paginate @other_author
483     end
484     it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
485       r = Sheet.himlist_paginate @other_author, 3, 10
486       r.limit_value.should eq 10
487       r.offset_value.should eq 20
488     end
489   end
490   
491   describe '一覧取得オプションに於いて' do
492     it '2つの項目を含んでいる' do
493       r = Sheet.list_opt
494       r.should have(2).items
495     end
496     it 'スクコマを含んでいる' do
497       r = Sheet.list_opt
498       r.has_key?(:sheet_panels).should be_true
499     end
500       it 'スクコマはコマを含んでいる' do
501         r = Sheet.list_opt
502         r[:sheet_panels].has_key?(:panel).should be_true
503       end
504     it '作家を含んでいる' do
505       r = Sheet.list_opt
506       r.has_key?(:author).should be_true
507     end
508   end
509   describe 'json一覧出力オプションに於いて' do
510     before do
511       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
512       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
513       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
514       @sbt = FactoryGirl.create :speech_balloon_template
515       @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
516       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
517       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
518     end
519     it 'スクコマを含んでいる' do
520       r = Sheet.list.to_json Sheet.list_json_opt
521       j = JSON.parse r
522       i = j.first
523       i.has_key?('sheet_panels').should be_true
524     end
525       it 'スクコマはコマを含んでいる' do
526         r = Sheet.list.to_json Sheet.list_json_opt
527         j = JSON.parse r
528         i = j.first
529         s = i['sheet_panels'].first
530         s.has_key?('panel').should be_true
531       end
532     it '作家を含んでいる' do
533       r = Sheet.list.to_json Sheet.list_json_opt
534       j = JSON.parse r
535       i = j.first
536       i.has_key?('author').should be_true
537     end
538   end
539   
540   describe '単体取得に於いて' do
541     before do
542       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
543     end
544     context 'つつがなく終わるとき' do
545       it '単体取得オプションを利用している' do
546         Sheet.stub(:show_opt).with(any_args).and_return({})
547         Sheet.should_receive(:show_opt).with(any_args).exactly(1)
548         r = Sheet.show @sheet.id, @author
549       end
550       it '閲覧許可を問い合わせている' do
551         Sheet.any_instance.stub(:visible?).with(any_args).and_return(true)
552         Sheet.any_instance.should_receive(:visible?).with(any_args).exactly(1)
553         r = Sheet.show @sheet.id, @author
554       end
555     end
556     it '指定の用紙を返す' do
557       c = Sheet.show @sheet.id, @author
558       c.should eq @sheet
559     end
560     context '閲覧許可が出なかったとき' do
561       it '403Forbidden例外を返す' do
562         Sheet.any_instance.stub(:visible?).and_return(false)
563         lambda{
564           Sheet.show @sheet.id, @author
565         }.should raise_error(ActiveRecord::Forbidden)
566       end
567     end
568     context '存在しない用紙を開こうとしたとき' do
569       it '404RecordNotFound例外を返す' do
570         lambda{
571           Sheet.show 110, @author
572         }.should raise_error(ActiveRecord::RecordNotFound)
573       end
574     end
575   end
576   
577   describe '編集取得に於いて' do
578     before do
579       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
580     end
581     context 'つつがなく終わるとき' do
582       it '単体取得オプションを利用している' do
583         Sheet.stub(:show_opt).with(any_args).and_return({})
584         Sheet.should_receive(:show_opt).with(any_args).exactly(1)
585         r = Sheet.edit @sheet.id, @author
586       end
587       it '所持判定を問い合わせている' do
588         Sheet.any_instance.stub(:own?).with(any_args).and_return(true)
589         Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
590         r = Sheet.edit @sheet.id, @author
591       end
592     end
593     it '指定の用紙を返す' do
594       Sheet.any_instance.stub(:own?).and_return(true)
595       c = Sheet.edit @sheet.id, @author.id
596       c.should eq @sheet
597     end
598     context '他人の用紙を開こうとしたとき' do
599       it '403Forbidden例外を返す' do
600         Sheet.any_instance.stub(:own?).and_return(false)
601         lambda{
602           Sheet.edit @sheet.id, @author
603         }.should raise_error(ActiveRecord::Forbidden)
604       end
605     end
606     context '存在しない用紙を開こうとしたとき' do
607       it '404RecordNotFound例外を返す' do
608         lambda{
609           Sheet.edit 110, @author
610         }.should raise_error(ActiveRecord::RecordNotFound)
611       end
612     end
613   end
614   describe '単体取得オプションに於いて' do
615     it 'includeキーを含んでいる' do
616       r = Sheet.show_opt
617       r.has_key?(:include).should be_true
618     end
619     it '2つの項目を含んでいる' do
620       r = Sheet.show_opt[:include]
621       r.should have(2).items
622     end
623     it '作家を含んでいる' do
624       r = Sheet.show_opt[:include]
625       r.has_key?(:author).should be_true
626     end
627     it 'スクコマを含んでいる' do
628       r = Sheet.show_opt[:include]
629       r.has_key?(:sheet_panels).should be_true
630     end
631       it 'スクコマはコマを含んでいる' do
632         r = Sheet.show_opt[:include]
633         r[:sheet_panels].has_key?(:panel).should be_true
634       end
635   end
636   describe 'json単体出力オプションに於いて' do
637     before do
638       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
639       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
640       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
641       @sbt = FactoryGirl.create :speech_balloon_template
642       @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
643       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
644       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
645     end
646     it 'スクコマを含んでいる' do
647       r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
648       j = JSON.parse r
649       i = j
650       i.has_key?('sheet_panels').should be_true
651     end
652       it 'スクコマはコマを含んでいる' do
653         r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
654         j = JSON.parse r
655         i = j
656         s = i['sheet_panels'].first
657         s.has_key?('panel').should be_true
658       end
659     it '作家を含んでいる' do
660       r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
661       j = JSON.parse r
662       i = j
663       i.has_key?('author').should be_true
664     end
665   end
666   
667   describe '検証値収集に於いて' do
668     context 'つつがなく終わるとき' do
669       it '第一パラメータで指定された配列中から第二引数のカラム値を収集している' do
670         elements = [[{:sheet_id => 1, :a => 'a'}, {:sheet_id => 2, :a => 'a'}], 
671           [{:sheet_id => 3, :a => 'a'}, {:sheet_id => 4, :a => 'a'}]]
672         r = Sheet.collect_element_value elements, :sheet_id
673         r.should eq [1, 2, 3, 4]
674       end
675       it '第一パラメータで指定された配列中から第二引数のカラム値を収集している' do
676         elements = [[{:t => 1, :a => 'a'}, {:t => 2, :a => 'a'}], 
677           [{:t => 3, :a => 'a'}, {:t => 0, :a => 'a'}]]
678         r = Sheet.collect_element_value elements, :t
679         r.should eq [1, 2, 3, 0]
680       end
681     end
682   end
683   describe 'シリアライズチェックに於いて' do
684     context 'つつがなく終わるとき' do
685       it '0からシリアライズされているならTrueを返す' do
686         r = Sheet.validate_serial [0, 1, 2]
687         r.should be_true
688       end
689       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
690         r = Sheet.validate_serial [0, 2, 1]
691         r.should be_true
692       end
693       it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
694         r = Sheet.validate_serial [ 2, 1, 4, 3, 0]
695         r.should be_true
696       end
697     end
698     context 'オフセットが1のとき' do
699       it '0からシリアライズされているならFalseを返す' do
700         r = Sheet.validate_serial [0, 1, 2], 1
701         r.should be_false
702       end
703       it '1からシリアライズされているならTrueを返す' do
704         r = Sheet.validate_serial [1, 2, 3], 1
705         r.should be_true
706       end
707     end
708     context '異常なとき' do
709       it '0から始まらないならFalseを返す' do
710         r = Sheet.validate_serial [1, 2, 3]
711         r.should be_false
712       end
713       it '連続していないならFalseを返す' do
714         r = Sheet.validate_serial [0, 1, 2, 4]
715         r.should be_false
716       end
717       it '連続していないならFalseを返す' do
718         r = Sheet.validate_serial [0, 1, 2, 4, 5]
719         r.should be_false
720       end
721     end
722   end
723   describe 'シリアライズチェック単体に於いて' do
724     before do
725     end
726     context 'つつがなく終わるとき' do
727       it '検証値収集を依頼している' do
728         Sheet.should_receive(:collect_element_value).with(any_args).exactly(1)
729         Sheet.stub(:collect_element_value).with(any_args).and_return([])
730         Sheet.stub(:validate_serial).with(any_args).and_return(true)
731         r = Sheet.validate_element_serial [], :t
732       end
733       it 'シリアライズチェック依頼している' do
734         Sheet.stub(:collect_element_value).with(any_args).and_return([])
735         Sheet.should_receive(:validate_serial).with(any_args).exactly(1)
736         Sheet.stub(:validate_serial).with(any_args).and_return(true)
737         r = Sheet.validate_element_serial [], :t
738       end
739     end
740   end
741   describe '従属データの検証に於いて' do
742     context 'つつがなく終わるとき' do
743       it 'trueを返している' do
744         @sheet = FactoryGirl.create :sheet, :author_id => @author.id
745         @panel = FactoryGirl.create :panel, :author_id => @author.id
746         @sheet.sheet_panels.create(
747           FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0, :z => 0+1)
748         )
749         @sheet.sheet_panels.create(
750           FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :z => 1+1)
751         )
752         r = @sheet.validate_child
753         r.should be_true
754       end
755     end
756     context 'tシリアライズされていないとき' do
757       it 'falseを返している' do
758         @sheet = FactoryGirl.create :sheet, :author_id => @author.id
759         @panel = FactoryGirl.create :panel, :author_id => @author.id
760         @sheet.sheet_panels.create(
761           FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 2, :z => 0+1)
762         )
763         @sheet.sheet_panels.create(
764           FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :z => 1+1)
765         )
766         r = @sheet.validate_child
767         r.should be_false
768       end
769     end
770     context 'zシリアライズされていないとき' do
771       it 'falseを返している' do
772         @sheet = FactoryGirl.create :sheet, :author_id => @author.id
773         @panel = FactoryGirl.create :panel, :author_id => @author.id
774         @sheet.sheet_panels.create(
775           FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0, :z => 0+2)
776         )
777         @sheet.sheet_panels.create(
778           FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :z => 1+1)
779         )
780         r = @sheet.validate_child
781         r.should be_false
782       end
783     end
784   end
785   describe '保存に於いて' do
786     before do
787       @attr = FactoryGirl.attributes_for :sheet
788       @sheet = Sheet.new
789       @sheet.supply_default
790     end
791     context 'つつがなく終わるとき' do
792       before do
793         Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
794         Sheet.any_instance.stub(:save).with(any_args).and_return(true)
795       end
796       it 'コマモデルに上書き補充を依頼している' do
797         Sheet.any_instance.should_receive(:overwrite).exactly(1)
798         r = @sheet.store @attr, @author
799       end
800       it '保存を依頼している' do
801         Sheet.any_instance.should_receive(:save).with(any_args).exactly(1)
802         @sheet = FactoryGirl.build :sheet, :author_id => @author.id
803         r = @sheet.store @attr, @author
804       end
805       it '従属データの検証を依頼している' do
806         Sheet.any_instance.should_receive(:validate_child).with(any_args).exactly(1)
807         r = @sheet.store @attr, @author
808       end
809     end
810     context 'つつがなく終わるとき' do
811       before do
812         Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
813       end
814       it 'Trueを返す' do
815         r = @sheet.store @attr, @author
816         r.should be_true
817       end
818       it '行が追加されている' do
819         Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
820         lambda {
821           r = @sheet.store @attr, @author
822         }.should change(Sheet, :count)
823       end
824     end
825     context '不正なjsonデータのとき' do
826       before do
827       end
828       it 'エラーメッセージがセットされている' do
829         r = @sheet.store false, @author
830         @sheet.errors[:base].should_not be_blank
831       end
832     end
833     context '従属データの検証に失敗したとき' do
834       before do
835         Sheet.any_instance.stub(:save).with(any_args).and_return(true)
836         Sheet.any_instance.stub(:validate_child).with(any_args).and_return(false)
837       end
838       it 'エラーメッセージがセットされている' do
839         r = @sheet.store @attr, @author
840         @sheet.errors[:base].should_not be_blank
841       end
842     end
843     context 'カラム値がFalseしたとき' do
844       before do
845         Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
846       end
847       it 'エラーメッセージがセットされている' do
848         r = @sheet.store false, @author
849         @sheet.errors.should_not be_empty
850       end
851     end
852   end
853   
854   describe '削除に於いて' do
855     before do
856       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
857       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
858       @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
859       @other_sheet = FactoryGirl.create :sheet, :author_id => @author.id
860       @other_sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @other_sheet.id, :panel_id => @panel.id
861     end
862     context 'つつがなく終わるとき' do
863       it '自身を削除する' do
864         lambda {
865           r = @sheet.destroy_with_sheet_panel
866         }.should change(Sheet, :count).by(-1)
867         lambda {
868           r = Sheet.find @sheet.id
869         }.should raise_error
870       end
871       it '自身にリンクしているスクコマをすべて削除する' do
872         lambda {
873           r = @sheet.destroy_with_sheet_panel
874         }.should change(SheetPanel, :count).by(-1)
875         lambda {
876           r = SheetPanel.find @sheet_panel.id
877         }.should raise_error
878       end
879       it 'Trueを返す' do
880         r = @sheet.destroy_with_sheet_panel
881         r.should be_true
882       end
883     end
884     context '削除に失敗したとき' do
885       before do
886         SheetPanel.any_instance.stub(:destroy).with(any_args).and_return(false)
887       end
888       it 'Falseを返す' do
889         r = @sheet.destroy_with_sheet_panel
890         r.should be_false
891       end
892       it 'ロールバックしている' do
893         lambda {
894           r = @sheet.destroy_with_sheet_panel
895         }.should_not change(Sheet, :count)
896         lambda {
897           r = @sheet.destroy_with_sheet_panel
898         }.should_not change(SheetPanel, :count)
899       end
900     end
901   end
902 end