OSDN Git Service

t#31566:add mylist sb
[pettanr/pettanr.git] / spec / models / speech_balloon_spec.rb
index 356ed84..650975c 100644 (file)
@@ -110,6 +110,16 @@ describe SpeechBalloon do
         }.should raise_error(Pettanr::BadRequest)
       end
     end
+    
+    context 'captionを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @sb.caption = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @sb.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+    
   end
   
   describe '閲覧許可に於いて' do
@@ -270,47 +280,203 @@ describe SpeechBalloon do
         pl.should eq [@sb]
       end
     end
-    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+  end
+  
+  describe '自分のフキダシ一覧取得に於いて' do
+    before do
+      @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
+      @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
+      @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
+    end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        SpeechBalloon.stub(:list_opt).with(any_args).and_return({:include => :panel})
+        SpeechBalloon.should_receive(:list_opt).with(any_args).exactly(1)
+        r = SpeechBalloon.mylist @author
+      end
+    end
+    it 'リストを返す' do
+      pl = SpeechBalloon.mylist @author
+      pl.should eq [@sb]
+    end
+    it '時系列で並んでいる' do
+      npl = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
+      pl = SpeechBalloon.mylist @author
+      pl.should eq [npl, @sb]
+    end
+    it '他人のコマのフキダシは公開コマでも含まない' do
+      hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
+      npl = FactoryGirl.create :speech_balloon, :panel_id => hpl.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
+      pl = SpeechBalloon.mylist @author
+      pl.should eq [@sb]
+    end
+    it '自分のコマのフキダシは非公開コマでも含んでいる' do
+      pl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
+      ni = FactoryGirl.create :speech_balloon, :panel_id => pl.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
+      r = SpeechBalloon.mylist @author
+      r.should eq [ni, @sb]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
-        @sb2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 1, :updated_at => Time.now + 100
-        @sb3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 2, :updated_at => Time.now + 200
-        @sb4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 3, :updated_at => Time.now + 300
-        @sb5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :t => 4, :updated_at => Time.now + 400
-        SpeechBalloon.stub(:default_page_size).and_return(2)
+        @npl2 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
+        @npl3 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 2, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 200
+        @npl4 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 3, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 300
+        @npl5 = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :t => 4, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 400
+      end
+      it '通常は2件を返す' do
+        r = SpeechBalloon.mylist @author, 1, 2
+        r.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        r = SpeechBalloon.mylist(@author, 1, 2)
+        r.should eq [@npl5, @npl4]
+      end
+      it 'page=2なら中間2件を返す' do
+        r = SpeechBalloon.mylist(@author, 2, 2)
+        r.should eq [@npl3, @npl2]
       end
-      it '通常は全件(5件)を返す' do
-        r = SpeechBalloon.list 5, 0
-        r.should have(5).items 
+      it 'page=3なら先頭1件を返す' do
+        r = SpeechBalloon.mylist(@author, 3, 2)
+        r.should eq [@sb]
       end
     end
   end
