OSDN Git Service

t#29400
[pettanr/pettanr.git] / spec / models / panel_picture_spec.rb
index 8842c55..bed7168 100644 (file)
@@ -3,50 +3,62 @@ require 'spec_helper'
 #コマ絵
 describe PanelPicture do
   before do
-    Factory :admin
-    @user = Factory( :user_yas)
+    FactoryGirl.create :admin
+    @user = FactoryGirl.create( :user_yas)
     @author = @user.author
-    @artist = Factory :artist_yas, :author_id => @author.id
-    @sp = Factory :system_picture
-    @lg = Factory :license_group
-    @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
-    @op = Factory :original_picture, :artist_id => @artist.id
-    @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
-    @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
-    @panel = Factory :panel, :author_id => @author.id
+    @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
+    @other_user = FactoryGirl.create( :user_yas)
+    @other_author = @other_user.author
+    @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
+    @sp = FactoryGirl.create :system_picture
+    @lg = FactoryGirl.create :license_group
+    @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+    @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+    @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+    @panel = FactoryGirl.create :panel, :author_id => @author.id
   end
   
   describe '検証に於いて' do
     before do
-    end
-    it 'オーソドックスなデータなら通る' do
-      @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
-      @pp.save!
-      @pp.should be_valid
+      @pp = FactoryGirl.build :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
+        :width => @p.width, :height => @p.height
+      Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(0)
+      Picture.any_instance.stub(:flag_resize).with(any_args).and_return(0)
+      Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(0)
     end
     
-    context 'panel_idを検証するとき' do
-      before do
-        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @pp.link = 'http://test.jp/'
+        @pp.x = -99999
+        @pp.y = -99999
+        @pp.z = 1
+        @pp.t = 0
+        @pp.width = -99999
+        @pp.height = -99999
+        @pp.should be_valid
       end
-      #ネストの保存はnilを許可しなければならないので数値チェックだけ
-      it 'テストデータの確認' do
-        @pp.panel_id = @panel.id
+      it '上限データが通る' do
+        @pp.link = 'http://test.jp/aaaaa' + 'a' * 180
+        @pp.x = 99999
+        @pp.y = 99999
+        @pp.z = 99999
+        @pp.t = 99999
+        @pp.width = 99999
+        @pp.height = 99999
         @pp.should be_valid
       end
+    end
+    
+    context 'panel_idを検証するとき' do
+      #ネストの保存はnilを許可しなければならないので数値チェックだけ
       it '数値でなければ失敗する' do
         @pp.panel_id = 'a'
         @pp.should_not be_valid
       end
     end
     context 'linkを検証するとき' do
-      before do
-        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
-      end
-      it 'テストデータの確認' do
-        @pp.link = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'*4
-        @pp.should be_valid
-      end
       it 'nullでも通る' do
         @pp.link = ''
         @pp.should be_valid
@@ -55,15 +67,12 @@ describe PanelPicture do
         @pp.link = 'a'*201
         @pp.should_not be_valid
       end
+      it 'url形式でないなら失敗する' do
+        @pp.link = 'a'*200\r
+        @pp.should_not be_valid\r
+      end
     end
     context 'xを検証するとき' do
-      before do
-        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
-      end
-      it 'テストデータの確認' do
-        @pp.x = '1'
-        @pp.should be_valid
-      end
       it 'nullなら失敗する' do
         @pp.x = nil
         @pp.should_not be_valid
@@ -82,13 +91,6 @@ describe PanelPicture do
       end
     end
     context 'yを検証するとき' do
-      before do
-        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
-      end
-      it 'テストデータの確認' do
-        @pp.y = '1'
-        @pp.should be_valid
-      end
       it 'nullなら失敗する' do
         @pp.y = nil
         @pp.should_not be_valid
@@ -107,13 +109,6 @@ describe PanelPicture do
       end
     end
     context 'widthを検証するとき' do
-      before do
-        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
-      end
-      it 'テストデータの確認' do
-        @pp.width = 1
-        @pp.should be_valid
-      end
       it 'nullなら失敗する' do
         @pp.width = nil
         @pp.should_not be_valid
