OSDN Git Service

t#30021:add repeat on gp
[pettanr/pettanr.git] / spec / models / ground_picture_spec.rb
index 9a45e75..5c8c283 100644 (file)
@@ -24,27 +24,33 @@ describe GroundPicture do
     before do
       @gp = FactoryGirl.build :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
     end
-    it 'オーソドックスなデータなら通る' do
-      @gp.save
-      @gp.should be_valid
+    
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @gp.repeat = 0
+        @gp.x = -99999
+        @gp.y = -99999
+        @gp.z = 1
+        @gp.should be_valid
+      end
+      it '上限データが通る' do
+        @gp.repeat = 3
+        @gp.x = 99999
+        @gp.y = 99999
+        @gp.z = 99999
+        @gp.should be_valid
+      end
     end
     
+    
     context 'panel_idを検証するとき' do
       #ネストの保存はnilを許可しなければならないので数値チェックだけ
-      it 'テストデータの確認' do
-        @gp.panel_id = @panel.id
-        @gp.should be_valid
-      end
       it '数値でなければ失敗する' do
         @gp.panel_id = 'a'
         @gp.should_not be_valid
       end
     end
     context 'picture_idを検証するとき' do
-      it 'テストデータの確認' do
-        @gp.picture_id = @panel.id
-        @gp.should be_valid
-      end
       it 'nullなら失敗する' do
         @gp.picture_id = nil
         @gp.should_not be_valid
@@ -58,11 +64,45 @@ describe GroundPicture do
         @gp.should_not be_valid
       end
     end
-    context 'zを検証するとき' do
-      it 'テストデータの確認' do
-        @gp.z = 1
-        @gp.should be_valid
+    context 'repeatを検証するとき' do
+      it 'nullなら失敗する' do
+        @gp.repeat = nil
+        @gp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @gp.repeat = 'a'
+        @gp.should_not be_valid
+      end
+      it '-1なら失敗する' do
+        @gp.repeat = -1
+        @gp.should_not be_valid
+      end
+      it '4なら失敗する' do
+        @gp.repeat = 4
+        @gp.should_not be_valid
+      end
+    end
+    context 'xを検証するとき' do
+      it 'nullなら失敗する' do
+        @gp.x = nil
+        @gp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @gp.x = 'a'
+        @gp.should_not be_valid
+      end
+    end
+    context 'yを検証するとき' do
+      it 'nullなら失敗する' do
+        @gp.y = nil
+        @gp.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @gp.y = 'a'
+        @gp.should_not be_valid
       end
+    end
+    context 'zを検証するとき' do
       it 'nullなら失敗する' do
         @gp.z = nil
         @gp.should_not be_valid
@@ -81,6 +121,32 @@ describe GroundPicture do
       end
     end
   end
+  
+  describe 'デフォルト値補充に於いて' do
+    before do
+      @gp = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
+    end
+    it 'xに0を補充している' do
+      @gp.supply_default
+      @gp.x.should eq 0
+    end
+    it 'yに0を補充している' do
+      @gp.supply_default
+      @gp.y.should eq 0
+    end
+    it '繰り返しに0を補充している' do
+      @gp.supply_default
+      @gp.repeat.should eq 0
+    end
+  end
+  
+  describe '上書き補充に於いて' do
+    it 'defined' do
+      @gp = FactoryGirl.build :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
+      @gp.overwrite
+    end
+  end
+  
   describe '一覧取得に於いて' do
     before do
       @gp = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
@@ -110,12 +176,21 @@ describe GroundPicture do
         GroundPicture.page_size('1000').should eq GroundPicture.max_page_size
       end
     end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        GroundPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})
+        GroundPicture.should_receive(:list_opt).with(any_args).exactly(1)
+        r = GroundPicture.list
+      end
+    end
     it 'リストを返す' do
       pl = GroundPicture.list
       pl.should eq [@gp]
     end
     it '時系列で並んでいる' do
-      npl = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id, :z => 2, :updated_at => Time.now + 100
+      #公開コマなら(他人のコマであっても)含んでいる
+      opl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
+      npl = FactoryGirl.create :ground_picture, :panel_id => opl.id, :picture_id => @p.id, :updated_at => Time.now + 100
       pl = GroundPicture.list
       pl.should eq [npl, @gp]
     end
@@ -127,10 +202,10 @@ describe GroundPicture do
     end
     context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
-        @npl2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
-        @npl3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
-        @npl4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
-        @npl5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
+        @gp2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
+        @gp3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
+        @gp4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
+        @gp5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
         GroundPicture.stub(:default_page_size).and_return(2)
       end
       it '通常は2件を返す' do
@@ -139,20 +214,33 @@ describe GroundPicture do
       end
       it 'page=1なら末尾2件を返す' do
         #時系列で並んでいる
-        pl = GroundPicture.list( {}, 1)
-        pl.should eq [@npl5, @npl4]
+        pl = GroundPicture.list(1)
+        pl.should eq [@gp5, @gp4]
       end
       it 'page=2なら中間2件を返す' do
