OSDN Git Service

Merge branch 'v04' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v04license
[pettanr/pettanr.git] / spec / models / original_picture_spec.rb
index 2b896d6..0ccc021 100644 (file)
@@ -1,5 +1,485 @@
+# -*- encoding: utf-8 -*-
 require 'spec_helper'
 
 describe OriginalPicture do
-  pending "add some examples to (or delete) #{__FILE__}"
+  before do
+    Factory :admin
+    @user = Factory( :user_yas)
+    @author = @user.author
+    @artist = Factory :artist_yas, :author_id => @author.id
+    @other_user = Factory( :user_yas)
+    @other_author = @other_user.author
+    @other_artist = Factory :artist_yas, :author_id => @other_author.id
+    @sp = Factory :system_picture
+    @lg = Factory :license_group
+    @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+  end
+  
+  describe '検証に於いて' do
+    before do
+    end
+    
+    it 'オーソドックスなデータなら通る' do
+      @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
+      @op.should be_valid
+    end
+    
+    context 'extを検証するとき' do
+      before do
+        @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
+      end
+      it 'テストデータの確認' do
+        @op.ext = 'jpeg'
+        @op.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @op.ext = ''
+        @op.should_not be_valid
+      end
+      it '5文字以上なら失敗する' do
+        @op.ext = 'a'*5
+        @op.should_not be_valid
+      end
+      it 'png,gif,jpeg以外なら失敗する' do
+        @op.ext = 'bmp'
+        @op.should_not be_valid
+      end
+    end
+    context 'widthを検証するとき' do
+      before do
+        @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
+      end
+      it 'テストデータの確認' do
+        @op.width = 1
+        @op.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @op.width = nil
+        @op.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @op.width = 'a'
+        @op.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @op.width = '0'
+        @op.should_not be_valid
+      end
+      it '負でも失敗する' do
+        @op.width = -1
+        @op.should_not be_valid
+      end
+    end
+    context 'heightを検証するとき' do
+      before do
+        @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
+      end
+      it 'テストデータの確認' do
+        @op.height = 1
+        @op.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @op.height = nil
+        @op.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @op.height = 'a'
+        @op.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @op.height = '0'
+        @op.should_not be_valid
+      end
+      it '負でも失敗する' do
+        @op.height = -1
+        @op.should_not be_valid
+      end
+    end
+    context 'filesizeを検証するとき' do
+      before do
+        @op = Factory.build :original_picture, :artist_id => @artist.id, :license_id => @license.id
+      end
+      it 'テストデータの確認' do
+        @op.filesize = 1
+        @op.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @op.filesize = nil
+        @op.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @op.filesize = 'a'
+        @op.should_not be_valid
+      end
+      it '負なら失敗する' do
+        @op.filesize = '-1'
+        @op.should_not be_valid
+      end
+      it '2MB以上なら失敗する' do
+        @op.filesize = 2000000+1
+        @op.should_not be_valid
+      end
+    end
+    context 'artist_idを検証するとき' do
+      before do
+        @op = Factory.build :original_picture, :license_id => @license.id
+      end
+      it 'テストデータの確認' do
+        @op.artist_id = @artist.id
+        @op.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @op.artist_id = nil
+        @op.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @op.artist_id = 'a'
+        @op.should_not be_valid
+      end
+      it '存在する絵師でなければ失敗する' do
+        @op.artist_id = 0
+        @op.should_not be_valid
+      end
+    end
+    context 'license_idを検証するとき' do
+      before do
+        @op = Factory.build :original_picture, :artist_id => @artist.id
+      end
+      it 'テストデータの確認' do
+        @op.license_id = @license.id
+        @op.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @op.license_id = nil
+        @op.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @op.license_id = 'a'
+        @op.should_not be_valid
+      end
+      it '存在するlicenseでなければ失敗する' do
+        @op.license_id = 0
+        @op.should_not be_valid
+      end
+    end
+  end
+  
+  describe 'データ補充に於いて' do
+    before do
+    end
+    
+  end
+  
+  describe '作者判定に於いて' do
+    before do
+    end
+    it '自作の原画ならyes' do
+      op = Factory :original_picture, :artist_id => @artist.id
+      op.own?(@author).should == true
+    end
+    it '他人のならno' do
+      op = Factory :original_picture, :artist_id => @other_artist.id
+      op.own?(@author).should == false
+    end
+    it '作家が不明ならno' do
+      op = Factory :original_picture, :artist_id => @artist.id
+      op.own?(nil).should == false
+    end
+    it '作家が絵師でないならno' do
+      other_user = Factory( :user_yas)
+      op = Factory :original_picture, :artist_id => @artist.id
+      op.own?(@other_author).should == false
+    end
+  end
+  describe '単体取得に於いて' do
+    before do
+      @op = Factory :original_picture, :artist_id => @artist.id
+    end
+    it '指定の原画を返す' do
+      pic = OriginalPicture.show @op.id, @artist
+      pic.should eq @op
+    end
+    context '関連テーブルオプションがないとき' do
+      it 'ライセンスと素材を含んでいる' do
+        r = OriginalPicture.show_include_opt
+        r.should eq [:license, :resource_picture]
+      end
+    end
+    context '関連テーブルオプションで絵師を含ませたとき' do
+      it 'ライセンスと素材と作者データを含んでいる' do
+        r = OriginalPicture.show_include_opt(:include => :artist)
+        r.should eq [:license, :resource_picture, :artist]
+      end
+    end
+    context '他人の原画を開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        lambda{
+          pic = OriginalPicture.show @op.id, @other_artist
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しない原画を開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          pic = OriginalPicture.show 0, @artist
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  describe 'json単体出力オプションに於いて' do
+    it 'includeキーがライセンスと素材を含んでいる' do
+      r = OriginalPicture.show_json_include_opt
+      r[:include].should eq [:license, :resource_picture]
+    end
+  end
+  describe '一覧取得に於いて' do
+    before do
+      @op = Factory :original_picture, :artist_id => @artist.id
+    end
+    context 'page補正について' do
+      it '文字列から数値に変換される' do
+        OriginalPicture.page('8').should eq 8
+      end
+      it 'nilの場合は1になる' do
+        OriginalPicture.page().should eq 1
+      end
+      it '0以下の場合は1になる' do
+        OriginalPicture.page('0').should eq 1
+      end
+    end
+    context 'page_size補正について' do
+      it '文字列から数値に変換される' do
+        OriginalPicture.page_size('7').should eq 7
+      end
+      it 'nilの場合はOriginalPicture.default_page_sizeになる' do
+        OriginalPicture.page_size().should eq OriginalPicture.default_page_size
+      end
+      it '0以下の場合はOriginalPicture.default_page_sizeになる' do
+        OriginalPicture.page_size('0').should eq OriginalPicture.default_page_size
+      end
+      it 'OriginalPicture.max_page_sizeを超えた場合はOriginalPicture.max_page_sizeになる' do
+        OriginalPicture.page_size('1000').should eq OriginalPicture.max_page_size
+      end
+    end
+    it 'リストを返す' do
+      pic = OriginalPicture.list @artist.id
+      pic.should eq [@op]
+    end
+    it '他人の原画は含んでいない' do
+      Factory :original_picture, :artist_id => @other_artist.id
+      pic = OriginalPicture.list @artist.id
+      pic.should eq [@op]
+    end
+    it '時系列で並んでいる' do
+      newpic = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
+      pic = OriginalPicture.list @artist.id
+      pic.should eq [newpic, @op]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @op2 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 100
+        @op3 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 200
+        @op4 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 300
+        @op5 = Factory :original_picture, :artist_id => @artist.id, :updated_at => Time.now + 400
+        OriginalPicture.stub(:default_page_size).and_return(2)
+      end
+      it '通常は2件を返す' do
+        pic = OriginalPicture.list @artist.id
+        pic.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        pic = OriginalPicture.list( @artist.id, {}, 1)
+        pic.should eq [@op5, @op4]
+      end
+      it 'page=2なら中間2件を返す' do
+        pic = OriginalPicture.list( @artist.id, {}, 2)
+        pic.should eq [@op3, @op2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        pic = OriginalPicture.list( @artist.id, {}, 3)
+        pic.should eq [@op]
+      end
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    it 'includeキーがライセンスと素材を含んでいる' do
+      r = OriginalPicture.list_json_opt
+      r[:include].should eq [:license, :resource_picture]
+    end
+  end
+  describe 'RMagick変換に於いて' do
+    before do
+      @op = Factory.build :original_picture, :artist_id => @artist.id
+    end
+    context 'つつがなく終わるとき' do
+      it '画像データをオブジェクト化している' do
+        Magick::Image.stub(:from_blob).and_return(['mgkobj'])
+        @op.data_to_mgk('mgkbin').should eq 'mgkobj'
+      end
+    end
+    context '失敗するとき' do
+      it 'Falseを返す' do
+        Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
+        @op.data_to_mgk('mgkbin').should be_false
+      end
+      it '全体エラーがセットされている' do
+        Magick::Image.should_receive(:from_blob).with(any_args).and_raise('StandardError')
+        lambda {
+          @op.data_to_mgk('mgkbin')
+        }.should change(@op.errors[:base], :count)
+      end
+    end
+  end
+  describe '作成・更新に於いて' do
+    before do
+      @op = Factory.build :original_picture, :artist_id => @artist.id
+      #RMagickのスタブをおいておく
+      class Mgk ; class Image ; end ; end
+      @filesize = 76543
+      Mgk::Image.stub(:from_blob).with(any_args).and_return([Mgk.new])
+      Mgk.any_instance.stub(:format).with(any_args).and_return('png')
+      Mgk.any_instance.stub(:rows).with(any_args).and_return(200)
+      Mgk.any_instance.stub(:columns).with(any_args).and_return(100)
+      Mgk.any_instance.stub(:filesize).with(any_args).and_return(@filesize)
+      Mgk.any_instance.stub(:to_blob).with(any_args).and_return('data')
+      #原画ファイル削除だけは必ず成功するものとしておく
+      PictureIO::LocalPicture.any_instance.stub(:delete).with(any_args).and_return(true)
+      #素材取得は新規作成ケースでテストしておく
+      ResourcePicture.stub(:update_picture).with(any_args).and_return(
+        Factory.build :resource_picture, :artist_id => @artist.id
+      )
+    end
+    context '事前チェック' do
+      before do
+        #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
+        #それで外部のメソッド呼び出しだけに着目してテストする。
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
+        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
+        ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
+      end
+      it 'RMagick変換を依頼している' do
+        OriginalPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
+        @op.store 'bindata', @artist, @license.id\r
+      end
+      it '自身に属性をセットしている' do
+        OriginalPicture.any_instance.should_receive(:attributes=).exactly(1)
+        @op.store 'bindata', @artist, @license.id\r
+      end
+      it '自身が保存されている' do
+        OriginalPicture.any_instance.should_receive(:save).exactly(1)\r
+        @op.store 'bindata', @artist, @license.id\r
+      end
+      it 'PictureIoに画像データの保存を依頼している' do
+        PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
+        @op.store 'bindata', @artist, @license.id\r
+      end
+      it '素材モデルに対象素材を問い合わせている' do
+        ResourcePicture.should_receive(:update_picture).with(any_args).exactly(1)
+        @op.store 'bindata', @artist, @license.id\r
+      end
+      it '対象素材オブジェクトに保存を依頼している' do
+        ResourcePicture.any_instance.should_receive(:store).with(any_args).exactly(1)
+        @op.store 'bindata', @artist, @license.id\r
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        #すべての処理を正常パターンで通過させ、保存機能をチェックする。
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+#        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
+        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
+        ResourcePicture.any_instance.stub(:store).with(any_args).and_return(true)
+      end
+      it '自身に属性をセットしている' do
+        lambda {
+          @op.store 'bindata', @artist, @license.id\r
+        }.should change @op, :filesize
+      end
+      it '原画モデルが作成されている' do
+        lambda {
+          @op.store 'bindata', @artist, @license.id
+        }.should change OriginalPicture, :count
+      end
+      it '原画が保存されている' do
+        @op.store 'bindata', @artist, @license.id
+        OriginalPicture.find(@op).should_not be_nil\r
+      end
+      it 'Trueを返す' do
+        @op.store('bindata', @artist, @license.id).should eq true\r
+      end
+    end
+    #以下から例外ケース。処理先頭から失敗させていく
+    context 'RMagick変換が失敗したとき' do
+      before do
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
+      end
+      it 'falseを返す' do
+        @op.store('bindata', @artist, @license.id).should be_false\r
+      end
+      it '自身の保存は呼ばれていない' do
+        OriginalPicture.any_instance.should_not_receive(:save)\r
+      end
+    end
+    context '自身の保存に失敗したとき' do
+      before do
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(false)\r
+      end
+      it 'falseを返す' do
+        @op.store('bindata', @artist, @license.id).should be_false\r
+      end
+      it '更新されていない' do
+        @op.store('bindata', @artist, @license.id)
+        @op.should be_a_new OriginalPicture\r
+      end
+      it '原画の保存は呼ばれていない' do
+        PictureIO::LocalPicture.any_instance.should_not_receive(:put)\r
+      end
+    end
+    context '原画の保存に失敗したとき' do
+      before do
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
+        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(false)
+      end
+      it 'falseを返す' do
+        @op.store('bindata', @artist, @license.id).should be_false\r
+      end
+      it '更新されていない' do
+        @op.store('bindata', @artist, @license.id)\r
+        @op.should be_a_new OriginalPicture\r
+      end
+      it '対象素材オブジェクトの保存は呼ばれていない' do
+        ResourcePicture.any_instance.should_not_receive(:save)\r
+        @op.store('bindata', @artist, @license.id)\r
+      end
+      it '全体エラーメッセージがセットされている' do
+        lambda {
+          @op.store('bindata', @artist, @license.id)\r
+        }.should change(@op.errors[:base], :count)
+      end
+    end
+    context '対象素材オブジェクトの保存に失敗したとき' do
+      before do
+        OriginalPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        OriginalPicture.any_instance.stub(:save).with(any_args).and_return(true)\r
+        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
+        ResourcePicture.any_instance.stub(:store).with(any_args).and_return(false)
+      end
+      it 'falseを返す' do
+        @op.store('bindata', @artist, @license.id).should be_false\r
+      end
+      it '更新されていない' do
+        @op.store('bindata', @artist, @license.id)\r
+        @op.should be_a_new OriginalPicture\r
+      end
+      it '全体エラーメッセージがセットされている' do
+        lambda {
+          @op.store('bindata', @artist, @license.id)\r
+        }.should change(@op.errors[:base], :count)
+      end
+    end
+  end
 end