OSDN Git Service

passed. resource picture model test
authoryasushiito <yasushiito@git.sourceforge.jp>
Wed, 11 Apr 2012 09:15:41 +0000 (18:15 +0900)
committeryasushiito <yasushiito@git.sourceforge.jp>
Wed, 11 Apr 2012 09:15:41 +0000 (18:15 +0900)
app/models/resource_picture.rb
spec/models/original_picture_spec.rb

index be16276..77fc2dd 100644 (file)
@@ -4,6 +4,14 @@ class ResourcePicture < ActiveRecord::Base
   has_many :panel_pictures
   belongs_to :original_picture
   
+  validates :ext, :presence => true, :length => {:maximum => 4}, :inclusion => {:in => ['png', 'jpeg', 'gif']}\r
+  validates :width, :presence => true, :numericality => true, :natural_number => true
+  validates :height, :presence => true, :numericality => true, :natural_number => true
+  validates :filesize, :presence => true, :numericality => {:greater_than => 0, :less_than_or_equal_to => 2000000}, :natural_number => true
+  validates :artist_id, :presence => true, :numericality => true, :existence => true
+  validates :license_id, :presence => true, :numericality => true, :existence => true
+  validates :original_picture_id, :presence => true, :numericality => true, :existence => true
+  
   before_destroy :destroy_with_file
   
   def destroy_with_file
@@ -86,10 +94,10 @@ class ResourcePicture < ActiveRecord::Base
   end
   
   def self.png_to_gif(data)
+    res = nil
     begin
       mgk = Magick::Image.from_blob(data).shift
       mgk.format = 'gif'
-      mgk.to_blob
       res = mgk
     rescue
       res = false
@@ -102,7 +110,7 @@ class ResourcePicture < ActiveRecord::Base
     if res = self.save
       if res = self.store_picture(mgk)
         if self.to_gif?
-          if gifmgk = ResourcePicture.png_to_gif(mgk.to_blob)
+          if gifmgk = ResourcePicture.png_to_gif(mgk)
             res = self.store_picture(gifmgk)
           else
             res = false
@@ -116,7 +124,7 @@ class ResourcePicture < ActiveRecord::Base
   def store_picture(mgk)
     res = false
     PictureIO.resource_picture_io.class.subdirs.each do |d|
-      picdata = d.empty? ? mgk.to_blob : self.__send__(d, mgk)
+      picdata = d.empty? ? mgk.to_blob : self.__send__(d, mgk.to_blob)
       res = PictureIO.resource_picture_io.put(picdata, self.filename, d)
       break unless res
     end
index 5bdc074..8366b4d 100644 (file)
@@ -18,111 +18,145 @@ describe OriginalPicture do
     end
     
     it 'オーソドックスなデータなら通る' do
-      @op = Factory.build :original_picture, :artist_id => @artist.id
+      @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 = Factory.build :original_picture, :artist_id => @artist.id, :ext => ''
+        @op.ext = ''
         @op.should_not be_valid
       end
       it '5文字以上なら失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :ext => 'a'*5
+        @op.ext = 'a'*5
         @op.should_not be_valid
       end
       it 'png,gif,jpeg以外なら失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :ext => 'bmp'
+        @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 = Factory.build :original_picture, :artist_id => @artist.id, :width => nil
+        @op.width = nil
         @op.should_not be_valid
       end
       it '数値でなければ失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :width => 'a'
+        @op.width = 'a'
         @op.should_not be_valid
       end
       it '0なら失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :width => '0'
+        @op.width = '0'
         @op.should_not be_valid
       end
       it '負でも失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :width => -1
+        @op.width = -1
         @op.should_not be_valid
       end
-      it '正なら通る' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :width => 1
-        @op.should 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 = Factory.build :original_picture, :artist_id => @artist.id, :height => nil
+        @op.height = nil
         @op.should_not be_valid
       end
       it '数値でなければ失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :height => 'a'
+        @op.height = 'a'
         @op.should_not be_valid
       end
       it '0なら失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :height => '0'
+        @op.height = '0'
         @op.should_not be_valid
       end
       it '負でも失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :height => -1
+        @op.height = -1
         @op.should_not be_valid
       end
-      it '正なら通る' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :height => 1
-        @op.should 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 = Factory.build :original_picture, :artist_id => @artist.id, :filesize => nil
+        @op.filesize = nil
         @op.should_not be_valid
       end
       it '数値でなければ失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => 'a'
+        @op.filesize = 'a'
         @op.should_not be_valid
       end
       it '負なら失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => '-1'
+        @op.filesize = '-1'
         @op.should_not be_valid
       end
       it '2MB以上なら失敗する' do
-        @op = Factory.build :original_picture, :artist_id => @artist.id, :filesize => 2000000+1
+        @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 = Factory.build :original_picture, :artist_id => nil
+        @op.artist_id = nil
         @op.should_not be_valid
       end
       it '数値でなければ失敗する' do
-        @op = Factory.build :original_picture, :artist_id => 'a'
+        @op.artist_id = 'a'
         @op.should_not be_valid
       end
       it '存在する絵師でなければ失敗する' do
-        @op = Factory.build :original_picture, :artist_id => 0
+        @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 = Factory.build :original_picture, :artist_id => @artist.id, :license_id => nil
+        @op.license_id = nil
         @op.should_not be_valid
       end
       it '数値でなければ失敗する' do
-        @op = Factory.build :original_picture, :artist_id => 'a', :license_id => 'a'
+        @op.license_id = 'a'
         @op.should_not be_valid
       end
-      it '存在する絵師でなければ失敗する' do
-        @op = Factory.build :original_picture, :artist_id => 0, :license_id => 0
+      it '存在するlicenseでなければ失敗する' do
+        @op.license_id = 0
         @op.should_not be_valid
       end
     end
@@ -272,6 +306,7 @@ describe OriginalPicture do
   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])
@@ -280,14 +315,17 @@ describe OriginalPicture do
       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)
@@ -320,6 +358,7 @@ describe OriginalPicture do
     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)
@@ -343,7 +382,7 @@ describe OriginalPicture 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)