OSDN Git Service

t#31470:create pager
[pettanr/pettanr.git] / spec / models / comic_spec.rb
index 098ff8f..63fe8a1 100644 (file)
@@ -441,71 +441,6 @@ describe Comic do
         c.should eq [@comic]
       end
     end
-    context 'DBに5件あって1ページの件数を2件に変えたとして' do
-      before do
-        @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
-        @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
-        @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
-        @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
-        Comic.stub(:default_page_size).and_return(2)
-      end
-      it '件数0は全件(5件)を返す' do
-        r = Comic.list 5, 0
-        r.should have(5).items 
-      end
-    end
-  end
-  describe '一覧取得オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = Comic.list_opt
-      r.has_key?(:include).should be_true
-    end
-    it '2つの項目を含んでいる' do
-      r = Comic.list_opt[:include]
-      r.should have(2).items
-    end
-    it 'ストーリーを含んでいる' do
-      r = Comic.list_opt[:include]
-      r.has_key?(:stories).should be_true
-    end
-      it 'ストーリーはコマを含んでいる' do
-        r = Comic.list_opt[:include]
-        r[:stories].has_key?(:panel).should be_true
-      end
-    it '作家を含んでいる' do
-      r = Comic.list_opt[:include]
-      r.has_key?(:author).should be_true
-    end
-  end
-  describe 'json一覧出力オプションに於いて' do
-    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
-    end
-    it 'ストーリーを含んでいる' do
-      r = Comic.list.to_json Comic.list_json_opt
-      j = JSON.parse r
-      i = j.first
-      i.has_key?('stories').should be_true
-    end
-      it 'ストーリーはコマを含んでいる' do
-        r = Comic.list.to_json Comic.list_json_opt
-        j = JSON.parse r
-        i = j.first
-        s = i['stories'].first
-        s.has_key?('panel').should be_true
-      end
-    it '作家を含んでいる' do
-      r = Comic.list.to_json Comic.list_json_opt
-      j = JSON.parse r
-      i = j.first
-      i.has_key?('author').should be_true
-    end
   end
   
   describe '自分のコミック一覧取得に於いて' do
@@ -563,19 +498,6 @@ describe Comic do
         c.should eq [@comic]
       end
     end
-    context 'DBに5件あって1ページの件数を0件に変えたとして' do
-      before do
-        @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
-        @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
-        @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
-        @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
-        Author.stub(:default_comic_page_size).and_return(2)
-      end
-      it '通常は全件(5件)を返す' do
-        r = Comic.mylist @author, 5, 0
-        r.should have(5).items 
-      end
-    end
   end
   
   describe '他作家のコミック一覧取得に於いて' do
@@ -629,18 +551,114 @@ describe Comic do
         c.should eq [@other_comic]
       end
     end
-    context 'DBに5件あって1ページの件数を0件に変えたとして' do
-      before do
-        @other_comic2 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
-        @other_comic3 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 200
-        @other_comic4 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 300
-        @other_comic5 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 400
-        Author.stub(:default_comic_page_size).and_return(2)
+  end
+  
+  describe 'コミック一覧ページ制御に於いて' do
+    before do
+      Comic.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = Comic.list_paginate 
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it 'コミック一覧の取得条件を利用している' do
+      Comic.stub(:list_where).with(any_args).and_return('')
+      Comic.should_receive(:list_where).with(any_args).exactly(1)
+      r = Comic.list_paginate 
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = Comic.list_paginate 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '自分のコミック一覧ページ制御に於いて' do
+    before do
+      Comic.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = Comic.mylist_paginate @author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '自分のコミック一覧の取得条件を利用している' do
+      Comic.stub(:mylist_where).with(any_args).and_return('')
+      Comic.should_receive(:mylist_where).with(any_args).exactly(1)
+      r = Comic.mylist_paginate @author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = Comic.mylist_paginate @author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '他作家のコミック一覧ページ制御に於いて' do
+    before do
+      Comic.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = Comic.himlist_paginate @other_author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '他作家のコミック一覧の取得条件を利用している' do
+      Comic.stub(:himlist_where).with(any_args).and_return('')
+      Comic.should_receive(:himlist_where).with(any_args).exactly(1)
+      r = Comic.himlist_paginate @other_author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = Comic.himlist_paginate @other_author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '一覧取得オプションに於いて' do
+    it '2つの項目を含んでいる' do
+      r = Comic.list_opt
+      r.should have(2).items
+    end
+    it 'ストーリーを含んでいる' do
+      r = Comic.list_opt
+      r.has_key?(:stories).should be_true
+    end
+      it 'ストーリーはコマを含んでいる' do
+        r = Comic.list_opt
+        r[:stories].has_key?(:panel).should be_true
       end
-      it '通常は全件(5件)を返す' do
-        r = Comic.himlist @other_author, 5, 0
-        r.should have(5).items 
+    it '作家を含んでいる' do
+      r = Comic.list_opt
+      r.has_key?(:author).should be_true
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    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
+    end
+    it 'ストーリーを含んでいる' do
+      r = Comic.list.to_json Comic.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('stories').should be_true
+    end
+      it 'ストーリーはコマを含んでいる' do
+        r = Comic.list.to_json Comic.list_json_opt
+        j = JSON.parse r
+        i = j.first
+        s = i['stories'].first
+        s.has_key?('panel').should be_true
       end
+    it '作家を含んでいる' do
+      r = Comic.list.to_json Comic.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('author').should be_true
     end
   end