-  describe '一覧取得オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = SpeechBalloon.list_opt
-      r.has_key?(:include).should be_true
+  
+  describe '他作家のフキダシ一覧取得に於いて' do
+    before do
+      @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
+      @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
+      @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id
+      @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
+      @other_sb = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :speech_balloon_template_id => @speech_balloon_template.id
+    end
+    it 'リストを返す' do
+      r = SpeechBalloon.himlist @other_author
+      r.should eq [@other_sb]
+    end
+    it '時系列で並んでいる' do
+      new_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100
+      new_sb = FactoryGirl.create :speech_balloon, :panel_id => new_panel.id, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
+      r = SpeechBalloon.himlist @other_author
+      r.should eq [new_sb, @other_sb]
+    end
+    it '公開コマに限る' do
+      hidden_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 0
+      hidden_sb = FactoryGirl.create :speech_balloon, :panel_id => hidden_panel.id, :speech_balloon_template_id => @speech_balloon_template.id
+      r = SpeechBalloon.himlist @other_author
+      r.should eq [@other_sb]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @other_sb2 = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :t => 1, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 100
+        @other_sb3 = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :t => 2, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 200
+        @other_sb4 = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :t => 3, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 300
+        @other_sb5 = FactoryGirl.create :speech_balloon, :panel_id => @other_panel.id, :t => 4, :speech_balloon_template_id => @speech_balloon_template.id, :updated_at => Time.now + 400
+      end
+      it '通常は2件を返す' do
+        r = SpeechBalloon.himlist @other_author, 1, 2
+        r.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        r = SpeechBalloon.himlist @other_author, 1, 2
+        r.should eq [@other_sb5, @other_sb4]
+      end
+      it 'page=2なら中間2件を返す' do
+        r = SpeechBalloon.himlist @other_author, 2, 2
+        r.should eq [@other_sb3, @other_sb2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        r = SpeechBalloon.himlist @other_author, 3, 2
+        r.should eq [@other_sb]
+      end
+    end
+  end
+  
+  describe 'フキダシ一覧ページ制御に於いて' do
+    before do
+      SpeechBalloon.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = SpeechBalloon.list_paginate 
+      r.is_a?(Kaminari::PaginatableArray).should be_true
     end
+    it 'フキダシ一覧の取得条件を利用している' do
+      SpeechBalloon.stub(:list_where).with(any_args).and_return('')
+      SpeechBalloon.should_receive(:list_where).with(any_args).exactly(1)
+      r = SpeechBalloon.list_paginate 
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = SpeechBalloon.list_paginate 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '自分のフキダシ一覧ページ制御に於いて' do
+    before do
+      SpeechBalloon.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = SpeechBalloon.mylist_paginate @author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '自分のフキダシ一覧の取得条件を利用している' do
+      SpeechBalloon.stub(:mylist_where).with(any_args).and_return('')
+      SpeechBalloon.should_receive(:mylist_where).with(any_args).exactly(1)
+      r = SpeechBalloon.mylist_paginate @author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = SpeechBalloon.mylist_paginate @author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '他作家のフキダシ一覧ページ制御に於いて' do
+    before do
+      SpeechBalloon.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = SpeechBalloon.himlist_paginate @other_author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '他作家のフキダシ一覧の取得条件を利用している' do
+      SpeechBalloon.stub(:himlist_where).with(any_args).and_return('')
+      SpeechBalloon.should_receive(:himlist_where).with(any_args).exactly(1)
+      r = SpeechBalloon.himlist_paginate @other_author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = SpeechBalloon.himlist_paginate @other_author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '一覧取得オプションに於いて' do
     it '4つの項目を含んでいる' do
-      r = SpeechBalloon.list_opt[:include]
+      r = SpeechBalloon.list_opt
       r.should have(4).items
     end
     it 'コマを含んでいる' do
-      r = SpeechBalloon.list_opt[:include]
+      r = SpeechBalloon.list_opt
       r.has_key?(:panel).should be_true
     end
       it 'コマは作家を含んでいる' do
-        r = SpeechBalloon.list_opt[:include]
+        r = SpeechBalloon.list_opt
         r[:panel].has_key?(:author).should be_true
       end
     it 'フキダシ枠を含んでいる' do
-      r = SpeechBalloon.list_opt[:include]
-      r.has_key?(:balloons).should be_true
+      r = SpeechBalloon.list_opt
+      r.has_key?(:balloon).should be_true
     end
     it 'セリフを含んでいる' do
-      r = SpeechBalloon.list_opt[:include]
-      r.has_key?(:speeches).should be_true
+      r = SpeechBalloon.list_opt
+      r.has_key?(:speech).should be_true
     end
     it 'フキダシテンプレートを含んでいる' do
-      r = SpeechBalloon.list_opt[:include]
+      r = SpeechBalloon.list_opt
       r.has_key?(:speech_balloon_template).should be_true
     end
   end
@@ -338,13 +504,13 @@ describe SpeechBalloon do
       r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
       j = JSON.parse r
       i = j.first
-      i.has_key?('balloons').should be_true
+      i.has_key?('balloon').should be_true
     end
     it 'セリフを含んでいる' do
       r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
       j = JSON.parse r
       i = j.first
-      i.has_key?('speeches').should be_true
+      i.has_key?('speech').should be_true
     end
     it 'フキダシテンプレートを含んでいる' do
       r = SpeechBalloon.list.to_json SpeechBalloon.list_json_opt
@@ -412,11 +578,11 @@ describe SpeechBalloon do
       end
     it 'フキダシ枠を含んでいる' do
       r = SpeechBalloon.show_opt[:include]
-      r.has_key?(:balloons).should be_true
+      r.has_key?(:balloon).should be_true
     end
     it 'セリフを含んでいる' do
       r = SpeechBalloon.show_opt[:include]
-      r.has_key?(:speeches).should be_true
+      r.has_key?(:speech).should be_true
     end
     it 'フキダシテンプレートを含んでいる' do
       r = SpeechBalloon.show_opt[:include]
@@ -447,13 +613,13 @@ describe SpeechBalloon do
       r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
       j = JSON.parse r
       i = j
-      i.has_key?('balloons').should be_true
+      i.has_key?('balloon').should be_true
     end
     it 'セリフを含んでいる' do
       r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt
       j = JSON.parse r
       i = j
-      i.has_key?('speeches').should be_true
+      i.has_key?('speech').should be_true
     end
     it 'フキダシテンプレートを含んでいる' do
       r = SpeechBalloon.show(@sb.id, @author).to_json SpeechBalloon.show_json_opt