OSDN Git Service

merge
[pettanr/pettanr.git] / spec / models / system_picture_spec.rb
index cc05684..de784bd 100644 (file)
@@ -1,5 +1,485 @@
+# -*- encoding: utf-8 -*-
 require 'spec_helper'
-
+#システム画像
 describe SystemPicture do
-  pending "add some examples to (or delete) #{__FILE__}"
+  before do
+    @admin = FactoryGirl.create :admin
+  end
+  
+  describe '検証に於いて' do
+    before do
+      @sp = FactoryGirl.build :system_picture
+    end
+    
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @sp.ext = 'png' #リストにない拡張子は通らないし
+        @sp.width = 1
+        @sp.height = 1
+        @sp.filesize = 1
+        @sp.md5 = 'a'*32
+        @sp.should be_valid
+      end
+      it '上限データが通る' do
+        @sp.ext = 'jpeg'
+        @sp.width = 99999
+        @sp.height = 99999
+        @sp.filesize = 2000000
+        @sp.md5 = 'a'*32
+        @sp.should be_valid
+      end
+    end
+    
+    context 'extを検証するとき' do
+      it 'nullなら失敗する' do
+        @sp.ext = ''
+        @sp.should_not be_valid
+      end
+      it '5文字以上なら失敗する' do
+        @sp.ext = 'a'*5
+        @sp.should_not be_valid
+      end
+      it 'png,gif,jpeg以外なら失敗する' do
+        @sp.ext = 'bmp'
+        @sp.should_not be_valid
+      end
+    end
+    context 'widthを検証するとき' do
+      it 'nullなら失敗する' do
+        @sp.width = nil
+        @sp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @sp.width = 'a'
+        @sp.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @sp.width = '0'
+        @sp.should_not be_valid
+      end
+      it '負でも失敗する' do
+        @sp.width = -1
+        @sp.should_not be_valid
+      end
+    end
+    context 'heightを検証するとき' do
+      it 'nullなら失敗する' do
+        @sp.height = nil
+        @sp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @sp.height = 'a'
+        @sp.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @sp.height = '0'
+        @sp.should_not be_valid
+      end
+      it '負でも失敗する' do
+        @sp.height = -1
+        @sp.should_not be_valid
+      end
+    end
+    context 'filesizeを検証するとき' do
+      it 'nullなら失敗する' do
+        @sp.filesize = nil
+        @sp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @sp.filesize = 'a'
+        @sp.should_not be_valid
+      end
+      it '負なら失敗する' do
+        @sp.filesize = '-1'
+        @sp.should_not be_valid
+      end
+      it '2MB以上なら失敗する' do
+        @sp.filesize = 2000000+1
+        @sp.should_not be_valid
+      end
+    end
+    context 'md5を検証するとき' do
+      it 'nullなら失敗する' do
+        @sp.md5 = ''
+        @sp.should_not be_valid
+      end
+      it '31文字なら失敗する' do
+        @sp.md5 = 'a'*31
+        @sp.should_not be_valid
+      end
+      it '33文字なら失敗する' do
+        @sp.md5 = 'a'*33
+        @sp.should_not be_valid
+      end
+    end
+  end
+  
+  describe 'デフォルト値補充に於いて' do
+    it 'defined' do
+      @sp = FactoryGirl.build :system_picture
+      @sp.supply_default
+    end
+  end
+  
+  describe '上書き補充に於いて' do
+    it 'defined' do
+      @sp = FactoryGirl.build :system_picture
+      @sp.overwrite
+    end
+  end
+  
+  describe '所持判定に於いて' do
+    before do
+      @sp = FactoryGirl.build :system_picture
+    end
+    it '管理者のアクセス権はすべて等しいので、Trueを返す' do
+      @sp.own?(@admin).should == true
+    end
+  end
+  
+  describe '閲覧許可に於いて' do
+    before do
+      @sp = FactoryGirl.build :system_picture
+    end
+    it '必ず許可となる' do
+      r = @sp.visible?(@admin)
+      r.should == true
+    end
+  end
+  
+  describe 'ファイル名に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+    end
+    it 'id+拡張子のフォーマットで返す' do
+      r = @sp.filename
+      r.should eq "#{@sp.id}.png"
+    end
+  end
+  
+  describe 'MimeTypeに於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+    end
+    it 'image/拡張子のフォーマットで返す' do
+      r = @sp.mime_type
+      r.should eq "image/png"
+    end
+  end
+  
+  describe 'ファイルのurlに於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      SystemPicture.any_instance.stub(:filename).and_return('3.gif')
+    end
+    it 'ファイル名取得を依頼している' do
+      SystemPicture.any_instance.should_receive(:filename).exactly(1)
+      @sp.url
+    end
+    it '/original_pictures/3.gifのフォーマットで返す' do
+      r = @sp.url
+      r.should eq "/system_pictures/3.gif"
+    end
+  end
+  
+  describe '一覧取得に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+    end
+    context 'page補正について' do
+      it '文字列から数値に変換される' do
+        SystemPicture.page('8').should eq 8
+      end
+      it 'nilの場合は1になる' do
+        SystemPicture.page().should eq 1
+      end
+      it '0以下の場合は1になる' do
+        SystemPicture.page('0').should eq 1
+      end
+    end
+    context 'page_size補正について' do
+      it '文字列から数値に変換される' do
+        SystemPicture.page_size('7').should eq 7
+      end
+      it 'nilの場合はSystemPicture.default_page_sizeになる' do
+        SystemPicture.page_size().should eq SystemPicture.default_page_size
+      end
+      it '0以下の場合はSystemPicture.default_page_sizeになる' do
+        SystemPicture.page_size('0').should eq SystemPicture.default_page_size
+      end
+      it 'SystemPicture.max_page_sizeを超えた場合はSystemPicture.max_page_sizeになる' do
+        SystemPicture.page_size('1000').should eq SystemPicture.max_page_size
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        SystemPicture.stub(:list_opt).with(any_args).and_return({})
+        SystemPicture.should_receive(:list_opt).with(any_args).exactly(1)
+        r = SystemPicture.list
+      end
+    end
+    it 'リストを返す' do
+      r = SystemPicture.list
+      r.should eq [@sp]
+    end
+    it '時系列で並んでいる' do
+      ni = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
+      r = SystemPicture.list
+      r.should eq [ni, @sp]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @sp2 = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
+        @sp3 = FactoryGirl.create :system_picture, :updated_at => Time.now + 200
+        @sp4 = FactoryGirl.create :system_picture, :updated_at => Time.now + 300
+        @sp5 = FactoryGirl.create :system_picture, :updated_at => Time.now + 400
+        SystemPicture.stub(:default_page_size).and_return(2)
+      end
+      it '通常は2件を返す' do
+        r = SystemPicture.list
+        r.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        r = SystemPicture.list 1, 2
+        r.should eq [@sp5, @sp4]
+      end
+      it 'page=2なら中間2件を返す' do
+        r = SystemPicture.list 2, 2
+        r.should eq [@sp3, @sp2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        r = SystemPicture.list 3, 2
+        r.should eq [@sp]
+      end
+    end
+    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+      before do
+        @sp2 = FactoryGirl.create :system_picture, :updated_at => Time.now + 100
+        @sp3 = FactoryGirl.create :system_picture, :updated_at => Time.now + 200
+        @sp4 = FactoryGirl.create :system_picture, :updated_at => Time.now + 300
+        @sp5 = FactoryGirl.create :system_picture, :updated_at => Time.now + 400
+        SystemPicture.stub(:default_page_size).and_return(2)
+      end
+      it '件数0は全件(5件)を返す' do
+        r = SystemPicture.list 5, 0
+        r.should have(5).items 
+      end
+    end
+  end
+  describe '一覧出力オプションに於いて' do
+    it 'Hashを返す' do
+      r = SystemPicture.list_opt
+      r.is_a?(Hash).should be_true
+    end
+    it '0の項目を含んでいる' do
+      r = SystemPicture.list_opt
+      r.should be_empty
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    it 'Hashを返す' do
+      r = SystemPicture.list_json_opt
+      r.is_a?(Hash).should be_true
+    end
+    it '0の項目を含んでいる' do
+      r = SystemPicture.list_json_opt
+      r.should be_empty
+    end
+  end
+  
+  describe '単体取得に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+    end
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        SystemPicture.stub(:show_opt).with(any_args).and_return({})
+        SystemPicture.should_receive(:show_opt).with(any_args).exactly(1)
+        r = SystemPicture.show @sp.id, @admin
+      end
+      it '閲覧許可を問い合わせている' do
+        SystemPicture.any_instance.stub(:visible?).with(any_args).and_return(true)
+        SystemPicture.any_instance.should_receive(:visible?).with(any_args).exactly(1)
+        r = SystemPicture.show @sp.id, @admin
+      end
+    end
+    it '指定のシステム画像を返す' do
+      r = SystemPicture.show @sp.id, @admin
+      r.should eq @sp
+    end
+    context '他人のシステム画像を開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        SystemPicture.any_instance.stub(:visible?).and_return(false)
+        lambda{
+          r = SystemPicture.show @sp.id, @admin
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しないシステム画像を開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          r = SystemPicture.show 0, @admin
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  describe '単体出力オプションに於いて' do
+    it 'Hashを返す' do
+      r = SystemPicture.show_opt
+      r.is_a?(Hash).should be_true
+    end
+    it '0の項目を含んでいる' do
+      r = SystemPicture.show_opt
+      r.should be_empty
+    end
+  end
+  describe 'json単体出力オプションに於いて' do
+    it 'Hashを返す' do
+      r = SystemPicture.show_json_opt
+      r.is_a?(Hash).should be_true
+    end
+    it '0の項目を含んでいる' do
+      r = SystemPicture.show_json_opt
+      r.should be_empty
+    end
+  end
+  
+  describe '作成・更新に於いて' do
+    before do
+      @sp = FactoryGirl.build :system_picture
+      @imager = ImagerTest.load "abc\ndef\nghi"
+    end
+    context '事前チェック' do
+      before do
+        #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
+        #それで外部のメソッド呼び出しだけに着目してテストする。
+        SystemPicture.any_instance.stub(:save).with(any_args).and_return(true)
+        PictureIO.system_picture_io.stub(:put).with(any_args).and_return(true)
+      end
+      it '自身が保存されている' do
+        SystemPicture.any_instance.should_receive(:save).exactly(1)
+        r = @sp.store @imager
+      end
+      it 'PictureIoに画像データの保存を依頼している' do
+        PictureIO.system_picture_io.should_receive(:put).with(any_args).exactly(1)
+        r = @sp.store @imager
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        #すべての処理を正常パターンで通過させ、保存機能をチェックする。
+        PictureIO.system_picture_io.stub(:put).with(any_args).and_return(true)
+      end
+      it 'システム画像モデルが作成されている' do
+        lambda {
+          r = @sp.store @imager
+        }.should change SystemPicture, :count
+      end
+      it 'Trueを返す' do
+        r = @sp.store @imager
+        r.should eq true
+      end
+    end
+    #以下から例外ケース。処理先頭から失敗させていく
+    context 'imagerが初期化に失敗したとき' do
+      before do
+      end
+      it 'falseを返す' do
+        @sp.store(false).should be_false
+      end
+      it '自身の保存は呼ばれていない' do
+        SystemPicture.any_instance.should_not_receive(:save)
+        @sp.store(false)
+      end
+      it '全体エラーメッセージがセットされている' do
+        lambda {
+          @sp.store(false)
+        }.should change(@sp.errors[:base], :count)
+      end
+    end
+    context '自身の保存に失敗したとき' do
+      before do
+        SystemPicture.any_instance.stub(:save).with(any_args).and_return(false)
+      end
+      it 'falseを返す' do
+        r = @sp.store @imager
+        r.should be_false
+      end
+      it '更新されていない' do
+        r = @sp.store @imager
+        @sp.should be_a_new SystemPicture
+      end
+      it '原画の保存は呼ばれていない' do
+        PictureIO.system_picture_io.should_not_receive(:put)
+        r = @sp.store @imager
+      end
+    end
+    context '画像データの保存に失敗したとき' do
+      before do
+        PictureIO.system_picture_io.stub(:put).with(any_args).and_raise(PictureIO::Error)
+      end
+      it 'falseを返す' do
+        r = @sp.store @imager
+        r.should be_false
+      end
+      it '更新されていない' do
+        r = @sp.store @imager
+        @sp.should be_a_new SystemPicture
+      end
+      it '全体エラーメッセージがセットされている' do
+        r = @sp.store @imager
+        @sp.errors[:base].should_not be_blank
+      end
+    end
+  end
+  
+  describe '置換に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      @imager = ImagerTest.load "abc\ndef\nghi"
+    end
+    context '事前チェック' do
+      before do
+        #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
+        #それで外部のメソッド呼び出しだけに着目してテストする。
+        SystemPicture.stub(:modify_object).with(any_args).and_return(@sp)
+        SystemPicture.any_instance.stub(:store).with(@imager).and_return(true)
+      end
+      it 'データ更新準備を依頼する' do
+        SystemPicture.should_receive(:modify_object).with(any_args).exactly(1)
+        r = SystemPicture.store @imager
+      end
+      it '作成依頼している' do
+        SystemPicture.any_instance.should_receive(:store).with(@imager).exactly(1)
+        r = SystemPicture.store @imager
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
+      end
+      it '自身に属性をセットしている' do
+        r = SystemPicture.store @imager
+        r.width.should eq @imager.width
+        r.height.should eq @imager.height
+        r.ext.should eq @imager.ext
+        r.filesize.should eq @imager.filesize
+        r.md5.should eq @imager.md5
+      end
+      it 'オブジェクトを返す' do
+        r = SystemPicture.store @imager
+        r.is_a?(SystemPicture).should eq true
+      end
+    end
+    context '保存失敗のとき' do
+      before do
+        SystemPicture.any_instance.stub(:store).with(any_args).and_return(false)
+      end
+      it 'nilを返す' do
+        r = SystemPicture.store @imager
+        r.should eq nil
+      end
+    end
+  end
 end