1 # -*- encoding: utf-8 -*-
5 describe OriginalPicture do
7 FactoryGirl.create :admin
8 @user = FactoryGirl.create( :user_yas)
10 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11 @other_user = FactoryGirl.create( :user_yas)
12 @other_author = @other_user.author
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
21 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
24 context 'オーソドックスなデータのとき' do
26 @op.ext = 'png' #リストにない拡張子は通らないし
37 @op.filesize = 2000000
43 context 'extを検証するとき' do
46 @op.should_not be_valid
50 @op.should_not be_valid
52 it 'png,gif,jpeg以外なら失敗する' do
54 @op.should_not be_valid
57 context 'widthを検証するとき' do
60 @op.should_not be_valid
64 @op.should_not be_valid
68 @op.should_not be_valid
72 @op.should_not be_valid
75 context 'heightを検証するとき' do
78 @op.should_not be_valid
82 @op.should_not be_valid
86 @op.should_not be_valid
90 @op.should_not be_valid
93 context 'filesizeを検証するとき' do
96 @op.should_not be_valid
100 @op.should_not be_valid
104 @op.should_not be_valid
107 @op.filesize = 2000000+1
108 @op.should_not be_valid
111 context 'artist_idを検証するとき' do
114 @op.should_not be_valid
118 @op.should_not be_valid
120 it '存在する絵師でなければ失敗する' do
122 @op.should_not be_valid
125 context 'md5を検証するとき' do
128 @op.should_not be_valid
132 @op.should_not be_valid
136 @op.should_not be_valid
141 describe 'デフォルト値補充に於いて' do
143 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
148 describe '上書き補充に於いて' do
150 @op = FactoryGirl.build :original_picture, :artist_id => nil
151 @op.overwrite @artist
152 @op.artist_id.should eq @artist.id
156 describe '作者判定に於いて' do
158 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
160 context 'パラメータが作家のとき' do
162 @op.own?(@author).should == true
165 @op.own?(@other_author).should == false
168 context 'パラメータが絵師のとき' do
170 @op.own?(@artist).should == true
173 @op.own?(@other_artist).should == false
178 @op.own?(nil).should == false
183 describe '閲覧許可に於いて' do
185 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
187 it '自分の原画を見るときは許可する' do
188 OriginalPicture.any_instance.stub(:own?).and_return(true)
189 r = @op.visible?(@artist)
193 OriginalPicture.any_instance.stub(:own?).and_return(false)
194 r = @op.visible?(@artist)
199 describe 'ファイル名に於いて' do
201 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
203 it 'id+拡張子のフォーマットで返す' do
205 r.should eq "#{@op.id}.png"
209 describe 'MimeTypeに於いて' do
211 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
213 it 'image/拡張子のフォーマットで返す' do
215 r.should eq "image/png"
219 describe 'ファイルのurlに於いて' do
221 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
222 OriginalPicture.any_instance.stub(:filename).and_return('3.gif')
224 it 'ファイル名取得を依頼している' do
225 OriginalPicture.any_instance.should_receive(:filename).exactly(1)
228 it '/original_pictures/3.gifのフォーマットで返す' do
230 r.should eq "/original_pictures/3.gif"
234 describe '画像タグオプションに於いて' do
236 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
237 OriginalPicture.any_instance.stub(:url).and_return('/original_pictures/3.gif')
238 PettanImager.stub(:thumbnail_size).with(any_args).and_return([40, 30])
240 it 'サムネイル画像の幅高さ取得を依頼している' do
241 PettanImager.should_receive(:thumbnail_size).with(any_args).exactly(1)
246 r.is_a?(Hash).should be_true
250 r.has_key?(:src).should be_true
251 r[:src].should eq '/original_pictures/3.gif'
253 it 'widthキーを含んでいる' do
255 r.has_key?(:width).should be_true
256 r[:width].should eq 40
258 it 'heightキーを含んでいる' do
260 r.has_key?(:height).should be_true
261 r[:height].should eq 30
265 describe '一覧取得に於いて' do
267 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
269 context 'page補正について' do
270 it '文字列から数値に変換される' do
271 OriginalPicture.page('8').should eq 8
274 OriginalPicture.page().should eq 1
277 OriginalPicture.page('0').should eq 1
280 context 'page_size補正について' do
281 it '文字列から数値に変換される' do
282 OriginalPicture.page_size('7').should eq 7
284 it 'nilの場合はOriginalPicture.default_page_sizeになる' do
285 OriginalPicture.page_size().should eq OriginalPicture.default_page_size
287 it '0以下の場合はOriginalPicture.default_page_sizeになる' do
288 OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
290 it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
291 OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
295 describe '一覧取得オプションに於いて' do
296 it 'includeキーを含んでいる' do
297 r = OriginalPicture.list_opt
298 r.has_key?(:include).should be_true
301 r = OriginalPicture.list_opt[:include]
302 r.should have(2).items
305 r = OriginalPicture.list_opt[:include]
306 r.has_key?(:resource_picture).should be_true
309 r = OriginalPicture.list_opt[:include]
310 r.has_key?(:pictures).should be_true
313 describe 'json一覧出力オプションに於いて' do
315 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
316 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
317 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
\r
318 @sbt = FactoryGirl.create :speech_balloon_template
\r
319 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
320 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
321 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
324 r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
327 i.has_key?('resource_picture').should be_true
330 r = OriginalPicture.mylist(@artist).to_json OriginalPicture.list_json_opt
333 i.has_key?('pictures').should be_true
337 describe '自分のコミック一覧取得に於いて' do
339 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
341 context 'つつがなく終わるとき' do
342 it '一覧取得オプションを利用している' do
343 OriginalPicture.stub(:list_opt).with(any_args).and_return({})
344 OriginalPicture.should_receive(:list_opt).with(any_args).exactly(1)
345 r = OriginalPicture.mylist @artist
349 r = OriginalPicture.mylist @artist
353 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
354 r = OriginalPicture.mylist @artist
355 r.should eq [nc, @op]
357 it '他人のコミックはxxxでも含まない' do
358 nc = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
359 r = OriginalPicture.mylist @artist
362 it '自分のコミックはxxxでも含んでいる' do
363 nc = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
364 r = OriginalPicture.mylist @artist
365 r.should eq [nc, @op]
367 context 'DBに5件あって1ページの件数を2件に変えたとして' do
369 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
370 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
371 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
372 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
375 r = OriginalPicture.mylist @artist, 1, 2
376 r.should have(2).items
378 it 'page=1なら末尾2件を返す' do
380 r = OriginalPicture.mylist @artist, 1, 2
381 r.should eq [@op5, @op4]
383 it 'page=2なら中間2件を返す' do
384 r = OriginalPicture.mylist @artist, 2, 2
385 r.should eq [@op3, @op2]
387 it 'page=3なら先頭1件を返す' do
388 r = OriginalPicture.mylist @artist, 3, 2
392 context 'DBに5件あって1ページの件数を0件に変えたとして' do
394 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
395 @op3 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
396 @op4 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
397 @op5 = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
398 OriginalPicture.stub(:default_page_size).and_return(2)
\r
401 r = OriginalPicture.mylist @artist, 5, 0
402 r.should have(5).items
407 describe '更新履歴一覧取得に於いて' do
409 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
410 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
411 :original_picture_id => @op.id
412 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
418 it '他の原画の実素材は含んでいない' do
419 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 0,
420 :original_picture_id => @op2.id
424 it 'revisionで並んでいる' do
425 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :license_id => @license.id, :revision => 1,
426 :original_picture_id => @op.id
428 r.should eq [@p2, @p]
432 describe '単体取得に於いて' do
434 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
436 context 'つつがなく終わるとき' do
437 it '単体取得オプションを利用している' do
438 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
439 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
440 r = OriginalPicture.show @op.id, @artist
442 it '閲覧許可を問い合わせている' do
443 OriginalPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
444 OriginalPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
445 r = OriginalPicture.show @op.id, @artist
449 pic = OriginalPicture.show @op.id, @artist
452 context '他人の原画を開こうとしたとき' do
453 it '403Forbidden例外を返す' do
454 OriginalPicture.any_instance.stub(:visible?).and_return(false)
456 pic = OriginalPicture.show @op.id, @other_artist
457 }.should raise_error(ActiveRecord::Forbidden)
460 context '存在しない原画を開こうとしたとき' do
461 it '404RecordNotFound例外を返す' do
463 pic = OriginalPicture.show 0, @artist
464 }.should raise_error(ActiveRecord::RecordNotFound)
468 describe '編集取得に於いて' do
470 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
472 context 'つつがなく終わるとき' do
473 it '単体取得オプションを利用している' do
474 OriginalPicture.stub(:show_opt).with(any_args).and_return({})
475 OriginalPicture.should_receive(:show_opt).with(any_args).exactly(1)
476 r = OriginalPicture.edit @op.id, @artist
478 it '所持判定を問い合わせている' do
479 OriginalPicture.any_instance.stub(:own?).with(any_args).and_return(true)
480 OriginalPicture.any_instance.should_receive(:own?).with(any_args).exactly(1)
481 r = OriginalPicture.edit @op.id, @artist
485 pic = OriginalPicture.edit @op.id, @artist
488 context '他人の原画を開こうとしたとき' do
489 it '403Forbidden例外を返す' do
490 OriginalPicture.any_instance.stub(:own?).and_return(false)
492 r = OriginalPicture.edit @op.id, @other_artist
493 }.should raise_error(ActiveRecord::Forbidden)
496 context '存在しない原画を開こうとしたとき' do
497 it '404RecordNotFound例外を返す' do
499 r = OriginalPicture.edit 0, @artist
500 }.should raise_error(ActiveRecord::RecordNotFound)
504 describe '単体取得オプションに於いて' do
505 it 'includeキーを含んでいる' do
506 r = OriginalPicture.show_opt
507 r.has_key?(:include).should be_true
510 r = OriginalPicture.show_opt[:include]
511 r.should have(2).items
514 r = OriginalPicture.show_opt[:include]
515 r.has_key?(:resource_picture).should be_true
518 r = OriginalPicture.show_opt[:include]
519 r.has_key?(:pictures).should be_true
522 describe 'json単体出力オプションに於いて' do
524 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
\r
525 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
\r
526 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
\r
527 @sbt = FactoryGirl.create :speech_balloon_template
\r
528 @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
529 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
530 @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
533 r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
536 i.has_key?('resource_picture').should be_true
539 r = OriginalPicture.show(@op.id, @artist).to_json OriginalPicture.show_json_opt
542 i.has_key?('pictures').should be_true
545 describe '作成・更新に於いて' do
547 @op = FactoryGirl.build :original_picture, :artist_id => @artist.id
548 @imager = ImagerTest.load "abc\ndef\nghi"
552 #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
553 #それで外部のメソッド呼び出しだけに着目してテストする。
554 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
555 PictureIO.original_picture_io.stub(:put).with(any_args).and_return(true)
557 it '自身に属性をセットしている' do
558 OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
562 OriginalPicture.any_instance.should_receive(:save).exactly(1)
565 it 'PictureIoに画像データの保存を依頼している' do
566 PictureIO.original_picture_io.should_receive(:put).with(any_args).exactly(1)
570 context 'つつがなく終わるとき' do
573 it '自身に属性をセットしている' do
575 @op.width.should eq 3
576 @op.height.should eq 3
577 @op.filesize.should eq 9
579 it '原画モデルが作成されている' do
582 }.should change OriginalPicture, :count
586 OriginalPicture.find(@op).should_not be_nil
589 @op.store(@imager).should eq true
592 #以下から例外ケース。処理先頭から失敗させていく
593 context 'imagerが初期化に失敗したとき' do
597 @op.store(false).should be_false
599 it '自身の保存は呼ばれていない' do
600 OriginalPicture.any_instance.should_not_receive(:save)
603 it '全体エラーメッセージがセットされている' do
606 }.should change(@op.errors[:base], :count)
609 context '自身の保存に失敗したとき' do
611 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)
614 @op.store(@imager).should be_false
618 @op.should be_a_new OriginalPicture
620 it '原画の保存は呼ばれていない' do
621 PictureIO::LocalPicture.any_instance.should_not_receive(:put)
624 context '原画の保存に失敗したとき' do
626 OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)
627 PictureIO.original_picture_io.stub(:put).with(any_args).and_return(false)
630 @op.store(@imager).should be_false
634 @op.should be_a_new OriginalPicture
636 it '全体エラーメッセージがセットされている' do
639 }.should change(@op.errors[:base], :count)
645 describe 'エクスポートに於いて' do
647 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
648 @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0
649 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id
651 @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id
653 context 'つつがなく終わるとき' do
654 it 'ライセンスグループに依頼してリストを取得している' do
655 LicenseGroup.stub(:list).with(any_args).and_return([@lg])
656 LicenseGroup.should_receive(:list).with(any_args).exactly(1)
657 r = OriginalPicture.export(@artist)
659 it '原画に依頼してリストを取得している' do
660 OriginalPicture.stub(:list).with(any_args).and_return([@op])
661 OriginalPicture.should_receive(:list).with(any_args).exactly(1)
662 r = OriginalPicture.export(@artist)
665 r = OriginalPicture.export(@artist)
666 r.is_a?(Hash).should be_true
668 it 'Hashはライセンスグループを含んでいる' do
669 r = OriginalPicture.export(@artist)
670 r.include?(:license_groups).should be_true
672 it 'Hashは原画を含んでいる' do
673 r = OriginalPicture.export(@artist)
674 r.include?(:original_pictures).should be_true
676 #素材がライセンスされていないケースもある
677 it 'Hashの原画は素材を含んでいる' do
678 r = OriginalPicture.export(@artist)
679 r[:original_pictures].first.resource_picture.should_not be_nil
681 it 'Hashの原画は実素材を含んでいる' do
682 r = OriginalPicture.export(@artist)
683 r[:original_pictures].first.pictures.should be_nil
686 context '実データ単体のとき' do
687 it 'ライセンスは配列構造になっている' do
688 r = OriginalPicture.export(@artist)
689 r[:license_groups].is_a?(Array).should be_true
692 r = OriginalPicture.export(@artist)
693 r[:license_groups].size.should eq 1
694 r[:license_groups].first.should eq @lg
696 it '原画は配列構造になっている' do
697 r = OriginalPicture.export(@artist)
698 r[:original_pictures].is_a?(Array).should be_true
701 r = OriginalPicture.export(@artist)
702 r[:original_pictures].size.should eq 1
703 r[:original_pictures].first.should eq @op
705 it '原画に素材が関連付いている' do
706 r = OriginalPicture.export(@artist)
707 i = r[:original_pictures].first
708 i.resource_picture.should eq @rp
710 it '原画に実素材が関連付いている' do
711 r = OriginalPicture.export(@artist)
712 i = r[:original_pictures].first
713 i.picture.should eq @p
716 context '実データ複数のとき' do
718 @lg2 = FactoryGirl.create :license_group, :name => 'export test', :url => 'http://export.test/'
719 @license2 = FactoryGirl.create :license, :license_group_id => @lg2.id, :system_picture_id => @sp.id, :name => 'export test license', :url => 'http://export.test/license'
720 @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id
721 @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :revision => 0
722 @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :picture_id => @p2.id
724 it 'ライセンスは配列構造になっている' do
725 r = OriginalPicture.export(@artist)
726 r[:license_groups].is_a?(Array).should be_true
729 r = OriginalPicture.export(@artist)
730 r[:license_groups].size.should eq 2
731 r[:license_groups].first.should eq @lg
732 r[:license_groups].last.should eq @lg2
734 it '原画は配列構造になっている' do
735 r = OriginalPicture.export(@artist)
736 r[:original_pictures].is_a?(Array).should be_true
739 r = OriginalPicture.export(@artist)
740 r[:original_pictures].size.should eq 2
741 r[:original_pictures].first.should eq @op
742 r[:original_pictures].last.should eq @op2
744 it '原画に素材が関連付いている' do
745 r = OriginalPicture.export(@artist)
746 i = r[:original_pictures].first
747 i.resource_picture.should eq @rp
748 i2 = r[:original_pictures].last
749 i2.resource_picture.should eq @rp2
751 it '原画に実素材が関連付いている' do
752 r = OriginalPicture.export(@artist)
753 i = r[:original_pictures].first
754 i.picture.should eq @p
755 i2 = r[:original_pictures].last
756 i2.picture.should eq @p2
761 describe 'エクスポートオプションに於いて' do
764 describe 'インポートに於いて' do
766 @imports = {:licenses => {}, :artist_id => @artist.id}
768 context '事前チェックしておく' do