@@ -126,19 +121,59 @@ describe PanelPicture do
         @pp.width = '0'
         @pp.should_not be_valid
       end
-      it '負でも通る' do
-        @pp.width = -1
-        @pp.should be_valid
+      context  '反転許可のとき' do
+        before do
+        end
+        it '負でも通る' do
+          @pp.width = -1
+          @pp.should be_valid
+        end
+      end
+      context  '反転禁止のとき' do
+        before do
+          Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(-1)
+        end
+        it '負なら失敗する' do
+          @pp.width = -1
+          @pp.should_not be_valid
+        end
+      end
+      context  'サイズ変更許可のとき' do
+        before do
+        end
+        it '実素材のサイズと違っても通る' do
+          @pp.width = @p.width-1
+          @pp.should be_valid
+        end
+      end
+      context  'サイズ変更禁止のとき' do
+        before do
+          Picture.any_instance.stub(:flag_resize).with(any_args).and_return(-1)
+        end
+        it '実素材のサイズと違うなら失敗する' do
+          @pp.width = @p.width-1
+          @pp.should_not be_valid
+        end
+      end
+      context  '縦横比変更許可のとき' do
+        before do
+        end
+        it '実素材の縦横比と違っても通る' do
+          @pp.width = @p.width / 2
+          @pp.should be_valid
+        end
+      end
+      context  '縦横比変更禁止のとき' do
+        before do
+          Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(-1)
+        end
+        it '実素材の縦横比と違うなら失敗する' do
+          @pp.width = @p.width / 2
+          @pp.should_not be_valid
+        end
       end
     end
     context 'heightを検証するとき' do
-      before do
-        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
-      end
-      it 'テストデータの確認' do
-        @pp.height = '1'
-        @pp.should be_valid
-      end
       it 'nullなら失敗する' do
         @pp.height = nil
         @pp.should_not be_valid
@@ -155,15 +190,59 @@ describe PanelPicture do
         @pp.height = -1
         @pp.should be_valid
       end
+      context  '反転許可のとき' do
+        before do
+        end
+        it '負でも通る' do
+          @pp.height = -1
+          @pp.should be_valid
+        end
+      end
+      context  '反転禁止のとき' do
+        before do
+          Picture.any_instance.stub(:flag_reverse).with(any_args).and_return(-1)
+        end
+        it '負なら失敗する' do
+          @pp.height = -1
+          @pp.should_not be_valid
+        end
+      end
+      context  'サイズ変更許可のとき' do
+        before do
+        end
+        it '実素材のサイズと違っても通る' do
+          @pp.height = @p.height-1
+          @pp.should be_valid
+        end
+      end
+      context  'サイズ変更禁止のとき' do
+        before do
+          Picture.any_instance.stub(:flag_resize).with(any_args).and_return(-1)
+        end
+        it '実素材のサイズと違うなら失敗する' do
+          @pp.height = @p.height-1
+          @pp.should_not be_valid
+        end
+      end
+      context  '縦横比変更許可のとき' do
+        before do
+        end
+        it '実素材の縦横比と違っても通る' do
+          @pp.height = @p.height / 2
+          @pp.should be_valid
+        end
+      end
+      context  '縦横比変更禁止のとき' do
+        before do
+          Picture.any_instance.stub(:flag_sync_vh).with(any_args).and_return(-1)
+        end
+        it '実素材の縦横比と違うなら失敗する' do
+          @pp.height = @p.height / 2
+          @pp.should_not be_valid
+        end
+      end
     end
     context 'zを検証するとき' do
-      before do
-        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
-      end
-      it 'テストデータの確認' do
-        @pp.z = 1
-        @pp.should be_valid
-      end
       it 'nullなら失敗する' do
         @pp.z = nil
         @pp.should_not be_valid
@@ -182,13 +261,6 @@ describe PanelPicture do
       end
     end
     context 'tを検証するとき' do
