OSDN Git Service

speech balloon template changed
[pettanr/pettanr.git] / spec / models / system_picture_spec.rb
index cc05684..626ec47 100644 (file)
@@ -1,5 +1,284 @@
+# -*- encoding: utf-8 -*-
 require 'spec_helper'
-
+#システム画像
 describe SystemPicture do
-  pending "add some examples to (or delete) #{__FILE__}"
+  before do
+    @admin = Factory :admin
+    @license = Factory :license
+  end
+  
+  describe '検証に於いて' do
+    before do
+    end
+    
+    it 'オーソドックスなデータなら通る' do
+      @sp = Factory.build :system_picture
+      @sp.should be_valid
+    end
+    
+    context 'extを検証するとき' do
+      before do
+        @sp = Factory.build :system_picture
+      end
+      it 'テストデータの確認' do
+        @sp.ext = 'jpeg'
+        @sp.should be_valid
+      end
+      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
+      before do
+        @sp = Factory.build :system_picture
+      end
+      it 'テストデータの確認' do
+        @sp.width = 1
+        @sp.should be_valid
+      end
+      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
+      before do
+        @sp = Factory.build :system_picture
+      end
+      it 'テストデータの確認' do
+        @sp.height = 1
+        @sp.should be_valid
+      end
+      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
+      before do
+        @sp = Factory.build :system_picture
+      end
+      it 'テストデータの確認' do
+        @sp.filesize = 1
+        @sp.should be_valid
+      end
+      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
+      before do
+        @sp = Factory.build :system_picture
+      end
+      it 'テストデータの確認' do
+        @sp.md5 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
+        @sp.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @sp.md5 = ''
+        @sp.should_not be_valid
+      end
+      it '32文字以上なら失敗する' do
+        @sp.md5 = 'a'*33
+        @sp.should_not be_valid
+      end
+    end
+  end
+  
+  describe 'データ補充に於いて' do
+    before do
+    end
+    
+  end
+  
+  describe '作成・更新に於いて' do
+    before do
+      @sp = Factory.build :system_picture
+      #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')
+    end
+    context '事前チェック' do
+      before do
+        #すべての処理が正常パターンで通過すれば、一番深い分岐まで通る。
+        #それで外部のメソッド呼び出しだけに着目してテストする。
+        SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        SystemPicture.any_instance.stub(:save).with(any_args).and_return(true)
+        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
+      end
+      it 'RMagick変換を依頼している' do
+        SystemPicture.any_instance.should_receive(:data_to_mgk).exactly(1)
+        @sp.store 'bindata'
+      end
+      it '自身に属性をセットしている' do
+        SystemPicture.any_instance.should_receive(:attributes=).exactly(1)
+        @sp.store 'bindata'
+      end
+      it '自身が保存されている' do
+        SystemPicture.any_instance.should_receive(:save).exactly(1)
+        @sp.store 'bindata'
+      end
+      it 'PictureIoに画像データの保存を依頼している' do
+        PictureIO::LocalPicture.any_instance.should_receive(:put).with(any_args).exactly(1)
+        @sp.store 'bindata'
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        #すべての処理を正常パターンで通過させ、保存機能をチェックする。
+        SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+#        SystemPicture.any_instance.stub(:save).with(any_args).and_return(true)
+        PictureIO::LocalPicture.any_instance.stub(:put).with(any_args).and_return(true)
+      end
+      it '自身に属性をセットしている' do
+        lambda {
+          @sp.store 'bindata'
+        }.should change @sp, :filesize
+      end
+      it 'システム画像モデルが作成されている' do
+        lambda {
+          @sp.store 'bindata'
+        }.should change SystemPicture, :count
+      end
+      it 'システム画像が保存されている' do
+        @sp.store 'bindata'
+        SystemPicture.find(@sp).should_not be_nil
+      end
+      it 'Trueを返す' do
+        @sp.store('bindata').should eq true
+      end
+    end
+    #以下から例外ケース。処理先頭から失敗させていく
+    context 'RMagick変換が失敗したとき' do
+      before do
+        SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(false)
+      end
+      it 'falseを返す' do
+        @sp.store('bindata').should be_false
+      end
+      it '自身の保存は呼ばれていない' do
+        SystemPicture.any_instance.should_not_receive(:save)
+      end
+    end
+    context '自身の保存に失敗したとき' do
+      before do
+        SystemPicture.any_instance.stub(:data_to_mgk).with(any_args).and_return(Mgk.new)
+        SystemPicture.any_instance.stub(:save).with(any_args).and_return(false)
+      end
+      it 'falseを返す' do
+        @sp.store('bindata').should be_false
+      end
+      it '更新されていない' do
+        @sp.store('bindata')
+        @sp.should be_a_new SystemPicture
+      end
+      it '原画の保存は呼ばれていない' do
+        PictureIO::LocalPicture.any_instance.should_not_receive(:put)
+      end
+    end
+  end
+  
+  describe '置換に於いて' do
+    before do
+      @sp = Factory :system_picture
+    end
+    context '新規のとき' do
+      before do
+        SystemPicture.stub(:find_by_md5).with(any_args).and_return(nil)
+        SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
+      end
+      it '新規オブジェクト生成している' do
+        r = SystemPicture.store 'bindata'
+        r.should be_a_new SystemPicture
+      end
+      it '作成依頼している' do
+        SystemPicture.any_instance.should_receive(:store).with('bindata').exactly(1)
+        SystemPicture.store 'bindata'
+      end
+      it '保存された行を返す' do
+        r = SystemPicture.store 'bindata'
+        r.should_not eq @sp
+      end
+    end
+    context '既存のとき' do
+      before do
+        SystemPicture.stub(:find_by_md5).with(any_args).and_return(@sp)
+        SystemPicture.any_instance.stub(:store).with(any_args).and_return(true)
+      end
+      it '新規オブジェクト生成していない' do
+        SystemPicture.should_receive(:new).with(any_args).exactly(0)
+        SystemPicture.store 'bindata'
+      end
+      it '作成依頼している' do
+        SystemPicture.any_instance.should_receive(:store).with('bindata').exactly(1)
+        SystemPicture.store 'bindata'
+      end
+      it '保存された行を返す' do
+        r = SystemPicture.store 'bindata'
+        r.should eq @sp
+      end
+    end
+    context '保存失敗のとき' do
+      before do
+        SystemPicture.stub(:find_by_md5).with(any_args).and_return(@sp)
+        SystemPicture.any_instance.stub(:store).with(any_args).and_return(false)
+      end
+      it 'nilを返す' do
+        r = SystemPicture.store 'bindata'
+        r.should eq nil
+      end
+    end
+  end
 end