-        pl = GroundPicture.list({}, 2)
-        pl.should eq [@npl3, @npl2]
+        pl = GroundPicture.list(2)
+        pl.should eq [@gp3, @gp2]
       end
       it 'page=3なら先頭1件を返す' do
-        pl = GroundPicture.list({}, 3)
+        pl = GroundPicture.list(3)
         pl.should eq [@gp]
       end
     end
+    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+      before do
+        @gp2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
+        @gp3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
+        @gp4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
+        @gp5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
+        GroundPicture.stub(:default_page_size).and_return(2)
+      end
+      it '通常は全件(5件)を返す' do
+        r = GroundPicture.list 5, 0
+        r.should have(5).items 
+      end
+    end
   end
-  describe 'list関連テーブルプションに於いて' do
+  describe '一覧取得オプションに於いて' do
     it 'includeキーを含んでいる' do
       r = GroundPicture.list_opt
       r.has_key?(:include).should be_true
@@ -183,33 +271,48 @@ describe GroundPicture do
       end
   end
   describe 'json一覧出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = GroundPicture.list_json_opt
-      r.has_key?(:include).should be_true
-    end
-    it '2つの項目を含んでいる' do
-      r = GroundPicture.list_json_opt[:include]
-      r.should have(2).items
+    before do
+      @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
+      @sbt = FactoryGirl.create :speech_balloon_template
+      @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
+      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
+      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
+      @gp = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
     end
     it 'コマを含んでいる' do
-      r = GroundPicture.list_json_opt[:include]
-      r.has_key?(:panel).should be_true
+      r = GroundPicture.list.to_json GroundPicture.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('panel').should be_true
     end
       it 'コマは作家を含んでいる' do
-        r = GroundPicture.list_json_opt[:include]
-        r[:panel].has_key?(:author).should be_true
+        r = GroundPicture.list.to_json GroundPicture.list_json_opt
+        j = JSON.parse r
+        i = j.first
+        s = i['panel']
+        s.has_key?('author').should be_true
       end
     it '実素材を含んでいる' do
-      r = GroundPicture.list_json_opt[:include]
-      r.has_key?(:picture).should be_true
+      r = GroundPicture.list.to_json GroundPicture.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('picture').should be_true
     end
       it '実素材は絵師を含んでいる' do
-        r = GroundPicture.list_json_opt[:include]
-        r[:picture].has_key?(:artist).should be_true
+        r = GroundPicture.list.to_json GroundPicture.list_json_opt
+        j = JSON.parse r
+        i = j.first
+        s = i['picture']
+        s.has_key?('artist').should be_true
       end
       it '実素材はライセンスを含んでいる' do
-        r = GroundPicture.list_json_opt[:include]
-        r[:picture].has_key?(:license).should be_true
+        r = GroundPicture.list.to_json GroundPicture.list_json_opt
+        j = JSON.parse r
+        i = j.first
+        s = i['picture']
+        s.has_key?('license').should be_true
       end
   end
   
@@ -217,6 +320,13 @@ describe GroundPicture do
     before do
       @gp = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :picture_id => @p.id
     end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        GroundPicture.stub(:list_opt).with(any_args).and_return({:include => :panel})
+        GroundPicture.should_receive(:list_opt).with(any_args).exactly(1)
+        r = GroundPicture.mylist @author
+      end
+    end
     it 'リストを返す' do
       pl = GroundPicture.mylist @author
       pl.should eq [@gp]
@@ -232,6 +342,51 @@ describe GroundPicture do
       pl = GroundPicture.mylist @author
       pl.should eq [@gp]
     end
+    it '自分のコマの景色は非公開でも含んでいる' do
+      hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
+      npl = FactoryGirl.create :ground_picture, :panel_id => hpl.id, :picture_id => @p.id, :z => 2, :updated_at => Time.now + 100
+      pl = GroundPicture.mylist @author
+      pl.should eq [npl, @gp]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @gp2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
+        @gp3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
+        @gp4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
+        @gp5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
+      end
+      it '通常は2件を返す' do
+        c = GroundPicture.mylist @author, 1, 2
+        c.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        c = GroundPicture.mylist(@author, 1, 2)
+        c.should eq [@gp5, @gp4]
+      end
+      it 'page=2なら中間2件を返す' do
+        c = GroundPicture.mylist(@author, 2, 2)
+        c.should eq [@gp3, @gp2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        c = GroundPicture.mylist(@author, 3, 2)
+        c.should eq [@gp]
+      end
+    end
+    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+      before do
+        @gp2 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 1, :picture_id => @p.id, :updated_at => Time.now + 100
+        @gp3 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 2, :picture_id => @p.id, :updated_at => Time.now + 200
+        @gp4 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 3, :picture_id => @p.id, :updated_at => Time.now + 300
+        @gp5 = FactoryGirl.create :ground_picture, :panel_id => @panel.id, :z => 4, :picture_id => @p.id, :updated_at => Time.now + 400
+        Author.stub(:default_ground_picture_page_size).and_return(2)
+      end
+      it '通常は全件(5件)を返す' do
+        r = GroundPicture.mylist @author, 5, 0
+        r.should have(5).items 
+      end
+    end
   end
   
+  
 end