-      before do
-        @pp = Factory.build :panel_picture, :panel_id => @panel.id, :resource_picture_id => @rp.id
-      end
-      it 'テストデータの確認' do
-        @pp.t = 0
-        @pp.should be_valid
-      end
       it 'nullなら失敗する' do
         @pp.t = nil
         @pp.should_not be_valid
@@ -202,5 +274,266 @@ describe PanelPicture do
         @pp.should_not be_valid
       end
     end
+    context 'picture_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @pp.picture_id = nil
+        @pp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @pp.picture_id = 'a'
+        @pp.should_not be_valid
+      end
+      it '存在する実素材でなければ失敗する' do
+        @pp.picture_id = 0
+        @pp.should_not be_valid
+      end
+    end
   end
+  describe '一覧取得に於いて' do
+    before do
+      @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
+        :width => @p.width, :height => @p.height
+    end
+    context 'page補正について' do
+      it '文字列から数値に変換される' do
+        PanelPicture.page('8').should eq 8
+      end
+      it 'nilの場合は1になる' do
+        PanelPicture.page().should eq 1
+      end
+      it '0以下の場合は1になる' do
+        PanelPicture.page('0').should eq 1
+      end
+    end
+    context 'page_size補正について' do
+      it '文字列から数値に変換される' do
+        PanelPicture.page_size('7').should eq 7
+      end
+      it 'nilの場合はPanelPicture.default_page_sizeになる' do
+        PanelPicture.page_size().should eq PanelPicture.default_page_size
+      end
+      it '0以下の場合はPanelPicture.default_page_sizeになる' do
+        PanelPicture.page_size('0').should eq PanelPicture.default_page_size
+      end
+      it 'PanelPicture.max_page_sizeを超えた場合はPanelPicture.max_page_sizeになる' do
+        PanelPicture.page_size('1000').should eq PanelPicture.max_page_size
+      end
+    end
+    context 'つつがなく終わるとき' do\r
+      it '一覧取得オプションを利用している' do\r
+        PanelPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})\r
+        PanelPicture.should_receive(:list_opt).with(any_args).exactly(1)\r
+        r = PanelPicture.list
+      end\r
+    end\r
+    it 'リストを返す' do
+      r = PanelPicture.list
+      r.should eq [@pp]
+    end
+    it '時系列で並んでいる' do
+      #公開されたコマのコマ絵は(他人のコマ絵であっても)含んでいる
+      hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
+      npl = FactoryGirl.create :panel_picture, :panel_id => hc.id, :t => 0, :picture_id => @p.id,
+        :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
+      r = PanelPicture.list
+      r.should eq [npl, @pp]
+    end
+    it '非公開のコマのコマ絵は自分のコマであっても含まない' do
+      hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
+      npl = FactoryGirl.create :panel_picture, :panel_id => hpl.id, :picture_id => @p.id,
+        :width => @p.width, :height => @p.height
+      pl = PanelPicture.list
+      pl.should eq [@pp]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
+        @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
+        @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
+        @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
+        PanelPicture.stub(:default_page_size).and_return(2)
+      end
+      it '通常は2件を返す' do
+        pl = PanelPicture.list
+        pl.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        pl = PanelPicture.list(1)
+        pl.should eq [@npl5, @npl4]
+      end
+      it 'page=2なら中間2件を返す' do
+        pl = PanelPicture.list(2)
+        pl.should eq [@npl3, @npl2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        pl = PanelPicture.list(3)
+        pl.should eq [@pp]
+      end
+    end
+    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+      before do
+        @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
+        @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
+        @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
+        @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
+      end
+      it '通常は全件(5件)を返す' do
+        r = PanelPicture.list 5, 0
+        r.should have(5).items 
+      end
+    end
+  end
+  describe 'list関連テーブルプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = PanelPicture.list_opt
+      r.has_key?(:include).should be_true
+    end
+    it '2つの項目を含んでいる' do
+      r = PanelPicture.list_opt[:include]
+      r.should have(2).items
+    end
+    it 'コマを含んでいる' do
+      r = PanelPicture.list_opt[:include]
+      r.has_key?(:panel).should be_true
+    end
+      it 'コマは作家を含んでいる' do
+        r = PanelPicture.list_opt[:include]
+        r[:panel].has_key?(:author).should be_true
+      end
+    it '実素材を含んでいる' do
+      r = PanelPicture.list_opt[:include]
+      r.has_key?(:picture).should be_true
+    end
+      it '実素材は絵師を含んでいる' do
+        r = PanelPicture.list_opt[:include]
+        r[:picture].has_key?(:artist).should be_true
+      end
+      it '実素材はライセンスを含んでいる' do
+        r = PanelPicture.list_opt[:include]
+        r[:picture].has_key?(:license).should be_true
+      end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = PanelPicture.list_json_opt
+      r.has_key?(:include).should be_true
+    end
+    it '2つの項目を含んでいる' do
+      r = PanelPicture.list_json_opt[:include]
+      r.should have(2).items
+    end
+    it 'コマを含んでいる' do
+      r = PanelPicture.list_json_opt[:include]
+      r.has_key?(:panel).should be_true
+    end
+      it 'コマは作家を含んでいる' do
+        r = PanelPicture.list_json_opt[:include]
+        r[:panel].has_key?(:author).should be_true
+      end
+    it '実素材を含んでいる' do
+      r = PanelPicture.list_json_opt[:include]
+      r.has_key?(:picture).should be_true
+    end
+      it '実素材は絵師を含んでいる' do
+        r = PanelPicture.list_json_opt[:include]
+        r[:picture].has_key?(:artist).should be_true
+      end
+      it '実素材はライセンスを含んでいる' do
+        r = PanelPicture.list_json_opt[:include]
+        r[:picture].has_key?(:license).should be_true
+      end
+  end
+  
+  describe '自分のコマ一覧取得に於いて' do
+    before do
+      @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
+        :width => @p.width, :height => @p.height
+    end
+    context 'つつがなく終わるとき' do\r
+      it '一覧取得オプションを利用している' do\r
+        PanelPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})\r
+        PanelPicture.should_receive(:list_opt).with(any_args).exactly(1)\r
+        r = PanelPicture.mylist @author
+      end\r
+    end\r
+    it 'リストを返す' do
+      pl = PanelPicture.mylist @author
+      pl.should eq [@pp]
+    end
+    it '時系列で並んでいる' do
+      npl = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
+        :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
+      pl = PanelPicture.mylist @author
+      pl.should eq [npl, @pp]
+    end
+    it '他人のコマのコマ絵は公開コマでも含まない' do
+      hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
+      npl = FactoryGirl.create :panel_picture, :panel_id => hpl.id, :picture_id => @p.id,
+        :width => @p.width, :height => @p.height
+      pl = PanelPicture.mylist @author
+      pl.should eq [@pp]
+    end
+    it '自分のコマのコマ絵は非公開コマでも含んでいる' do
+      pl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
+      ni = FactoryGirl.create :panel_picture, :panel_id => pl.id, :picture_id => @p.id,
+        :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
+      r = PanelPicture.mylist @author
+      r.should eq [ni, @pp]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
+        @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
+        @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
+        @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
+      end
+      it '通常は2件を返す' do
+        r = PanelPicture.mylist @author, 1, 2
+        r.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        r = PanelPicture.mylist(@author, 1, 2)
+        r.should eq [@npl5, @npl4]
+      end
+      it 'page=2なら中間2件を返す' do
+        r = PanelPicture.mylist(@author, 2, 2)
+        r.should eq [@npl3, @npl2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        r = PanelPicture.mylist(@author, 3, 2)
+        r.should eq [@pp]
+      end
+    end
+    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+      before do
+        @npl2 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 1, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 100
+        @npl3 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 2, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 200
+        @npl4 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 3, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 300
+        @npl5 = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :t => 4, :picture_id => @p.id,
+          :width => @p.width, :height => @p.height, :updated_at => Time.now + 400
+      end
+      it '通常は全件(5件)を返す' do
+        r = PanelPicture.mylist @author, 5, 0
+        r.should have(5).items 
+      end
+    end
+  end
+  
 end