OSDN Git Service

t30350#:fix destroy
[pettanr/pettanr.git] / spec / models / original_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 #原画
3 require 'spec_helper'
4
5 describe OriginalPicture do
6   before do
7     @admin = FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = FactoryGirl.create :author, :user_id => @user.id
10     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11     @other_user = FactoryGirl.create( :user_yas)
12     @other_author = FactoryGirl.create :author, :user_id => @other_user.id
13     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
14     @sp = FactoryGirl.create :system_picture
15     @lg = FactoryGirl.create :license_group
16     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
17   end
18   
19   describe '検証に於いて' do
20     before do
21       @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
22     end
23     
24     context 'オーソドックスなデータのとき' do
25       it '下限データが通る' do
26         @op.ext = 'png' #リストにない拡張子は通らないし
27         @op.width = 1
28         @op.height = 1
29         @op.filesize = 1
30         @op.md5 = 'a'*32
31         @op.should be_valid
32       end
33       it '上限データが通る' do
34         @op.ext = 'jpeg'
35         @op.width = 99999
36         @op.height = 99999
37         @op.filesize = 2000000
38         @op.md5 = 'a'*32
39         @op.should be_valid
40       end
41     end
42     
43     context 'extを検証するとき' do
44       it 'nullなら失敗する' do
45         @op.ext = ''
46         @op.should_not be_valid
47       end
48       it '5文字以上なら失敗する' do
49         @op.ext = 'a'*5
50         @op.should_not be_valid
51       end
52       it 'png,gif,jpeg以外なら失敗する' do
53         @op.ext = 'bmp'
54         @op.should_not be_valid
55       end
56     end
57     context 'widthを検証するとき' do
58       it 'nullなら失敗する' do
59         @op.width = nil
60         @op.should_not be_valid
61       end
62       it '数値でなければ失敗する' do
63         @op.width = 'a'
64         @op.should_not be_valid
65       end
66       it '0なら失敗する' do
67         @op.width = '0'
68         @op.should_not be_valid
69       end
70       it '負でも失敗する' do
71         @op.width = -1
72         @op.should_not be_valid
73       end
74     end
75     context 'heightを検証するとき' do
76       it 'nullなら失敗する' do
77         @op.height = nil
78         @op.should_not be_valid
79       end
80       it '数値でなければ失敗する' do
81         @op.height = 'a'
82         @op.should_not be_valid
83       end
84       it '0なら失敗する' do
85         @op.height = '0'
86         @op.should_not be_valid
87       end
88       it '負でも失敗する' do
89         @op.height = -1
90         @op.should_not be_valid
91       end
92     end
93     context 'filesizeを検証するとき' do
94       it 'nullなら失敗する' do
95         @op.filesize = nil
96         @op.should_not be_valid
97       end
98       it '数値でなければ失敗する' do
99         @op.filesize = 'a'
100         @op.should_not be_valid
101       end
102       it '負なら失敗する' do
103         @op.filesize = '-1'
104         @op.should_not be_valid
105       end
106       it '2MB以上なら失敗する' do
107         @op.filesize = 2000000+1
108         @op.should_not be_valid
109       end
110     end
111     context 'artist_idを検証するとき' do
112       it 'nullなら失敗する' do
113         @op.artist_id = nil
114         @op.should_not be_valid
115       end
116       it '数値でなければ失敗する' do
117         @op.artist_id = 'a'
118         @op.should_not be_valid
119       end
120       it '存在する絵師でなければ失敗する' do
121         @op.artist_id = 0
122         @op.should_not be_valid
123       end
124     end
125     context 'md5を検証するとき' do
126       it 'nullなら失敗する' do
127         @op.md5 = ''
128         @op.should_not be_valid
129       end
130       it '31文字なら失敗する' do
131         @op.md5 = 'a'*31
132         @op.should_not be_valid
133       end
134       it '33文字なら失敗する' do
135         @op.md5 = 'a'*33
136         @op.should_not be_valid
137       end
138     end
139   end
140   
141   describe 'デフォルト値補充に於いて' do
142     it 'defined' do
143       @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
144       @op.supply_default
145     end
146   end
147   
148   describe '上書き補充に於いて' do
149     it '絵師idが設定されている' do
150       @op = FactoryGirl.build :original_picture, :artist_id => nil
151       @op.overwrite @artist
152       @op.artist_id.should eq @artist.id
153     end
154   end
155   
156   describe '作者判定に於いて' do
157     before do
158       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
159     end
160     context 'パラメータが作家のとき' do
161       it '自作の原画ならyes' do
162         @op.own?(@author).should == true
163       end
164       it '他人のならno' do
165         @op.own?(@other_author).should == false
166       end
167     end
168     context 'パラメータが絵師のとき' do
169       it '自作の原画ならyes' do
170         @op.own?(@artist).should == true
171       end
172       it '他人のならno' do
173         @op.own?(@other_artist).should == false
174       end
175     end
176     context 'それ以外のとき' do
177       it 'no' do
178         @op.own?(nil).should == false
179       end
180     end
181   end
182   
183   describe '閲覧許可に於いて' do
184     before do
185       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
186     end
187     it '検査対象が管理者のとき、許可を返す' do
188       OriginalPicture.any_instance.stub(:own?).and_return(false)
189       r = @op.visible?(@admin)
190       r.should == true
191     end
192     it '所持判定を問い合わせ、自分の原画なら許可する' do
193       OriginalPicture.any_instance.stub(:own?).and_return(true)
194       r = @op.visible?(@artist)
195       r.should == true
196     end
197     it '他人の原画なら許可しない' do
198       OriginalPicture.any_instance.stub(:own?).and_return(false)
199       r = @op.visible?(@artist)
200       r.should == false
201     end
202   end
203   
204   describe 'ファイル名に於いて' do
205     before do
206       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
207     end
208     it 'id+拡張子のフォーマットで返す' do
209       r = @op.filename
210       r.should eq "#{@op.id}.png"
211     end
212   end
213   
214   describe 'MimeTypeに於いて' do
215     before do
216       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
217     end
218     it 'image/拡張子のフォーマットで返す' do
219       r = @op.mime_type
220       r.should eq "image/png"
221     end
222   end
223   
224   describe 'ファイルのurlに於いて' do
225     before do
226       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
227       OriginalPicture.any_instance.stub(:filename).and_return('3.gif')
228     end
229     it 'ファイル名取得を依頼している' do
230       OriginalPicture.any_instance.should_receive(:filename).exactly(1)
231       @op.url
232     end
233     it '/original_pictures/3.gifのフォーマットで返す' do
234       r = @op.url
235       r.should eq "/original_pictures/3.gif"
236     end
237   end
238   
239   describe 'サムネイル画像タグオプションに於いて' do
240     before do
241       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
242       OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
243       PettanImager.stub(:thumbnail_size).with(any_args).and_return([40, 30])
244     end
245     it 'サムネイル画像の幅高さ取得を依頼している' do
246       PettanImager.should_receive(:thumbnail_size).with(any_args).exactly(1)
247       @op.tmb_opt_img_tag
248     end
249     it '戻り値はHashで返す' do
250       r = @op.tmb_opt_img_tag
251       r.is_a?(Hash).should be_true
252     end
253     it 'srcキーを含んでいる' do
254       r = @op.tmb_opt_img_tag
255       r.has_key?(:src).should be_true
256       r[:src].should eq '/original_pictures/3.gif'
257     end
258     it 'widthキーを含んでいる' do
259       r = @op.tmb_opt_img_tag
260       r.has_key?(:width).should be_true
261       r[:width].should eq 40
262     end
263     it 'heightキーを含んでいる' do
264       r = @op.tmb_opt_img_tag
265       r.has_key?(:height).should be_true
266       r[:height].should eq 30
267     end
268   end
269   
270   describe '画像タグオプションに於いて' do
271     before do
272       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
273       OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
274     end
275     it '戻り値はHashで返す' do
276       r = @op.opt_img_tag
277       r.is_a?(Hash).should be_true
278     end
279     it 'srcキーを含んでいる' do
280       r = @op.opt_img_tag
281       r.has_key?(:src).should be_true
282       r[:src].should eq '/original_pictures/3.gif'
283     end
284     it 'widthキーを含んでいる' do
285       r = @op.opt_img_tag
286       r.has_key?(:width).should be_true
287       r[:width].should eq @op.width
288     end
289     it 'heightキーを含んでいる' do
290       r = @op.opt_img_tag
291       r.has_key?(:height).should be_true
292       r[:height].should eq @op.height
293     end
294   end
295   
296   describe '未公開に於いて' do
297     before do
298       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
299     end
300     context '実素材がゼロのとき' do
301       it 'Trueを返す' do
302         r = @op.unpublished?
303         r.should be_true
304       end
305     end
306     context '実素材がゼロではないとき' do
307       it 'Falseを返す' do
308         @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id 
309         r = @op.unpublished?
310         r.should be_false
311       end
312     end
313   end
314
315   describe '停止中に於いて' do
316     before do
317       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
318     end
319     context '未公開ではないが、素材がないとき' do
320       it 'Trueを返す' do
321         @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id 
322         r = @op.stopped?
323         r.should be_true
324       end
325     end
326     context '未公開のとき' do
327       it 'Falseを返す' do
328         r = @op.stopped?
329         r.should be_false
330       end
331     end
332     context '未公開ではなく、素材もあるとき' do
333       it 'falseを返す' do
334         @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id 
335         @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
336         r = @op.stopped?
337         r.should be_false
338       end
339     end
340   end
341
342   describe 'ライセンス待ちに於いて' do
343     before do
344       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
345     end
346     context '未公開ではなく、素材もあるが、原画の更新日時が実素材のheadのそれより後のとき' do
347       it 'Trueを返す' do
348         @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :updated_at => Time.now - 1000
349         @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
350         r = @op.unlicensed?
351         r.should be_true
352       end
353     end
354     context '未公開のとき' do
355       it 'Falseを返す' do
356         r = @op.unlicensed?
357         r.should be_false
358       end
359     end
360     context '未公開ではないが、素材がないとき' do
361       it 'Falseを返す' do
362         @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id 
363         r = @op.unlicensed?
364         r.should be_false
365       end
366     end
367     context '未公開ではなく、素材もあり、実素材のheadの更新日時が原画のそれより後のとき' do
368       it 'Falseを返す' do
369         @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :updated_at => Time.now + 1000
370         @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
371         r = @op.unlicensed?
372         r.should be_false
373       end
374     end
375   end
376
377   describe '公開中に於いて' do
378     before do
379       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
380     end
381     context '未公開ではなく、素材もあり、実素材のheadの更新日時が原画のそれより後のとき' do
382       it 'Trueを返す' do
383         @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :updated_at => Time.now + 1000
384         @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
385         r = @op.published?
386         r.should be_true
387       end
388     end
389     context '未公開のとき' do
390       it 'Falseを返す' do
391         r = @op.published?
392         r.should be_false
393       end
394     end
395     context '未公開ではないが、素材がないとき' do
396       it 'falseを返す' do
397         @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id 
398         r = @op.published?
399         r.should be_false
400       end
401     end
402     context '未公開ではなく、素材もあるが、原画の更新日時が実素材のheadのそれより後のとき' do
403       it 'falseを返す' do
404         @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :updated_at => Time.now - 1000
405         @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
406         r = @op.published?
407         r.should be_false
408       end
409     end
410   end
411
412   describe '一覧取得に於いて' do
413     before do
414       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
415     end
416     context 'page補正について' do
417       it '文字列から数値に変換される' do
418         OriginalPicture.page('8').should eq 8
419       end
420       it 'nilの場合は1になる' do
421         OriginalPicture.page().should eq 1
422       end
423       it '0以下の場合は1になる' do
424         OriginalPicture.page('0').should eq 1
425       end
426     end
427     context 'page_size補正について' do
428       it '文字列から数値に変換される' do
429         OriginalPicture.page_size('7').should eq 7
430       end
431       it 'nilの場合はOriginalPicture.default_page_sizeになる' do
432         OriginalPicture.page_size().should eq OriginalPicture.default_page_size
433       end
434       it '0以下の場合はOriginalPicture.default_page_sizeになる' do
435         OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
436       end
437       it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
438         OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
439       end
440     end
441   end
442   describe '一覧取得オプションに於いて' do
443     it 'includeキーを含んでいる' do
444       r = OriginalPicture.list_opt
445       r.has_key?(:include).should be_true
446     end
447     it '2つの項目を含んでいる' do
448       r = OriginalPicture.list_opt[:include]
449       r.should have(2).items
450     end
451     it '素材を含んでいる' do
452       r = OriginalPicture.list_opt[:include]
453       r.has_key?(:resource_picture).should be_true
454     end
455     it '実素材を含んでいる' do
456       r = OriginalPicture.list_opt[:include]
457       r.has_key?(:pictures).should be_true
458     end
459   end
460   describe 'json一覧出力オプションに於いて' do
461     before do
462       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
463       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
464       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
465       @sbt = FactoryGirl.create :speech_balloon_template
466       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
467       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
468       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
469     end
470     it '素材を含んでいる' do
471       r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
472       j = JSON.parse r
473       i = j.first
474       i.has_key?('resource_picture').should be_true
475     end
476     it '実素材を含んでいる' do
477       r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
478       j = JSON.parse r
479       i = j.first
480       i.has_key?('pictures').should be_true
481     end
482   end
483   
484   describe '自分のコミック一覧取得に於いて' do
485     before do
486       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
487     end
488     context 'つつがなく終わるとき' do
489       it '一覧取得オプションを利用している' do
490         OriginalPicture.stub(:list_opt).with(any_args).and_return({})
491         OriginalPicture.should_receive(:list_opt).with(any_args).exactly(1)
492         r = OriginalPicture.mylist @artist
493       end
494     end
495     it 'リストを返す' do
496       r = OriginalPicture.mylist @artist
497       r.should eq [@op]
498     end
499     it '時系列で並んでいる' do
500       nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
501       r = OriginalPicture.mylist @artist
502       r.should eq [nc, @op]
503     end
504     it '他人のコミックはxxxでも含まない' do
505       nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
506       r = OriginalPicture.mylist @artist
507       r.should eq [@op]
508     end
509     it '自分のコミックはxxxでも含んでいる' do
510       nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
511       r = OriginalPicture.mylist @artist
512       r.should eq [nc, @op]
513     end
514     context 'DBに5件あって1ページの件数を2件に変えたとして' do
515       before do
516         @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
517         @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
518         @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
519         @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
520       end
521       it '通常は2件を返す' do
522         r = OriginalPicture.mylist @artist, 1, 2
523         r.should have(2).items 
524       end
525       it 'page=1なら末尾2件を返す' do
526         #時系列で並んでいる
527         r = OriginalPicture.mylist @artist, 1, 2
528         r.should eq [@op5, @op4]
529       end
530       it 'page=2なら中間2件を返す' do
531         r = OriginalPicture.mylist @artist, 2, 2
532         r.should eq [@op3, @op2]
533       end
534       it 'page=3なら先頭1件を返す' do
535         r = OriginalPicture.mylist @artist, 3, 2
536         r.should eq [@op]
537       end
538     end
539     context 'DBに5件あって1ページの件数を0件に変えたとして' do
540       before do
541         @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
542         @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
543         @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
544         @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
545         OriginalPicture.stub(:default_page_size).and_return(2)
546       end
547       it '通常は全件(5件)を返す' do
548         r = OriginalPicture.mylist @artist, 5, 0
549         r.should have(5).items 
550       end
551     end
552   end
553   
554   describe '更新履歴一覧取得に於いて' do
555     before do
556       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
557       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
558         :original_picture_id => @op.id
559       @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
560     end
561     it 'リストを返す' do
562       r = @op.history
563       r.should eq [@p]
564     end
565     it '他の原画の実素材は含んでいない' do
566       @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
567         :original_picture_id => @op2.id
568       r = @op.history
569       r.should eq [@p]
570     end
571     it 'revisionで並んでいる' do
572       @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
573         :original_picture_id => @op.id
574       r = @op.history
575       r.should eq [@p2, @p]
576     end
577   end
578   
579   describe '単体取得に於いて' do
580     before do
581       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
582     end
583     context 'つつがなく終わるとき' do
584       it '単体取得オプションを利用している' do
585         OriginalPicture.stub(:show_opt).with(any_args).and_return({})
586         OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
587         r = OriginalPicture.show @op.id, @artist
588       end
589       it '閲覧許可を問い合わせている' do
590         OriginalPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
591         OriginalPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
592         r = OriginalPicture.show @op.id, @artist
593       end
594     end
595     it '指定の原画を返す' do
596       pic = OriginalPicture.show @op.id, @artist
597       pic.should eq @op
598     end
599     context '他人の原画を開こうとしたとき' do
600       it '403Forbidden例外を返す' do
601         OriginalPicture.any_instance.stub(:visible?).and_return(false)
602         lambda{
603           pic = OriginalPicture.show @op.id, @other_artist
604         }.should raise_error(ActiveRecord::Forbidden)
605       end
606     end
607     context '存在しない原画を開こうとしたとき' do
608       it '404RecordNotFound例外を返す' do
609         lambda{
610           pic = OriginalPicture.show 0, @artist
611         }.should raise_error(ActiveRecord::RecordNotFound)
612       end
613     end
614   end
615   describe '編集取得に於いて' do
616     before do
617       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
618     end
619     context 'つつがなく終わるとき' do
620       it '単体取得オプションを利用している' do
621         OriginalPicture.stub(:show_opt).with(any_args).and_return({})
622         OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
623         r = OriginalPicture.edit @op.id, @artist
624       end
625       it '所持判定を問い合わせている' do
626         OriginalPicture.any_instance.stub(:own?).with(any_args).and_return(true)
627         OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
628         r = OriginalPicture.edit @op.id, @artist
629       end
630     end
631     it '指定の原画を返す' do
632       pic = OriginalPicture.edit @op.id, @artist
633       pic.should eq @op
634     end
635     context '他人の原画を開こうとしたとき' do
636       it '403Forbidden例外を返す' do
637         OriginalPicture.any_instance.stub(:own?).and_return(false)
638         lambda{
639           r = OriginalPicture.edit @op.id, @other_artist
640         }.should raise_error(ActiveRecord::Forbidden)
641       end
642     end
643     context '存在しない原画を開こうとしたとき' do
644       it '404RecordNotFound例外を返す' do
645         lambda{
646           r = OriginalPicture.edit 0, @artist
647         }.should raise_error(ActiveRecord::RecordNotFound)
648       end
649     end
650   end
651   describe '単体取得オプションに於いて' do
652     it 'includeキーを含んでいる' do
653       r = OriginalPicture.show_opt
654       r.has_key?(:include).should be_true
655     end
656     it '2つの項目を含んでいる' do
657       r = OriginalPicture.show_opt[:include]
658       r.should have(2).items
659     end
660     it '素材を含んでいる' do
661       r = OriginalPicture.show_opt[:include]
662       r.has_key?(:resource_picture).should be_true
663     end
664     it '実素材を含んでいる' do
665       r = OriginalPicture.show_opt[:include]
666       r.has_key?(:pictures).should be_true
667     end
668   end
669   describe 'json単体出力オプションに於いて' do
670     before do
671       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
672       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
673       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
674       @sbt = FactoryGirl.create :speech_balloon_template
675       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
676       @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
677       @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
678     end
679     it '素材を含んでいる' do
680       r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
681       j = JSON.parse r
682       i = j
683       i.has_key?('resource_picture').should be_true
684     end
685     it '実素材を含んでいる' do
686       r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
687       j = JSON.parse r
688       i = j
689       i.has_key?('pictures').should be_true
690     end
691   end
692   describe '作成・更新に於いて' do
693     before do
694       @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
695       @imager = ImagerTest.load "abc\ndef\nghi"
696     end
697     context '事前チェック' do
698       before do
699         #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
700         #それで外部のメソッド呼び出しだけに着目してテストする。
701         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
702         PictureIO.original_picture_io.stub(:put).with(any_args).and_return(true)
703       end
704       it '自身に属性をセットしている' do
705         OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
706         @op.store @imager
707       end
708       it '自身が保存されている' do
709         OriginalPicture.any_instance.should_receive(:save).exactly(1)
710         @op.store @imager
711       end
712       it 'PictureIoに画像データの保存を依頼している' do
713         PictureIO.original_picture_io.should_receive(:put).with(any_args).exactly(1)
714         @op.store @imager
715       end
716     end
717     context 'つつがなく終わるとき' do
718       before do
719       end
720       it '自身に属性をセットしている' do
721         @op.store @imager
722         @op.width.should eq 3
723         @op.height.should eq 3
724         @op.filesize.should eq 9
725       end
726       it '原画モデルが作成されている' do
727         lambda {
728           @op.store @imager
729         }.should change OriginalPicture, :count
730       end
731       it '原画が保存されている' do
732         @op.store @imager
733         OriginalPicture.find(@op).should_not be_nil
734       end
735       it 'Trueを返す' do
736         @op.store(@imager).should eq true
737       end
738     end
739     #以下から例外ケース。処理先頭から失敗させていく
740     context 'imagerが初期化に失敗したとき' do
741       before do
742       end
743       it 'falseを返す' do
744         @op.store(false).should be_false
745       end
746       it '自身の保存は呼ばれていない' do
747         OriginalPicture.any_instance.should_not_receive(:save)
748         @op.store(false)
749       end
750       it '全体エラーメッセージがセットされている' do
751         lambda {
752           @op.store(false)
753         }.should change(@op.errors[:base], :count)
754       end
755     end
756     context '自身の保存に失敗したとき' do
757       before do
758         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
759       end
760       it 'falseを返す' do
761         @op.store(@imager).should be_false
762       end
763       it '更新されていない' do
764         @op.store(@imager)
765         @op.should be_a_new OriginalPicture
766       end
767       it '原画の保存は呼ばれていない' do
768         PictureIO::LocalPicture.any_instance.should_not_receive(:put)
769       end
770     end
771     context '原画の保存に失敗したとき' do
772       before do
773         OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
774         PictureIO.original_picture_io.stub(:put).with(any_args).and_raise(PictureIO::Error)
775       end
776       it 'falseを返す' do
777         @op.store(@imager).should be_false
778       end
779       it '更新されていない' do
780         @op.store(@imager)
781         @op.should be_a_new OriginalPicture
782       end
783       it '全体エラーメッセージがセットされている' do
784         lambda {
785           @op.store(@imager)
786         }.should change(@op.errors[:base], :count)
787       end
788     end
789   end
790   
791 =begin
792   describe 'エクスポートに於いて' do
793     before do
794       @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
795       @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
796       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
797       #他人の原画排除
798       @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
799     end
800     context 'つつがなく終わるとき' do
801       it 'ライセンスグループに依頼してリストを取得している' do
802         LicenseGroup.stub(:list).with(any_args).and_return([@lg])
803         LicenseGroup.should_receive(:list).with(any_args).exactly(1)
804         r = OriginalPicture.export(@artist)
805       end
806       it '原画に依頼してリストを取得している' do
807         OriginalPicture.stub(:list).with(any_args).and_return([@op])
808         OriginalPicture.should_receive(:list).with(any_args).exactly(1)
809         r = OriginalPicture.export(@artist)
810       end
811       it 'Hashを返す' do
812         r = OriginalPicture.export(@artist)
813         r.is_a?(Hash).should be_true
814       end
815       it 'Hashはライセンスグループを含んでいる' do
816         r = OriginalPicture.export(@artist)
817         r.include?(:license_groups).should be_true
818       end
819       it 'Hashは原画を含んでいる' do
820         r = OriginalPicture.export(@artist)
821         r.include?(:original_pictures).should be_true
822       end
823       #素材がライセンスされていないケースもある
824       it 'Hashの原画は素材を含んでいる' do
825         r = OriginalPicture.export(@artist)
826         r[:original_pictures].first.resource_picture.should_not be_nil
827       end
828       it 'Hashの原画は実素材を含んでいる' do
829         r = OriginalPicture.export(@artist)
830         r[:original_pictures].first.pictures.should be_nil
831       end
832     end
833     context '実データ単体のとき' do
834       it 'ライセンスは配列構造になっている' do
835         r = OriginalPicture.export(@artist)
836         r[:license_groups].is_a?(Array).should be_true
837       end
838       it 'ライセンスが全件出ている' do
839         r = OriginalPicture.export(@artist)
840         r[:license_groups].size.should eq 1
841         r[:license_groups].first.should eq @lg
842       end
843       it '原画は配列構造になっている' do
844         r = OriginalPicture.export(@artist)
845         r[:original_pictures].is_a?(Array).should be_true
846       end
847       it '原画が全件出ている' do
848         r = OriginalPicture.export(@artist)
849         r[:original_pictures].size.should eq 1
850         r[:original_pictures].first.should eq @op
851       end
852       it '原画に素材が関連付いている' do
853         r = OriginalPicture.export(@artist)
854         i = r[:original_pictures].first
855         i.resource_picture.should eq @rp
856       end
857       it '原画に実素材が関連付いている' do
858         r = OriginalPicture.export(@artist)
859         i = r[:original_pictures].first
860         i.picture.should eq @p
861       end
862     end
863     context '実データ複数のとき' do
864       before do
865         @lg2 = FactoryGirl.create :license_group, :name => 'export test', :url => 'http://export.test/'
866         @license2 = FactoryGirl.create :license, :license_group_id => @lg2.id, :system_picture_id => @sp.id, :name => 'export test license', :url => 'http://export.test/license'
867         @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
868         @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :revision => 0
869         @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :picture_id => @p2.id
870       end
871       it 'ライセンスは配列構造になっている' do
872         r = OriginalPicture.export(@artist)
873         r[:license_groups].is_a?(Array).should be_true
874       end
875       it 'ライセンスが全件出ている' do
876         r = OriginalPicture.export(@artist)
877         r[:license_groups].size.should eq 2
878         r[:license_groups].first.should eq @lg
879         r[:license_groups].last.should eq @lg2
880       end
881       it '原画は配列構造になっている' do
882         r = OriginalPicture.export(@artist)
883         r[:original_pictures].is_a?(Array).should be_true
884       end
885       it '原画が全件出ている' do
886         r = OriginalPicture.export(@artist)
887         r[:original_pictures].size.should eq 2
888         r[:original_pictures].first.should eq @op
889         r[:original_pictures].last.should eq @op2
890       end
891       it '原画に素材が関連付いている' do
892         r = OriginalPicture.export(@artist)
893         i = r[:original_pictures].first
894         i.resource_picture.should eq @rp
895         i2 = r[:original_pictures].last
896         i2.resource_picture.should eq @rp2
897       end
898       it '原画に実素材が関連付いている' do
899         r = OriginalPicture.export(@artist)
900         i = r[:original_pictures].first
901         i.picture.should eq @p
902         i2 = r[:original_pictures].last
903         i2.picture.should eq @p2
904       end
905     end
906   end
907   
908   describe 'エクスポートオプションに於いて' do
909   end
910   
911   describe 'インポートに於いて' do
912     before do
913       @imports = {:licenses => {}, :artist_id => @artist.id}
914     end
915     context '事前チェックしておく' do
916     end
917   end
918   
919 =end
920 end